Leviathan  0.8.0.0
Leviathan game engine
StringDataIterator.h
Go to the documentation of this file.
1 #pragma once
2 // ------------------------------------ //
3 #include "Define.h"
4 // ------------------------------------ //
5 
6 
7 namespace Leviathan{
8 
13 public:
16 
17  // Implement these in all subclasses to wrap the input for iteration //
18 
25  virtual bool GetNextCharCode(int &codepointreceiver, size_t forward) = 0;
26 
31  virtual bool GetPreviousCharacter(int &receiver) = 0;
32 
34  virtual void MoveToNextCharacter() = 0;
35 
36 
37  // These are for getting the results //
38  // If the internal string doesn't support efficient conversions false should be returned
39  // and nothing set
40 
41 
42  DLLEXPORT virtual bool ReturnSubString(size_t startpos, size_t endpos,
43  std::string &receiver);
44  DLLEXPORT virtual bool ReturnSubString(size_t startpos, size_t endpos,
45  std::wstring &receiver);
46 
47 
49  virtual size_t CurrentIteratorPosition() const = 0;
50 
53  virtual size_t GetLastValidIteratorPosition() const = 0;
54 
55 
57  virtual bool IsPositionValid() const = 0;
58 
59 
60  // Basic functions that should all be the same //
61 
63  DLLEXPORT virtual size_t GetCurrentCharacterNumber() const;
64 
66  DLLEXPORT virtual size_t GetCurrentLineNumber() const;
67 
68 
69 protected:
70 
73 
74 protected:
75 
79 
82 
83 
84 };
85 
86 
88 template<class STRType>
90  typedef size_t ITType;
91 public:
92 
95  StringClassDataIterator(const STRType &str) : OurString(str), Current(0), End(str.size()){
96  // If the first character is a newline the line number needs to be incremented immediately //
97  if(OurString.size() && OurString[0] == '\n'){
98 
100  }
101 
102  }
103 
104  virtual bool GetNextCharCode(int &codepointreceiver, size_t forward){
105  if(Current+forward >= End)
106  return false;
107  // Copy the character //
108  codepointreceiver = static_cast<int>(OurString[Current+forward]);
109  return true;
110  }
111 
112  virtual bool GetPreviousCharacter(int &receiver){
113  if(Current-1 >= End)
114  return false;
115 
116  // Copy the character //
117  receiver = static_cast<int>(OurString[Current-1]);
118  return true;
119  }
120 
121  virtual void MoveToNextCharacter(){
122  ++Current;
123  // Don't forget to increment these //
125  // There might be a better way to check this //
126  CheckLineChange();
127  }
128 
129  virtual bool ReturnSubString(size_t startpos, size_t endpos, STRType &receiver){
130  if(startpos >= End || endpos >= End || startpos > endpos)
131  return false;
132 
133  receiver = OurString.substr(startpos, endpos-startpos+1);
134  return true;
135  }
136 
137  virtual size_t CurrentIteratorPosition() const{
138  return Current;
139  }
140 
141  virtual bool IsPositionValid() const{
142  if(Current < End)
143  return true;
144  return false;
145  }
146 
147  virtual size_t GetLastValidIteratorPosition() const{
148 
149  return End-1;
150  }
151 
152 
153 
154 protected:
155 
156  STRType OurString;
157 
159  ITType Current;
161  ITType End;
162 };
163 
164 
166 template<class STRType>
168  typedef size_t ITType;
169 public:
170 
173  StringClassPointerIterator(const STRType* str) : OurString(str), Current(0){
174  End = str ? str->size(): 0;
175 
176  // If the first character is a newline the line number needs to be incremented immediately //
177  if(OurString && OurString->size() && OurString->at(0) == '\n'){
178 
180  }
181  }
182 
183  virtual bool GetNextCharCode(int &codepointreceiver, size_t forward){
184  if(Current+forward >= End)
185  return false;
186  // Copy the character //
187  codepointreceiver = static_cast<int>(OurString->at(Current+forward));
188  return true;
189  }
190 
191  virtual bool GetPreviousCharacter(int &receiver){
192  if(Current-1 >= End)
193  return false;
194 
195  // Copy the character //
196  receiver = static_cast<int>(OurString->at(Current-1));
197  return true;
198  }
199 
200  virtual void MoveToNextCharacter(){
201  ++Current;
202  // Don't forget to increment these //
204 
205  CheckLineChange();
206  }
207 
208  virtual bool ReturnSubString(size_t startpos, size_t endpos, STRType &receiver){
209  if(startpos >= End || endpos >= End || startpos > endpos)
210  return false;
211 
212  receiver = OurString->substr(startpos, endpos-startpos+1);
213  return true;
214  }
215 
216  virtual size_t CurrentIteratorPosition() const{
217  return Current;
218  }
219 
220  virtual bool IsPositionValid() const{
221  if(Current < End)
222  return true;
223  return false;
224  }
225 
226  virtual size_t GetLastValidIteratorPosition() const{
227 
228  return End-1;
229  }
230 
231 protected:
232 
233  const STRType* OurString;
234 
236  ITType Current;
238  ITType End;
239 };
240 
243 public:
244 
252  DLLEXPORT UTF8PointerDataIterator(const char* begin, const char* end);
253 
255  DLLEXPORT UTF8PointerDataIterator(const std::string &fromstr);
256 
257  DLLEXPORT virtual bool GetNextCharCode(int &codepointreceiver, size_t forward);
258 
259  DLLEXPORT virtual bool GetPreviousCharacter(int &receiver);
260 
261  DLLEXPORT virtual void MoveToNextCharacter();
262 
263  DLLEXPORT virtual size_t CurrentIteratorPosition() const;
264 
265  DLLEXPORT virtual bool IsPositionValid() const;
266 
267  DLLEXPORT virtual size_t GetLastValidIteratorPosition() const;
268 
269  DLLEXPORT virtual bool ReturnSubString(size_t startpos, size_t endpos,
270  std::string &receiver);
271 
272 protected:
273 
275  const char* Current;
277  const char* End;
278 
280  const char* BeginPos;
281 
282 };
283 
284 
287  typedef std::string::iterator ITType;
288 public:
289 
291  DLLEXPORT UTF8DataIterator(const std::string &str);
292 
293 protected:
294 
295  std::string OurString;
296 };
297 
298 
299 }
300 
301 #ifdef LEAK_INTO_GLOBAL
303 #endif
304 
virtual bool ReturnSubString(size_t startpos, size_t endpos, STRType &receiver)
Raw pointer utf8 iterator.
virtual bool GetNextCharCode(int &codepointreceiver, size_t forward)=0
Gets the next character unicode code point (usually an ascii value)
virtual DLLEXPORT bool ReturnSubString(size_t startpos, size_t endpos, std::string &receiver)
virtual void MoveToNextCharacter()
Moves the iterator forward.
Iterator for string types.
ITType End
The end of the string.
size_t CurrentLineNumber
The current line number, the amount of characters.
Unicode iterator for utf8 on top of string.
virtual void MoveToNextCharacter()
Moves the iterator forward.
ITType End
The end of the string.
virtual bool GetNextCharCode(int &codepointreceiver, size_t forward)
Gets the next character unicode code point (usually an ascii value)
virtual size_t CurrentIteratorPosition() const
Gets the position of the iterator, for use with ReturnSubString and others.
virtual bool ReturnSubString(size_t startpos, size_t endpos, STRType &receiver)
StringClassDataIterator(const STRType &str)
virtual void MoveToNextCharacter()=0
Moves the iterator forward.
virtual size_t CurrentIteratorPosition() const
Gets the position of the iterator, for use with ReturnSubString and others.
virtual size_t CurrentIteratorPosition() const =0
Gets the position of the iterator, for use with ReturnSubString and others.
virtual DLLEXPORT bool ReturnSubString(size_t startpos, size_t endpos, std::string &receiver)
const char * Current
The current position of the iterator.
virtual DLLEXPORT size_t CurrentIteratorPosition() const
Gets the position of the iterator, for use with ReturnSubString and others.
virtual bool IsPositionValid() const =0
Returns true when the iterator is still valid.
virtual DLLEXPORT bool GetNextCharCode(int &codepointreceiver, size_t forward)
Gets the next character unicode code point (usually an ascii value)
const char * End
The end of the string.
virtual DLLEXPORT ~StringDataIterator()
virtual DLLEXPORT size_t GetCurrentCharacterNumber() const
Returns the 0 based character number (NOT position, number of characters)
DLLEXPORT UTF8PointerDataIterator(const char *begin, const char *end)
virtual DLLEXPORT size_t GetLastValidIteratorPosition() const
Gets the last valid index of the underlying string (not the last character but the last byte) ...
Iterator that doesn&#39;t hold own copy of a string.
DLLEXPORT void CheckLineChange()
Updates CurrentLineNumber if currently on a line break.
ITType Current
The current position of the iterator.
virtual DLLEXPORT size_t GetCurrentLineNumber() const
Returns the 1 based line number.
DLLEXPORT UTF8DataIterator(const std::string &str)
Wraps an utf8 encoded string for StringIterator.
virtual size_t GetLastValidIteratorPosition() const =0
Gets the last valid index of the underlying string (not the last character but the last byte) ...
virtual DLLEXPORT void MoveToNextCharacter()
Moves the iterator forward.
virtual bool IsPositionValid() const
Returns true when the iterator is still valid.
virtual size_t GetLastValidIteratorPosition() const
Gets the last valid index of the underlying string (not the last character but the last byte) ...
virtual bool GetPreviousCharacter(int &receiver)
Gets the previous character code point.
virtual DLLEXPORT bool IsPositionValid() const
Returns true when the iterator is still valid.
ITType Current
The current position of the iterator.
virtual bool GetPreviousCharacter(int &receiver)
Gets the previous character code point.
#define DLLEXPORT
Definition: Include.h:84
virtual bool GetNextCharCode(int &codepointreceiver, size_t forward)
Gets the next character unicode code point (usually an ascii value)
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
virtual DLLEXPORT bool GetPreviousCharacter(int &receiver)
Gets the previous character code point.
virtual bool IsPositionValid() const
Returns true when the iterator is still valid.
const char * BeginPos
The starting point for distance checking.
virtual bool GetPreviousCharacter(int &receiver)=0
Gets the previous character code point.
virtual size_t GetLastValidIteratorPosition() const
Gets the last valid index of the underlying string (not the last character but the last byte) ...