summaryrefslogtreecommitdiff
path: root/dil2al/BigString.hh
blob: 50779c460e28762537f8ba38f46fce8a3ecbaab7 (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
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
// This may look like C code, but it is really -*- C++ -*-
/* 
Copyright (C) 1988 Free Software Foundation
    written by Doug Lea (dl@rocky.oswego.edu)

Modified by Randal A. Koene, 20000224
    allocation types modified to enable big strings
    added ability to find index directly after a substring
    added constructors with istream, long and double
    added sub() function
Modified by Randal A. Koene, 20070622
    updated for new ISO C++ friend function scope rules

This file is part of the GNU C++ Library.  This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.  This library is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/


#ifndef _BigString_hh
#ifdef __GNUG__
#pragma interface
#endif
#define _BigString_hh 1
#define _String_hh 1

#include "BigRegex.hh"
//#include <iostream.h>
#include <iostream>
using namespace std;
#include <stdlib.h>

#undef OK

// Don't use named return values! It is deprecated!
#define _G_NO_NRV

//#define DEBUG_BIGSTRING

#ifdef DEBUG_BIGSTRING
extern bool globaldebugop;
#endif

#define BIGSTRING_MAXIMIZE_SPEED

#define BIGSTRING_ISTREAM_INITIALIZATION
#define BIGSTRING_LONGLONG_INITIALIZATION
#define BIGSTRING_BOOL_INITIALIZATION
#define BIGSTRING_DOUBLE_INITIALIZATION

#ifdef BIGSTRING_DOUBLE_INITIALIZATION
extern int untruncated_double_string_length; // provides the string length that would have been needed to represent a double if it is not truncated by snprintf() (30 or more indicates that the string was truncated)
#endif

#ifdef BIGSTRING_LONGLONG_INITIALIZATION
	#include <stdio.h>
#else
	#ifdef BIGSTRING_DOUBLE_INITIALIZATION
		#include <stdio.h>
	#endif
#endif

/*
#define _BIGSTRING_ALLOC		unsigned int	// was unsigned short
#define _BIGSTRING_LENGTH	unsigned int	// was unsigned short
#define _BIGSTRING_SIZE		int			// was int (-1=unknown)
#define _BIGSTRING_ITERATOR	int			// was int
#define _BIGSTRING_REVERSE	int			// was short
#define _BIGSTRING_ALLOCMANIP	unsigned long	// was unsigned int
*/

typedef unsigned int		_BIGSTRING_ALLOC;		// was unsigned short
typedef unsigned int		_BIGSTRING_LENGTH;		// was unsigned short
typedef int				_BIGSTRING_SIZE;		// was int (-1=unknown)
typedef int				_BIGSTRING_ITERATOR;	// was int
typedef int				_BIGSTRING_REVERSE;		// was short
typedef unsigned long		_BIGSTRING_ALLOCMANIP;	// was unsigned int

typedef char * BigString_char_ptr;

struct StrRep                     // internal String representations
{
  _BIGSTRING_LENGTH		len;    // string length 
  _BIGSTRING_ALLOC		sz;     // allocated space
  char				s[1];   // the string starts here 
                                 // (at least 1 char for trailing null)
                                 // allocated & expanded via non-public fcts
#ifdef DEBUG_BIGSTRING
	bool debugop;				// enables debugging of operations on this String
#endif
};

// primitive ops on StrReps -- nearly all String fns go through these.

StrRep*     Salloc(StrRep* old, const char* src, _BIGSTRING_SIZE srclen, _BIGSTRING_SIZE newlen);
StrRep*     Scopy(StrRep*, const StrRep*);
StrRep*     Scat(StrRep*, const char*, _BIGSTRING_SIZE, const char*, _BIGSTRING_SIZE);
StrRep*     Scat(StrRep*, const char*, _BIGSTRING_SIZE,const char*,_BIGSTRING_SIZE, const char*,_BIGSTRING_SIZE);
StrRep*     Sprepend(StrRep*, const char*, _BIGSTRING_SIZE);
StrRep*     Sreverse(const StrRep*, StrRep*);
StrRep*     Supcase(const StrRep*, StrRep*);
StrRep*     Sdowncase(const StrRep*, StrRep*);
StrRep*     Scapitalize(const StrRep*, StrRep*);
#ifdef BIGSTRING_ISTREAM_INITIALIZATION
StrRep*     Salloc_istream(StrRep* old, istream & src, _BIGSTRING_SIZE srclen, _BIGSTRING_SIZE newlen);
StrRep*     Scat_istream(StrRep*, const char*, _BIGSTRING_SIZE, istream&, _BIGSTRING_SIZE);
#endif

// These classes need to be defined in the order given

class String;
class SubString;

class SubString
{
  friend class      String;
protected:

  String&           S;        // The String I'm a substring of
  _BIGSTRING_LENGTH    pos;      // starting position in S's rep
  _BIGSTRING_LENGTH    len;      // length of substring

  void              assign(const StrRep*, const char*, _BIGSTRING_SIZE = -1);
                    SubString(String& x, _BIGSTRING_SIZE p, _BIGSTRING_SIZE l);
                    SubString(const SubString& x);

public:

// Note there are no public constructors. SubStrings are always
// created via String operations

                   ~SubString();

  SubString&        operator =  (const String&     y);
  SubString&        operator =  (const SubString&  y);
  SubString&        operator =  (const char* t);
  SubString&        operator =  (char        c);

// return 1 if target appears anywhere in SubString; else 0

  int               contains(char        c) const;
  int               contains(const String&     y) const;
  int               contains(const SubString&  y) const;
  int               contains(const char* t) const;
  int               contains(const BigRegex&       r) const;

// return 1 if target matches entire SubString

  int               matches(const BigRegex&  r) const;

// IO 

  friend ostream&   operator<<(ostream& s, const SubString& x);

// status

  _BIGSTRING_LENGTH      length() const;
  int               empty() const;
  const char*       chars() const;

  int               OK() const; 

};


class String
{
  friend class      SubString;

protected:
  StrRep*           rep;   // Strings are pointers to their representations

// some helper functions

  _BIGSTRING_SIZE   search(_BIGSTRING_SIZE, _BIGSTRING_SIZE, const char*, _BIGSTRING_SIZE = -1) const;
  _BIGSTRING_SIZE   search(_BIGSTRING_SIZE, _BIGSTRING_SIZE, char) const;
  _BIGSTRING_SIZE   match(_BIGSTRING_SIZE, _BIGSTRING_SIZE, int, const char*, _BIGSTRING_SIZE = -1) const;
  _BIGSTRING_SIZE   _gsub(const char*, _BIGSTRING_SIZE, const char* ,_BIGSTRING_SIZE);
  _BIGSTRING_SIZE   _gsub(const BigRegex&, const char*, _BIGSTRING_SIZE, char substrs = '\0');
  SubString         _substr(int, int);

public:
#ifdef DEBUG_BIGSTRING
	void debug_on() { rep->debugop=true; }
	void debug_off() { rep->debugop=false; }
	bool debug_state() { return rep->debugop; }
#endif

// constructors & assignment

                    String();
                    String(const String& x);
                    String(const SubString&  x);
                    String(const char* t);
                    String(const char* t, _BIGSTRING_SIZE len);
                    String(char c);
#ifdef BIGSTRING_ISTREAM_INITIALIZATION
				String(istream & ist);
#endif
#ifdef BIGSTRING_LONGLONG_INITIALIZATION
				String(long l);
#endif
#ifdef BIGSTRING_BOOL_INITIALIZATION
				String(bool b);
#endif
#ifdef BIGSTRING_DOUBLE_INITIALIZATION
				String(double d,const char * format);
#endif

                    ~String();

  String&           operator =  (const String&     y);
  String&           operator =  (const char* y);
  String&           operator =  (char        c);
  String&           operator =  (const SubString&  y);
#ifdef BIGSTRING_ISTREAM_INITIALIZATION
  String&           operator =  (istream&  y);
  String&	    readnoseek(istream& y); // useful for istreams such as cin
  String&           readtotag(istream& y, const char * t, char terminator = '\n'); // useful for data input from standard input
#endif

// concatenation

  String&           operator += (const String&     y); 
  String&           operator += (const SubString&  y);
  String&           operator += (const char* y);
  String&           operator += (char        c);
#ifdef BIGSTRING_ISTREAM_INITIALIZATION
  String&           operator += (istream&  y);
#endif

  void              prepend(const String&     y); 
  void              prepend(const SubString&  y);
  void              prepend(const char* t);
  void              prepend(char        c);


// procedural versions:
// concatenate first 2 args, store result in last arg

#ifdef BIGSTRING_ISTREM_INITIALIZE
  friend inline void	cat(const String& x, istream & y, String& r);
#endif
  friend inline void     cat(const String&, const String&, String&);
  friend inline void     cat(const String&, const SubString&, String&);
  friend inline void     cat(const String&, const char*, String&);
  friend inline void     cat(const String&, char, String&);

  friend inline void     cat(const SubString&, const String&, String&);
  friend inline void     cat(const SubString&, const SubString&, String&);
  friend inline void     cat(const SubString&, const char*, String&);
  friend inline void     cat(const SubString&, char, String&);

  friend inline void     cat(const char*, const String&, String&);
  friend inline void     cat(const char*, const SubString&, String&);
  friend inline void     cat(const char*, const char*, String&);
  friend inline void     cat(const char*, char, String&);

// double concatenation, by request. (yes, there are too many versions, 
// but if one is supported, then the others should be too...)
// Concatenate first 3 args, store in last arg

  friend inline void     cat(const String&,const String&, const String&,String&);
  friend inline void     cat(const String&,const String&,const SubString&,String&);
  friend inline void     cat(const String&,const String&, const char*, String&);
  friend inline void     cat(const String&,const String&, char, String&);
  friend inline void     cat(const String&,const SubString&,const String&,String&);
  inline friend void     cat(const String&,const SubString&,const SubString&,String&);
  friend inline void     cat(const String&,const SubString&, const char*, String&);
  friend inline void     cat(const String&,const SubString&, char, String&);
  friend inline void     cat(const String&,const char*, const String&,    String&);
  friend inline void     cat(const String&,const char*, const SubString&, String&);
  friend inline void     cat(const String&,const char*, const char*, String&);
  friend inline void     cat(const String&,const char*, char, String&);

  friend inline void     cat(const char*, const String&, const String&,String&);
  friend inline void     cat(const char*,const String&,const SubString&,String&);
  friend inline void     cat(const char*,const String&, const char*, String&);
  friend inline void     cat(const char*,const String&, char, String&);
  friend inline void     cat(const char*,const SubString&,const String&,String&);
  friend inline void     cat(const char*,const SubString&,const SubString&,String&);
  friend inline void     cat(const char*,const SubString&, const char*, String&);
  friend inline void     cat(const char*,const SubString&, char, String&);
  friend inline void     cat(const char*,const char*, const String&,    String&);
  friend inline void     cat(const char*,const char*, const SubString&, String&);
  friend inline void     cat(const char*,const char*, const char*, String&);
  friend inline void     cat(const char*,const char*, char, String&);


// searching & matching

// search for start of or position directly after match (default = start)

  enum BigString_Search_t { SEARCH_START, SEARCH_END };

// return position of target in string or -1 for failure

  _BIGSTRING_SIZE   index(char	   c, _BIGSTRING_SIZE startpos = 0) const;	 
  _BIGSTRING_SIZE   index(const String&     y, _BIGSTRING_SIZE startpos = 0) const;	  
  _BIGSTRING_SIZE   index(const SubString&  y, _BIGSTRING_SIZE startpos = 0) const;	  
  _BIGSTRING_SIZE   index(const char* t, _BIGSTRING_SIZE startpos = 0) const;  
  _BIGSTRING_SIZE   index(const BigRegex&      r, _BIGSTRING_SIZE startpos = 0) const;	   
  _BIGSTRING_SIZE   index(const BigRegex&      r, BigString_Search_t _st, _BIGSTRING_SIZE startpos = 0) const;	   

// return 1 if target appears anyhere in String; else 0

  int               contains(char        c) const;
  int               contains(const String&     y) const;
  int               contains(const SubString&  y) const;
  int               contains(const char* t) const;
  int               contains(const BigRegex&      r) const;

// return 1 if target appears anywhere after position pos 
// (or before, if pos is negative) in String; else 0

  int               contains(char        c, _BIGSTRING_SIZE pos) const;
  int               contains(const String&     y, _BIGSTRING_SIZE pos) const;
  int               contains(const SubString&  y, _BIGSTRING_SIZE pos) const;
  int               contains(const char* t, _BIGSTRING_SIZE pos) const;
  int               contains(const BigRegex&      r, _BIGSTRING_SIZE pos) const;

// return 1 if target appears at position pos in String; else 0

  int               matches(char        c, _BIGSTRING_SIZE pos = 0) const;
  int               matches(const String&     y, _BIGSTRING_SIZE pos = 0) const;
  int               matches(const SubString&  y, _BIGSTRING_SIZE pos = 0) const;
  int               matches(const char* t, _BIGSTRING_SIZE pos = 0) const;
  int               matches(const BigRegex&      r, _BIGSTRING_SIZE pos = 0) const;
  // BEWARE: An empty string matches anything, since the BigRegex match() function
  // returns the number of characters matched and that is compared with the length
  // of the string to produce the boolean result for String::matches()!

//  return number of occurences of target in String

  _BIGSTRING_SIZE   freq(char        c) const; 
  _BIGSTRING_SIZE   freq(const String&     y) const;
  _BIGSTRING_SIZE   freq(const SubString&  y) const;
  _BIGSTRING_SIZE   freq(const char* t) const;

// SubString extraction

// Note that you can't take a substring of a const String, since
// this leaves open the possiblility of indirectly modifying the
// String through the SubString

  SubString         at(_BIGSTRING_SIZE         pos, _BIGSTRING_SIZE len);
  SubString         operator () (_BIGSTRING_SIZE         pos, _BIGSTRING_SIZE len); // synonym for at

  SubString         at(const String&     x, _BIGSTRING_SIZE startpos = 0); 
  SubString         at(const SubString&  x, _BIGSTRING_SIZE startpos = 0); 
  SubString         at(const char* t, _BIGSTRING_SIZE startpos = 0);
  SubString         at(char        c, _BIGSTRING_SIZE startpos = 0);
  SubString         at(const BigRegex&      r, _BIGSTRING_SIZE startpos = 0); 

  SubString         before(_BIGSTRING_SIZE          pos);
  SubString         before(const String&      x, _BIGSTRING_SIZE startpos = 0);
  SubString         before(const SubString&   x, _BIGSTRING_SIZE startpos = 0);
  SubString         before(const char*  t, _BIGSTRING_SIZE startpos = 0);
  SubString         before(char         c, _BIGSTRING_SIZE startpos = 0);
  SubString         before(const BigRegex&       r, _BIGSTRING_SIZE startpos = 0);

  SubString         through(_BIGSTRING_SIZE          pos);
  SubString         through(const String&      x, _BIGSTRING_SIZE startpos = 0);
  SubString         through(const SubString&   x, _BIGSTRING_SIZE startpos = 0);
  SubString         through(const char*  t, _BIGSTRING_SIZE startpos = 0);
  SubString         through(char         c, _BIGSTRING_SIZE startpos = 0);
  SubString         through(const BigRegex&       r, _BIGSTRING_SIZE startpos = 0);

  SubString         from(_BIGSTRING_SIZE          pos);
  SubString         from(const String&      x, _BIGSTRING_SIZE startpos = 0);
  SubString         from(const SubString&   x, _BIGSTRING_SIZE startpos = 0);
  SubString         from(const char*  t, _BIGSTRING_SIZE startpos = 0);
  SubString         from(char         c, _BIGSTRING_SIZE startpos = 0);
  SubString         from(const BigRegex&       r, _BIGSTRING_SIZE startpos = 0);

  SubString         after(_BIGSTRING_SIZE         pos);
  SubString         after(const String&     x, _BIGSTRING_SIZE startpos = 0);
  SubString         after(const SubString&  x, _BIGSTRING_SIZE startpos = 0);
  SubString         after(const char* t, _BIGSTRING_SIZE startpos = 0);
  SubString         after(char        c, _BIGSTRING_SIZE startpos = 0);
  SubString         after(const BigRegex&      r, _BIGSTRING_SIZE startpos = 0);

// String copies of SubStrings indicated by search with BigRegex

  String            sub(const BigRegex & r,int nth);


// deletion

// delete len chars starting at pos
  void              del(_BIGSTRING_SIZE         pos, _BIGSTRING_SIZE len);

// delete the first occurrence of target after startpos

  void              del(const String&     y, _BIGSTRING_SIZE startpos = 0);
  void              del(const SubString&  y, _BIGSTRING_SIZE startpos = 0);
  void              del(const char* t, _BIGSTRING_SIZE startpos = 0);
  void              del(char        c, _BIGSTRING_SIZE startpos = 0);
  void              del(const BigRegex&      r, _BIGSTRING_SIZE startpos = 0);

// global substitution: substitute all occurrences of pat with repl

  _BIGSTRING_SIZE   gsub(const String&     pat, const String&	 repl);
  _BIGSTRING_SIZE   gsub(const SubString&  pat, const String&	 repl);
  _BIGSTRING_SIZE   gsub(const char* pat, const String&	repl);
  _BIGSTRING_SIZE   gsub(const char* pat, const char* repl);
  _BIGSTRING_SIZE   gsub(const BigRegex&	   pat, const String&	 repl, char substrs = '\0');

// friends & utilities

// split string into array res at separators; return number of elements

  friend _BIGSTRING_SIZE        split(const String& x, String res[], _BIGSTRING_SIZE maxn, 
                          const String& sep);
  friend _BIGSTRING_SIZE        split(const String& x, String res[], _BIGSTRING_SIZE maxn, 
                          const BigRegex&  sep);

  friend String     common_prefix(const String& x, const String& y, 
                                  _BIGSTRING_SIZE startpos = 0);
  friend String     common_suffix(const String& x, const String& y, 
                                  _BIGSTRING_SIZE startpos = -1);
  friend String     replicate(char        c, _BIGSTRING_SIZE n);
  friend String     replicate(const String&     y, _BIGSTRING_SIZE n);
  friend String     join(String src[], _BIGSTRING_SIZE n, const String& sep);

// simple builtin transformations

  friend inline String     reverse(const String& x);
  friend inline String     upcase(const String& x);
  friend inline String     downcase(const String& x);
  friend inline String     capitalize(const String& x);

// in-place versions of above

  void              reverse();
  void              upcase();
  void              downcase();
  void              capitalize();

// element extraction

  char&             operator [] (_BIGSTRING_ITERATOR i);
  const char&       operator [] (_BIGSTRING_ITERATOR i) const;
  char              elem(_BIGSTRING_ITERATOR i) const;
  char              firstchar() const;
  char              lastchar() const;

// conversion

                    operator const char*() const;
  const char*       chars() const;


// IO

  friend inline ostream&   operator<<(ostream& s, const String& x);
  friend ostream&   operator<<(ostream& s, const SubString& x);
  friend istream&   operator>>(istream& s, String& x);

  friend _BIGSTRING_SIZE   readline(istream& s, String& x, 
                             char terminator = '\n',
                             int discard_terminator = 1);

// status

  _BIGSTRING_LENGTH length() const;
  int               empty() const;

// preallocate some space for String
  void              alloc(_BIGSTRING_SIZE newsize);

// report current allocation (not length!)

  _BIGSTRING_ALLOC  allocation() const;


  void     error(const char* msg) const;

  int               OK() const;
};

// Randal A. Koene, update 20070622
// The new ISO C++ standard implemented in GCC 4.1 requires a
// definition/declaration of friend functions outside the class
// body in order to extend their scope beyond the class.

#ifdef BIGSTRING_ISTREM_INITIALIZE
inline void	cat(const String& x, istream & y, String& r);
#endif
inline void     cat(const String&, const String&, String&);
inline void     cat(const String&, const SubString&, String&);
inline void     cat(const String&, const char*, String&);
inline void     cat(const String&, char, String&);

inline void     cat(const SubString&, const String&, String&);
inline void     cat(const SubString&, const SubString&, String&);
inline void     cat(const SubString&, const char*, String&);
inline void     cat(const SubString&, char, String&);

inline void     cat(const char*, const String&, String&);
inline void     cat(const char*, const SubString&, String&);
inline void     cat(const char*, const char*, String&);
inline void     cat(const char*, char, String&);

// double concatenation, by request. (yes, there are too many versions, 
// but if one is supported, then the others should be too...)
// Concatenate first 3 args, store in last arg

inline void     cat(const String&,const String&, const String&,String&);
inline void     cat(const String&,const String&,const SubString&,String&);
inline void     cat(const String&,const String&, const char*, String&);
inline void     cat(const String&,const String&, char, String&);
inline void     cat(const String&,const SubString&,const String&,String&);
void     cat(const String&,const SubString&,const SubString&,String&);
inline void     cat(const String&,const SubString&, const char*, String&);
inline void     cat(const String&,const SubString&, char, String&);
inline void     cat(const String&,const char*, const String&,    String&);
inline void     cat(const String&,const char*, const SubString&, String&);
inline void     cat(const String&,const char*, const char*, String&);
inline void     cat(const String&,const char*, char, String&);

inline void     cat(const char*, const String&, const String&,String&);
inline void     cat(const char*,const String&,const SubString&,String&);
inline void     cat(const char*,const String&, const char*, String&);
inline void     cat(const char*,const String&, char, String&);
inline void     cat(const char*,const SubString&,const String&,String&);
inline void     cat(const char*,const SubString&,const SubString&,String&);
inline void     cat(const char*,const SubString&, const char*, String&);
inline void     cat(const char*,const SubString&, char, String&);
inline void     cat(const char*,const char*, const String&,    String&);
inline void     cat(const char*,const char*, const SubString&, String&);
inline void     cat(const char*,const char*, const char*, String&);
inline void     cat(const char*,const char*, char, String&);

_BIGSTRING_SIZE        split(const String& x, String res[], _BIGSTRING_SIZE maxn, 
			     const String& sep);
_BIGSTRING_SIZE        split(const String& x, String res[], _BIGSTRING_SIZE maxn, 
			     const BigRegex&  sep);

String     common_prefix(const String& x, const String& y, 
			 _BIGSTRING_SIZE startpos);
String     common_suffix(const String& x, const String& y, 
			 _BIGSTRING_SIZE startpos);
String     replicate(char        c, _BIGSTRING_SIZE n);
String replicate(const String&     y, _BIGSTRING_SIZE n);
String     join(String src[], _BIGSTRING_SIZE n, const String& sep);

// simple builtin transformations

inline String     reverse(const String& x);
inline String     upcase(const String& x);
inline String     downcase(const String& x);
inline String     capitalize(const String& x);
inline ostream&   operator<<(ostream& s, const String& x);
ostream&   operator<<(ostream& s, const SubString& x);
istream&   operator>>(istream& s, String& x);

_BIGSTRING_SIZE   readline(istream& s, String& x, 
			   char terminator,
			   int discard_terminator);

typedef String StrTmp; // for backward compatibility

// other externs

_BIGSTRING_SIZE        compare(const String&    x, const String&     y);
_BIGSTRING_SIZE        compare(const String&    x, const SubString&  y);
_BIGSTRING_SIZE        compare(const String&    x, const char* y);
_BIGSTRING_SIZE        compare(const SubString& x, const String&     y);
_BIGSTRING_SIZE        compare(const SubString& x, const SubString&  y);
_BIGSTRING_SIZE        compare(const SubString& x, const char* y);
_BIGSTRING_SIZE        fcompare(const String&   x, const String&     y); // ignore case

extern StrRep  _nilStrRep;
extern String _nilString;

// status reports, needed before defining other things

inline _BIGSTRING_LENGTH String::length() const {  return rep->len; }
inline int         String::empty() const { return rep->len == 0; }
inline const char* String::chars() const { return &(rep->s[0]); }
inline _BIGSTRING_ALLOC         String::allocation() const { return rep->sz; }

inline _BIGSTRING_LENGTH SubString::length() const { return len; }
inline int         SubString::empty() const { return len == 0; }
inline const char* SubString::chars() const { return &(S.rep->s[pos]); }


// constructors

//#define CONSTCHAR_INITIALIZE_TEST

inline String::String() 
  : rep(&_nilStrRep) {}
inline String::String(const String& x) 
  : rep(Scopy(0, x.rep)) {}
inline String::String(const char* t) 
  : rep(Salloc(0, t, -1, -1)) {
#ifdef CONSTCHAR_INITIALIZE_TEST
	cout << "dil2al: Debug CONSTCHAR_INITIALIZE_TEST: ";
	for (char * tp = (char *) t; *tp!='\0'; tp++) cout << (*tp) << '[' << (int) (*tp) << ']';
	cout << '\n';
	cout << '{' << (*this) << "}\n";
#endif
}
inline String::String(const char* t, _BIGSTRING_SIZE tlen)
  : rep(Salloc(0, t, tlen, tlen)) {}
inline String::String(const SubString& y)
  : rep(Salloc(0, y.chars(), y.length(), y.length())) {}
inline String::String(char c) 
  : rep(Salloc(0, &c, 1, 1)) {}
#ifdef BIGSTRING_ISTREAM_INITIALIZATION
inline String::String(istream & ist)
  : rep(Salloc_istream(0,ist,-1,-1)) {}
#endif
#ifdef BIGSTRING_LONGLONG_INITIALIZATION
inline String::String(long l) { char s[12]; snprintf(s,12,"%ld",l); rep = Salloc(0,s,-1,-1); }
#endif
#ifdef BIGSTRING_BOOL_INITIALIZATION
inline String::String(bool b) { if (b) rep = Salloc(0,"true",-1,-1); else rep = Salloc(0,"false",-1,-1); }
#endif
#ifdef BIGSTRING_DOUBLE_INITIALIZATION
inline String::String(double d,const char * format) { char s[30]; untruncated_double_string_length=snprintf(s,30,format,d); rep = Salloc(0,s,-1,-1); }
#endif

inline String::~String() { if (rep != &_nilStrRep) delete rep; }

inline SubString::SubString(const SubString& x)
  :S(x.S), pos(x.pos), len(x.len) {}
inline SubString::SubString(String& x, _BIGSTRING_SIZE first, _BIGSTRING_SIZE l)
  :S(x), pos(first), len(l) {}

inline SubString::~SubString() {}

// assignment

inline String& String::operator =  (const String& y)
{ 
  rep = Scopy(rep, y.rep);
  return *this;
}

inline String& String::operator=(const char* t)
{
  rep = Salloc(rep, t, -1, -1);
  return *this;
}

inline String& String::operator=(const SubString&  y)
{
  rep = Salloc(rep, y.chars(), y.length(), y.length());
  return *this;
}

inline String& String::operator=(char c)
{
  rep = Salloc(rep, &c, 1, 1);
  return *this;
}

#ifdef BIGSTRING_ISTREAM_INITIALIZATION
inline String & String::operator=(istream& y) {
	rep = Salloc_istream(rep, y, -1, -1);
	return *this;
}

inline String & String::readnoseek(istream& y) {
	char buf[4097]; int bufread;
	do {
		y.read(buf,4096);
		bufread = y.gcount();
		if (bufread>0) {
			buf[bufread] = '\0';
			(*this) += buf;
		}
	} while (bufread==4096);
	return *this;
}

inline String & String::readtotag(istream& y, const char * t, char terminator) {
  String buf;
  while (readline(y,buf,terminator,0)>0) {
    if (buf.contains(t)) break;
    else (*this) += buf;
  }
  return *this;
}
#endif

inline SubString& SubString::operator = (const char* ys)
{
  assign(0, ys);
  return *this;
}

inline SubString& SubString::operator = (char ch)
{
  assign(0, &ch, 1);
  return *this;
}

inline SubString& SubString::operator = (const String& y)
{
  assign(y.rep, y.chars(), y.length()); // INVOLVED in lfstr.at(...) = ...
  return *this;
}

inline SubString& SubString::operator = (const SubString& y)
{
  assign(y.S.rep, y.chars(), y.length());
  return *this;
}

// Zillions of cats...

#ifdef BIGSTRING_ISTREAM_INITIALIZE
inline void cat(const String& x, istream & y, String& r)
{
	r.rep = Scat_istream(r.rep, x.chars(), x.length(), y, -1);
}
#endif

inline void cat(const String& x, const String& y, String& r)
{
  r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const String& x, const SubString& y, String& r)
{
  r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const String& x, const char* y, String& r)
{
  r.rep = Scat(r.rep, x.chars(), x.length(), y, -1);
}

inline void cat(const String& x, char y, String& r)
{
  r.rep = Scat(r.rep, x.chars(), x.length(), &y, 1);
}

inline void cat(const SubString& x, const String& y, String& r)
{
  r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const SubString& x, const SubString& y, String& r)
{
  r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const SubString& x, const char* y, String& r)
{
  r.rep = Scat(r.rep, x.chars(), x.length(), y, -1);
}

inline void cat(const SubString& x, char y, String& r)
{
  r.rep = Scat(r.rep, x.chars(), x.length(), &y, 1);
}

inline void cat(const char* x, const String& y, String& r)
{
  r.rep = Scat(r.rep, x, -1, y.chars(), y.length());
}

inline void cat(const char* x, const SubString& y, String& r)
{
  r.rep = Scat(r.rep, x, -1, y.chars(), y.length());
}

inline void cat(const char* x, const char* y, String& r)
{
  r.rep = Scat(r.rep, x, -1, y, -1);
}

inline void cat(const char* x, char y, String& r)
{
  r.rep = Scat(r.rep, x, -1, &y, 1);
}

inline void cat(const String& a, const String& x, const String& y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const String& a, const String& x, const SubString& y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const String& a, const String& x, const char* y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y, -1);
}

