]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Utility/StringLexer.cpp
Merge ^/head r312968 through r313054.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Utility / StringLexer.cpp
1 //===--------------------- StringLexer.cpp -----------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "lldb/Utility/StringLexer.h"
12
13 #include <algorithm>
14 #include <assert.h>
15
16 using namespace lldb_utility;
17
18 StringLexer::StringLexer(std::string s) : m_data(s), m_position(0) {}
19
20 StringLexer::StringLexer(const StringLexer &rhs)
21     : m_data(rhs.m_data), m_position(rhs.m_position) {}
22
23 StringLexer::Character StringLexer::Peek() { return m_data[m_position]; }
24
25 bool StringLexer::NextIf(Character c) {
26   auto val = Peek();
27   if (val == c) {
28     Next();
29     return true;
30   }
31   return false;
32 }
33
34 std::pair<bool, StringLexer::Character>
35 StringLexer::NextIf(std::initializer_list<Character> cs) {
36   auto val = Peek();
37   for (auto c : cs) {
38     if (val == c) {
39       Next();
40       return {true, c};
41     }
42   }
43   return {false, 0};
44 }
45
46 bool StringLexer::AdvanceIf(const std::string &token) {
47   auto pos = m_position;
48   bool matches = true;
49   for (auto c : token) {
50     if (!NextIf(c)) {
51       matches = false;
52       break;
53     }
54   }
55   if (!matches) {
56     m_position = pos;
57     return false;
58   }
59   return true;
60 }
61
62 StringLexer::Character StringLexer::Next() {
63   auto val = Peek();
64   Consume();
65   return val;
66 }
67
68 bool StringLexer::HasAtLeast(Size s) {
69   return (m_data.size() - m_position) >= s;
70 }
71
72 void StringLexer::PutBack(Size s) {
73   assert(m_position >= s);
74   m_position -= s;
75 }
76
77 bool StringLexer::HasAny(Character c) {
78   return m_data.find(c, m_position) != std::string::npos;
79 }
80
81 std::string StringLexer::GetUnlexed() {
82   return std::string(m_data, m_position);
83 }
84
85 void StringLexer::Consume() { m_position++; }
86
87 StringLexer &StringLexer::operator=(const StringLexer &rhs) {
88   if (this != &rhs) {
89     m_data = rhs.m_data;
90     m_position = rhs.m_position;
91   }
92   return *this;
93 }