aboutsummaryrefslogtreecommitdiff
path: root/src/3rdParty/efsw/String.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/3rdParty/efsw/String.hpp')
-rwxr-xr-xsrc/3rdParty/efsw/String.hpp631
1 files changed, 631 insertions, 0 deletions
diff --git a/src/3rdParty/efsw/String.hpp b/src/3rdParty/efsw/String.hpp
new file mode 100755
index 0000000..65bce33
--- /dev/null
+++ b/src/3rdParty/efsw/String.hpp
@@ -0,0 +1,631 @@
1/** NOTE:
2 * This code is based on the Utf implementation from SFML2. License zlib/png (
3 *http://www.sfml-dev.org/license.php ) The class was modified to fit efsw own needs. This is not
4 *the original implementation from SFML2. Functions and methods are the same that in std::string to
5 *facilitate portability.
6 **/
7
8#ifndef EFSW_STRING_HPP
9#define EFSW_STRING_HPP
10
11#include <cstdlib>
12#include <cstring>
13#include <efsw/base.hpp>
14#include <fstream>
15#include <iostream>
16#include <locale>
17#include <sstream>
18#include <string>
19#include <vector>
20
21namespace efsw {
22
23/** @brief Utility string class that automatically handles conversions between types and encodings
24 * **/
25class String {
26 public:
27 typedef Uint32 StringBaseType;
28 typedef std::basic_string<StringBaseType> StringType;
29 typedef StringType::iterator Iterator; //! Iterator type
30 typedef StringType::const_iterator ConstIterator; //! Constant iterator type
31 typedef StringType::reverse_iterator ReverseIterator; //! Reverse Iterator type
32 typedef StringType::const_reverse_iterator ConstReverseIterator; //! Constant iterator type
33
34 static const std::size_t InvalidPos; ///< Represents an invalid position in the string
35
36 template <class T> static std::string toStr( const T& i ) {
37 std::ostringstream ss;
38 ss << i;
39 return ss.str();
40 }
41
42 /** Converts from a string to type */
43 template <class T>
44 static bool fromString( T& t, const std::string& s,
45 std::ios_base& ( *f )( std::ios_base& ) = std::dec ) {
46 std::istringstream iss( s );
47 return !( iss >> f >> t ).fail();
48 }
49
50 /** Converts from a String to type */
51 template <class T>
52 static bool fromString( T& t, const String& s,
53 std::ios_base& ( *f )( std::ios_base& ) = std::dec ) {
54 std::istringstream iss( s.toUtf8() );
55 return !( iss >> f >> t ).fail();
56 }
57
58 /** Split a string and hold it on a vector */
59 static std::vector<std::string> split( const std::string& str, const char& splitchar,
60 const bool& pushEmptyString = false );
61
62 /** Split a string and hold it on a vector */
63 static std::vector<String> split( const String& str, const Uint32& splitchar,
64 const bool& pushEmptyString = false );
65
66 /** Determine if a string starts with the string passed
67 ** @param start The substring expected to start
68 ** @param str The string to compare
69 ** @return -1 if the substring is no in str, otherwise the size of the substring
70 */
71 static int strStartsWith( const std::string& start, const std::string& str );
72
73 static int strStartsWith( const String& start, const String& str );
74
75 /** @brief Construct from an UTF-8 string to UTF-32 according
76 ** @param uf8String UTF-8 string to convert
77 **/
78 static String fromUtf8( const std::string& utf8String );
79
80 /** @brief Default constructor
81 ** This constructor creates an empty string.
82 **/
83 String();
84
85 /** @brief Construct from a single ANSI character and a locale
86 ** The source character is converted to UTF-32 according
87 ** to the given locale. If you want to use the current global
88 ** locale, rather use the other constructor.
89 ** @param ansiChar ANSI character to convert
90 ** @param locale Locale to use for conversion
91 **/
92 String( char ansiChar, const std::locale& locale = std::locale() );
93
94#ifndef EFSW_NO_WIDECHAR
95 /** @brief Construct from single wide character
96 ** @param wideChar Wide character to convert
97 **/
98 String( wchar_t wideChar );
99#endif
100
101 /** @brief Construct from single UTF-32 character
102 ** @param utf32Char UTF-32 character to convert
103 **/
104 String( StringBaseType utf32Char );
105
106 /** @brief Construct from an from a null-terminated C-style UTF-8 string to UTF-32
107 ** @param uf8String UTF-8 string to convert
108 **/
109 String( const char* uf8String );
110
111 /** @brief Construct from an UTF-8 string to UTF-32 according
112 ** @param uf8String UTF-8 string to convert
113 **/
114 String( const std::string& utf8String );
115
116 /** @brief Construct from a null-terminated C-style ANSI string and a locale
117 ** The source string is converted to UTF-32 according
118 ** to the given locale. If you want to use the current global
119 ** locale, rather use the other constructor.
120 ** @param ansiString ANSI string to convert
121 ** @param locale Locale to use for conversion
122 **/
123 String( const char* ansiString, const std::locale& locale );
124
125 /** @brief Construct from an ANSI string and a locale
126 ** The source string is converted to UTF-32 according
127 ** to the given locale. If you want to use the current global
128 ** locale, rather use the other constructor.
129 ** @param ansiString ANSI string to convert
130 ** @param locale Locale to use for conversion
131 **/
132 String( const std::string& ansiString, const std::locale& locale );
133
134#ifndef EFSW_NO_WIDECHAR
135 /** @brief Construct from null-terminated C-style wide string
136 ** @param wideString Wide string to convert
137 **/
138 String( const wchar_t* wideString );
139
140 /** @brief Construct from a wide string
141 ** @param wideString Wide string to convert
142 **/
143 String( const std::wstring& wideString );
144#endif
145
146 /** @brief Construct from a null-terminated C-style UTF-32 string
147 ** @param utf32String UTF-32 string to assign
148 **/
149 String( const StringBaseType* utf32String );
150
151 /** @brief Construct from an UTF-32 string
152 ** @param utf32String UTF-32 string to assign
153 **/
154 String( const StringType& utf32String );
155
156 /** @brief Copy constructor
157 ** @param str Instance to copy
158 **/
159 String( const String& str );
160
161 /** @brief Implicit cast operator to std::string (ANSI string)
162 ** The current global locale is used for conversion. If you
163 ** want to explicitely specify a locale, see toAnsiString.
164 ** Characters that do not fit in the target encoding are
165 ** discarded from the returned string.
166 ** This operator is defined for convenience, and is equivalent
167 ** to calling toAnsiString().
168 ** @return Converted ANSI string
169 ** @see toAnsiString, operator String
170 **/
171 operator std::string() const;
172
173 /** @brief Convert the unicode string to an ANSI string
174 ** The UTF-32 string is converted to an ANSI string in
175 ** the encoding defined by \a locale. If you want to use
176 ** the current global locale, see the other overload
177 ** of toAnsiString.
178 ** Characters that do not fit in the target encoding are
179 ** discarded from the returned string.
180 ** @param locale Locale to use for conversion
181 ** @return Converted ANSI string
182 ** @see toWideString, operator std::string
183 **/
184 std::string toAnsiString( const std::locale& locale = std::locale() ) const;
185
186#ifndef EFSW_NO_WIDECHAR
187 /** @brief Convert the unicode string to a wide string
188 ** Characters that do not fit in the target encoding are
189 ** discarded from the returned string.
190 ** @return Converted wide string
191 ** @see toAnsiString, operator String
192 **/
193 std::wstring toWideString() const;
194#endif
195
196 std::string toUtf8() const;
197
198 /** @brief Overload of assignment operator
199 ** @param right Instance to assign
200 ** @return Reference to self
201 **/
202 String& operator=( const String& right );
203
204 String& operator=( const StringBaseType& right );
205
206 /** @brief Overload of += operator to append an UTF-32 string
207 ** @param right String to append
208 ** @return Reference to self
209 **/
210 String& operator+=( const String& right );
211
212 String& operator+=( const StringBaseType& right );
213
214 /** @brief Overload of [] operator to access a character by its position
215 ** This function provides read-only access to characters.
216 ** Note: this function doesn't throw if \a index is out of range.
217 ** @param index Index of the character to get
218 ** @return Character at position \a index
219 **/
220 StringBaseType operator[]( std::size_t index ) const;
221
222 /** @brief Overload of [] operator to access a character by its position
223 ** This function provides read and write access to characters.
224 ** Note: this function doesn't throw if \a index is out of range.
225 ** @param index Index of the character to get
226 ** @return Reference to the character at position \a index
227 **/
228
229 StringBaseType& operator[]( std::size_t index );
230
231 /** @brief Get character in string
232 ** Performs a range check, throwing an exception of type out_of_range in case that pos is not an
233 *actual position in the string.
234 ** @return The character at position pos in the string.
235 */
236 StringBaseType at( std::size_t index ) const;
237
238 /** @brief clear the string
239 ** This function removes all the characters from the string.
240 ** @see empty, erase
241 **/
242 void clear();
243
244 /** @brief Get the size of the string
245 ** @return Number of characters in the string
246 ** @see empty
247 **/
248 std::size_t size() const;
249
250 /** @see size() */
251 std::size_t length() const;
252
253 /** @brief Check whether the string is empty or not
254 ** @return True if the string is empty (i.e. contains no character)
255 ** @see clear, size
256 **/
257 bool empty() const;
258
259 /** @brief Erase one or more characters from the string
260 ** This function removes a sequence of \a count characters
261 ** starting from \a position.
262 ** @param position Position of the first character to erase
263 ** @param count Number of characters to erase
264 **/
265 void erase( std::size_t position, std::size_t count = 1 );
266
267 /** @brief Insert one or more characters into the string
268 ** This function inserts the characters of \a str
269 ** into the string, starting from \a position.
270 ** @param position Position of insertion
271 ** @param str Characters to insert
272 **/
273 String& insert( std::size_t position, const String& str );
274
275 String& insert( std::size_t pos1, const String& str, std::size_t pos2, std::size_t n );
276
277 String& insert( std::size_t pos1, const char* s, std::size_t n );
278
279 String& insert( std::size_t pos1, const char* s );
280
281 String& insert( std::size_t pos1, size_t n, char c );
282
283 Iterator insert( Iterator p, char c );
284
285 void insert( Iterator p, std::size_t n, char c );
286
287 template <class InputIterator>
288 void insert( Iterator p, InputIterator first, InputIterator last ) {
289 mString.insert( p, first, last );
290 }
291
292 /** @brief Find a sequence of one or more characters in the string
293 ** This function searches for the characters of \a str
294 ** into the string, starting from \a start.
295 ** @param str Characters to find
296 ** @param start Where to begin searching
297 ** @return Position of \a str in the string, or String::InvalidPos if not found
298 **/
299 std::size_t find( const String& str, std::size_t start = 0 ) const;
300
301 std::size_t find( const char* s, std::size_t pos, std::size_t n ) const;
302
303 std::size_t find( const char* s, std::size_t pos = 0 ) const;
304
305 std::size_t find( char c, std::size_t pos = 0 ) const;
306
307 /** @brief Get a pointer to the C-style array of characters
308 ** This functions provides a read-only access to a
309 ** null-terminated C-style representation of the string.
310 ** The returned pointer is temporary and is meant only for
311 ** immediate use, thus it is not recommended to store it.
312 ** @return Read-only pointer to the array of characters
313 **/
314 const StringBaseType* c_str() const;
315
316 /** @brief Get string data
317 ** Notice that no terminating null character is appended (see member c_str for such a
318 *functionality).
319 ** The returned array points to an internal location which should not be modified directly in
320 *the program.
321 ** Its contents are guaranteed to remain unchanged only until the next call to a non-constant
322 *member function of the string object.
323 ** @return Pointer to an internal array containing the same content as the string.
324 **/
325 const StringBaseType* data() const;
326
327 /** @brief Return an iterator to the beginning of the string
328 ** @return Read-write iterator to the beginning of the string characters
329 ** @see end
330 **/
331 Iterator begin();
332
333 /** @brief Return an iterator to the beginning of the string
334 ** @return Read-only iterator to the beginning of the string characters
335 ** @see end
336 **/
337 ConstIterator begin() const;
338
339 /** @brief Return an iterator to the beginning of the string
340 ** The end iterator refers to 1 position past the last character;
341 ** thus it represents an invalid character and should never be
342 ** accessed.
343 ** @return Read-write iterator to the end of the string characters
344 ** @see begin
345 **/
346 Iterator end();
347
348 /** @brief Return an iterator to the beginning of the string
349 ** The end iterator refers to 1 position past the last character;
350 ** thus it represents an invalid character and should never be
351 ** accessed.
352 ** @return Read-only iterator to the end of the string characters
353 ** @see begin
354 **/
355 ConstIterator end() const;
356
357 /** @brief Return an reverse iterator to the beginning of the string
358 ** @return Read-write reverse iterator to the beginning of the string characters
359 ** @see end
360 **/
361 ReverseIterator rbegin();
362
363 /** @brief Return an reverse iterator to the beginning of the string
364 ** @return Read-only reverse iterator to the beginning of the string characters
365 ** @see end
366 **/
367 ConstReverseIterator rbegin() const;
368
369 /** @brief Return an reverse iterator to the beginning of the string
370 ** The end reverse iterator refers to 1 position past the last character;
371 ** thus it represents an invalid character and should never be
372 ** accessed.
373 ** @return Read-write reverse iterator to the end of the string characters
374 ** @see begin
375 **/
376 ReverseIterator rend();
377
378 /** @brief Return an reverse iterator to the beginning of the string
379 ** The end reverse iterator refers to 1 position past the last character;
380 ** thus it represents an invalid character and should never be
381 ** accessed.
382 ** @return Read-only reverse iterator to the end of the string characters
383 ** @see begin
384 **/
385 ConstReverseIterator rend() const;
386
387 /** @brief Resize String */
388 void resize( std::size_t n, StringBaseType c );
389
390 /** @brief Resize String */
391 void resize( std::size_t n );
392
393 /** @return Maximum size of string */
394 std::size_t max_size() const;
395
396 /** @brief Request a change in capacity */
397 void reserve( size_t res_arg = 0 );
398
399 /** @return Size of allocated storage */
400 std::size_t capacity() const;
401
402 /** @brief Append character to string */
403 void push_back( StringBaseType c );
404
405 /** @brief Swap contents with another string */
406 void swap( String& str );
407
408 String& assign( const String& str );
409
410 String& assign( const String& str, std::size_t pos, std::size_t n );
411
412 String& assign( const char* s, std::size_t n );
413
414 String& assign( const char* s );
415
416 String& assign( std::size_t n, char c );
417
418 template <class InputIterator> String& assign( InputIterator first, InputIterator last ) {
419 mString.assign( first, last );
420 return *this;
421 }
422
423 String& append( const String& str );
424
425 String& append( const String& str, std::size_t pos, std::size_t n );
426
427 String& append( const char* s, std::size_t n );
428
429 String& append( const char* s );
430
431 String& append( std::size_t n, char c );
432
433 String& append( std::size_t n, StringBaseType c );
434
435 template <class InputIterator> String& append( InputIterator first, InputIterator last ) {
436 mString.append( first, last );
437 return *this;
438 }
439
440 String& replace( std::size_t pos1, std::size_t n1, const String& str );
441
442 String& replace( Iterator i1, Iterator i2, const String& str );
443
444 String& replace( std::size_t pos1, std::size_t n1, const String& str, std::size_t pos2,
445 std::size_t n2 );
446
447 String& replace( std::size_t pos1, std::size_t n1, const char* s, std::size_t n2 );
448
449 String& replace( Iterator i1, Iterator i2, const char* s, std::size_t n2 );
450
451 String& replace( std::size_t pos1, std::size_t n1, const char* s );
452
453 String& replace( Iterator i1, Iterator i2, const char* s );
454
455 String& replace( std::size_t pos1, std::size_t n1, std::size_t n2, char c );
456
457 String& replace( Iterator i1, Iterator i2, std::size_t n2, char c );
458
459 template <class InputIterator>
460 String& replace( Iterator i1, Iterator i2, InputIterator j1, InputIterator j2 ) {
461 mString.replace( i1, i2, j1, j2 );
462 return *this;
463 }
464
465 std::size_t rfind( const String& str, std::size_t pos = StringType::npos ) const;
466
467 std::size_t rfind( const char* s, std::size_t pos, std::size_t n ) const;
468
469 std::size_t rfind( const char* s, std::size_t pos = StringType::npos ) const;
470
471 std::size_t rfind( char c, std::size_t pos = StringType::npos ) const;
472
473 String substr( std::size_t pos = 0, std::size_t n = StringType::npos ) const;
474
475 std::size_t copy( StringBaseType* s, std::size_t n, std::size_t pos = 0 ) const;
476
477 int compare( const String& str ) const;
478
479 int compare( const char* s ) const;
480
481 int compare( std::size_t pos1, std::size_t n1, const String& str ) const;
482
483 int compare( std::size_t pos1, std::size_t n1, const char* s ) const;
484
485 int compare( std::size_t pos1, std::size_t n1, const String& str, std::size_t pos2,
486 std::size_t n2 ) const;
487
488 int compare( std::size_t pos1, std::size_t n1, const char* s, std::size_t n2 ) const;
489
490 std::size_t find_first_of( const String& str, std::size_t pos = 0 ) const;
491
492 std::size_t find_first_of( const char* s, std::size_t pos, std::size_t n ) const;
493
494 std::size_t find_first_of( const char* s, std::size_t pos = 0 ) const;
495
496 std::size_t find_first_of( StringBaseType c, std::size_t pos = 0 ) const;
497
498 std::size_t find_last_of( const String& str, std::size_t pos = StringType::npos ) const;
499
500 std::size_t find_last_of( const char* s, std::size_t pos, std::size_t n ) const;
501
502 std::size_t find_last_of( const char* s, std::size_t pos = StringType::npos ) const;
503
504 std::size_t find_last_of( StringBaseType c, std::size_t pos = StringType::npos ) const;
505
506 std::size_t find_first_not_of( const String& str, std::size_t pos = 0 ) const;
507
508 std::size_t find_first_not_of( const char* s, std::size_t pos, std::size_t n ) const;
509
510 std::size_t find_first_not_of( const char* s, std::size_t pos = 0 ) const;
511
512 std::size_t find_first_not_of( StringBaseType c, std::size_t pos = 0 ) const;
513
514 std::size_t find_last_not_of( const String& str, std::size_t pos = StringType::npos ) const;
515
516 std::size_t find_last_not_of( const char* s, std::size_t pos, std::size_t n ) const;
517
518 std::size_t find_last_not_of( const char* s, std::size_t pos = StringType::npos ) const;
519
520 std::size_t find_last_not_of( StringBaseType c, std::size_t pos = StringType::npos ) const;
521
522 private:
523 friend bool operator==( const String& left, const String& right );
524 friend bool operator<( const String& left, const String& right );
525
526 StringType mString; ///< Internal string of UTF-32 characters
527};
528
529/** @relates String
530** @brief Overload of == operator to compare two UTF-32 strings
531** @param left Left operand (a string)
532** @param right Right operand (a string)
533** @return True if both strings are equal
534**/
535bool operator==( const String& left, const String& right );
536
537/** @relates String
538** @brief Overload of != operator to compare two UTF-32 strings
539** @param left Left operand (a string)
540** @param right Right operand (a string)
541** @return True if both strings are different
542**/
543bool operator!=( const String& left, const String& right );
544
545/** @relates String
546** @brief Overload of < operator to compare two UTF-32 strings
547** @param left Left operand (a string)
548** @param right Right operand (a string)
549** @return True if \a left is alphabetically lesser than \a right
550**/
551bool operator<( const String& left, const String& right );
552
553/** @relates String
554** @brief Overload of > operator to compare two UTF-32 strings
555** @param left Left operand (a string)
556** @param right Right operand (a string)
557** @return True if \a left is alphabetically greater than \a right
558**/
559bool operator>( const String& left, const String& right );
560
561/** @relates String
562** @brief Overload of <= operator to compare two UTF-32 strings
563** @param left Left operand (a string)
564** @param right Right operand (a string)
565** @return True if \a left is alphabetically lesser or equal than \a right
566**/
567bool operator<=( const String& left, const String& right );
568
569/** @relates String
570** @brief Overload of >= operator to compare two UTF-32 strings
571** @param left Left operand (a string)
572** @param right Right operand (a string)
573** @return True if \a left is alphabetically greater or equal than \a right
574**/
575bool operator>=( const String& left, const String& right );
576
577/** @relates String
578** @brief Overload of binary + operator to concatenate two strings
579** @param left Left operand (a string)
580** @param right Right operand (a string)
581** @return Concatenated string
582**/
583String operator+( const String& left, const String& right );
584
585} // namespace efsw
586
587#endif
588
589/** @class efsw::String
590** @ingroup system
591** efsw::String is a utility string class defined mainly for
592** convenience. It is a Unicode string (implemented using
593** UTF-32), thus it can store any character in the world
594** (european, chinese, arabic, hebrew, etc.).
595** It automatically handles conversions from/to ANSI and
596** wide strings, so that you can work with standard string
597** classes and still be compatible with functions taking a
598** efsw::String.
599** @code
600** efsw::String s;
601** std::string s1 = s; // automatically converted to ANSI string
602** String s2 = s; // automatically converted to wide string
603** s = "hello"; // automatically converted from ANSI string
604** s = L"hello"; // automatically converted from wide string
605** s += 'a'; // automatically converted from ANSI string
606** s += L'a'; // automatically converted from wide string
607** @endcode
608** Conversions involving ANSI strings use the default user locale. However
609** it is possible to use a custom locale if necessary:
610** @code
611** std::locale locale;
612** efsw::String s;
613** ...
614** std::string s1 = s.toAnsiString(locale);
615** s = efsw::String("hello", locale);
616** @endcode
617**
618** efsw::String defines the most important functions of the
619** standard std::string class: removing, random access, iterating,
620** appending, comparing, etc. However it is a simple class
621** provided for convenience, and you may have to consider using
622** a more optimized class if your program requires complex string
623** handling. The automatic conversion functions will then take
624** care of converting your string to efsw::String whenever EE
625** requires it.
626**
627** Please note that EE also defines a low-level, generic
628** interface for Unicode handling, see the efsw::Utf classes.
629**
630** All credits to Laurent Gomila, i just modified and expanded a little bit the implementation.
631**/