inline void cat(const String& a, const String& x, char y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), &y, 1);
}

inline void cat(const String& a, const SubString& x, const String& y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const String& a, const SubString& x, const SubString& y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const String& a, const SubString& x, const char* y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), y, -1);
}

inline void cat(const String& a, const SubString& x, char y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x.chars(), x.length(), &y, 1);
}

inline void cat(const String& a, const char* x, const String& y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x, -1, y.chars(), y.length());
}

inline void cat(const String& a, const char* x, const SubString& y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x, -1, y.chars(), y.length());
}

inline void cat(const String& a, const char* x, const char* y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x, -1, y, -1);
}

inline void cat(const String& a, const char* x, char y, String& r)
{
  r.rep = Scat(r.rep, a.chars(), a.length(), x, -1, &y, 1);
}


inline void cat(const char* a, const String& x, const String& y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const char* a, const String& x, const SubString& y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const char* a, const String& x, const char* y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y, -1);
}

inline void cat(const char* a, const String& x, char y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), &y, 1);
}

inline void cat(const char* a, const SubString& x, const String& y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const char* a, const SubString& x, const SubString& y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y.chars(), y.length());
}

inline void cat(const char* a, const SubString& x, const char* y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), y, -1);
}

inline void cat(const char* a, const SubString& x, char y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x.chars(), x.length(), &y, 1);
}

