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