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