inline void cat(const char* a, const char* x, const String& y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x, -1, y.chars(), y.length());
}

inline void cat(const char* a, const char* x, const SubString& y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x, -1, y.chars(), y.length());
}

inline void cat(const char* a, const char* x, const char* y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x, -1, y, -1);
}

inline void cat(const char* a, const char* x, char y, String& r)
{
  r.rep = Scat(r.rep, a, -1, x, -1, &y, 1);
}

// operator versions

inline String& String::operator +=(const String& y)
{
  cat(*this, y, *this);
  return *this;
}

inline String& String::operator +=(const SubString& y)
{
  cat(*this, y, *this);
  return *this;
}

inline String& String::operator += (const char* y)
{
  cat(*this, y, *this);
  return *this;
}

inline String& String:: operator +=(char y)
{
  cat(*this, y, *this);
  return *this;
}

#ifdef BIGSTRING_ISTREAM_INITIALIZE
inline String& String::operator += (istream&  y) {
	cat(*this, y, *this);
	return *this;
}
#endif

// prepend

inline void String::prepend(const String& y)
{
  rep = Sprepend(rep, y.chars(), y.length());
}

inline void String::prepend(const char* y)
{
  rep = Sprepend(rep, y, -1); 
}

inline void String::prepend(char y)
{
  rep = Sprepend(rep, &y, 1); 
}

