coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parse_buffer.h
Go to the documentation of this file.
1 #ifndef PARSE_BUFFER_H
2 #define PARSE_BUFFER_H
3 #pragma once
4 /***************************************************************************
5  * Copyright (C) 2013 Mike Kinghan, imk@burroingroingjoing.com *
6  * All rights reserved. *
7  * *
8  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
9  * *
10  * Redistribution and use in source and binary forms, with or without *
11  * modification, are permitted provided that the following conditions *
12  * are met: *
13  * *
14  * Redistributions of source code must retain the above copyright *
15  * notice, this list of conditions and the following disclaimer. *
16  * *
17  * Redistributions in binary form must reproduce the above copyright *
18  * notice, this list of conditions and the following disclaimer in the *
19  * documentation and/or other materials provided with the distribution. *
20  * *
21  * Neither the name of Mike Kinghan nor the names of its contributors *
22  * may be used to endorse or promote products derived from this software *
23  * without specific prior written permission. *
24  * *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
32  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
35  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
36  * DAMAGE. *
37  * *
38  **************************************************************************/
39 
40 #include <string>
41 #include "eol.h"
42 
52 {
54  using value_type = char;
55 
57  parse_buffer() = default;
58 
60  virtual ~parse_buffer(){};
61 
63  explicit parse_buffer(std::string const & str)
64  : _text(str){}
65 
67  parse_buffer & operator=(parse_buffer const &) = default;
68 
70  parse_buffer & operator=(std::string const & str) {
71  _text = str;
72  return *this;
73  }
74 
76  bool operator==(parse_buffer const & other) const {
77  return _text == other._text;
78  }
79 
81  bool operator!=(parse_buffer const & other) const {
82  return _text != other._text;
83  }
84 
86  size_t size() const {
87  return _text.size();
88  }
89 
92  std::string & str() {
93  return _text;
94  }
95  std::string const & str() const {
96  return _text;
97  }
99 
101  explicit operator std::string const &() const {
102  return str();
103  }
104 
107  char const * data() const {
108  return _text.data();
109  }
110  char * data() {
111  return const_cast<char *>(_text.data());
112  }
114 
116  std::string substr(size_t start, size_t len = std::string::npos) const {
117  return _text.substr(start,len);
118  }
119 
121  bool overshoot(size_t off = 0) const {
122  return off >= size();
123  }
124 
126  char at(size_t off) const {
127  return _text[off];
128  }
129 
131  char & at(size_t off) {
132  return _text[off];
133  }
135  char operator[](size_t off) const {
136  return at(off);
137  }
138 
140  char & operator[](size_t offset) {
141  return at(offset);
142  }
143 
145  void clear() {
146  _text.clear();
147  }
148 
156  unsigned eol(size_t offset) const {
157  return ::eol(_text,offset);
158  }
159 
168  virtual size_t extend() {
169  return 0;
170  }
171 
183  virtual size_t extension_pending(size_t off) const {
184  return 0;
185  }
186 
202  virtual size_t extend(size_t skip) {
203  return 0;
204  }
205 
206 protected:
207 
209  std::string _text;
210 
211 };
212 
213 #endif // EOF
unsigned eol(CharSeq const &seq, size_t off)
Test for a newline-sequence at an offset in a character sequence.
Definition: eol.h:53
virtual size_t extend(size_t skip)
Extend the string, perhaps.
Definition: parse_buffer.h:202
virtual ~parse_buffer()
Destructor.
Definition: parse_buffer.h:60
void clear()
Empty the parse_buffer.
Definition: parse_buffer.h:145
virtual size_t extension_pending(size_t off) const
Test whether an extension of the string is pending at an offset.
Definition: parse_buffer.h:183
parse_buffer(std::string const &str)
Explicitly construct from an std::string
Definition: parse_buffer.h:63
parse_buffer & operator=(std::string const &str)
Assign an std::string
Definition: parse_buffer.h:70
std::string _text
The data.
Definition: parse_buffer.h:209
parse_buffer & operator=(parse_buffer const &)=default
Assign another parse_buffer
char & operator[](size_t offset)
Get a reference to the character at an offset. Not range checked.
Definition: parse_buffer.h:140
bool overshoot(size_t off=0) const
Say whether an offset is out of tange.
Definition: parse_buffer.h:121
unsigned eol(size_t offset) const
Say whether there is a newline sequence at an offset.
Definition: parse_buffer.h:156
bool operator!=(parse_buffer const &other) const
Inequality operator.
Definition: parse_buffer.h:81
struct parse_buffer is a polymorphic base for classes representing possibly extensible lines of parse...
Definition: parse_buffer.h:51
char & at(size_t off)
Get a reference to the character an offset. Not range-checked.
Definition: parse_buffer.h:131
std::string substr(size_t start, size_t len=std::string::npos) const
Get a substring of the parse_buffer.
Definition: parse_buffer.h:116
bool operator==(parse_buffer const &other) const
Equality.
Definition: parse_buffer.h:76
char value_type
Value-type of parse_buffer
Definition: parse_buffer.h:54
char const * data() const
Get a [const] pointer to the data.
Definition: parse_buffer.h:107
parse_buffer()=default
Default constuctor.
char at(size_t off) const
Get the character indexed by an offset. Not range-checked.
Definition: parse_buffer.h:126
char operator[](size_t off) const
Get the character at an offset. Not range checked.
Definition: parse_buffer.h:135
std::string & str()
Get a [const] reference to the underlying std::string.
Definition: parse_buffer.h:92
virtual size_t extend()
Extend the string, perhaps.
Definition: parse_buffer.h:168
size_t size() const
Get the length of the parse_buffer
Definition: parse_buffer.h:86