forked from minetest/minetest
-
Notifications
You must be signed in to change notification settings - Fork 11
/
irrUString.h
3891 lines (3279 loc) · 95.5 KB
/
irrUString.h
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Basic Unicode string class for Irrlicht.
Copyright (c) 2009-2011 John Norman
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
The original version of this class can be located at:
http:https://irrlicht.suckerfreegames.com/
John Norman
*/
#pragma once
#if (__cplusplus > 199711L) || (_MSC_VER >= 1600) || defined(__GXX_EXPERIMENTAL_CXX0X__)
# define USTRING_CPP0X
# if defined(__GXX_EXPERIMENTAL_CXX0X__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)))
# define USTRING_CPP0X_NEWLITERALS
# endif
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cstddef>
#ifdef _WIN32
#define __BYTE_ORDER 0
#define __LITTLE_ENDIAN 0
#define __BIG_ENDIAN 1
#elif defined(__MACH__) && defined(__APPLE__)
#include <machine/endian.h>
#elif defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/endian.h>
#else
#include <endian.h>
#endif
#ifdef USTRING_CPP0X
# include <utility>
#endif
#ifndef USTRING_NO_STL
# include <string>
# include <iterator>
# include <ostream>
#endif
#include "irrTypes.h"
#include "irrAllocator.h"
#include "irrArray.h"
#include "irrMath.h"
#include "irrString.h"
#include "path.h"
//! UTF-16 surrogate start values.
static const irr::u16 UTF16_HI_SURROGATE = 0xD800;
static const irr::u16 UTF16_LO_SURROGATE = 0xDC00;
//! Is a UTF-16 code point a surrogate?
#define UTF16_IS_SURROGATE(c) (((c) & 0xF800) == 0xD800)
#define UTF16_IS_SURROGATE_HI(c) (((c) & 0xFC00) == 0xD800)
#define UTF16_IS_SURROGATE_LO(c) (((c) & 0xFC00) == 0xDC00)
namespace irr
{
// Define our character types.
#ifdef USTRING_CPP0X_NEWLITERALS // C++0x
typedef char32_t uchar32_t;
typedef char16_t uchar16_t;
typedef char uchar8_t;
#else
typedef u32 uchar32_t;
typedef u16 uchar16_t;
typedef u8 uchar8_t;
#endif
namespace core
{
namespace unicode
{
//! The unicode replacement character. Used to replace invalid characters.
const irr::u16 UTF_REPLACEMENT_CHARACTER = 0xFFFD;
//! Convert a UTF-16 surrogate pair into a UTF-32 character.
//! \param high The high value of the pair.
//! \param low The low value of the pair.
//! \return The UTF-32 character expressed by the surrogate pair.
inline uchar32_t toUTF32(uchar16_t high, uchar16_t low)
{
// Convert the surrogate pair into a single UTF-32 character.
uchar32_t x = ((high & ((1 << 6) -1)) << 10) | (low & ((1 << 10) -1));
uchar32_t wu = ((high >> 6) & ((1 << 5) - 1)) + 1;
return (wu << 16) | x;
}
//! Swaps the endianness of a 16-bit value.
//! \return The new value.
inline uchar16_t swapEndian16(const uchar16_t& c)
{
return ((c >> 8) & 0x00FF) | ((c << 8) & 0xFF00);
}
//! Swaps the endianness of a 32-bit value.
//! \return The new value.
inline uchar32_t swapEndian32(const uchar32_t& c)
{
return ((c >> 24) & 0x000000FF) |
((c >> 8) & 0x0000FF00) |
((c << 8) & 0x00FF0000) |
((c << 24) & 0xFF000000);
}
//! The Unicode byte order mark.
const u16 BOM = 0xFEFF;
//! The size of the Unicode byte order mark in terms of the Unicode character size.
const u8 BOM_UTF8_LEN = 3;
const u8 BOM_UTF16_LEN = 1;
const u8 BOM_UTF32_LEN = 1;
//! Unicode byte order marks for file operations.
const u8 BOM_ENCODE_UTF8[3] = { 0xEF, 0xBB, 0xBF };
const u8 BOM_ENCODE_UTF16_BE[2] = { 0xFE, 0xFF };
const u8 BOM_ENCODE_UTF16_LE[2] = { 0xFF, 0xFE };
const u8 BOM_ENCODE_UTF32_BE[4] = { 0x00, 0x00, 0xFE, 0xFF };
const u8 BOM_ENCODE_UTF32_LE[4] = { 0xFF, 0xFE, 0x00, 0x00 };
//! The size in bytes of the Unicode byte marks for file operations.
const u8 BOM_ENCODE_UTF8_LEN = 3;
const u8 BOM_ENCODE_UTF16_LEN = 2;
const u8 BOM_ENCODE_UTF32_LEN = 4;
//! Unicode encoding type.
enum EUTF_ENCODE
{
EUTFE_NONE = 0,
EUTFE_UTF8,
EUTFE_UTF16,
EUTFE_UTF16_LE,
EUTFE_UTF16_BE,
EUTFE_UTF32,
EUTFE_UTF32_LE,
EUTFE_UTF32_BE
};
//! Unicode endianness.
enum EUTF_ENDIAN
{
EUTFEE_NATIVE = 0,
EUTFEE_LITTLE,
EUTFEE_BIG
};
//! Returns the specified unicode byte order mark in a byte array.
//! The byte order mark is the first few bytes in a text file that signifies its encoding.
/** \param mode The Unicode encoding method that we want to get the byte order mark for.
If EUTFE_UTF16 or EUTFE_UTF32 is passed, it uses the native system endianness. **/
//! \return An array that contains a byte order mark.
inline core::array<u8> getUnicodeBOM(EUTF_ENCODE mode)
{
#define COPY_ARRAY(source, size) \
memcpy(ret.pointer(), source, size); \
ret.set_used(size)
core::array<u8> ret(4);
switch (mode)
{
case EUTFE_UTF8:
COPY_ARRAY(BOM_ENCODE_UTF8, BOM_ENCODE_UTF8_LEN);
break;
case EUTFE_UTF16:
#ifdef __BIG_ENDIAN__
COPY_ARRAY(BOM_ENCODE_UTF16_BE, BOM_ENCODE_UTF16_LEN);
#else
COPY_ARRAY(BOM_ENCODE_UTF16_LE, BOM_ENCODE_UTF16_LEN);
#endif
break;
case EUTFE_UTF16_BE:
COPY_ARRAY(BOM_ENCODE_UTF16_BE, BOM_ENCODE_UTF16_LEN);
break;
case EUTFE_UTF16_LE:
COPY_ARRAY(BOM_ENCODE_UTF16_LE, BOM_ENCODE_UTF16_LEN);
break;
case EUTFE_UTF32:
#ifdef __BIG_ENDIAN__
COPY_ARRAY(BOM_ENCODE_UTF32_BE, BOM_ENCODE_UTF32_LEN);
#else
COPY_ARRAY(BOM_ENCODE_UTF32_LE, BOM_ENCODE_UTF32_LEN);
#endif
break;
case EUTFE_UTF32_BE:
COPY_ARRAY(BOM_ENCODE_UTF32_BE, BOM_ENCODE_UTF32_LEN);
break;
case EUTFE_UTF32_LE:
COPY_ARRAY(BOM_ENCODE_UTF32_LE, BOM_ENCODE_UTF32_LEN);
break;
case EUTFE_NONE:
// TODO sapier: fixed warning only,
// don't know if something needs to be done here
break;
}
return ret;
#undef COPY_ARRAY
}
//! Detects if the given data stream starts with a unicode BOM.
//! \param data The data stream to check.
//! \return The unicode BOM associated with the data stream, or EUTFE_NONE if none was found.
inline EUTF_ENCODE determineUnicodeBOM(const char* data)
{
if (memcmp(data, BOM_ENCODE_UTF8, 3) == 0) return EUTFE_UTF8;
if (memcmp(data, BOM_ENCODE_UTF16_BE, 2) == 0) return EUTFE_UTF16_BE;
if (memcmp(data, BOM_ENCODE_UTF16_LE, 2) == 0) return EUTFE_UTF16_LE;
if (memcmp(data, BOM_ENCODE_UTF32_BE, 4) == 0) return EUTFE_UTF32_BE;
if (memcmp(data, BOM_ENCODE_UTF32_LE, 4) == 0) return EUTFE_UTF32_LE;
return EUTFE_NONE;
}
} // end namespace unicode
//! UTF-16 string class.
template <typename TAlloc = irrAllocator<uchar16_t> >
class ustring16
{
public:
///------------------///
/// iterator classes ///
///------------------///
//! Access an element in a unicode string, allowing one to change it.
class _ustring16_iterator_access
{
public:
_ustring16_iterator_access(const ustring16<TAlloc>* s, u32 p) : ref(s), pos(p) {}
//! Allow the class to be interpreted as a single UTF-32 character.
operator uchar32_t() const
{
return _get();
}
//! Allow one to change the character in the unicode string.
//! \param c The new character to use.
//! \return Myself.
_ustring16_iterator_access& operator=(const uchar32_t c)
{
_set(c);
return *this;
}
//! Increments the value by 1.
//! \return Myself.
_ustring16_iterator_access& operator++()
{
_set(_get() + 1);
return *this;
}
//! Increments the value by 1, returning the old value.
//! \return A unicode character.
uchar32_t operator++(int)
{
uchar32_t old = _get();
_set(old + 1);
return old;
}
//! Decrements the value by 1.
//! \return Myself.
_ustring16_iterator_access& operator--()
{
_set(_get() - 1);
return *this;
}
//! Decrements the value by 1, returning the old value.
//! \return A unicode character.
uchar32_t operator--(int)
{
uchar32_t old = _get();
_set(old - 1);
return old;
}
//! Adds to the value by a specified amount.
//! \param val The amount to add to this character.
//! \return Myself.
_ustring16_iterator_access& operator+=(int val)
{
_set(_get() + val);
return *this;
}
//! Subtracts from the value by a specified amount.
//! \param val The amount to subtract from this character.
//! \return Myself.
_ustring16_iterator_access& operator-=(int val)
{
_set(_get() - val);
return *this;
}
//! Multiples the value by a specified amount.
//! \param val The amount to multiply this character by.
//! \return Myself.
_ustring16_iterator_access& operator*=(int val)
{
_set(_get() * val);
return *this;
}
//! Divides the value by a specified amount.
//! \param val The amount to divide this character by.
//! \return Myself.
_ustring16_iterator_access& operator/=(int val)
{
_set(_get() / val);
return *this;
}
//! Modulos the value by a specified amount.
//! \param val The amount to modulo this character by.
//! \return Myself.
_ustring16_iterator_access& operator%=(int val)
{
_set(_get() % val);
return *this;
}
//! Adds to the value by a specified amount.
//! \param val The amount to add to this character.
//! \return A unicode character.
uchar32_t operator+(int val) const
{
return _get() + val;
}
//! Subtracts from the value by a specified amount.
//! \param val The amount to subtract from this character.
//! \return A unicode character.
uchar32_t operator-(int val) const
{
return _get() - val;
}
//! Multiplies the value by a specified amount.
//! \param val The amount to multiply this character by.
//! \return A unicode character.
uchar32_t operator*(int val) const
{
return _get() * val;
}
//! Divides the value by a specified amount.
//! \param val The amount to divide this character by.
//! \return A unicode character.
uchar32_t operator/(int val) const
{
return _get() / val;
}
//! Modulos the value by a specified amount.
//! \param val The amount to modulo this character by.
//! \return A unicode character.
uchar32_t operator%(int val) const
{
return _get() % val;
}
private:
//! Gets a uchar32_t from our current position.
uchar32_t _get() const
{
const uchar16_t* a = ref->c_str();
if (!UTF16_IS_SURROGATE(a[pos]))
return static_cast<uchar32_t>(a[pos]);
else
{
if (pos + 1 >= ref->size_raw())
return 0;
return unicode::toUTF32(a[pos], a[pos + 1]);
}
}
//! Sets a uchar32_t at our current position.
void _set(uchar32_t c)
{
ustring16<TAlloc>* ref2 = const_cast<ustring16<TAlloc>*>(ref);
const uchar16_t* a = ref2->c_str();
if (c > 0xFFFF)
{
// c will be multibyte, so split it up into the high and low surrogate pairs.
uchar16_t x = static_cast<uchar16_t>(c);
uchar16_t vh = UTF16_HI_SURROGATE | ((((c >> 16) & ((1 << 5) - 1)) - 1) << 6) | (x >> 10);
uchar16_t vl = UTF16_LO_SURROGATE | (x & ((1 << 10) - 1));
// If the previous position was a surrogate pair, just replace them. Else, insert the low pair.
if (UTF16_IS_SURROGATE_HI(a[pos]) && pos + 1 != ref2->size_raw())
ref2->replace_raw(vl, static_cast<u32>(pos) + 1);
else ref2->insert_raw(vl, static_cast<u32>(pos) + 1);
ref2->replace_raw(vh, static_cast<u32>(pos));
}
else
{
// c will be a single byte.
uchar16_t vh = static_cast<uchar16_t>(c);
// If the previous position was a surrogate pair, remove the extra byte.
if (UTF16_IS_SURROGATE_HI(a[pos]))
ref2->erase_raw(static_cast<u32>(pos) + 1);
ref2->replace_raw(vh, static_cast<u32>(pos));
}
}
const ustring16<TAlloc>* ref;
u32 pos;
};
typedef typename ustring16<TAlloc>::_ustring16_iterator_access access;
//! Iterator to iterate through a UTF-16 string.
#ifndef USTRING_NO_STL
class _ustring16_const_iterator : public std::iterator<
std::bidirectional_iterator_tag, // iterator_category
access, // value_type
ptrdiff_t, // difference_type
const access, // pointer
const access // reference
>
#else
class _ustring16_const_iterator
#endif
{
public:
typedef _ustring16_const_iterator _Iter;
typedef std::iterator<std::bidirectional_iterator_tag, access, ptrdiff_t, const access, const access> _Base;
typedef const access const_pointer;
typedef const access const_reference;
#ifndef USTRING_NO_STL
typedef typename _Base::value_type value_type;
typedef typename _Base::difference_type difference_type;
typedef typename _Base::difference_type distance_type;
typedef typename _Base::pointer pointer;
typedef const_reference reference;
#else
typedef access value_type;
typedef u32 difference_type;
typedef u32 distance_type;
typedef const_pointer pointer;
typedef const_reference reference;
#endif
//! Constructors.
_ustring16_const_iterator(const _Iter& i) : ref(i.ref), pos(i.pos) {}
_ustring16_const_iterator(const ustring16<TAlloc>& s) : ref(&s), pos(0) {}
_ustring16_const_iterator(const ustring16<TAlloc>& s, const u32 p) : ref(&s), pos(0)
{
if (ref->size_raw() == 0 || p == 0)
return;
// Go to the appropriate position.
u32 i = p;
u32 sr = ref->size_raw();
const uchar16_t* a = ref->c_str();
while (i != 0 && pos < sr)
{
if (UTF16_IS_SURROGATE_HI(a[pos]))
pos += 2;
else ++pos;
--i;
}
}
//! Test for equalness.
bool operator==(const _Iter& iter) const
{
if (ref == iter.ref && pos == iter.pos)
return true;
return false;
}
//! Test for unequalness.
bool operator!=(const _Iter& iter) const
{
if (ref != iter.ref || pos != iter.pos)
return true;
return false;
}
//! Switch to the next full character in the string.
_Iter& operator++()
{ // ++iterator
if (pos == ref->size_raw()) return *this;
const uchar16_t* a = ref->c_str();
if (UTF16_IS_SURROGATE_HI(a[pos]))
pos += 2; // TODO: check for valid low surrogate?
else ++pos;
if (pos > ref->size_raw()) pos = ref->size_raw();
return *this;
}
//! Switch to the next full character in the string, returning the previous position.
_Iter operator++(int)
{ // iterator++
_Iter _tmp(*this);
++*this;
return _tmp;
}
//! Switch to the previous full character in the string.
_Iter& operator--()
{ // --iterator
if (pos == 0) return *this;
const uchar16_t* a = ref->c_str();
--pos;
if (UTF16_IS_SURROGATE_LO(a[pos]) && pos != 0) // low surrogate, go back one more.
--pos;
return *this;
}
//! Switch to the previous full character in the string, returning the previous position.
_Iter operator--(int)
{ // iterator--
_Iter _tmp(*this);
--*this;
return _tmp;
}
//! Advance a specified number of full characters in the string.
//! \return Myself.
_Iter& operator+=(const difference_type v)
{
if (v == 0) return *this;
if (v < 0) return operator-=(v * -1);
if (pos >= ref->size_raw())
return *this;
// Go to the appropriate position.
// TODO: Don't force u32 on an x64 OS. Make it agnostic.
u32 i = (u32)v;
u32 sr = ref->size_raw();
const uchar16_t* a = ref->c_str();
while (i != 0 && pos < sr)
{
if (UTF16_IS_SURROGATE_HI(a[pos]))
pos += 2;
else ++pos;
--i;
}
if (pos > sr)
pos = sr;
return *this;
}
//! Go back a specified number of full characters in the string.
//! \return Myself.
_Iter& operator-=(const difference_type v)
{
if (v == 0) return *this;
if (v > 0) return operator+=(v * -1);
if (pos == 0)
return *this;
// Go to the appropriate position.
// TODO: Don't force u32 on an x64 OS. Make it agnostic.
u32 i = (u32)v;
const uchar16_t* a = ref->c_str();
while (i != 0 && pos != 0)
{
--pos;
if (UTF16_IS_SURROGATE_LO(a[pos]) != 0 && pos != 0)
--pos;
--i;
}
return *this;
}
//! Return a new iterator that is a variable number of full characters forward from the current position.
_Iter operator+(const difference_type v) const
{
_Iter ret(*this);
ret += v;
return ret;
}
//! Return a new iterator that is a variable number of full characters backward from the current position.
_Iter operator-(const difference_type v) const
{
_Iter ret(*this);
ret -= v;
return ret;
}
//! Returns the distance between two iterators.
difference_type operator-(const _Iter& iter) const
{
// Make sure we reference the same object!
if (ref != iter.ref)
return difference_type();
_Iter i = iter;
difference_type ret;
// Walk up.
if (pos > i.pos)
{
while (pos > i.pos)
{
++i;
++ret;
}
return ret;
}
// Walk down.
while (pos < i.pos)
{
--i;
--ret;
}
return ret;
}
//! Accesses the full character at the iterator's position.
const_reference operator*() const
{
if (pos >= ref->size_raw())
{
const uchar16_t* a = ref->c_str();
u32 p = ref->size_raw();
if (UTF16_IS_SURROGATE_LO(a[p]))
--p;
reference ret(ref, p);
return ret;
}
const_reference ret(ref, pos);
return ret;
}
//! Accesses the full character at the iterator's position.
reference operator*()
{
if (pos >= ref->size_raw())
{
const uchar16_t* a = ref->c_str();
u32 p = ref->size_raw();
if (UTF16_IS_SURROGATE_LO(a[p]))
--p;
reference ret(ref, p);
return ret;
}
reference ret(ref, pos);
return ret;
}
//! Accesses the full character at the iterator's position.
const_pointer operator->() const
{
return operator*();
}
//! Accesses the full character at the iterator's position.
pointer operator->()
{
return operator*();
}
//! Is the iterator at the start of the string?
bool atStart() const
{
return pos == 0;
}
//! Is the iterator at the end of the string?
bool atEnd() const
{
const uchar16_t* a = ref->c_str();
if (UTF16_IS_SURROGATE(a[pos]))
return (pos + 1) >= ref->size_raw();
else return pos >= ref->size_raw();
}
//! Moves the iterator to the start of the string.
void toStart()
{
pos = 0;
}
//! Moves the iterator to the end of the string.
void toEnd()
{
pos = ref->size_raw();
}
//! Returns the iterator's position.
//! \return The iterator's position.
u32 getPos() const
{
return pos;
}
protected:
const ustring16<TAlloc>* ref;
u32 pos;
};
//! Iterator to iterate through a UTF-16 string.
class _ustring16_iterator : public _ustring16_const_iterator
{
public:
typedef _ustring16_iterator _Iter;
typedef _ustring16_const_iterator _Base;
typedef typename _Base::const_pointer const_pointer;
typedef typename _Base::const_reference const_reference;
typedef typename _Base::value_type value_type;
typedef typename _Base::difference_type difference_type;
typedef typename _Base::distance_type distance_type;
typedef access pointer;
typedef access reference;
using _Base::pos;
using _Base::ref;
//! Constructors.
_ustring16_iterator(const _Iter& i) : _ustring16_const_iterator(i) {}
_ustring16_iterator(const ustring16<TAlloc>& s) : _ustring16_const_iterator(s) {}
_ustring16_iterator(const ustring16<TAlloc>& s, const u32 p) : _ustring16_const_iterator(s, p) {}
//! Accesses the full character at the iterator's position.
reference operator*() const
{
if (pos >= ref->size_raw())
{
const uchar16_t* a = ref->c_str();
u32 p = ref->size_raw();
if (UTF16_IS_SURROGATE_LO(a[p]))
--p;
reference ret(ref, p);
return ret;
}
reference ret(ref, pos);
return ret;
}
//! Accesses the full character at the iterator's position.
reference operator*()
{
if (pos >= ref->size_raw())
{
const uchar16_t* a = ref->c_str();
u32 p = ref->size_raw();
if (UTF16_IS_SURROGATE_LO(a[p]))
--p;
reference ret(ref, p);
return ret;
}
reference ret(ref, pos);
return ret;
}
//! Accesses the full character at the iterator's position.
pointer operator->() const
{
return operator*();
}
//! Accesses the full character at the iterator's position.
pointer operator->()
{
return operator*();
}
};
typedef typename ustring16<TAlloc>::_ustring16_iterator iterator;
typedef typename ustring16<TAlloc>::_ustring16_const_iterator const_iterator;
///----------------------///
/// end iterator classes ///
///----------------------///
//! Default constructor
ustring16()
: array(0), allocated(1), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
array = allocator.allocate(1); // new u16[1];
array[0] = 0x0;
}
//! Constructor
ustring16(const ustring16<TAlloc>& other)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
*this = other;
}
//! Constructor from other string types
template <class B, class A>
ustring16(const string<B, A>& other)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
*this = other;
}
#ifndef USTRING_NO_STL
//! Constructor from std::string
template <class B, class A, typename Alloc>
ustring16(const std::basic_string<B, A, Alloc>& other)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
*this = other.c_str();
}
//! Constructor from iterator.
template <typename Itr>
ustring16(Itr first, Itr last)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
reserve(std::distance(first, last));
array[used] = 0;
for (; first != last; ++first)
append((uchar32_t)*first);
}
#endif
#ifndef USTRING_CPP0X_NEWLITERALS
//! Constructor for copying a character string from a pointer.
ustring16(const char* const c)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
loadDataStream(c, strlen(c));
//append((uchar8_t*)c);
}
//! Constructor for copying a character string from a pointer with a given length.
ustring16(const char* const c, u32 length)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
loadDataStream(c, length);
}
#endif
//! Constructor for copying a UTF-8 string from a pointer.
ustring16(const uchar8_t* const c)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
append(c);
}
//! Constructor for copying a UTF-8 string from a single char.
ustring16(const char c)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
append((uchar32_t)c);
}
//! Constructor for copying a UTF-8 string from a pointer with a given length.
ustring16(const uchar8_t* const c, u32 length)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
append(c, length);
}
//! Constructor for copying a UTF-16 string from a pointer.
ustring16(const uchar16_t* const c)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
append(c);
}
//! Constructor for copying a UTF-16 string from a pointer with a given length
ustring16(const uchar16_t* const c, u32 length)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;
#endif
append(c, length);
}
//! Constructor for copying a UTF-32 string from a pointer.
ustring16(const uchar32_t* const c)
: array(0), allocated(0), used(0)
{
#if __BYTE_ORDER == __BIG_ENDIAN
encoding = unicode::EUTFE_UTF16_BE;
#else
encoding = unicode::EUTFE_UTF16_LE;