inline void String::prepend(const SubString& y)
{
  rep = Sprepend(rep, y.chars(), y.length());
}

// constructive concatenation

#if defined(_USE_NAMED_RETURN_VALUE_EXTENSION) && defined(__GNUG__) && !defined(_G_NO_NRV)

inline String operator + (const String& x, const String& y) return r;
{
  cat(x, y, r);
}

inline String operator + (const String& x, const SubString& y) return r;
{
  cat(x, y, r);
}

inline String operator + (const String& x, const char* y) return r;
{
  cat(x, y, r);
}

inline String operator + (const String& x, char y) return r;
{
  cat(x, y, r);
}

inline String operator + (const SubString& x, const String& y) return r;
{
  cat(x, y, r);
}

inline String operator + (const SubString& x, const SubString& y) return r;
{
  cat(x, y, r);
}

inline String operator + (const SubString& x, const char* y) return r;
{
  cat(x, y, r);
}

inline String operator + (const SubString& x, char y) return r;
{
  cat(x, y, r);
}

inline String operator + (const char* x, const String& y) return r;
{
  cat(x, y, r);
}

inline String operator + (char x, const String& y) return r;
{
  r = y;
  r.prepend(x);
}

inline String operator + (const char* x, const SubString& y) return r;
{
  cat(x, y, r);
}

