Path: blob/main/contrib/llvm-project/lldb/source/Utility/StringLexer.cpp
39587 views
//===-- StringLexer.cpp ---------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Utility/StringLexer.h"910#include <algorithm>11#include <cassert>12#include <utility>1314using namespace lldb_private;1516StringLexer::StringLexer(std::string s) : m_data(std::move(s)), m_position(0) {}1718StringLexer::Character StringLexer::Peek() { return m_data[m_position]; }1920bool StringLexer::NextIf(Character c) {21auto val = Peek();22if (val == c) {23Next();24return true;25}26return false;27}2829std::pair<bool, StringLexer::Character>30StringLexer::NextIf(std::initializer_list<Character> cs) {31auto val = Peek();32for (auto c : cs) {33if (val == c) {34Next();35return {true, c};36}37}38return {false, 0};39}4041bool StringLexer::AdvanceIf(const std::string &token) {42auto pos = m_position;43bool matches = true;44for (auto c : token) {45if (!NextIf(c)) {46matches = false;47break;48}49}50if (!matches) {51m_position = pos;52return false;53}54return true;55}5657StringLexer::Character StringLexer::Next() {58auto val = Peek();59Consume();60return val;61}6263bool StringLexer::HasAtLeast(Size s) {64return (m_data.size() - m_position) >= s;65}6667void StringLexer::PutBack(Size s) {68assert(m_position >= s);69m_position -= s;70}7172std::string StringLexer::GetUnlexed() {73return std::string(m_data, m_position);74}7576void StringLexer::Consume() { m_position++; }7778StringLexer &StringLexer::operator=(const StringLexer &rhs) {79if (this != &rhs) {80m_data = rhs.m_data;81m_position = rhs.m_position;82}83return *this;84}858687