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