inline String reverse(const String& x) return r;
{
  r.rep = Sreverse(x.rep, r.rep);
}

inline String upcase(const String& x) return r;
{
  r.rep = Supcase(x.rep, r.rep);
}

inline String downcase(const String& x) return r;
{
  r.rep = Sdowncase(x.rep, r.rep);
}

inline String capitalize(const String& x) return r;
{
  r.rep = Scapitalize(x.rep, r.rep);
}

#else /* NO_NRV */

inline String operator + (const String& x, const String& y)
{
  String r;  cat(x, y, r);  return r;
}

inline String operator + (const String& x, const SubString& y) 
{
  String r; cat(x, y, r); return r;
}

inline String operator + (const String& x, const char* y) 
{
  String r; cat(x, y, r); return r;
}

inline String operator + (const String& x, char y) 
{
  String r; cat(x, y, r); return r;
}

inline String operator + (const SubString& x, const String& y) 
{
  String r; cat(x, y, r); return r;
}

inline String operator + (const SubString& x, const SubString& y) 
{
  String r; cat(x, y, r); return r;
}

inline String operator + (const SubString& x, const char* y) 
{
  String r; cat(x, y, r); return r;
}

inline String operator + (const SubString& x, char y) 
{
  String r; cat(x, y, r); return r;
}

