Leviathan  0.8.0.0
Leviathan game engine
Leviathan::UTF8PointerDataIterator Class Reference

Raw pointer utf8 iterator. More...

#include <StringDataIterator.h>

+ Inheritance diagram for Leviathan::UTF8PointerDataIterator:

Public Member Functions

DLLEXPORT UTF8PointerDataIterator (const char *begin, const char *end)
 
DLLEXPORT UTF8PointerDataIterator (const std::string &fromstr)
 Helper for creating from strings. More...
 
virtual DLLEXPORT bool GetNextCharCode (int &codepointreceiver, size_t forward)
 Gets the next character unicode code point (usually an ascii value) More...
 
virtual DLLEXPORT bool GetPreviousCharacter (int &receiver)
 Gets the previous character code point. More...
 
virtual DLLEXPORT void MoveToNextCharacter ()
 Moves the iterator forward. More...
 
virtual DLLEXPORT size_t CurrentIteratorPosition () const
 Gets the position of the iterator, for use with ReturnSubString and others. More...
 
virtual DLLEXPORT bool IsPositionValid () const
 Returns true when the iterator is still valid. More...
 
virtual DLLEXPORT size_t GetLastValidIteratorPosition () const
 Gets the last valid index of the underlying string (not the last character but the last byte) More...
 
virtual DLLEXPORT bool ReturnSubString (size_t startpos, size_t endpos, std::string &receiver)
 
- Public Member Functions inherited from Leviathan::StringDataIterator
DLLEXPORT StringDataIterator ()
 
virtual DLLEXPORT ~StringDataIterator ()
 
virtual DLLEXPORT bool ReturnSubString (size_t startpos, size_t endpos, std::wstring &receiver)
 
virtual DLLEXPORT size_t GetCurrentCharacterNumber () const
 Returns the 0 based character number (NOT position, number of characters) More...
 
virtual DLLEXPORT size_t GetCurrentLineNumber () const
 Returns the 1 based line number. More...
 

Protected Attributes

const char * Current
 The current position of the iterator. More...
 
const char * End
 The end of the string. More...
 
const char * BeginPos
 The starting point for distance checking. More...
 
- Protected Attributes inherited from Leviathan::StringDataIterator
size_t CurrentCharacterNumber
 
size_t CurrentLineNumber
 The current line number, the amount of
characters. More...
 

Additional Inherited Members

- Protected Member Functions inherited from Leviathan::StringDataIterator
DLLEXPORT void CheckLineChange ()
 Updates CurrentLineNumber if currently on a line break. More...
 

Detailed Description

Raw pointer utf8 iterator.

Definition at line 242 of file StringDataIterator.h.

Constructor & Destructor Documentation

◆ UTF8PointerDataIterator() [1/2]

DLLEXPORT UTF8PointerDataIterator::UTF8PointerDataIterator ( const char *  begin,
const char *  end 
)

end points 1 past the end of the data "this is my string\0" ^-begin ^-end meaning the null terminator is optional

Note
Child classes may pass null pointers here as long as they call CheckLineChange in their constructors (to not miss line changes if the first character is a line change, see UTF8DataIterator(const std::string &str) constructor)

Definition at line 68 of file StringDataIterator.cpp.

68  :
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 }
const char * Current
The current position of the iterator.
const char * End
The end of the string.
DLLEXPORT void CheckLineChange()
Updates CurrentLineNumber if currently on a line break.
const char * BeginPos
The starting point for distance checking.

◆ UTF8PointerDataIterator() [2/2]

DLLEXPORT UTF8PointerDataIterator::UTF8PointerDataIterator ( const std::string &  fromstr)

Helper for creating from strings.

Definition at line 77 of file StringDataIterator.cpp.

77  :
78  UTF8PointerDataIterator(fromstr.c_str(), fromstr.c_str() + fromstr.size())
79 {
80 
81 }
DLLEXPORT UTF8PointerDataIterator(const char *begin, const char *end)

Member Function Documentation

◆ CurrentIteratorPosition()

DLLEXPORT size_t UTF8PointerDataIterator::CurrentIteratorPosition ( ) const
virtual

Gets the position of the iterator, for use with ReturnSubString and others.

