[mlir][int-range] Limit xor int range inference to i1 (#116968)
[llvm-project.git] / lldb / source / Utility / StringLexer.cpp
blobbda6e25ce7a35c1e7a7ff630cd01dfb90ce09985
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 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/StringLexer.h"
11 #include <algorithm>
12 #include <cassert>
13 #include <utility>
15 using namespace lldb_private;
17 StringLexer::StringLexer(std::string s) : m_data(std::move(s)), m_position(0) {}
19 StringLexer::Character StringLexer::Peek() { return m_data[m_position]; }
21 bool StringLexer::NextIf(Character c) {
22 auto val = Peek();
23 if (val == c) {
24 Next();
25 return true;
27 return false;
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};
39 return {false, 0};
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;
51 if (!matches) {
52 m_position = pos;
53 return false;
55 return true;
58 StringLexer::Character StringLexer::Next() {
59 auto val = Peek();
60 Consume();
61 return val;
64 bool StringLexer::HasAtLeast(Size s) {
65 return (m_data.size() - m_position) >= s;
68 void StringLexer::PutBack(Size s) {
69 assert(m_position >= s);
70 m_position -= s;
73 std::string StringLexer::GetUnlexed() {
74 return std::string(m_data, m_position);
77 void StringLexer::Consume() { m_position++; }
79 StringLexer &StringLexer::operator=(const StringLexer &rhs) {
80 if (this != &rhs) {
81 m_data = rhs.m_data;
82 m_position = rhs.m_position;
84 return *this;