summaryrefslogtreecommitdiff
path: root/src/stable/string.h
blob: af147a7c76e7fa03eba4e2fdedd15b66c54f763d (plain)
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
/*
 * Copyright (C) 2007-2014 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_STRING_H
#define BU_STRING_H

#include <stdint.h>
#include <memory>

#include "bu/util.h"
#include "bu/sharedcore.h"
#include "bu/exceptionbase.h"
#include "bu/archivebase.h"
#include "bu/list.h"
#include "bu/fmt.h"
#include "bu/variant.h"
#include <string.h>

namespace Bu
{
    class String;
    class MemBuf;

    /** @cond DEVEL */
    class StringCore
    {
    friend class String;
    friend class SharedCore<String, StringCore>;
    private:
        struct Chunk
        {
            long nLength;
            char *pData;
            Chunk *pNext;
        };

        StringCore();
        StringCore( const StringCore &rSrc );
        virtual ~StringCore();

        mutable long nLength;
        mutable Chunk *pFirst;
        mutable Chunk *pLast;

        void clear() const;
        Chunk *newChunk() const;
        Chunk *newChunk( long nLen ) const;
        Chunk *copyChunk( Chunk *pSrc ) const;
        void appendChunk( Chunk *pNewChunk );
        void prependChunk( Chunk *pNewChunk );
    };
    /** @endcond */

    /**
     */
    class String : public SharedCore<String, StringCore>
    {
    protected:
        using SharedCore<String, StringCore >::core;
        using SharedCore<String, StringCore >::_hardCopy;

    private:
        typedef StringCore::Chunk Chunk;

    public: // Iterators
        struct iterator;
        typedef struct const_iterator
        {
            friend class String;
            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() :
                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=( 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 char &operator *() const
            {
                if( !pChunk ) throw Bu::ExceptionBase("Not a valid const_iterator.");
                return pChunk->pData[iPos];
            }

            bool operator==( const char &c ) const
            {
                if( !pChunk ) return false;
                return pChunk->pData[iPos] == c;
            }

            bool operator!=( const char &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;
                }
                if( (bool)a != (bool)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; j < nLen; a++, b++, j++ )
                {
                    if( !a || !b || *a != *b )
                        return false;
                }
                return true;
            }

            bool compare( const char *c ) const
            {
                if( !pChunk ) return false;
                const_iterator a = *this;
                for(; a && *c; a++, c++ )
                {
                    if( *a != *c )
                        return false;
                }
                if( a.isValid() != (*c!=(char)0) )
                    return false;
                return true;
            }

            bool compare( const char *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 String &s ) const
            {
                if( !pChunk ) return false;
                return compare( s.begin() );
            }

            bool compare( const String &s, int nLen ) const
            {
                if( !pChunk ) return false;
                return compare( s.begin(), nLen );
            }

            const_iterator find( const char c ) const
            {
                for( const_iterator i = *this; i; i++ )
                {
                    if( *i == c )
                        return i;
                }
                return const_iterator( NULL, 0 );
            }

            const_iterator find( const char *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 String &s ) const
            {
                for( const_iterator i = *this; i; i++ )
                {
                    if( i.compare( s ) )
                        return i;
                }
                return const_iterator( NULL, 0 );
            }

            const_iterator find( const String &s, int nLen ) const
            {
                for( const_iterator i = *this; i; i++ )
                {
                    if( i.compare( s, nLen ) )
                        return i;
                }
                return const_iterator( NULL, 0 );
            }
        } const_iterator;
        
        typedef struct iterator
        {
            friend class String;
            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);
            }
            
            bool operator==( const const_iterator &i ) const
            {
                return pChunk == i.pChunk && iPos == i.iPos;
            }

            bool operator!=( const 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;
            }

            char &operator*()
            {
                if( !pChunk ) throw Bu::ExceptionBase("Not a valid iterator.");
                return pChunk->pData[iPos];
            }

            const char &operator*() const
            {
                if( !pChunk ) throw Bu::ExceptionBase("Not a valid iterator.");
                return pChunk->pData[iPos];
            }

            bool operator==( const char &c ) const
            {
                if( !pChunk ) return false;
                return pChunk->pData[iPos] == c;
            }

            bool operator!=( const char &c ) const
            {
                if( !pChunk ) return false;
                return pChunk->pData[iPos] != c;
            }

            iterator &operator=( const char &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 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;
                }
                if( (bool)a != (bool)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; j < nLen; a++, b++, j++ )
                {
                    if( !a || !b || *a != *b )
                        return false;
                }
                return true;
            }

            bool compare( const char *c ) const
            {
                if( !pChunk ) return false;
                iterator a = *this;
                for(; a && *c; a++, c++ )
                {
                    if( *a != *c )
                        return false;
                }
                if( a.isValid() != (*c!=(char)0) )
                    return false;
                return true;
            }

            bool compare( const char *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 String &s ) const
            {
                if( !pChunk ) return false;
                return compare( s.begin() );
            }

            bool compare( const String &s, int nLen ) const
            {
                if( !pChunk ) return false;
                return compare( s.begin(), nLen );
            }

            iterator find( const char c ) const
            {
                for( iterator i = *this; i; i++ )
                {
                    if( *i == c )
                        return i;
                }
                return iterator( NULL, 0 );
            }

            iterator find( const char *pStr, int nLen ) const
            {
                for( iterator i = *this; i; i++ )
                {
                    if( i.compare( pStr, nLen ) )
                        return i;
                }
                return iterator( NULL, 0 );
            }

            iterator find( const String &s ) const
            {
                for( iterator i = *this; i; i++ )
                {
                    if( i.compare( s ) )
                        return i;
                }
                return iterator( NULL, 0 );
            }

            iterator find( const String &s, int nLen ) const
            {
                for( iterator i = *this; i; i++ )
                {
                    if( i.compare( s, nLen ) )
                        return i;
                }
                return iterator( NULL, 0 );
            }
        } iterator;

    public:
        String();
        String( const const_iterator &s );
        String( const const_iterator &s, const const_iterator &e );
        String( const char *pData );
        String( const char *pData, long nLength );
        String( const String &rSrc );
        String( const String &rSrc, long nLength );
        String( const String &rSrc, long nStart, long nLength );
        String( long nSize );
        virtual ~String();

        /**
         * Append data to your string.
         *@param pData (const char *) The data to append.
         */
        void append( const char *pData );

        /**
         * Append data to your string.
         *@param pData (const char *) The data to append.
         *@param nLen (long) The length of the data to append.
         */
        void append( const char *pData, long nLen );
        
        /**
         * Append data to your string.
         *@param pData (const char *) The data to append.
         *@param nStart (long) The start position to copy from.
         *@param nLen (long) The length of the data to append.
         */
        void append( const char *pData, long nStart, long nLen );

        /**
         * Append a single char to your string.
         *@param cData (const char &) The character to append.
         */
        void append( const char &cData );

        /**
         * Append another String to this one.
         *@param sData (String &) The String to append.
         *@todo This function can be made much faster by not using getStr()
         */
        void append( const String & sData );
        
        /**
         * Append another String to this one.
         *@param sData (String &) The String to append.
         *@param nLen How much data to append.
         *@todo This function can be made much faster by not using getStr()
         */
        void append( const String & sData, long nLen );
        
        /**
         * Append another String to this one.
         *@param sData (String &) The String to append.
         *@param nStart Start position in sData to start copying from.
         *@param nLen How much data to append.
         *@todo This function can be made much faster by not using getStr()
         */
        void append( const String & sData, long nStart, long nLen );
        
        /**
         * Append data to this String using the passed in iterator as a base.
         * The iterator is const, it is not changed.
         *@param s Iterator from any compatible String to copy data from.
         */
        void append( const const_iterator &s );

        /**
         * Append data to this String using the passed in iterator as a base.
         * The iterator is const, it is not changed.
         *@param s Iterator from any compatible String to copy data from.
         */
        void append( const iterator &s );

        /**
         * Append data to this String 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 String to copy data from.
         *@param e Iterator to stop copying at.
         */
        void append( const const_iterator &s, const const_iterator &e );
        
        /**
         * Prepend another String to this one.
         *@param sData (String &) The String to prepend.
         *@todo This function can be made much faster by not using getStr()
         */
        void prepend( const String & sData );

        /**
         * Prepend data to your string.
         *@param pData (const char *) The data to prepend.
         */
        void prepend( const char *pData );

        /**
         * Prepend data to your string.
         *@param pData (const char *) The data to prepend.
         *@param nLen (long) The length of the data to prepend.
         */
        void prepend( const char *pData, long nLen );

        void prepend( const char c );

        /**
         * Insert pData before byte nPos, that is, the first byte of pData will
         * start at nPos.  This could probably be made faster by avoiding
         * flattening.
         */
        void insert( long nPos, const char *pData, long nLen );

        void insert( long nPos, const String &str );

        /**
         *@todo This function shouldn't use strlen, we should add our own to
         * this class, one that can be overridden in a specific implementation.
         */
        void insert( long nPos, const char *pData );

        void remove( long nPos, long nLen );

        /**
         * Clear all data from the string.
         */
        void clear();

        String replace( const String &fnd, const String &rep ) const;

        /**
         * Force the string to resize
         *@param nNewSize (long) The new size of the string.
         */
        void resize( long nNewSize );

        /**
         * Get the current size of the string.
         *@returns (long) The current size of the string.
         */
        long getSize() const;
        
        /**
         * Get a pointer to the string array.
         *@returns (char *) The string data.
         */
        char *getStr();
        
        /**
         * Get a const pointer to the string array.
         *@returns (const char *) The string data.
         */
        const char *getStr() const;

        /**
         * A convinience function, this one won't cause as much work as the
         * non-const getStr, so if you're not changing the data, consider it.
         */
        const char *getConstStr() const;

        String getSubStrIdx( long iStart, long iSize=-1 ) const;

        String getSubStr( const_iterator iBegin,
                const_iterator iEnd=String::const_iterator() ) const;

        Bu::List<String> split( const char c ) const;

        /**
         * Plus equals operator for String.
         *@param pData (const char *) The data to append to your String.
         */
        String &operator+=( const char *pData );
        
        /**
         * Plus equals operator for String.
         *@param rSrc (const String &) The String to append to your String.
         */
        String &operator+=( const String &rSrc );

        String &operator+=( const String::const_iterator &i );

        /**
         * Plus equals operator for String.
         *@param cData (const char) The character to append to your String.
         */
        String &operator+=( const char cData );

        /**
         * Assignment operator.
         *@param pData (const char *) The character array to append to your
         *      String.
         */
        String &operator=( const char *pData );

        String operator+( const String &rRight ) const;

        String operator+( const char *pRight ) const;

        String operator+( char *pRight ) const;

        /**
         * Reset your String to this character array.
         *@param pData (const char *) The character array to set your String to.
         */
        void set( const char *pData );

        /**
         * Reset your String to this character array.
         *@param pData (const char *) The character array to set your String to.
         *@param nSize (long) The length of the inputted character array.
         */
        void set( const char *pData, long nSize );

        void set( const char *pData, long nStart, long nSize );
        
        void set( const String &rData );

        void set( const String &rData, long nSize );

        void set( const String &rData, long nStart, long nSize );

        void set( const_iterator s );

        void set( const_iterator s, const_iterator e );

        /**
         * Resize the string, possibly to make room for a copy.  At the moment
         * this operation *is* destructive.  What was in the string will in no
         * way be preserved.  This is, however, very fast.  If you want to
         * keep your data check out resize.
         *@param iSize the new size in bytes.  The string is guranteed to have
         * at least this much contiguous space available when done.
         */
        void setSize( long iSize );

        /**
         * Equals comparison operator.
         *@param pData (const char *) The character array to compare your String
         *      to.
         */
        bool operator==( const char *pData ) const;
        
        /**
         * Equals comparison operator.
         *@param pData (const String &) The String to compare your String to.
         */
        bool operator==( const String &pData ) const;

        /**
         * Not equals comparison operator.
         *@param pData (const char *) The character array to compare your String
         *      to.
         */
        bool operator!=(const char *pData ) const;
        
        /**
         * Not equals comparison operator.
         *@param pData (const String &) The String to compare your String to.
         */
        bool operator!=(const String &pData ) const;

        bool operator<(const String &pData ) const;
        
        bool operator<=(const String &pData ) const;

        bool operator>(const String &pData ) const;

        bool operator>=(const String &pData ) const;

        /**
         * Indexing operator
         *@param nIndex (long) The index of the character you want.
         *@returns (char &) The character at position (nIndex).
         */
        char &operator[]( long nIndex );
        
        /**
         * Const indexing operator
         *@param nIndex (long) The index of the character you want.
         *@returns (const char &) The character at position (nIndex).
         */
        const char &operator[]( long nIndex ) const;

        bool isSet() const;

        bool compareSub( const char *pData, long nIndex, long nLen ) const;

        bool compareSub( const String &rData, long nIndex, long nLen ) const;
        
        /**
         * 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;

        /**
         * 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;

        /**
         * Convert your alpha characters to lower case.
         */
        String toLower() const;

        /**
         * Convert your alpha characters to upper case.
         */
        String toUpper() const;

        int16_t toInt16( int iRadix=10 ) const;
        int32_t toInt32( int iRadix=10 ) const;
        int64_t toInt64( int iRadix=10 ) const;

        const_iterator find( const char cChar,
                const_iterator iStart=const_iterator() ) const;

        const_iterator find( const char *sText, int nLen,
                const_iterator iStart=const_iterator() ) const;

        const_iterator find( const String &rStr,
                const_iterator iStart=const_iterator() ) const;

        const_iterator find( const String &rStr, int nLen,
                const_iterator iStart=const_iterator() ) const;

        iterator find( const char cChar,
                const_iterator iStart=const_iterator() );

        iterator find( const char *sText, int nLen,
                const_iterator iStart=const_iterator() );

        iterator find( const String &rStr,
                const_iterator iStart=const_iterator() );

        iterator find( const String &rStr, int nLen,
                const_iterator iStart=const_iterator() );

        /**
         * 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 char cChar, long iStart=0 ) const;
        
        /**
         * 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 char *sText, long iStart=0 ) const;

        /**
         * Do a reverse search for (sText)
         *@param sText (const char *) The string to search for.
         *@returns (long) The index of the last occurrance. -1 for not found.
         */
        long rfindIdx( const char *sText ) const;

        /**
         * 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 );

        void trimBack( long iAmnt );

        Bu::String trimWhitespace() const;
        
        iterator begin();

        const_iterator begin() const;

        iterator end();

        const_iterator end() const;

        bool isEmpty() const;

    private:
        void flatten() const;
        bool isFlat() const;

    public:
        class FormatProxyEndAction
        {
        public:
            virtual ~FormatProxyEndAction() { };
            virtual void operator()( const Bu::String &sFinal )=0;
        };

        class FormatProxy
        {
        friend class Bu::String;
        private:
            FormatProxy( const String &rFmt, FormatProxyEndAction *pAct=NULL );

        public:
            virtual ~FormatProxy();
            template<typename T>
            FormatProxy &arg( const T &x )
            {
                lArgs.append( Arg( x ) );

                return *this;
            }

            template<typename T>
            FormatProxy &arg( const T &x, const Bu::Fmt &f )
            {
                lArgs.append( Arg( x, f ) );

                return *this;
            }

            String end() const;
            operator String() const { return end(); }

        private:
            const String &rFmt;
            class Arg
            {
            public:
                template<typename T>
                Arg( const T &v ) :
                    value( v )
                {
                }

                template<typename T>
                Arg( const T &v, const Bu::Fmt &f ) :
                    value( v ),
                    format( f )
                {
                }

                Bu::Variant value;
                Bu::Fmt format;
            };
            typedef Bu::List<Arg> ArgList;
            ArgList lArgs;
            FormatProxyEndAction *pAct;
            mutable bool bOpen;
        };

    public:
        template<typename ArgType>
        FormatProxy arg( const ArgType &x ) const
        {
            return FormatProxy( *this ).arg( x );
        }

        template<typename ArgType>
        FormatProxy arg( const ArgType &x, const Bu::Fmt &f ) const
        {
            return FormatProxy( *this ).arg( x, f );
        }
        
        FormatProxy format() const
        {
            return FormatProxy( *this );
        }

        FormatProxy format( FormatProxyEndAction *pEndAction ) const
        {
            return FormatProxy( *this, pEndAction );
        }
    };
    
    template<class T> String operator+( const T *pLeft, const String &rRight )
    {
        Bu::String ret( pLeft );
        ret.append( rRight );
        return ret;
    }

    ArchiveBase &operator<<( ArchiveBase &ar, const String &s );
    ArchiveBase &operator>>( ArchiveBase &ar, String &s );

    template<typename T>
    uint32_t __calcHashCode( const T &k );

    template<typename T>
    bool __cmpHashKeys( const T &a, const T &b );

    template<> uint32_t __calcHashCode<String>( const String &k );
    template<> bool __cmpHashKeys<String>(
        const String &a, const String &b );
    
    template<typename t> void __tracer_format( const t &v );
    template<> void __tracer_format<String>( const String &v );

    bool &operator<<( bool &dst, const String &sIn );
    uint8_t &operator<<( uint8_t &dst, const String &sIn );
    int8_t &operator<<( int8_t &dst, const String &sIn );
    char &operator<<( char &dst, const String &sIn );
    uint16_t &operator<<( uint16_t &dst, const String &sIn );
    int16_t &operator<<( int16_t &dst, const String &sIn );
    uint32_t &operator<<( uint32_t &dst, const String &sIn );
    int32_t &operator<<( int32_t &dst, const String &sIn );
    uint64_t &operator<<( uint64_t &dst, const String &sIn );
    int64_t &operator<<( int64_t &dst, const String &sIn );
    float &operator<<( float &dst, const String &sIn );
    double &operator<<( double &dst, const String &sIn );
    long double &operator<<( long double &dst, const String &sIn );
    Bu::String &operator<<( Bu::String &dst, const String &sIn );

    typedef Bu::List<String> StringList;
};

#include "bu/formatter.h"

#endif