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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
|
/*
* Copyright (C) 2007-2008 Xagasoft, All rights reserved.
*
* This file is part of the libbu++ library and is released under the
* terms of the license contained in the file LICENSE.
*/
#ifndef BU_F_BASIC_STRING_H
#define BU_F_BASIC_STRING_H
#include <stdint.h>
#include <memory>
#ifndef WIN32
#include <wordexp.h>
#endif
#include "bu/archival.h"
#include "bu/archive.h"
#include "bu/util.h"
namespace Bu
{
template< typename chr >
struct FStringChunk
{
long nLength;
chr *pData;
FStringChunk *pNext;
};
/**
* Flexible String class. This class was designed with string passing and
* generation in mind. Like the standard string class you can specify what
* datatype to use for each character. Unlike the standard string class,
* collection of appended and prepended terms is done lazily, making long
* operations that involve many appends very inexpensive. In addition
* internal ref-counting means that if you pass strings around between
* functions there's almost no overhead in time or memory since a reference
* is created and no data is actually copied. This also means that you
* never need to put any FBasicString into a ref-counting container class.
*
*@param chr (typename) Type of character (i.e. char)
*@param nMinSize (int) Chunk size (default: 256)
*@param chralloc (typename) Memory Allocator for chr
*@param chunkalloc (typename) Memory Allocator for chr chunks
*/
template< typename chr, int nMinSize=256, typename chralloc=std::allocator<chr>, typename chunkalloc=std::allocator<struct FStringChunk<chr> > >
class FBasicString : public Archival
{
#ifndef VALTEST
#define cpy( dest, src, size ) memcpy( dest, src, size*sizeof(chr) )
#endif
private:
//template< typename chr >
/* struct Chunk
{
long nLength;
chr *pData;
FChunk *pNext;
}; */
typedef struct FStringChunk<chr> Chunk;
typedef struct FBasicString<chr, nMinSize, chralloc, chunkalloc> MyType;
public:
FBasicString() :
nLength( 0 ),
pFirst( NULL ),
pLast( NULL )
{
}
FBasicString( const chr *pData ) :
nLength( 0 ),
pFirst( NULL ),
pLast( NULL )
{
append( pData );
}
FBasicString( const chr *pData, long nLength ) :
nLength( 0 ),
pFirst( NULL ),
pLast( NULL )
{
append( pData, nLength );
}
FBasicString( const MyType &rSrc ) :
Archival(),
nLength( 0 ),
pFirst( NULL ),
pLast( NULL )
{
if( rSrc.nLength > 0 )
{
rSrc.flatten();
append( rSrc.pFirst->pData, rSrc.nLength );
}
}
FBasicString( const MyType &rSrc, long nLength ) :
nLength( 0 ),
pFirst( NULL ),
pLast( NULL )
{
append( rSrc.pFirst->pData, nLength );
}
FBasicString( const MyType &rSrc, long nStart, long nLength ) :
nLength( 0 ),
pFirst( NULL ),
pLast( NULL )
{
append( rSrc.pFirst->pData+nStart, nLength );
}
FBasicString( long nSize ) :
nLength( nSize ),
pFirst( NULL ),
pLast( NULL )
{
pFirst = pLast = newChunk( nSize );
}
struct iterator;
struct const_iterator
{
friend class FBasicString<chr, nMinSize, chralloc, chunkalloc>;
friend struct iterator;
private:
const_iterator( Chunk *pChunk, int iPos ) :
pChunk( pChunk ),
iPos( iPos )
{
}
Chunk *pChunk;
int iPos;
public:
const_iterator( const const_iterator &i ) :
pChunk( i.pChunk ),
iPos( i.iPos )
{
}
const_iterator( const iterator &i ) :
pChunk( i.pChunk ),
iPos( i.iPos )
{
}
const_iterator() :
pChunk( NULL ),
iPos( 0 )
{
}
bool operator==( const const_iterator &i ) const
{
return pChunk == i.pChunk && iPos == i.iPos;
}
bool operator!=( const const_iterator &i ) const
{
return !(*this == i);
}
const_iterator &operator=( const const_iterator &i )
{
pChunk = i.pChunk;
iPos = i.iPos;
return *this;
}
const_iterator &operator++()
{
if( !pChunk ) return *this;
iPos++;
if( iPos >= pChunk->nLength )
{
iPos = 0;
pChunk = pChunk->pNext;
}
return *this;
}
const_iterator &operator++( int )
{
if( !pChunk ) return *this;
iPos++;
if( iPos >= pChunk->nLength )
{
iPos = 0;
pChunk = pChunk->pNext;
}
return *this;
}
const_iterator &operator+=( int iAmnt )
{
if( !pChunk ) return *this;
iPos += iAmnt;
while( iPos >= pChunk->nLength )
{
iPos -= pChunk->nLength;
pChunk = pChunk->pNext;
if( pChunk == NULL )
break;
}
return *this;
}
const_iterator operator+( int iAmnt ) const
{
if( !pChunk ) return *this;
const_iterator ret( *this );
ret += iAmnt;
return ret;
}
const chr &operator *() const
{
if( !pChunk ) throw Bu::ExceptionBase("Not a valid const_iterator.");
return pChunk->pData[iPos];
}
bool operator==( const chr &c ) const
{
if( !pChunk ) return false;
return pChunk->pData[iPos] == c;
}
bool operator!=( const chr &c ) const
{
if( !pChunk ) return false;
return pChunk->pData[iPos] != c;
}
operator bool() const
{
return pChunk != NULL;
}
bool isValid() const
{
return pChunk != NULL;
}
bool compare( const const_iterator &c ) const
{
const_iterator a = *this;
const_iterator b = c;
if( a == b )
return true;
for(; a && b; a++, b++ )
{
if( *a != *b )
return false;
}
return true;
}
bool compare( const const_iterator &c, int nLen ) const
{
const_iterator a = *this;
const_iterator b = c;
if( a == b )
return true;
for(int j = 0; a && b && j < nLen; a++, b++, j++ )
{
if( *a != *b )
return false;
}
return true;
}
bool compare( const chr *c ) const
{
if( !pChunk ) return false;
const_iterator a = *this;
for(; a && *c; a++, c++ )
{
if( *a != *c )
return false;
}
if( a.isValid() != (*c!=(chr)0) )
return false;
return true;
}
bool compare( const chr *c, int nLen ) const
{
if( !pChunk ) return false;
const_iterator a = *this;
int j = 0;
for(; a && j < nLen; a++, c++, j++ )
{
if( *a != *c )
return false;
}
if( j < nLen )
return false;
return true;
}
bool compare( const MyType &s ) const
{
if( !pChunk ) return false;
return compare( s.begin() );
}
bool compare( const MyType &s, int nLen ) const
{
if( !pChunk ) return false;
return compare( s.begin(), nLen );
}
const_iterator find( const chr c ) const
{
for( const_iterator i = *this; i; i++ )
{
if( *i == c )
return i;
}
return const_iterator( NULL, 0 );
}
const_iterator find( const chr *pStr, int nLen ) const
{
for( const_iterator i = *this; i; i++ )
{
if( i.compare( pStr, nLen ) )
return i;
}
return const_iterator( NULL, 0 );
}
const_iterator find( const MyType &s ) const
{
for( const_iterator i = *this; i; i++ )
{
if( i.compare( s ) )
return i;
}
return const_iterator( NULL, 0 );
}
const_iterator find( const MyType &s, int nLen ) const
{
for( const_iterator i = *this; i; i++ )
{
if( i.compare( s, nLen ) )
return i;
}
return const_iterator( NULL, 0 );
}
};
typedef struct iterator
{
friend class FBasicString<chr, nMinSize, chralloc, chunkalloc>;
friend struct const_iterator;
private:
iterator( Chunk *pChunk, int iPos ) :
pChunk( pChunk ),
iPos( iPos )
{
}
Chunk *pChunk;
int iPos;
public:
iterator( const iterator &i ) :
pChunk( i.pChunk ),
iPos( i.iPos )
{
}
iterator() :
pChunk( NULL ),
iPos( 0 )
{
}
operator const_iterator() const
{
return const_iterator( pChunk, iPos );
}
bool operator==( const iterator &i ) const
{
return pChunk == i.pChunk && iPos == i.iPos;
}
bool operator!=( const iterator &i ) const
{
return !(*this == i);
}
iterator &operator=( const iterator &i )
{
pChunk = i.pChunk;
iPos = i.iPos;
return *this;
}
iterator &operator++()
{
if( !pChunk ) return *this;
iPos++;
if( iPos >= pChunk->nLength )
{
iPos = 0;
pChunk = pChunk->pNext;
}
return *this;
}
iterator &operator++( int )
{
if( !pChunk ) return *this;
iPos++;
if( iPos >= pChunk->nLength )
{
iPos = 0;
pChunk = pChunk->pNext;
}
return *this;
}
iterator &operator+=( int iAmnt )
{
if( !pChunk ) return *this;
iPos += iAmnt;
while( iPos >= pChunk->nLength )
{
iPos -= pChunk->nLength;
pChunk = pChunk->pNext;
if( pChunk == NULL )
break;
}
return *this;
}
iterator operator+( int iAmnt ) const
{
if( !pChunk ) return *this;
iterator ret( *this );
ret += iAmnt;
return ret;
}
chr &operator*()
{
if( !pChunk ) throw Bu::ExceptionBase("Not a valid iterator.");
return pChunk->pData[iPos];
}
const chr &operator*() const
{
if( !pChunk ) throw Bu::ExceptionBase("Not a valid iterator.");
return pChunk->pData[iPos];
}
bool operator==( const chr &c ) const
{
if( !pChunk ) return false;
return pChunk->pData[iPos] == c;
}
bool operator!=( const chr &c ) const
{
if( !pChunk ) return false;
return pChunk->pData[iPos] != c;
}
iterator &operator=( const chr &c )
{
if( !pChunk ) throw Bu::ExceptionBase("Not a valid iterator.");
pChunk->pData[iPos] = c;
return *this;
}
operator bool() const
{
return pChunk != NULL;
}
bool isValid() const
{
return pChunk != NULL;
}
bool compare( const iterator &c ) const
{
iterator a = *this;
iterator b = c;
if( a == b )
return true;
for(; a && b; a++, b++ )
{
if( *a != *b )
return false;
}
return true;
}
bool compare( const iterator &c, int nLen ) const
{
iterator a = *this;
iterator b = c;
if( a == b )
return true;
for(int j = 0; a && b && j < nLen; a++, b++, j++ )
{
if( *a != *b )
return false;
}
return true;
}
bool compare( const chr *c ) const
{
if( !pChunk ) return false;
iterator a = *this;
for(; a && *c; a++, c++ )
{
if( *a != *c )
return false;
}
if( a.isValid() != (*c!=(chr)0) )
return false;
return true;
}
bool compare( const chr *c, int nLen ) const
{
if( !pChunk ) return false;
iterator a = *this;
int j = 0;
for(; a && j < nLen; a++, c++, j++ )
{
if( *a != *c )
return false;
}
if( j < nLen )
return false;
return true;
}
bool compare( const MyType &s ) const
{
if( !pChunk ) return false;
return compare( s.begin() );
}
bool compare( const MyType &s, int nLen ) const
{
if( !pChunk ) return false;
return compare( s.begin(), nLen );
}
iterator find( const chr c ) const
{
for( iterator i = *this; i; i++ )
{
if( *i == c )
return i;
}
return iterator( NULL, 0 );
}
iterator find( const chr *pStr, int nLen ) const
{
for( iterator i = *this; i; i++ )
{
if( i.compare( pStr, nLen ) )
return i;
}
return iterator( NULL, 0 );
}
iterator find( const MyType &s ) const
{
for( iterator i = *this; i; i++ )
{
if( i.compare( s ) )
return i;
}
return iterator( NULL, 0 );
}
iterator find( const MyType &s, int nLen ) const
{
for( iterator i = *this; i; i++ )
{
if( i.compare( s, nLen ) )
return i;
}
return iterator( NULL, 0 );
}
} iterator;
typedef struct const_iterator const_iterator;
//typedef chr *iterator;
// typedef const chr *const_iterator;
// typedef iterator const_iterator;
FBasicString( const const_iterator &s ) :
nLength( 0 ),
pFirst( NULL ),
pLast( NULL )
{
append( s );
}
FBasicString( const const_iterator &s, const const_iterator &e ) :
nLength( 0 ),
pFirst( NULL ),
pLast( NULL )
{
append( s, e );
}
virtual ~FBasicString()
{
clear();
}
/**
* Append data to your string.
*@param pData (const chr *) The data to append.
*/
void append( const chr *pData )
{
if( !pData ) return;
long nLen;
for( nLen = 0; pData[nLen] != (chr)0; nLen++ ) { }
if( nLen == 0 )
return;
Chunk *pNew = newChunk( nLen );
cpy( pNew->pData, pData, nLen );
appendChunk( pNew );
}
/**
* Append data to your string.
*@param pData (const chr *) The data to append.
*@param nLen (long) The length of the data to append.
*/
void append( const chr *pData, long nLen )
{
if( nLen == 0 )
return;
Chunk *pNew = newChunk( nLen );
cpy( pNew->pData, pData, nLen );
appendChunk( pNew );
}
/**
* Append a single chr to your string.
*@param cData (const chr &) The character to append.
*/
void append( const chr &cData )
{
if( pLast && pLast->nLength < nMinSize )
{
pLast->pData[pLast->nLength] = cData;
++pLast->nLength; ++nLength;
// pLast->pData[pLast->nLength] = (chr)0;
}
else
{
append( &cData, 1 );
}
}
/**
* Append another FString to this one.
*@param sData (MyType &) The FString to append.
*/
void append( const MyType & sData )
{
append( sData.getStr(), sData.getSize() );
}
/**
* Append another FString to this one.
*@param sData (MyType &) The FString to append.
*@param nLen How much data to append.
*/
void append( const MyType & sData, long nLen )
{
append( sData.getStr(), nLen );
}
/**
* Append another FString to this one.
*@param sData (MyType &) The FString to append.
*@param nStart Start position in sData to start copying from.
*@param nLen How much data to append.
*/
void append( const MyType & sData, long nStart, long nLen )
{
if( nLen < 0 )
nLen = sData.getSize() - nStart;
append( sData.getStr(), nStart, nLen );
}
/**
* Append data to this FString using the passed in iterator as a base.
* The iterator is const, it is not changed.
*@param s Iterator from any compatible FBasicString to copy data from.
*/
void append( const const_iterator &s )
{
if( !s.isValid() )
return;
Chunk *pSrc = s.pChunk;
Chunk *pNew = newChunk( pSrc->nLength-s.iPos );
cpy( pNew->pData, pSrc->pData+s.iPos, pSrc->nLength-s.iPos );
appendChunk( pNew );
while( (pSrc = pSrc->pNext) )
{
appendChunk( copyChunk( pSrc ) );
}
}
/**
* Append data to this FString using the passed in iterator as a base.
* The iterator is const, it is not changed.
*@param s Iterator from any compatible FBasicString to copy data from.
*/
void append( const iterator &s ) // I get complainst without this one
{
append( const_iterator( s ) );
}
/**
* Append data to this FString using the passed in iterator as a base,
* and copy data until the ending iterator is reached. The character
* at the ending iterator is not copied.
* The iterators are const, they are not changed.
*@param s Iterator from any compatible FBasicString to copy data from.
*@param e Iterator to stop copying at.
*/
void append( const const_iterator &s, const const_iterator &e )
{
if( !s.isValid() )
return;
if( !e.isValid() )
{
append( s );
return;
}
if( s.pChunk == e.pChunk )
{
// Simple case, they're the same chunk
Chunk *pNew = newChunk( e.iPos-s.iPos );
cpy( pNew->pData, s.pChunk->pData+s.iPos, e.iPos-s.iPos );
appendChunk( pNew );
}
else
{
// A little trickier, scan the blocks...
Chunk *pSrc = s.pChunk;
Chunk *pNew = newChunk( pSrc->nLength-s.iPos );
cpy( pNew->pData, pSrc->pData+s.iPos, pSrc->nLength-s.iPos );
appendChunk( pNew );
while( (pSrc = pSrc->pNext) != e.pChunk )
{
appendChunk( copyChunk( pSrc ) );
}
pNew = newChunk( e.iPos );
cpy( pNew->pData, pSrc->pData, e.iPos );
appendChunk( pNew );
}
}
/**
* Prepend another FString to this one.
*@param sData (MyType &) The FString to prepend.
*/
void prepend( const MyType & sData )
{
prepend( sData.getStr(), sData.getSize() );
}
/**
* Prepend data to your string.
*@param pData (const chr *) The data to prepend.
*/
void prepend( const chr *pData )
{
if( pData == NULL )
return;
long nLen;
for( nLen = 0; pData[nLen] != (chr)0; nLen++ ) { }
Chunk *pNew = newChunk( nLen );
cpy( pNew->pData, pData, nLen );
prependChunk( pNew );
}
/**
* Prepend data to your string.
*@param pData (const chr *) The data to prepend.
*@param nLen (long) The length of the data to prepend.
*/
void prepend( const chr *pData, long nLen )
{
Chunk *pNew = newChunk( nLen );
cpy( pNew->pData, pData, nLen );
prependChunk( pNew );
}
void insert( long nPos, const chr *pData, long nLen )
{
if( nLen <= 0 )
return;
if( nPos <= 0 )
{
prepend( pData, nLen );
}
else if( nPos >= nLength )
{
append( pData, nLen );
}
else
{
flatten();
Chunk *p1 = newChunk( nPos );
Chunk *p2 = newChunk( nLen );
Chunk *p3 = newChunk( nLength-nPos );
cpy( p1->pData, pFirst->pData, nPos );
cpy( p2->pData, pData, nLen );
cpy( p3->pData, pFirst->pData+nPos, nLength-nPos );
clear();
appendChunk( p1 );
appendChunk( p2 );
appendChunk( p3 );
}
}
void insert( long nPos, const MyType &str )
{
if( nPos <= 0 )
{
prepend( str );
}
else if( nPos >= nLength )
{
append( str );
}
else
{
flatten();
Chunk *p1 = newChunk( nPos );
Chunk *p3 = newChunk( nLength-nPos );
cpy( p1->pData, pFirst->pData, nPos );
cpy( p3->pData, pFirst->pData+nPos, nLength-nPos );
clear();
appendChunk( p1 );
for( Chunk *pChnk = str.pFirst; pChnk; pChnk = pChnk->pNext )
{
appendChunk( copyChunk( pChnk ) );
}
appendChunk( p3 );
}
}
void insert( long nPos, const chr *pData )
{
insert( nPos, pData, strlen( pData ) );
}
void remove( long nPos, long nLen )
{
if( nLen <= 0 || nPos < 0 || nPos >= nLength )
return;
if( nLen > nLength-nPos )
nLen = nLength-nPos;
flatten();
cpy( pFirst->pData+nPos, pFirst->pData+nPos+nLen, nLength-nPos-nLen+1 );
nLength -= nLen;
pFirst->nLength -= nLen;
}
/**
* Clear all data from the string.
*/
void clear()
{
realClear();
}
/**
* Force the string to resize
*@param nNewSize (long) The new size of the string.
*/
void resize( long nNewSize )
{
if( nLength == nNewSize )
return;
if( nNewSize < 0 )
nNewSize = 0;
flatten();
Chunk *pNew = newChunk( nNewSize );
long nNewLen = (nNewSize<nLength)?(nNewSize):(nLength);
if( nLength > 0 )
{
cpy( pNew->pData, pFirst->pData, nNewLen );
aChr.deallocate( pFirst->pData, pFirst->nLength+1 );
aChunk.deallocate( pFirst, 1 );
}
pNew->pData[nNewLen] = (chr)0;
pFirst = pLast = pNew;
nLength = nNewSize;
}
/**
* Get the current size of the string.
*@returns (long) The current size of the string.
*/
long getSize() const
{
return nLength;
}
/**
* Get a pointer to the string array.
*@returns (chr *) The string data.
*/
chr *getStr()
{
if( pFirst == NULL )
return (chr *)"";
flatten();
pFirst->pData[nLength] = (chr)0;
return pFirst->pData;
}
/**
* Get a const pointer to the string array.
*@returns (const chr *) The string data.
*/
const chr *getStr() const
{
if( pFirst == NULL )
return (chr *)"";
flatten();
pFirst->pData[nLength] = (chr)0;
return pFirst->pData;
}
MyType getSubStrIdx( long iStart, long iSize=-1 ) const
{
if( iStart < 0 )
iStart = 0;
if( iStart >= nLength )
return "";
if( iSize < 0 )
iSize = nLength;
if( iStart+iSize > nLength )
iSize = nLength-iStart;
if( iSize == 0 )
return "";
flatten();
MyType ret( pFirst->pData+iStart, iSize );
return ret;
}
MyType getSubStr( const_iterator iBegin,
const_iterator iEnd=typename MyType::const_iterator() ) const
{
if( !iBegin.isValid() )
return MyType();
if( iBegin.pChunk == iEnd.pChunk )
{
return MyType( iBegin.pChunk->pData+iBegin.iPos,
iEnd.iPos-iBegin.iPos );
}
else if( !iEnd.isValid() )
{
MyType ret;
ret.append(
iBegin.pChunk->pData+iBegin.iPos,
iBegin.pChunk->nLength-iBegin.iPos
);
for( Chunk *pCur = iBegin.pChunk->pNext;
pCur; pCur = pCur->pNext )
{
ret.append( pCur->pData, pCur->nLength );
}
return ret;
}
else
{
MyType ret;
ret.append(
iBegin.pChunk->pData+iBegin.iPos,
iBegin.pChunk->nLength-iBegin.iPos
);
for( Chunk *pCur = iBegin.pChunk->pNext;
pCur != iEnd.pChunk; pCur = pCur->pNext )
{
ret.append( pCur->pData, pCur->nLength );
}
ret.append(
iEnd.pChunk->pData,
iEnd.iPos
);
return ret;
}
}
/**
* (std::string compatability) Get a pointer to the string array.
*@returns (chr *) The string data.
*/
DEPRECATED
chr *c_str()
{
if( pFirst == NULL )
return NULL;
flatten();
pFirst->pData[nLength] = (chr)0;
return pFirst->pData;
}
/**
* (std::string compatability) Get a const pointer to the string array.
*@returns (const chr *) The string data.
*/
DEPRECATED
const chr *c_str() const
{
if( pFirst == NULL )
return NULL;
flatten();
pFirst->pData[nLength] = (chr)0;
return pFirst->pData;
}
Bu::List<MyType> split( const chr c ) const
{
Bu::List<MyType> ret;
const_iterator l, r;
l = begin();
for(r=l; l;)
{
for( r = l; r && r != c; r++ ) { }
ret.append( MyType( l, r ) );
l = r;
l++;
}
return ret;
}
/**
* Plus equals operator for FString.
*@param pData (const chr *) The data to append to your FString.
*/
MyType &operator+=( const chr *pData )
{
append( pData );
return (*this);
}
/**
* Plus equals operator for FString.
*@param rSrc (const MyType &) The FString to append to your FString.
*/
MyType &operator+=( const MyType &rSrc )
{
if( rSrc.nLength == 0 )
return (*this);
rSrc.flatten();
append( rSrc.pFirst->pData, rSrc.nLength );
return (*this);
}
/**
* Plus equals operator for FString.
*@param cData (const chr) The character to append to your FString.
*/
MyType &operator+=( const chr cData )
{
if( pLast && pLast->nLength < nMinSize )
{
pLast->pData[pLast->nLength] = cData;
++pLast->nLength; ++nLength;
// pLast->pData[pLast->nLength] = (chr)0;
}
else
{
append( &cData, 1 );
}
//append( pData );
return (*this);
}
/**
* Assignment operator.
*@param pData (const chr *) The character array to append to your
* FString.
*/
MyType &operator=( const chr *pData )
{
clear();
append( pData );
return (*this);
}
MyType &operator=( const std::basic_string<chr> &rData )
{
clear();
append( rData.c_str(), rData.size() );
return (*this);
}
MyType operator+( const MyType &rRight ) const
{
MyType ret( *this );
ret.append( rRight );
return ret;
}
MyType operator+( const chr *pRight ) const
{
MyType ret( *this );
ret.append( pRight );
return ret;
}
MyType operator+( chr *pRight ) const
{
MyType ret( *this );
ret.append( pRight );
return ret;
}
/**
* Reset your FString to this character array.
*@param pData (const chr *) The character array to set your FString to.
*/
void set( const chr *pData )
{
clear();
append( pData );
}
/**
* Reset your FString to this character array.
*@param pData (const chr *) The character array to set your FString to.
*@param nSize (long) The length of the inputted character array.
*/
void set( const chr *pData, long nSize )
{
clear();
append( pData, nSize );
}
void set( const chr *pData, long nStart, long nSize )
{
clear();
append( pData, nStart, nSize );
}
void set( const MyType &rData )
{
clear();
append( rData );
}
void set( const MyType &rData, long nSize )
{
clear();
append( rData, nSize );
}
void set( const MyType &rData, long nStart, long nSize )
{
clear();
append( rData, nStart, nSize );
}
void set( const_iterator s )
{
clear();
append( s );
}
void set( const_iterator s, const_iterator e )
{
clear();
append( s, e );
}
void expand()
{
flatten();
#ifndef WIN32
wordexp_t result;
/* Expand the string for the program to run. */
switch (wordexp (pFirst->pData, &result, 0))
{
case 0: /* Successful. */
{
set( result.we_wordv[0] );
wordfree( &result );
return;
}
break;
case WRDE_NOSPACE:
/* If the error was `WRDE_NOSPACE',
then perhaps part of the result was allocated. */
wordfree (&result);
default: /* Some other error. */
return;
}
#endif
}
/**
* Assignment operator.
*@param rSrc (const MyType &) The FString to set your FString to.
*/
MyType &operator=( const MyType &rSrc )
{
copyFrom( rSrc );
return (*this);
}
/**
* Equals comparison operator.
*@param pData (const chr *) The character array to compare your FString
* to.
*/
bool operator==( const chr *pData ) const
{
if( pFirst == NULL ) {
if( pData == NULL )
return true;
if( pData[0] == (chr)0 )
return true;
return false;
}
flatten();
pFirst->pData[nLength] = (chr)0;
const chr *a = pData;
chr *b = pFirst->pData;
for( long j = 0; *a!=(chr)0 || *b!=(chr)0; j++, a++, b++ )
{
if( *a != *b )
return false;
if( *a == (chr)0 && j < nLength )
return false;
}
return true;
}
/**
* Equals comparison operator.
*@param pData (const MyType &) The FString to compare your FString to.
*/
bool operator==( const MyType &pData ) const
{
if( pFirst == pData.pFirst )
return true;
if( pFirst == NULL )
return false;
if( nLength != pData.nLength )
return false;
flatten();
pData.flatten();
const chr *a = pData.pFirst->pData;
chr *b = pFirst->pData;
for( long j = 0; j < nLength; j++, a++, b++ )
{
if( *a != *b )
return false;
}
return true;
}
/**
* Not equals comparison operator.
*@param pData (const chr *) The character array to compare your FString
* to.
*/
bool operator!=(const chr *pData ) const
{
return !(*this == pData);
}
/**
* Not equals comparison operator.
*@param pData (const MyType &) The FString to compare your FString to.
*/
bool operator!=(const MyType &pData ) const
{
return !(*this == pData);
}
bool operator<(const MyType &pData ) const
{
flatten();
pData.flatten();
const chr *a = pData.pFirst->pData;
chr *b = pFirst->pData;
for( long j = 0; j < nLength; j++, a++, b++ )
{
if( *a != *b )
return *a < *b;
}
return false;
}
bool operator>(const MyType &pData ) const
{
flatten();
pData.flatten();
const chr *a = pData.pFirst->pData;
chr *b = pFirst->pData;
for( long j = 0; j < nLength; j++, a++, b++ )
{
if( *a != *b )
return *a > *b;
}
return false;
}
/**
* Indexing operator
*@param nIndex (long) The index of the character you want.
*@returns (chr &) The character at position (nIndex).
*/
chr &operator[]( long nIndex )
{
if( nIndex < 0 || nIndex >= nLength )
throw Bu::ExceptionBase("Index out of range.");
flatten();
return pFirst->pData[nIndex];
}
/**
* Const indexing operator
*@param nIndex (long) The index of the character you want.
*@returns (const chr &) The character at position (nIndex).
*/
const chr &operator[]( long nIndex ) const
{
if( nIndex < 0 || nIndex >= nLength )
throw Bu::ExceptionBase("Index out of range.");
flatten();
return pFirst->pData[nIndex];
}
/*
operator const chr *() const
{
if( !pFirst ) return NULL;
flatten();
return pFirst->pData;
}
*/
operator bool() const
{
return (pFirst != NULL);
}
bool isSet() const
{
return (pFirst != NULL);
}
bool compareSub( const chr *pData, long nIndex, long nLen ) const
{
if( pFirst == NULL ) {
if( pData == NULL )
return true;
if( pData[0] == (chr)0 )
return true;
return false;
}
if( nIndex+nLen > nLength )
return false;
flatten();
pFirst->pData[nLength] = (chr)0;
const chr *a = pData;
chr *b = pFirst->pData+nIndex;
for( long j = 0; j < nLen; j++, a++, b++ )
{
if( *a != *b )
return false;
if( *a == (chr)0 && j < nLength )
return false;
}
return true;
}
bool compareSub( const MyType &rData, long nIndex, long nLen ) const
{
if( pFirst == NULL || rData.pFirst == NULL )
return false;
if( nLen < 0 )
nLen = rData.nLength;
if( nIndex+nLen > nLength )
return false;
flatten();
rData.flatten();
const chr *a = rData.pFirst->pData;
chr *b = pFirst->pData + nIndex;
for( long j = 0; j < nLen; j++, a++, b++ )
{
if( *a != *b )
return false;
}
return true;
}
/**
* Is the character at index (nIndex) white space?
*@param nIndex (long) The index of the character you want to check.
*@returns (bool) Is it white space?
*/
bool isWS( long nIndex ) const
{
flatten();
return pFirst->pData[nIndex]==' ' || pFirst->pData[nIndex]=='\t'
|| pFirst->pData[nIndex]=='\r' || pFirst->pData[nIndex]=='\n';
}
/**
* Is the character at index (nIndex) a letter?
*@param nIndex (long) The index of the character you want to check.
*@returns (bool) Is it a letter?
*/
bool isAlpha( long nIndex ) const
{
flatten();
return (pFirst->pData[nIndex] >= 'a' && pFirst->pData[nIndex] <= 'z')
|| (pFirst->pData[nIndex] >= 'A' && pFirst->pData[nIndex] <= 'Z');
}
/**
* Convert your alpha characters to lower case.
*/
void toLower()
{
flatten();
for( long j = 0; j < nLength; j++ )
{
if( pFirst->pData[j] >= 'A' && pFirst->pData[j] <= 'Z' )
pFirst->pData[j] -= 'A'-'a';
}
}
/**
* Convert your alpha characters to upper case.
*/
void toUpper()
{
flatten();
for( long j = 0; j < nLength; j++ )
{
if( pFirst->pData[j] >= 'a' && pFirst->pData[j] <= 'z' )
pFirst->pData[j] += 'A'-'a';
}
}
const_iterator find( const chr cChar,
const_iterator iStart=typename MyType::const_iterator() ) const
{
if( !iStart ) iStart = begin();
for( ; iStart; iStart++ )
{
if( cChar == *iStart )
return iStart;
}
return end();
}
const_iterator find( const chr *sText, int nLen,
const_iterator iStart=typename MyType::const_iterator() ) const
{
if( !iStart ) iStart = begin();
for( ; iStart; iStart++ )
{
if( iStart.compare( sText, nLen ) )
return iStart;
}
return end();
}
const_iterator find( const MyType &rStr,
const_iterator iStart=typename MyType::const_iterator() ) const
{
if( !iStart ) iStart = begin();
for( ; iStart; iStart++ )
{
if( iStart.compare( rStr ) )
return iStart;
}
return end();
}
const_iterator find( const MyType &rStr, int nLen,
const_iterator iStart=typename MyType::const_iterator() ) const
{
if( !iStart ) iStart = begin();
for( ; iStart; iStart++ )
{
if( iStart.compare( rStr, nLen ) )
return iStart;
}
return end();
}
iterator find( const chr cChar,
const_iterator iStart=typename MyType::const_iterator() )
{
if( !iStart ) iStart = begin();
for( ; iStart; iStart++ )
{
if( cChar == *iStart )
return iterator( iStart.pChunk, iStart.iPos );
}
return end();
}
iterator find( const chr *sText, int nLen,
const_iterator iStart=typename MyType::const_iterator() )
{
if( !iStart ) iStart = begin();
for( ; iStart; iStart++ )
{
if( iStart.compare( sText, nLen ) )
return iterator( iStart.pChunk, iStart.iPos );
}
return end();
}
iterator find( const MyType &rStr,
const_iterator iStart=typename MyType::const_iterator() )
{
if( !iStart ) iStart = begin();
for( ; iStart; iStart++ )
{
if( iStart.compare( rStr ) )
return iterator( iStart.pChunk, iStart.iPos );
}
return end();
}
iterator find( const MyType &rStr, int nLen,
const_iterator iStart=typename MyType::const_iterator() )
{
if( !iStart ) iStart = begin();
for( ; iStart; iStart++ )
{
if( iStart.compare( rStr, nLen ) )
return iterator( iStart.pChunk, iStart.iPos );
}
return end();
}
/**
* Find the index of the first occurrance of cChar
*@param cChar The character to search for.
*@param iStart The position in the string to start searching from.
*@returns (long) The index of the first occurrance. -1 for not found.
*/
long findIdx( const chr cChar, long iStart=0 ) const
{
flatten();
for( long j = iStart; j < pFirst->nLength; j++ )
{
if( pFirst->pData[j] == cChar )
return j;
}
return -1;
}
/**
* Find the index of the first occurrance of sText
*@param sText The null-terminated string to search for.
*@param iStart The position in the string to start searching from.
*@returns The index of the first occurrance. -1 for not found.
*/
long findIdx( const chr *sText, long iStart=0 ) const
{
long nTLen = strlen( sText );
flatten();
for( long j = iStart; j < pFirst->nLength-nTLen; j++ )
{
if( !strncmp( sText, pFirst->pData+j, nTLen ) )
return j;
}
return -1;
}
/**
* Do a reverse search for (sText)
*@param sText (const chr *) The string to search for.
*@returns (long) The index of the last occurrance. -1 for not found.
*/
long rfindIdx( const chr *sText ) const
{
long nTLen = strlen( sText );
flatten();
for( long j = pFirst->nLength-nTLen-1; j >= 0; j-- )
{
if( !strncmp( sText, pFirst->pData+j, nTLen ) )
return j;
}
return -1;
}
/**
* Remove nAmnt bytes from the front of the string. This function
* operates in O(n) time and should be used sparingly.
*/
void trimFront( long nAmnt )
{
long nNewLen = nLength - nAmnt;
flatten();
Chunk *pNew = newChunk( nNewLen );
cpy( pNew->pData, pFirst->pData+nAmnt, nNewLen );
clear();
appendChunk( pNew );
}
void format( const char *sFrmt, ...)
{
clear();
va_list ap;
va_start( ap, sFrmt );
long iLen = vsnprintf( NULL, 0, sFrmt, ap );
Chunk *pNew = newChunk( iLen );
vsnprintf( pNew->pData, iLen+1, sFrmt, ap );
appendChunk( pNew );
va_end( ap );
}
void formatAppend( const char *sFrmt, ...)
{
va_list ap;
va_start( ap, sFrmt );
long iLen = vsnprintf( NULL, 0, sFrmt, ap );
Chunk *pNew = newChunk( iLen );
vsnprintf( pNew->pData, iLen+1, sFrmt, ap );
appendChunk( pNew );
va_end( ap );
}
void formatPrepend( const char *sFrmt, ...)
{
va_list ap;
va_start( ap, sFrmt );
long iLen = vsnprintf( NULL, 0, sFrmt, ap );
Chunk *pNew = newChunk( iLen );
vsnprintf( pNew->pData, iLen+1, sFrmt, ap );
prependChunk( pNew );
va_end( ap );
}
/**
* Function the archiver calls to archive your FString.
*@param ar (Archive) The archive which is archiving your FString.
*/
void archive( class Archive &ar )
{
if( ar.isLoading() )
{
clear();
long nLen;
ar >> nLen;
if( nLen > 0 )
{
Chunk *pNew = newChunk( nLen );
ar.read( pNew->pData, nLen*sizeof(chr) );
appendChunk( pNew );
}
}
else
{
flatten();
ar << nLength;
if( nLength )
ar.write( pFirst->pData, nLength*sizeof(chr) );
}
}
iterator begin()
{
if( nLength == 0 )
return iterator( NULL, 0 );
return iterator( pFirst, 0 );
}
const_iterator begin() const
{
if( nLength == 0 )
return const_iterator( NULL, 0 );
return iterator( pFirst, 0 );
}
iterator end()
{
return iterator( NULL, 0 );
}
const_iterator end() const
{
return const_iterator( NULL, 0 );
}
bool isEmpty() const
{
if( nLength == 0 )
return true;
return false;
}
private:
void flatten() const
{
if( isFlat() )
return;
if( pFirst == NULL )
return;
Chunk *pNew = newChunk( nLength );
chr *pos = pNew->pData;
Chunk *i = pFirst;
for(;;)
{
cpy( pos, i->pData, i->nLength );
pos += i->nLength;
i = i->pNext;
if( i == NULL )
break;
}
realClear();
pLast = pFirst = pNew;
nLength = pNew->nLength;
}
void realClear() const
{
if( pFirst == NULL )
return;
Chunk *i = pFirst;
for(;;)
{
Chunk *n = i->pNext;
aChr.deallocate( i->pData, i->nLength+1 );
aChunk.deallocate( i, 1 );
if( n == NULL )
break;
i = n;
}
pFirst = pLast = NULL;
nLength = 0;
}
void copyFrom( const FBasicString<chr, nMinSize, chralloc, chunkalloc> &rSrc )
{
if( rSrc.pFirst == NULL )
{
clear();
return;
}
Chunk *pNew = newChunk( rSrc.nLength );
chr *pos = pNew->pData;
Chunk *i = rSrc.pFirst;
for(;;)
{
cpy( pos, i->pData, i->nLength );
pos += i->nLength;
i = i->pNext;
if( i == NULL )
break;
}
clear();
appendChunk( pNew );
}
bool isFlat() const
{
return (pFirst == pLast);
}
Chunk *newChunk() const
{
Chunk *pNew = aChunk.allocate( 1 );
pNew->pNext = NULL;
return pNew;
}
Chunk *newChunk( long nLen ) const
{
Chunk *pNew = aChunk.allocate( 1 );
pNew->pNext = NULL;
pNew->nLength = nLen;
pNew->pData = aChr.allocate( (nLen<nMinSize)?(nMinSize):(nLen)+1 );
pNew->pData[nLen] = (chr)0;
return pNew;
}
Chunk *copyChunk( Chunk *pSrc ) const
{
Chunk *pNew = aChunk.allocate( 1 );
pNew->pNext = pSrc->pNext;
pNew->nLength = pSrc->nLength;
pNew->pData = aChr.allocate( pSrc->nLength+1 );
cpy( pNew->pData, pSrc->pData, pSrc->nLength );
pNew->pData[pNew->nLength] = (chr)0;
return pNew;
}
void appendChunk( Chunk *pNewChunk )
{
if( pFirst == NULL )
pLast = pFirst = pNewChunk;
else
{
pLast->pNext = pNewChunk;
pLast = pNewChunk;
}
nLength += pNewChunk->nLength;
}
void prependChunk( Chunk *pNewChunk )
{
if( pFirst == NULL )
pLast = pFirst = pNewChunk;
else
{
pNewChunk->pNext = pFirst;
pFirst = pNewChunk;
}
nLength += pNewChunk->nLength;
}
#ifdef VALTEST
void cpy( chr *dest, const chr *src, long count ) const
{
for( int j = 0; j < count; j++ )
{
*dest = *src;
dest++;
src++;
}
}
#endif
private:
mutable long nLength;
mutable Chunk *pFirst;
mutable Chunk *pLast;
mutable chralloc aChr;
mutable chunkalloc aChunk;
};
template<class T> FBasicString<T> operator+( const T *pLeft, const FBasicString<T> &rRight )
{
Bu::FBasicString<T> ret( pLeft );
ret.append( rRight );
return ret;
}
}
#ifndef VALTEST
#undef cpy
#endif
#endif
|