Leviathan  0.8.0.0
Leviathan game engine
StringDataIterator.cpp
Go to the documentation of this file.
1 #include "Include.h"
2 // ------------------------------------ //
3 #include "StringDataIterator.h"
5 
6 #if !defined(ALTERNATIVE_EXCEPTIONS_FATAL) || defined(ALLOW_INTERNAL_EXCEPTIONS)
7 #include "utf8/checked.h"
8 #else
9 #include "utf8/unchecked.h"
10 #endif //ALTERNATIVE_EXCEPTIONS_FATAL
11 using namespace Leviathan;
12 // ------------------------------------ //
14  CurrentCharacterNumber(0), CurrentLineNumber(1)
15 {
16 
17 }
18 
20 
21 }
22 // ------------------------------------ //
23 DLLEXPORT bool Leviathan::StringDataIterator::ReturnSubString(size_t startpos, size_t endpos,
24  std::string &receiver)
25 {
26  DEBUG_BREAK;
27  LEVIATHAN_ASSERT(0, "StringDataIterator doesn't support getting with type: string, "
28  "make sure your provided data source string type is the same as the "
29  "request template type");
30  return false;
31 }
32 
33 DLLEXPORT bool Leviathan::StringDataIterator::ReturnSubString(size_t startpos, size_t endpos,
34  std::wstring &receiver)
35 {
36  DEBUG_BREAK;
37  LEVIATHAN_ASSERT(0, "StringDataIterator doesn't support getting with type: wstring, make "
38  "sure your provided data source string type is the same as the request template type");
39  return false;
40 }
41 // ------------------------------------ //
43  return CurrentCharacterNumber;
44 }
45 
47  return CurrentLineNumber;
48 }
49 
51 
52  int32_t checkchar;
53 
54  if (GetNextCharCode(checkchar, 0) && StringOperations::IsLineTerminator(checkchar)) {
55 
56  // Check for multiline //
57  int32_t evenfurthercharacter;
58  if (!GetNextCharCode(evenfurthercharacter, 1) ||
59  !StringOperations::IsLineTerminator(checkchar, evenfurthercharacter))
60  {
61  ++CurrentLineNumber;
62  }
63  }
64 }
65 
66 // ------------------------------------ //
67 // UTF8PointerDataIterator
68 DLLEXPORT UTF8PointerDataIterator::UTF8PointerDataIterator(const char* begin, const char* end) :
69  Current(begin), End(end), BeginPos(Current)
70 {
71  // If the first character is a newline the line number needs to be
72  // incremented immediately
73  if(Current)
75 }
76 
78  UTF8PointerDataIterator(fromstr.c_str(), fromstr.c_str() + fromstr.size())
79 {
80 
81 }
82 
84  size_t forward)
85 {
86  // Check is it out of range
87  if(Current + forward >= End)
88  return false;
89 
90  // We can just peek the next character if forward is 0 //
91  if(!forward){
92 
93  #if !defined(ALTERNATIVE_EXCEPTIONS_FATAL) || defined(ALLOW_INTERNAL_EXCEPTIONS)
94  codepointreceiver = utf8::peek_next(Current, End);
95  #else
96  codepointreceiver = utf8::unchecked::peek_next(Current);
97 
98  // receiver might be full of garbage at this point //
99 
100 
101  #endif //ALTERNATIVE_EXCEPTIONS_FATAL
102  return true;
103  }
104 
105  // UTF8 string use the utf8 iterating functions //
106  auto shouldbepos = Current;
107 
108 #if !defined(ALTERNATIVE_EXCEPTIONS_FATAL) || defined(ALLOW_INTERNAL_EXCEPTIONS)
109 
110  utf8::advance(shouldbepos, forward, End);
111 
112  if (shouldbepos == End)
113  return false;
114 
115  codepointreceiver = utf8::next(shouldbepos, End);
116 #else
117  utf8::unchecked::advance(shouldbepos, forward);
118 
119  if (shouldbepos == End)
120  return false;
121 
122  codepointreceiver = utf8::unchecked::next(shouldbepos);
123 
124  LEVIATHAN_ASSERT(shouldbepos != End, "GetNextCharCode next moved past the end");
125 
126 #endif //ALTERNATIVE_EXCEPTIONS_FATAL
127 
128  return true;
129 }
130 
132 
133  // Try to get the prior code point //
134  auto shouldbepos = Current;
135 
136 #if !defined(ALTERNATIVE_EXCEPTIONS_FATAL) || defined(ALLOW_INTERNAL_EXCEPTIONS)
137 
138  try{
139  // Try to copy the previous code point into the receiver //
140  receiver = utf8::prior(shouldbepos, BeginPos);
141 
142  } catch(const utf8::not_enough_room&){
143  return false;
144  } catch(const utf8::invalid_utf8&){
145  return false;
146  }
147 
148 #else
149  receiver = utf8::unchecked::prior(shouldbepos);
150 
151  if (shouldbepos == End)
152  return false;
153 
154 #endif //ALTERNATIVE_EXCEPTIONS_FATAL
155 
156 
157  // If it didn't throw it worked //
158  return true;
159 }
160 // ------------------------------------ //
162 
163  if (!IsPositionValid())
164  return;
165 
166  // We need to move whole code points //
167 #if !defined(ALTERNATIVE_EXCEPTIONS_FATAL) || defined(ALLOW_INTERNAL_EXCEPTIONS)
168 
170 
171 #else
173 
174 #endif //ALTERNATIVE_EXCEPTIONS_FATAL
175 
176  // Don't forget to increment these //
178 
179  // Return if position is not valid //
180  if(!IsPositionValid())
181  return;
182 
183  // There might be a better way to check this //
184  CheckLineChange();
185 }
186 // ------------------------------------ //
188  return std::distance(BeginPos, Current);
189 }
190 
192  return Current != End;
193 }
194 // ------------------------------------ //
196  return static_cast<size_t>((End - 1) - BeginPos);
197 }
198 // ------------------------------------ //
199 DLLEXPORT bool UTF8PointerDataIterator::ReturnSubString(size_t startpos, size_t endpos,
200  std::string &receiver)
201 {
202  if(startpos >= static_cast<size_t>(End - BeginPos) ||
203  endpos >= static_cast<size_t>(End - BeginPos) || startpos > endpos)
204  {
205  return false;
206  }
207 
208  receiver = std::string(BeginPos + startpos, (endpos - startpos) + 1);
209  return true;
210 }
211 
212 
213 
214 // ------------------------------------ //
215 // UTF8DataIterator
217  UTF8PointerDataIterator(nullptr, nullptr), OurString(str)
218 {
219  // This is a bit hacky
220  Current = OurString.c_str();
221  End = Current + OurString.size();
222  BeginPos = Current;
223 
224 
225  // If the first character is a newline the line number needs to be
226  // incremented immediately
227  CheckLineChange();
228 }
Raw pointer utf8 iterator.
static bool IsLineTerminator(int32_t codepoint)
virtual DLLEXPORT bool ReturnSubString(size_t startpos, size_t endpos, std::string &receiver)
uint32_t next(octet_iterator &it, octet_iterator end)
Definition: checked.h:137
uint32_t peek_next(octet_iterator it)
Definition: unchecked.h:92
uint32_t next(octet_iterator &it)
Definition: unchecked.h:61
uint32_t peek_next(octet_iterator it, octet_iterator end)
Definition: checked.h:157
virtual DLLEXPORT bool ReturnSubString(size_t startpos, size_t endpos, std::string &receiver)
void advance(octet_iterator &it, distance_type n, octet_iterator end)
Definition: checked.h:190
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 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) ...
DLLEXPORT void CheckLineChange()
Updates CurrentLineNumber if currently on a line break.
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
Definition: checked.h:198
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:92
virtual DLLEXPORT size_t GetCurrentLineNumber() const
Returns the 1 based line number.
uint32_t prior(octet_iterator &it, octet_iterator start)
Definition: checked.h:163
DLLEXPORT UTF8DataIterator(const std::string &str)
Wraps an utf8 encoded string for StringIterator.
virtual DLLEXPORT void MoveToNextCharacter()
Moves the iterator forward.
void advance(octet_iterator &it, distance_type n)
Definition: unchecked.h:113
virtual DLLEXPORT bool IsPositionValid() const
Returns true when the iterator is still valid.
#define DLLEXPORT
Definition: Include.h:84
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.
const char * BeginPos
The starting point for distance checking.
uint32_t prior(octet_iterator &it)
Definition: unchecked.h:98