inline String operator + (const char* x, const String& y) 
{
  String r; cat(x, y, r); return r;
}

inline String operator + (char x, const String& y)
{
//cout << "<*>" ;
  String r;
  r = y;
//for (char *tp = (char *) r.chars(); *tp!='\0'; tp++) cout << (*tp) << '{' << (int) (*tp) << '}';
//cout << '\n';
  r.prepend(x);
//for (char *tp = (char *) r.chars(); *tp!='\0'; tp++) cout << (*tp) << '{' << (int) (*tp) << '}';
  return r;
}

inline String operator + (const char* x, const SubString& y) 
{
  String r; cat(x, y, r); return r;
}

inline String reverse(const String& x) 
{
  String r; r.rep = Sreverse(x.rep, r.rep); return r;
}

inline String upcase(const String& x) 
{
  String r; r.rep = Supcase(x.rep, r.rep); return r;
}

inline String downcase(const String& x) 
{
  String r; r.rep = Sdowncase(x.rep, r.rep); return r;
}

inline String capitalize(const String& x) 
{
  String r; r.rep = Scapitalize(x.rep, r.rep); return r;
}

#endif

// misc transformations


inline void String::reverse()
{
  rep = Sreverse(rep, rep);
}


inline void String::upcase()
{
  rep = Supcase(rep, rep);
}