Implements Leviathan::StringDataIterator.

Definition at line 187 of file StringDataIterator.cpp.

187  {
188  return std::distance(BeginPos, Current);
189 }
const char * Current
The current position of the iterator.
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
Definition: checked.h:198
const char * BeginPos
The starting point for distance checking.

◆ GetLastValidIteratorPosition()

DLLEXPORT size_t UTF8PointerDataIterator::GetLastValidIteratorPosition ( ) const
virtual

Gets the last valid index of the underlying string (not the last character but the last byte)

Implements Leviathan::StringDataIterator.

Definition at line 195 of file StringDataIterator.cpp.

195  {
196  return static_cast<size_t>((End - 1) - BeginPos);
197 }
const char * End
The end of the string.
const char * BeginPos
The starting point for distance checking.

◆ GetNextCharCode()

DLLEXPORT bool UTF8PointerDataIterator::GetNextCharCode ( int &  codepointreceiver,
size_t  forward 
)
virtual

Gets the next character unicode code point (usually an ascii value)

Parameters
forwardDefines how many characters past the current position the wanted character is. This is usually 0 or 1
Note
This is used to get the next character but also used for peeking so this should not increment the underlying iterator
Returns
Returns true when the input is still valid

Implements Leviathan::StringDataIterator.

Definition at line 83 of file StringDataIterator.cpp.

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 }
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
void advance(octet_iterator &it, distance_type n, octet_iterator end)
Definition: checked.h:190
const char * Current
The current position of the iterator.
const char * End
The end of the string.
void advance(octet_iterator &it, distance_type n)
Definition: unchecked.h:113

◆ GetPreviousCharacter()

DLLEXPORT bool UTF8PointerDataIterator::GetPreviousCharacter ( int &  receiver)
virtual

Gets the previous character code point.

Returns
True when succeeded false when this is the first character and there is nothing before it
See also
GetNextCharCode

Implements Leviathan::StringDataIterator.

Definition at line 131 of file StringDataIterator.cpp.

131  {
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 }
const char * Current
The current position of the iterator.
const char * End
The end of the string.
uint32_t prior(octet_iterator &it, octet_iterator start)
Definition: checked.h:163
const char * BeginPos
The starting point for distance checking.
uint32_t prior(octet_iterator &it)
Definition: unchecked.h:98

◆ IsPositionValid()

DLLEXPORT bool UTF8PointerDataIterator::IsPositionValid ( ) const
virtual

Returns true when the iterator is still valid.

Implements Leviathan::StringDataIterator.

Definition at line 191 of file StringDataIterator.cpp.

191  {
192  return Current != End;
193 }
const char * Current
The current position of the iterator.
const char * End
The end of the string.

◆ MoveToNextCharacter()

DLLEXPORT void UTF8PointerDataIterator::MoveToNextCharacter ( )
virtual

Moves the iterator forward.

Implements Leviathan::StringDataIterator.

Definition at line 161 of file StringDataIterator.cpp.

161  {
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 }
void advance(octet_iterator &it, distance_type n, octet_iterator end)
Definition: checked.h:190
const char * Current
The current position of the iterator.
const char * End
The end of the string.
DLLEXPORT void CheckLineChange()
Updates CurrentLineNumber if currently on a line break.
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.

◆ ReturnSubString()

DLLEXPORT bool UTF8PointerDataIterator::ReturnSubString ( size_t  startpos,
size_t  endpos,
std::string &  receiver 
)
virtual

Reimplemented from Leviathan::StringDataIterator.

Definition at line 199 of file StringDataIterator.cpp.

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 }
const char * End
The end of the string.
const char * BeginPos
The starting point for distance checking.

Member Data Documentation

◆ BeginPos

const char* Leviathan::UTF8PointerDataIterator::BeginPos
protected

The starting point for distance checking.

Definition at line 280 of file StringDataIterator.h.

◆ Current

const char* Leviathan::UTF8PointerDataIterator::Current
protected

The current position of the iterator.

Definition at line 275 of file StringDataIterator.h.

◆ End

const char* Leviathan::UTF8PointerDataIterator::End
protected

The end of the string.

Definition at line 277 of file StringDataIterator.h.


The documentation for this class was generated from the following files: