Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/lldb/source/Utility/StringLexer.cpp
39587 views
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
15
using namespace lldb_private;
16
17
StringLexer::StringLexer(std::string s) : m_data(std::move(s)), m_position(0) {}
18
19
StringLexer::Character StringLexer::Peek() { return m_data[m_position]; }
20
21
bool StringLexer::NextIf(Character c) {
22
auto val = Peek();
23
if (val == c) {
24
Next();
25
return true;
26
}
27
return false;
28
}
29
30
std::pair<bool, StringLexer::Character>
31
StringLexer::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
42
bool 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
58
StringLexer::Character StringLexer::Next() {
59
auto val = Peek();
60
Consume();
61
return val;
62
}
63
64
bool StringLexer::HasAtLeast(Size s) {
65
return (m_data.size() - m_position) >= s;
66
}
67
68
void StringLexer::PutBack(Size s) {
69
assert(m_position >= s);
70
m_position -= s;
71
}
72
73
std::string StringLexer::GetUnlexed() {
74
return std::string(m_data, m_position);
75
}
76
77
void StringLexer::Consume() { m_position++; }
78
79
StringLexer &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
87