inline void String::downcase()
{
  rep = Sdowncase(rep, rep);
}


inline void String::capitalize()
{
  rep = Scapitalize(rep, rep);
}

// element extraction

inline char&  String::operator [] (_BIGSTRING_ITERATOR i) 
{ 
  if (((unsigned)i) >= length()) error("invalid index");
  return rep->s[i];
}

inline const char&  String::operator [] (_BIGSTRING_ITERATOR i) const
{ 
  if (((unsigned)i) >= length()) error("invalid index");
  return rep->s[i];
}

inline char  String::elem (_BIGSTRING_ITERATOR i) const
{ 
  if (((unsigned)i) >= length()) error("invalid index");
  return rep->s[i];
}

inline char  String::firstchar() const
{ 
  return elem(0);
}

inline char  String::lastchar() const
{ 
  return elem(length() - 1);
}

// searching

inline _BIGSTRING_SIZE String::index(char c, _BIGSTRING_SIZE startpos) const
{
  return search(startpos, length(), c);
}

inline _BIGSTRING_SIZE String::index(const char* t, _BIGSTRING_SIZE startpos) const
{   
  return search(startpos, length(), t);
}

inline _BIGSTRING_SIZE String::index(const String& y, _BIGSTRING_SIZE startpos) const
{   
  return search(startpos, length(), y.chars(), y.length());
}

inline _BIGSTRING_SIZE String::index(const SubString& y, _BIGSTRING_SIZE startpos) const
{   
  return search(startpos, length(), y.chars(), y.length());
}

inline _BIGSTRING_SIZE String::index(const BigRegex& r, _BIGSTRING_SIZE startpos) const
{
  int unused;  return r.search(chars(), length(), unused, startpos);
}

inline _BIGSTRING_SIZE String::index(const BigRegex& r, BigString_Search_t _st, _BIGSTRING_SIZE startpos) const
{
  int matchlen;
  int matchstart = r.search(chars(), length(), matchlen, startpos);
  if ((matchstart<0) || (_st==SEARCH_START)) return matchstart;
  else return (matchstart+matchlen);
}

inline int String::contains(char c) const
{
  return search(0, length(), c) >= 0;
}

inline int String::contains(const char* t) const
{   
  return search(0, length(), t) >= 0;
}

inline int String::contains(const String& y) const
{   
  return search(0, length(), y.chars(), y.length()) >= 0;
}

inline int String::contains(const SubString& y) const
{   
  return search(0, length(), y.chars(), y.length()) >= 0;
}

inline int String::contains(char c, _BIGSTRING_SIZE p) const
{
  return match(p, length(), 0, &c, 1) >= 0;
}

inline int String::contains(const char* t, _BIGSTRING_SIZE p) const
{
  return match(p, length(), 0, t) >= 0;
}

inline int String::contains(const String& y, _BIGSTRING_SIZE p) const
{
  return match(p, length(), 0, y.chars(), y.length()) >= 0;
}

inline int String::contains(const SubString& y, _BIGSTRING_SIZE p) const
{
  return match(p, length(), 0, y.chars(), y.length()) >= 0;
}

inline int String::contains(const BigRegex& r) const
{
  int unused;  return r.search(chars(), length(), unused, 0) >= 0;
}

inline int String::contains(const BigRegex& r, _BIGSTRING_SIZE p) const
{
  return r.match(chars(), length(), p) >= 0;
}


inline int String::matches(const SubString& y, _BIGSTRING_SIZE p) const
{
  return match(p, length(), 1, y.chars(), y.length()) >= 0;
}

inline int String::matches(const String& y, _BIGSTRING_SIZE p) const
{
  return match(p, length(), 1, y.chars(), y.length()) >= 0;
}

inline int String::matches(const char* t, _BIGSTRING_SIZE p) const
{
  return match(p, length(), 1, t) >= 0;
}

inline int String::matches(char c, _BIGSTRING_SIZE p) const
{
  return match(p, length(), 1, &c, 1) >= 0;
}

inline int String::matches(const BigRegex& r, _BIGSTRING_SIZE p) const
{
  _BIGSTRING_SIZE l = (p < 0)? -p : length() - p;
  return r.match(chars(), length(), p) == l;
}


