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