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 using namespace lldb_utility;
13 
14 StringLexer::StringLexer (std::string s) :
15 m_data(s),
16 m_position(0),
17 m_putback_data()
18 { }
19 
20 StringLexer::StringLexer (const StringLexer& rhs) :
21 m_data(rhs.m_data),
22 m_position(rhs.m_position),
23 m_putback_data(rhs.m_putback_data)
24 { }
25 
26 StringLexer::Character
27 StringLexer::Peek ()
28 {
29     if (m_putback_data.empty())
30         return m_data[m_position];
31     else
32         return m_putback_data.front();
33 }
34 
35 bool
36 StringLexer::NextIf (Character c)
37 {
38     auto val = Peek();
39     if (val == c)
40     {
41         Next();
42         return true;
43     }
44     return false;
45 }
46 
47 StringLexer::Character
48 StringLexer::Next ()
49 {
50     auto val = Peek();
51     Consume();
52     return val;
53 }
54 
55 bool
56 StringLexer::HasAtLeast (Size s)
57 {
58     auto in_m_data = m_data.size()-m_position;
59     auto in_putback = m_putback_data.size();
60     return (in_m_data + in_putback >= s);
61 }
62 
63 
64 void
65 StringLexer::PutBack (Character c)
66 {
67     m_putback_data.push_back(c);
68 }
69 
70 bool
71 StringLexer::HasAny (Character c)
72 {
73     const auto begin(m_putback_data.begin());
74     const auto end(m_putback_data.end());
75     if (std::find(begin, end, c) != end)
76         return true;
77     return m_data.find(c, m_position) != std::string::npos;
78 }
79 
80 void
81 StringLexer::Consume()
82 {
83     if (m_putback_data.empty())
84         m_position++;
85     else
86         m_putback_data.pop_front();
87 }
88 
89 StringLexer&
90 StringLexer::operator = (const StringLexer& rhs)
91 {
92     if (this != &rhs)
93     {
94         m_data = rhs.m_data;
95         m_position = rhs.m_position;
96         m_putback_data = rhs.m_putback_data;
97     }
98     return *this;
99 }
100