inline int SubString::contains(const char* t) const
{   
  return S.search(pos, pos+len, t) >= 0;
}

inline int SubString::contains(const String& y) const
{   
  return S.search(pos, pos+len, y.chars(), y.length()) >= 0;
}

inline int SubString::contains(const SubString&  y) const
{   
  return S.search(pos, pos+len, y.chars(), y.length()) >= 0;
}

inline int SubString::contains(char c) const
{
  return S.search(pos, pos+len, c) >= 0;
}

inline int SubString::contains(const BigRegex& r) const
{
  int unused;  return r.search(chars(), len, unused, 0) >= 0;
}

inline int SubString::matches(const BigRegex& r) const
{
  // Note the comparison between int and converted unsigned int.
  // r.match can return -1 or -2 (internal error), which might in
  // rare cases be equal to a very large SubString len value!
  return r.match(chars(), len, 0) == (int) len;
}

inline _BIGSTRING_SIZE String::gsub(const String& pat, const String& r)
{
  return _gsub(pat.chars(), pat.length(), r.chars(), r.length());
}

inline _BIGSTRING_SIZE String::gsub(const SubString&  pat, const String& r)
{
  return _gsub(pat.chars(), pat.length(), r.chars(), r.length());
}

inline _BIGSTRING_SIZE String::gsub(const BigRegex& pat, const String& r, char substrs)
{
  return _gsub(pat, r.chars(), r.length(), substrs);
}

inline _BIGSTRING_SIZE String::gsub(const char* pat, const String& r)
{
  return _gsub(pat, -1, r.chars(), r.length());
}

inline _BIGSTRING_SIZE String::gsub(const char* pat, const char* r)
{
  return _gsub(pat, -1, r, -1);
}



inline  ostream& operator<<(ostream& s, const String& x)
{
   s << x.chars(); return s;
}

// a zillion comparison operators

inline int operator==(const String& x, const String& y) 
{
  return compare(x, y) == 0; 
}

inline int operator!=(const String& x, const String& y)
{
  return compare(x, y) != 0; 
}

inline int operator>(const String& x, const String& y)
{
  return compare(x, y) > 0; 
}

inline int operator>=(const String& x, const String& y)
{
  return compare(x, y) >= 0; 
}

inline int operator<(const String& x, const String& y)
{
  return compare(x, y) < 0; 
}

inline int operator<=(const String& x, const String& y)
{
  return compare(x, y) <= 0; 
}

inline int operator==(const String& x, const SubString&  y) 
{
  return compare(x, y) == 0; 
}

inline int operator!=(const String& x, const SubString&  y)
{
  return compare(x, y) != 0; 
}

inline int operator>(const String& x, const SubString&  y)      
{
  return compare(x, y) > 0; 
}

inline int operator>=(const String& x, const SubString&  y)
{
  return compare(x, y) >= 0; 
}

inline int operator<(const String& x, const SubString&  y) 
{
  return compare(x, y) < 0; 
}

inline int operator<=(const String& x, const SubString&  y)
{
  return compare(x, y) <= 0; 
}

inline int operator==(const String& x, const char* t) 
{
  return compare(x, t) == 0; 
}

inline int operator!=(const String& x, const char* t) 
{
  return compare(x, t) != 0; 
}

inline int operator>(const String& x, const char* t)  
{
  return compare(x, t) > 0; 
}

inline int operator>=(const String& x, const char* t) 
{
  return compare(x, t) >= 0; 
}

inline int operator<(const String& x, const char* t)  
{
  return compare(x, t) < 0; 
}

inline int operator<=(const String& x, const char* t) 
{
  return compare(x, t) <= 0; 
}

inline int operator==(const SubString& x, const String& y) 
{
  return compare(y, x) == 0; 
}

inline int operator!=(const SubString& x, const String& y)
{
  return compare(y, x) != 0;
}

inline int operator>(const SubString& x, const String& y)      
{
  return compare(y, x) < 0;
}

inline int operator>=(const SubString& x, const String& y)     
{
  return compare(y, x) <= 0;
}

inline int operator<(const SubString& x, const String& y)      
{
  return compare(y, x) > 0;
}

inline int operator<=(const SubString& x, const String& y)     
{
  return compare(y, x) >= 0;
}

inline int operator==(const SubString& x, const SubString&  y) 
{
  return compare(x, y) == 0; 
}

inline int operator!=(const SubString& x, const SubString&  y)
{
  return compare(x, y) != 0;
}

inline int operator>(const SubString& x, const SubString&  y)      
{
  return compare(x, y) > 0;
}

inline int operator>=(const SubString& x, const SubString&  y)
{
  return compare(x, y) >= 0;
}

inline int operator<(const SubString& x, const SubString&  y) 
{
  return compare(x, y) < 0;
}

inline int operator<=(const SubString& x, const SubString&  y)
{
  return compare(x, y) <= 0;
}

inline int operator==(const SubString& x, const char* t) 
{
  return compare(x, t) == 0; 
}

inline int operator!=(const SubString& x, const char* t) 
{
  return compare(x, t) != 0;
}

inline int operator>(const SubString& x, const char* t)  
{
  return compare(x, t) > 0; 
}

inline int operator>=(const SubString& x, const char* t) 
{
  return compare(x, t) >= 0; 
}

inline int operator<(const SubString& x, const char* t)  
{
  return compare(x, t) < 0; 
}

inline int operator<=(const SubString& x, const char* t) 
{
  return compare(x, t) <= 0; 
}


// a helper needed by at, before, etc.

inline SubString String::_substr(_BIGSTRING_SIZE first, _BIGSTRING_SIZE l)
{
  if (first < 0 || (unsigned)(first + l) > length() )
    return SubString(_nilString, 0, 0) ;
  else 
    return SubString(*this, first, l);
}

#endif