1//===-- StringLexer.cpp ---------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Utility/StringLexer.h"
10
11#include <algorithm>
12#include <cassert>
13#include <utility>
14
15using namespace lldb_private;
16
17StringLexer::StringLexer(std::string s) : m_data(std::move(s)), m_position(0) {}
18
19StringLexer::Character StringLexer::Peek() { return m_data[m_position]; }
20
21bool StringLexer::NextIf(Character c) {
22 auto val = Peek();
23 if (val == c) {
24 Next();
25 return true;
26 }
27 return false;
28}
29
30std::pair<bool, StringLexer::Character>
31StringLexer::NextIf(std::initializer_list<Character> cs) {
32 auto val = Peek();
33 for (auto c : cs) {
34 if (val == c) {
35 Next();
36 return {true, c};
37 }
38 }
39 return {false, 0};
40}
41
42bool StringLexer::AdvanceIf(const std::string &token) {
43 auto pos = m_position;
44 bool matches = true;
45 for (auto c : token) {
46 if (!NextIf(c)) {
47 matches = false;
48 break;
49 }
50 }
51 if (!matches) {
52 m_position = pos;
53 return false;
54 }
55 return true;
56}
57
58StringLexer::Character StringLexer::Next() {
59 auto val = Peek();
60 Consume();
61 return val;
62}
63
64bool StringLexer::HasAtLeast(Size s) {
65 return (m_data.size() - m_position) >= s;
66}
67
68void StringLexer::PutBack(Size s) {
69 assert(m_position >= s);
70 m_position -= s;
71}
72
73std::string StringLexer::GetUnlexed() {
74 return std::string(m_data, m_position);
75}
76
77void StringLexer::Consume() { m_position++; }
78
79StringLexer &StringLexer::operator=(const StringLexer &rhs) {
80 if (this != &rhs) {
81 m_data = rhs.m_data;
82 m_position = rhs.m_position;
83 }
84 return *this;
85}
86

source code of lldb/source/Utility/StringLexer.cpp