aboutsummaryrefslogtreecommitdiff
path: root/guilib/StdString.h
blob: 9800f946b2b0dac71ebbc5bdc7eb4e9727acae33 (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
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
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
#pragma once
#include <string>
#include <stdint.h>
#include <vector>

#if defined(_WIN32) && !defined(va_copy)
#define va_copy(dst, src) ((dst) = (src))
#endif

// =============================================================================
//  FILE:  StdString.h
//  AUTHOR:  Joe O'Leary (with outside help noted in comments)
//
//    If you find any bugs in this code, please let me know:
//
//        jmoleary@earthlink.net
//        http://www.joeo.net/stdstring.htm (a bit outdated)
//
//      The latest version of this code should always be available at the
//      following link:
//
//              http://www.joeo.net/code/StdString.zip (Dec 6, 2003)
//
//
//  REMARKS:
//    This header file declares the CStdStr template.  This template derives
//    the Standard C++ Library basic_string<> template and add to it the
//    the following conveniences:
//      - The full MFC CString set of functions (including implicit cast)
//      - writing to/reading from COM IStream interfaces
//      - Functional objects for use in STL algorithms
//
//    From this template, we intstantiate two classes:  CStdStringA and
//    CStdStringW.  The name "CStdString" is just a #define of one of these,
//    based upone the UNICODE macro setting
//
//    This header also declares our own version of the MFC/ATL UNICODE-MBCS
//    conversion macros.  Our version looks exactly like the Microsoft's to
//    facilitate portability.
//
//  NOTE:
//    If you you use this in an MFC or ATL build, you should include either
//    afx.h or atlbase.h first, as appropriate.
//
//  PEOPLE WHO HAVE CONTRIBUTED TO THIS CLASS:
//
//    Several people have helped me iron out problems and othewise improve
//    this class.  OK, this is a long list but in my own defense, this code
//    has undergone two major rewrites.  Many of the improvements became
//    necessary after I rewrote the code as a template.  Others helped me
//    improve the CString facade.
//
//    Anyway, these people are (in chronological order):
//
//      - Pete the Plumber (???)
//      - Julian Selman
//      - Chris (of Melbsys)
//      - Dave Plummer
//      - John C Sipos
//      - Chris Sells
//      - Nigel Nunn
//      - Fan Xia
//      - Matthew Williams
//      - Carl Engman
//      - Mark Zeren
//      - Craig Watson
//      - Rich Zuris
//      - Karim Ratib
//      - Chris Conti
//      - Baptiste Lepilleur
//      - Greg Pickles
//      - Jim Cline
//      - Jeff Kohn
//      - Todd Heckel
//      - Ullrich Poll�hne
//      - Joe Vitaterna
//      - Joe Woodbury
//      - Aaron (no last name)
//      - Joldakowski (???)
//      - Scott Hathaway
//      - Eric Nitzche
//      - Pablo Presedo
//      - Farrokh Nejadlotfi
//      - Jason Mills
//      - Igor Kholodov
//      - Mike Crusader
//      - John James
//      - Wang Haifeng
//      - Tim Dowty
//          - Arnt Witteveen
//          - Glen Maynard
//          - Paul DeMarco
//          - Bagira (full name?)
//          - Ronny Schulz
//          - Jakko Van Hunen
//      - Charles Godwin
//      - Henk Demper
//      - Greg Marr
//      - Bill Carducci
//      - Brian Groose
//      - MKingman
//      - Don Beusee
//
//  REVISION HISTORY
//
//    2005-JAN-10 - Thanks to Don Beusee for pointing out the danger in mapping
//          length-checked formatting functions to non-length-checked
//          CRT equivalents.  Also thanks to him for motivating me to
//          optimize my implementation of Replace()
//
//    2004-APR-22 - A big, big thank you to "MKingman" (whoever you are) for
//          finally spotting a silly little error in StdCodeCvt that
//          has been causing me (and users of CStdString) problems for
//          years in some relatively rare conversions.  I had reversed
//          two length arguments.
//
//    2003-NOV-24 - Thanks to a bunch of people for helping me clean up many
//          compiler warnings (and yes, even a couple of actual compiler
//          errors).  These include Henk Demper for figuring out how
//          to make the Intellisense work on with CStdString on VC6,
//          something I was never able to do.  Greg Marr pointed out
//          a compiler warning about an unreferenced symbol and a
//          problem with my version of Load in MFC builds.  Bill
//          Carducci took a lot of time with me to help me figure out
//          why some implementations of the Standard C++ Library were
//          returning error codes for apparently successful conversions
//          between ASCII and UNICODE.  Finally thanks to Brian Groose
//          for helping me fix compiler signed unsigned warnings in
//          several functions.
//
//    2003-JUL-10 - Thanks to Charles Godwin for making me realize my 'FmtArg'
//          fixes had inadvertently broken the DLL-export code (which is
//                  normally commented out.  I had to move it up higher.  Also
//          this helped me catch a bug in ssicoll that would prevent
//                  compilation, otherwise.
//
//    2003-MAR-14 - Thanks to Jakko Van Hunen for pointing out a copy-and-paste
//                  bug in one of the overloads of FmtArg.
//
//    2003-MAR-10 - Thanks to Ronny Schulz for (twice!) sending me some changes
//                  to help CStdString build on SGI and for pointing out an
//                  error in placement of my preprocessor macros for ssfmtmsg.
//
//    2002-NOV-26 - Thanks to Bagira for pointing out that my implementation of
//                  SpanExcluding was not properly handling the case in which
//                  the string did NOT contain any of the given characters
//
//    2002-OCT-21 - Many thanks to Paul DeMarco who was invaluable in helping me
//                  get this code working with Borland's free compiler as well
//                  as the Dev-C++ compiler (available free at SourceForge).
//
//    2002-SEP-13 - Thanks to Glen Maynard who helped me get rid of some loud
//                  but harmless warnings that were showing up on g++.  Glen
//                  also pointed out that some pre-declarations of FmtArg<>
//                  specializations were unnecessary (and no good on G++)
//
//    2002-JUN-26 - Thanks to Arnt Witteveen for pointing out that I was using
//                  static_cast<> in a place in which I should have been using
//                  reinterpret_cast<> (the ctor for unsigned char strings).
//                  That's what happens when I don't unit-test properly!
//                  Arnt also noticed that CString was silently correcting the
//                  'nCount' argument to Left() and Right() where CStdString was
//                  not (and crashing if it was bad).  That is also now fixed!
//
//    2002-FEB-25 - Thanks to Tim Dowty for pointing out (and giving me the fix
//          for) a conversion problem with non-ASCII MBCS characters.
//          CStdString is now used in my favorite commercial MP3 player!
//
//    2001-DEC-06 - Thanks to Wang Haifeng for spotting a problem in one of the
//          assignment operators (for _bstr_t) that would cause compiler
//          errors when refcounting protection was turned off.
//
//    2001-NOV-27 - Remove calls to operator!= which involve reverse_iterators
//          due to a conflict with the rel_ops operator!=.  Thanks to
//          John James for pointing this out.
//
//    2001-OCT-29 - Added a minor range checking fix for the Mid function to
//          make it as forgiving as CString's version is.  Thanks to
//          Igor Kholodov for noticing this.
//          - Added a specialization of std::swap for CStdString.  Thanks
//          to Mike Crusader for suggesting this!  It's commented out
//          because you're not supposed to inject your own code into the
//          'std' namespace.  But if you don't care about that, it's
//          there if you want it
//          - Thanks to Jason Mills for catching a case where CString was
//          more forgiving in the Delete() function than I was.
//
//    2001-JUN-06 - I was violating the Standard name lookup rules stated
//          in [14.6.2(3)].  None of the compilers I've tried so
//          far apparently caught this but HP-UX aCC 3.30 did.  The
//          fix was to add 'this->' prefixes in many places.
//          Thanks to Farrokh Nejadlotfi for this!
//
//    2001-APR-27 - StreamLoad was calculating the number of BYTES in one
//          case, not characters.  Thanks to Pablo Presedo for this.
//
//    2001-FEB-23 - Replace() had a bug which caused infinite loops if the
//          source string was empty.  Fixed thanks to Eric Nitzsche.
//
//    2001-FEB-23 - Scott Hathaway was a huge help in providing me with the
//          ability to build CStdString on Sun Unix systems.  He
//          sent me detailed build reports about what works and what
//          does not.  If CStdString compiles on your Unix box, you
//          can thank Scott for it.
//
//    2000-DEC-29 - Joldakowski noticed one overload of Insert failed to do a
//          range check as CString's does.  Now fixed -- thanks!
//
//    2000-NOV-07 - Aaron pointed out that I was calling static member
//          functions of char_traits via a temporary.  This was not
//          technically wrong, but it was unnecessary and caused
//          problems for poor old buggy VC5.  Thanks Aaron!
//
//    2000-JUL-11 - Joe Woodbury noted that the CString::Find docs don't match
//          what the CString::Find code really ends up doing.   I was
//          trying to match the docs.  Now I match the CString code
//          - Joe also caught me truncating strings for GetBuffer() calls
//          when the supplied length was less than the current length.
//
//    2000-MAY-25 - Better support for STLPORT's Standard library distribution
//          - Got rid of the NSP macro - it interfered with Koenig lookup
//          - Thanks to Joe Woodbury for catching a TrimLeft() bug that
//          I introduced in January.  Empty strings were not getting
//          trimmed
//
//    2000-APR-17 - Thanks to Joe Vitaterna for pointing out that ReverseFind
//          is supposed to be a const function.
//
//    2000-MAR-07 - Thanks to Ullrich Poll�hne for catching a range bug in one
//          of the overloads of assign.
//
//    2000-FEB-01 - You can now use CStdString on the Mac with CodeWarrior!
//          Thanks to Todd Heckel for helping out with this.
//
//    2000-JAN-23 - Thanks to Jim Cline for pointing out how I could make the
//          Trim() function more efficient.
//          - Thanks to Jeff Kohn for prompting me to find and fix a typo
//          in one of the addition operators that takes _bstr_t.
//          - Got rid of the .CPP file -  you only need StdString.h now!
//
//    1999-DEC-22 - Thanks to Greg Pickles for helping me identify a problem
//          with my implementation of CStdString::FormatV in which
//          resulting string might not be properly NULL terminated.
//
//    1999-DEC-06 - Chris Conti pointed yet another basic_string<> assignment
//          bug that MS has not fixed.  CStdString did nothing to fix
//          it either but it does now!  The bug was: create a string
//          longer than 31 characters, get a pointer to it (via c_str())
//          and then assign that pointer to the original string object.
//          The resulting string would be empty.  Not with CStdString!
//
//    1999-OCT-06 - BufferSet was erasing the string even when it was merely
//          supposed to shrink it.  Fixed.  Thanks to Chris Conti.
//          - Some of the Q172398 fixes were not checking for assignment-
//          to-self.  Fixed.  Thanks to Baptiste Lepilleur.
//
//    1999-AUG-20 - Improved Load() function to be more efficient by using
//          SizeOfResource().  Thanks to Rich Zuris for this.
//          - Corrected resource ID constructor, again thanks to Rich.
//          - Fixed a bug that occurred with UNICODE characters above
//          the first 255 ANSI ones.  Thanks to Craig Watson.
//          - Added missing overloads of TrimLeft() and TrimRight().
//          Thanks to Karim Ratib for pointing them out
//
//    1999-JUL-21 - Made all calls to GetBuf() with no args check length first.
//
//    1999-JUL-10 - Improved MFC/ATL independence of conversion macros
//          - Added SS_NO_REFCOUNT macro to allow you to disable any
//          reference-counting your basic_string<> impl. may do.
//          - Improved ReleaseBuffer() to be as forgiving as CString.
//          Thanks for Fan Xia for helping me find this and to
//          Matthew Williams for pointing it out directly.
//
//    1999-JUL-06 - Thanks to Nigel Nunn for catching a very sneaky bug in
//          ToLower/ToUpper.  They should call GetBuf() instead of
//          data() in order to ensure the changed string buffer is not
//          reference-counted (in those implementations that refcount).
//
//    1999-JUL-01 - Added a true CString facade.  Now you can use CStdString as
//          a drop-in replacement for CString.  If you find this useful,
//          you can thank Chris Sells for finally convincing me to give
//          in and implement it.
//          - Changed operators << and >> (for MFC CArchive) to serialize
//          EXACTLY as CString's do.  So now you can send a CString out
//          to a CArchive and later read it in as a CStdString.   I have
//          no idea why you would want to do this but you can.
//
//    1999-JUN-21 - Changed the CStdString class into the CStdStr template.
//          - Fixed FormatV() to correctly decrement the loop counter.
//          This was harmless bug but a bug nevertheless.  Thanks to
//          Chris (of Melbsys) for pointing it out
//          - Changed Format() to try a normal stack-based array before
//          using to _alloca().
//          - Updated the text conversion macros to properly use code
//          pages and to fit in better in MFC/ATL builds.  In other
//          words, I copied Microsoft's conversion stuff again.
//          - Added equivalents of CString::GetBuffer, GetBufferSetLength
//          - new sscpy() replacement of CStdString::CopyString()
//          - a Trim() function that combines TrimRight() and TrimLeft().
//
//    1999-MAR-13 - Corrected the "NotSpace" functional object to use _istpace()
//          instead of _isspace()   Thanks to Dave Plummer for this.
//
//    1999-FEB-26 - Removed errant line (left over from testing) that #defined
//          _MFC_VER.  Thanks to John C Sipos for noticing this.
//
//    1999-FEB-03 - Fixed a bug in a rarely-used overload of operator+() that
//          caused infinite recursion and stack overflow
//          - Added member functions to simplify the process of
//          persisting CStdStrings to/from DCOM IStream interfaces
//          - Added functional objects (e.g. StdStringLessNoCase) that
//          allow CStdStrings to be used as keys STL map objects with
//          case-insensitive comparison
//          - Added array indexing operators (i.e. operator[]).  I
//          originally assumed that these were unnecessary and would be
//          inherited from basic_string.  However, without them, Visual
//          C++ complains about ambiguous overloads when you try to use
//          them.  Thanks to Julian Selman to pointing this out.
//
//    1998-FEB-?? - Added overloads of assign() function to completely account
//          for Q172398 bug.  Thanks to "Pete the Plumber" for this
//
//    1998-FEB-?? - Initial submission
//
// COPYRIGHT:
//    2002 Joseph M. O'Leary.  This code is 100% free.  Use it anywhere you
//      want.  Rewrite it, restructure it, whatever.  If you can write software
//      that makes money off of it, good for you.  I kinda like capitalism.
//      Please don't blame me if it causes your $30 billion dollar satellite
//      explode in orbit.  If you redistribute it in any form, I'd appreciate it
//      if you would leave this notice here.
// =============================================================================

// Avoid multiple inclusion

#ifndef STDSTRING_H
#define STDSTRING_H

// When using VC, turn off browser references
// Turn off unavoidable compiler warnings

#if defined(_MSC_VER) && (_MSC_VER > 1100)
  #pragma component(browser, off, references, "CStdString")
  #pragma warning (disable : 4290) // C++ Exception Specification ignored
  #pragma warning (disable : 4127) // Conditional expression is constant
  #pragma warning (disable : 4097) // typedef name used as synonym for class name
#endif

// Borland warnings to turn off

#ifdef __BORLANDC__
    #pragma option push -w-inl
//  #pragma warn -inl   // Turn off inline function warnings
#endif

// SS_IS_INTRESOURCE
// -----------------
//    A copy of IS_INTRESOURCE from VC7.  Because old VC6 version of winuser.h
//    doesn't have this.

#define SS_IS_INTRESOURCE(_r) (false)

#if !defined (SS_ANSI) && defined(_MSC_VER)
  #undef SS_IS_INTRESOURCE
  #if defined(_WIN64)
    #define SS_IS_INTRESOURCE(_r) (((unsigned __int64)(_r) >> 16) == 0)
  #else
    #define SS_IS_INTRESOURCE(_r) (((unsigned long)(_r) >> 16) == 0)
  #endif
#endif


// MACRO: SS_UNSIGNED
// ------------------
//      This macro causes the addition of a constructor and assignment operator
//      which take unsigned characters.  CString has such functions and in order
//      to provide maximum CString-compatability, this code needs them as well.
//      In practice you will likely never need these functions...

//#define SS_UNSIGNED

#ifdef SS_ALLOW_UNSIGNED_CHARS
  #define SS_UNSIGNED
#endif

// MACRO: SS_SAFE_FORMAT
// ---------------------
//      This macro provides limited compatability with a questionable CString
//      "feature".  You can define it in order to avoid a common problem that
//      people encounter when switching from CString to CStdString.
//
//      To illustrate the problem -- With CString, you can do this:
//
//          CString sName("Joe");
//          CString sTmp;
//          sTmp.Format("My name is %s", sName);                    // WORKS!
//
//      However if you were to try this with CStdString, your program would
//      crash.
//
//          CStdString sName("Joe");
//          CStdString sTmp;
//          sTmp.Format("My name is %s", sName);                    // CRASHES!
//
//      You must explicitly call c_str() or cast the object to the proper type
//
//          sTmp.Format("My name is %s", sName.c_str());            // WORKS!
//          sTmp.Format("My name is %s", static_cast<PCSTR>(sName));// WORKS!
//          sTmp.Format("My name is %s", (PCSTR)sName);        // WORKS!
//
//      This is because it is illegal to pass anything but a POD type as a
//      variadic argument to a variadic function (i.e. as one of the "..."
//      arguments).  The type const char* is a POD type.  The type CStdString
//      is not.  Of course, neither is the type CString, but CString lets you do
//      it anyway due to the way they laid out the class in binary.  I have no
//      control over this in CStdString since I derive from whatever
//      implementation of basic_string is available.
//
//      However if you have legacy code (which does this) that you want to take
//      out of the MFC world and you don't want to rewrite all your calls to
//      Format(), then you can define this flag and it will no longer crash.
//
//      Note however that this ONLY works for Format(), not sprintf, fprintf,
//      etc.  If you pass a CStdString object to one of those functions, your
//      program will crash.  Not much I can do to get around this, short of
//      writing substitutes for those functions as well.

#define SS_SAFE_FORMAT  // use new template style Format() function


// MACRO: SS_NO_IMPLICIT_CAST
// --------------------------
//      Some people don't like the implicit cast to const char* (or rather to
//      const CT*) that CStdString (and MFC's CString) provide.  That was the
//      whole reason I created this class in the first place, but hey, whatever
//      bakes your cake.  Just #define this macro to get rid of the the implicit
//      cast.

//#define SS_NO_IMPLICIT_CAST // gets rid of operator const CT*()


// MACRO: SS_NO_REFCOUNT
// ---------------------
//    turns off reference counting at the assignment level.  Only needed
//    for the version of basic_string<> that comes with Visual C++ versions
//    6.0 or earlier, and only then in some heavily multithreaded scenarios.
//    Uncomment it if you feel you need it.

//#define SS_NO_REFCOUNT

// MACRO: SS_WIN32
// ---------------
//      When this flag is set, we are building code for the Win32 platform and
//      may use Win32 specific functions (such as LoadString).  This gives us
//      a couple of nice extras for the code.
//
//      Obviously, Microsoft's is not the only compiler available for Win32 out
//      there.  So I can't just check to see if _MSC_VER is defined to detect
//      if I'm building on Win32.  So for now, if you use MS Visual C++ or
//      Borland's compiler, I turn this on.  Otherwise you may turn it on
//      yourself, if you prefer

#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(_WIN32)
 #define SS_WIN32
#endif

// MACRO: SS_ANSI
// --------------
//      When this macro is defined, the code attempts only to use ANSI/ISO
//      standard library functions to do it's work.  It will NOT attempt to use
//      any Win32 of Visual C++ specific functions -- even if they are
//      available.  You may define this flag yourself to prevent any Win32
//      of VC++ specific functions from being called.

// If we're not on Win32, we MUST use an ANSI build

#ifndef SS_WIN32
    #if !defined(SS_NO_ANSI)
        #define SS_ANSI
    #endif
#endif

// MACRO: SS_ALLOCA
// ----------------
//      Some implementations of the Standard C Library have a non-standard
//      function known as alloca().  This functions allows one to allocate a
//      variable amount of memory on the stack.  It is needed to implement
//      the ASCII/MBCS conversion macros.
//
//      I wanted to find some way to determine automatically if alloca() is
//    available on this platform via compiler flags but that is asking for
//    trouble.  The crude test presented here will likely need fixing on
//    other platforms.  Therefore I'll leave it up to you to fiddle with
//    this test to determine if it exists.  Just make sure SS_ALLOCA is or
//    is not defined as appropriate and you control this feature.

#if defined(_MSC_VER) && !defined(SS_ANSI)
  #define SS_ALLOCA
#endif


// MACRO: SS_MBCS
// --------------
//    Setting this macro means you are using MBCS characters.  In MSVC builds,
//    this macro gets set automatically by detection of the preprocessor flag
//    _MBCS.  For other platforms you may set it manually if you wish.  The
//    only effect it currently has is to cause the allocation of more space
//    for wchar_t --> char conversions.
//    Note that MBCS does not mean UNICODE.
//
//  #define SS_MBCS
//

#ifdef _MBCS
  #define SS_MBCS
#endif


// MACRO SS_NO_LOCALE
// ------------------
// If your implementation of the Standard C++ Library lacks the <locale> header,
// you can #define this macro to make your code build properly.  Note that this
// is some of my newest code and frankly I'm not very sure of it, though it does
// pass my unit tests.

// #define SS_NO_LOCALE


// Compiler Error regarding _UNICODE and UNICODE
// -----------------------------------------------
// Microsoft header files are screwy.  Sometimes they depend on a preprocessor
// flag named "_UNICODE".  Other times they check "UNICODE" (note the lack of
// leading underscore in the second version".  In several places, they silently
// "synchronize" these two flags this by defining one of the other was defined.
// In older version of this header, I used to try to do the same thing.
//
// However experience has taught me that this is a bad idea.  You get weird
// compiler errors that seem to indicate things like LPWSTR and LPTSTR not being
// equivalent in UNICODE builds, stuff like that (when they MUST be in a proper
// UNICODE  build).  You end up scratching your head and saying, "But that HAS
// to compile!".
//
// So what should you do if you get this error?
//
// Make sure that both macros (_UNICODE and UNICODE) are defined before this
// file is included.  You can do that by either
//
//    a) defining both yourself before any files get included
//    b) including the proper MS headers in the proper order
//    c) including this file before any other file, uncommenting
//       the #defines below, and commenting out the #errors
//
//  Personally I recommend solution a) but it's your call.

#ifdef _MSC_VER
  #if defined (_UNICODE) && !defined (UNICODE)
    #error UNICODE defined  but not UNICODE
  //  #define UNICODE  // no longer silently fix this
  #endif
  #if defined (UNICODE) && !defined (_UNICODE)
    #error Warning, UNICODE defined  but not _UNICODE
  //  #define _UNICODE  // no longer silently fix this
  #endif
#endif


// -----------------------------------------------------------------------------
// MIN and MAX.  The Standard C++ template versions go by so many names (at
// at least in the MS implementation) that you never know what's available
// -----------------------------------------------------------------------------
template<class Type>
inline const Type& SSMIN(const Type& arg1, const Type& arg2)
{
  return arg2 < arg1 ? arg2 : arg1;
}
template<class Type>
inline const Type& SSMAX(const Type& arg1, const Type& arg2)
{
  return arg2 > arg1 ? arg2 : arg1;
}

// If they have not #included W32Base.h (part of my W32 utility library) then
// we need to define some stuff.  Otherwise, this is all defined there.

#if !defined(W32BASE_H)

  // If they want us to use only standard C++ stuff (no Win32 stuff)

  #ifdef SS_ANSI

    // On Win32 we have TCHAR.H so just include it.  This is NOT violating
        // the spirit of SS_ANSI as we are not calling any Win32 functions here.

    #ifdef SS_WIN32

      #include <TCHAR.H>
      #include <WTYPES.H>
      #ifndef STRICT
        #define STRICT
      #endif

        // ... but on non-Win32 platforms, we must #define the types we need.

    #else

      typedef const char*    PCSTR;
      typedef char*      PSTR;
      typedef const wchar_t*  PCWSTR;
      typedef wchar_t*    PWSTR;
      #ifdef UNICODE
        typedef wchar_t    TCHAR;
      #else
        typedef char    TCHAR;
      #endif
      typedef wchar_t      OLECHAR;

    #endif  // #ifndef _WIN32


    // Make sure ASSERT and verify are defined using only ANSI stuff

    #ifndef ASSERT
      #include <assert.h>
      #define ASSERT(f) assert((f))
    #endif
    #ifndef VERIFY
      #ifdef _DEBUG
        #define VERIFY(x) ASSERT((x))
      #else
        #define VERIFY(x) x
      #endif
    #endif

  #else // ...else SS_ANSI is NOT defined

    #include <TCHAR.H>
    #include <WTYPES.H>
    #ifndef STRICT
      #define STRICT
    #endif

    // Make sure ASSERT and verify are defined

    #ifndef ASSERT
      #include <crtdbg.h>
      #define ASSERT(f) _ASSERTE((f))
    #endif
    #ifndef VERIFY
      #ifdef _DEBUG
        #define VERIFY(x) ASSERT((x))
      #else
        #define VERIFY(x) x
      #endif
    #endif

  #endif // #ifdef SS_ANSI

  #ifndef UNUSED
    #define UNUSED(x) x
  #endif

#endif // #ifndef W32BASE_H

// Standard headers needed

#include <string>      // basic_string
#include <algorithm>    // for_each, etc.
#include <functional>    // for StdStringLessNoCase, et al
#ifndef SS_NO_LOCALE
  #include <locale>      // for various facets
#endif

// If this is a recent enough version of VC include comdef.h, so we can write
// member functions to deal with COM types & compiler support classes e.g.
// _bstr_t

#if defined (_MSC_VER) && (_MSC_VER >= 1100)
 #include <comdef.h>
 #define SS_INC_COMDEF  // signal that we #included MS comdef.h file
 #define STDSTRING_INC_COMDEF
 #define SS_NOTHROW __declspec(nothrow)
#else
  #define SS_NOTHROW
#endif

#ifndef TRACE
  #define TRACE_DEFINED_HERE
  #define TRACE
#endif

// Microsoft defines PCSTR, PCWSTR, etc, but no PCTSTR.  I hate to use the
// versions with the "L" in front of them because that's a leftover from Win 16
// days, even though it evaluates to the same thing.  Therefore, Define a PCSTR
// as an LPCTSTR.

#if !defined(PCTSTR) && !defined(PCTSTR_DEFINED)
  typedef const TCHAR*      PCTSTR;
  #define PCTSTR_DEFINED
#endif

#if !defined(PCOLESTR) && !defined(PCOLESTR_DEFINED)
  typedef const OLECHAR*      PCOLESTR;
  #define PCOLESTR_DEFINED
#endif

#if !defined(POLESTR) && !defined(POLESTR_DEFINED)
  typedef OLECHAR*        POLESTR;
  #define POLESTR_DEFINED
#endif

#if !defined(PCUSTR) && !defined(PCUSTR_DEFINED)
  typedef const unsigned char*  PCUSTR;
  typedef unsigned char*      PUSTR;
  #define PCUSTR_DEFINED
#endif


// SGI compiler 7.3 doesnt know these  types - oh and btw, remember to use
// -LANG:std in the CXX Flags
#if defined(__sgi)
    typedef unsigned long           DWORD;
    typedef void *                  LPCVOID;
#endif


// SS_USE_FACET macro and why we need it:
//
// Since I'm a good little Standard C++ programmer, I use locales.  Thus, I
// need to make use of the use_facet<> template function here.   Unfortunately,
// this need is complicated by the fact the MS' implementation of the Standard
// C++ Library has a non-standard version of use_facet that takes more
// arguments than the standard dictates.  Since I'm trying to write CStdString
// to work with any version of the Standard library, this presents a problem.
//
// The upshot of this is that I can't do 'use_facet' directly.  The MS' docs
// tell me that I have to use a macro, _USE() instead.  Since _USE obviously
// won't be available in other implementations, this means that I have to write
// my OWN macro -- SS_USE_FACET -- that evaluates either to _USE or to the
// standard, use_facet.
//
// If you are having trouble with the SS_USE_FACET macro, in your implementation
// of the Standard C++ Library, you can define your own version of SS_USE_FACET.

#ifndef schMSG
  #define schSTR(x)     #x
  #define schSTR2(x)  schSTR(x)
  #define schMSG(desc) message(__FILE__ "(" schSTR2(__LINE__) "):" #desc)
#endif

#ifndef SS_USE_FACET

  // STLPort #defines a macro (__STL_NO_EXPLICIT_FUNCTION_TMPL_ARGS) for
  // all MSVC builds, erroneously in my opinion.  It causes problems for
  // my SS_ANSI builds.  In my code, I always comment out that line.  You'll
  // find it in   \stlport\config\stl_msvc.h

  #if defined(__SGI_STL_PORT) && (__SGI_STL_PORT >= 0x400 )

    #if defined(__STL_NO_EXPLICIT_FUNCTION_TMPL_ARGS) && defined(_MSC_VER)
      #ifdef SS_ANSI
        #pragma schMSG(__STL_NO_EXPLICIT_FUNCTION_TMPL_ARGS defined!!)
      #endif
    #endif
    #define SS_USE_FACET(loc, fac) std::use_facet<fac >(loc)

  #elif defined(_MSC_VER )

    #define SS_USE_FACET(loc, fac) std::_USE(loc, fac)

  // ...and
  #elif defined(_RWSTD_NO_TEMPLATE_ON_RETURN_TYPE)

        #define SS_USE_FACET(loc, fac) std::use_facet(loc, (fac*)0)

  #else

    #define SS_USE_FACET(loc, fac) std::use_facet<fac >(loc)

  #endif

#endif

// =============================================================================
// UNICODE/MBCS conversion macros.  Made to work just like the MFC/ATL ones.
// =============================================================================

#include <wchar.h>      // Added to Std Library with Amendment #1.

// First define the conversion helper functions.  We define these regardless of
// any preprocessor macro settings since their names won't collide.

// Not sure if we need all these headers.   I believe ANSI says we do.

#include <stdio.h>
#include <stdarg.h>
#include <wctype.h>
#include <ctype.h>
#include <stdlib.h>
#ifndef va_start
  #include <varargs.h>
#endif


#ifdef SS_NO_LOCALE

  #if defined(_WIN32) || defined (_WIN32_WCE)

    inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCSTR pSrcA, int nSrc,
      UINT acp=CP_ACP)
    {
      ASSERT(0 != pSrcA);
      ASSERT(0 != pDstW);
      pDstW[0] = '\0';
      MultiByteToWideChar(acp, 0, pSrcA, nSrc, pDstW, nDst);
      return pDstW;
    }
    inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCUSTR pSrcA, int nSrc,
      UINT acp=CP_ACP)
    {
      return StdCodeCvt(pDstW, nDst, (PCSTR)pSrcA, nSrc, acp);
    }

    inline PSTR StdCodeCvt(PSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
      UINT acp=CP_ACP)
    {
      ASSERT(0 != pDstA);
      ASSERT(0 != pSrcW);
      pDstA[0] = '\0';
      WideCharToMultiByte(acp, 0, pSrcW, nSrc, pDstA, nDst, 0, 0);
      return pDstA;
    }
    inline PUSTR StdCodeCvt(PUSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
      UINT acp=CP_ACP)
    {
      return (PUSTR)StdCodeCvt((PSTR)pDstA, nDst, pSrcW, nSrc, acp);
    }
  #else
  #endif

#else

  // StdCodeCvt - made to look like Win32 functions WideCharToMultiByte
  //        and MultiByteToWideChar but uses locales in SS_ANSI
  //        builds.  There are a number of overloads.
  //              First argument is the destination buffer.
  //              Second argument is the source buffer
  //#if defined (SS_ANSI) || !defined (SS_WIN32)

  // 'SSCodeCvt' - shorthand name for the codecvt facet we use

  typedef std::codecvt<wchar_t, char, mbstate_t> SSCodeCvt;

  inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCSTR pSrcA, int nSrc,
    const std::locale& loc=std::locale())
  {
    ASSERT(0 != pSrcA);
    ASSERT(0 != pDstW);

    pDstW[0]          = '\0';

    if ( nSrc > 0 )
    {
      PCSTR pNextSrcA      = pSrcA;
      PWSTR pNextDstW      = pDstW;
      SSCodeCvt::result res  = SSCodeCvt::ok;
      const SSCodeCvt& conv  = SS_USE_FACET(loc, SSCodeCvt);
      SSCodeCvt::state_type st= { 0 };
      res            = conv.in(st,
                    pSrcA, pSrcA + nSrc, pNextSrcA,
                    pDstW, pDstW + nDst, pNextDstW);
#ifdef _LINUX
#define ASSERT2(a) if (!(a)) {fprintf(stderr, "StdString: Assertion Failed on line %d\n", __LINE__);}
#else
#define ASSERT2 ASSERT
#endif
      ASSERT2(SSCodeCvt::ok == res);
      ASSERT2(SSCodeCvt::error != res);
      ASSERT2(pNextDstW >= pDstW);
      ASSERT2(pNextSrcA >= pSrcA);
#undef ASSERT2
      // Null terminate the converted string

      if ( pNextDstW - pDstW > nDst )
        *(pDstW + nDst) = '\0';
      else
        *pNextDstW = '\0';
    }
    return pDstW;
  }
  inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCUSTR pSrcA, int nSrc,
    const std::locale& loc=std::locale())
  {
    return StdCodeCvt(pDstW, nDst, (PCSTR)pSrcA, nSrc, loc);
  }

  inline PSTR StdCodeCvt(PSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
    const std::locale& loc=std::locale())
  {
    ASSERT(0 != pDstA);
    ASSERT(0 != pSrcW);

    pDstA[0]          = '\0';

    if ( nSrc > 0 )
    {
      PSTR pNextDstA      = pDstA;
      PCWSTR pNextSrcW    = pSrcW;
      SSCodeCvt::result res  = SSCodeCvt::ok;
      const SSCodeCvt& conv  = SS_USE_FACET(loc, SSCodeCvt);
      SSCodeCvt::state_type st= { 0 };
      res            = conv.out(st,
                    pSrcW, pSrcW + nSrc, pNextSrcW,
                    pDstA, pDstA + nDst, pNextDstA);
#ifdef _LINUX
#define ASSERT2(a) if (!(a)) {fprintf(stderr, "StdString: Assertion Failed on line %d\n", __LINE__);}
#else
#define ASSERT2 ASSERT
#endif
      ASSERT2(SSCodeCvt::error != res);
      ASSERT2(SSCodeCvt::ok == res);  // strict, comment out for sanity
      ASSERT2(pNextDstA >= pDstA);
      ASSERT2(pNextSrcW >= pSrcW);
#undef ASSERT2

      // Null terminate the converted string

      if ( pNextDstA - pDstA > nDst )
        *(pDstA + nDst) = '\0';
      else
        *pNextDstA = '\0';
    }
    return pDstA;
  }

  inline PUSTR StdCodeCvt(PUSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
    const std::locale& loc=std::locale())
  {
    return (PUSTR)StdCodeCvt((PSTR)pDstA, nDst, pSrcW, nSrc, loc);
  }

#endif



// Unicode/MBCS conversion macros are only available on implementations of
// the "C" library that have the non-standard _alloca function.  As far as I
// know that's only Microsoft's though I've heard that the function exists
// elsewhere.

#if defined(SS_ALLOCA) && !defined SS_NO_CONVERSION

    #include <malloc.h>  // needed for _alloca

    // Define our conversion macros to look exactly like Microsoft's to
    // facilitate using this stuff both with and without MFC/ATL

    #ifdef _CONVERSION_USES_THREAD_LOCALE

      #ifndef _DEBUG
        #define SSCVT int _cvt; _cvt; UINT _acp=GetACP(); \
          _acp; PCWSTR _pw; _pw; PCSTR _pa; _pa
      #else
        #define SSCVT int _cvt = 0; _cvt; UINT _acp=GetACP();\
           _acp; PCWSTR _pw=0; _pw; PCSTR _pa=0; _pa
      #endif
      #define SSA2W(pa) (\
        ((_pa = pa) == 0) ? 0 : (\
          _cvt = (sslen(_pa)),\
          StdCodeCvt((PWSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
              _pa, _cvt, _acp)))
      #define SSW2A(pw) (\
        ((_pw = pw) == 0) ? 0 : (\
          _cvt = sslen(_pw), \
          StdCodeCvt((LPSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
          _pw, _cvt, _acp)))
  #else

      #ifndef _DEBUG
        #define SSCVT int _cvt; _cvt; UINT _acp=CP_ACP; _acp;\
           PCWSTR _pw; _pw; PCSTR _pa; _pa
      #else
        #define SSCVT int _cvt = 0; _cvt; UINT _acp=CP_ACP; \
          _acp; PCWSTR _pw=0; _pw; PCSTR _pa=0; _pa
      #endif
      #define SSA2W(pa) (\
        ((_pa = pa) == 0) ? 0 : (\
          _cvt = (sslen(_pa)),\
          StdCodeCvt((PWSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
          _pa, _cvt)))
      #define SSW2A(pw) (\
        ((_pw = pw) == 0) ? 0 : (\
          _cvt = (sslen(_pw)),\
          StdCodeCvt((LPSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
          _pw, _cvt)))
    #endif

    #define SSA2CW(pa) ((PCWSTR)SSA2W((pa)))
    #define SSW2CA(pw) ((PCSTR)SSW2A((pw)))

    #ifdef UNICODE
      #define SST2A  SSW2A
      #define SSA2T  SSA2W
      #define SST2CA  SSW2CA
      #define SSA2CT  SSA2CW
    // (Did you get a compiler error here about not being able to convert
    // PTSTR into PWSTR?  Then your _UNICODE and UNICODE flags are messed
    // up.  Best bet: #define BOTH macros before including any MS headers.)
      inline PWSTR  SST2W(PTSTR p)      { return p; }
      inline PTSTR  SSW2T(PWSTR p)      { return p; }
      inline PCWSTR  SST2CW(PCTSTR p)    { return p; }
      inline PCTSTR  SSW2CT(PCWSTR p)    { return p; }
    #else
      #define SST2W  SSA2W
      #define SSW2T  SSW2A
      #define SST2CW  SSA2CW
      #define SSW2CT  SSW2CA
      inline PSTR    SST2A(PTSTR p)      { return p; }
      inline PTSTR  SSA2T(PSTR p)      { return p; }
      inline PCSTR  SST2CA(PCTSTR p)    { return p; }
      inline PCTSTR  SSA2CT(PCSTR p)      { return p; }
    #endif // #ifdef UNICODE

    #if defined(UNICODE)
    // in these cases the default (TCHAR) is the same as OLECHAR
      inline PCOLESTR  SST2COLE(PCTSTR p)    { return p; }
      inline PCTSTR  SSOLE2CT(PCOLESTR p)  { return p; }
      inline POLESTR  SST2OLE(PTSTR p)    { return p; }
      inline PTSTR  SSOLE2T(POLESTR p)    { return p; }
    #elif defined(OLE2ANSI)
    // in these cases the default (TCHAR) is the same as OLECHAR
      inline PCOLESTR  SST2COLE(PCTSTR p)    { return p; }
      inline PCTSTR  SSOLE2CT(PCOLESTR p)  { return p; }
      inline POLESTR  SST2OLE(PTSTR p)    { return p; }
      inline PTSTR  SSOLE2T(POLESTR p)    { return p; }
    #else
      //CharNextW doesn't work on Win95 so we use this
      #define SST2COLE(pa)  SSA2CW((pa))
      #define SST2OLE(pa)    SSA2W((pa))
      #define SSOLE2CT(po)  SSW2CA((po))
      #define SSOLE2T(po)    SSW2A((po))
    #endif

    #ifdef OLE2ANSI
      #define SSW2OLE    SSW2A
      #define SSOLE2W    SSA2W
      #define SSW2COLE  SSW2CA
      #define SSOLE2CW  SSA2CW
      inline POLESTR    SSA2OLE(PSTR p)    { return p; }
      inline PSTR      SSOLE2A(POLESTR p)  { return p; }
      inline PCOLESTR    SSA2COLE(PCSTR p)  { return p; }
      inline PCSTR    SSOLE2CA(PCOLESTR p){ return p; }
    #else
      #define SSA2OLE    SSA2W
      #define SSOLE2A    SSW2A
      #define SSA2COLE  SSA2CW
      #define SSOLE2CA  SSW2CA
      inline POLESTR    SSW2OLE(PWSTR p)  { return p; }
      inline PWSTR    SSOLE2W(POLESTR p)  { return p; }
      inline PCOLESTR    SSW2COLE(PCWSTR p)  { return p; }
      inline PCWSTR    SSOLE2CW(PCOLESTR p){ return p; }
    #endif

    // Above we've defined macros that look like MS' but all have
    // an 'SS' prefix.  Now we need the real macros.  We'll either
    // get them from the macros above or from MFC/ATL.

  #if defined (USES_CONVERSION)

    #define _NO_STDCONVERSION  // just to be consistent

  #else

    #ifdef _MFC_VER

      #include <afxconv.h>
      #define _NO_STDCONVERSION // just to be consistent

    #else

      #define USES_CONVERSION SSCVT
      #define A2CW      SSA2CW
      #define W2CA      SSW2CA
      #define T2A        SST2A
      #define A2T        SSA2T
      #define T2W        SST2W
      #define W2T        SSW2T
      #define T2CA      SST2CA
      #define A2CT      SSA2CT
      #define T2CW      SST2CW
      #define W2CT      SSW2CT
      #define ocslen      sslen
      #define ocscpy      sscpy
      #define T2COLE      SST2COLE
      #define OLE2CT      SSOLE2CT
      #define T2OLE      SST2COLE
      #define OLE2T      SSOLE2CT
      #define A2OLE      SSA2OLE
      #define OLE2A      SSOLE2A
      #define W2OLE      SSW2OLE
      #define OLE2W      SSOLE2W
      #define A2COLE      SSA2COLE
      #define OLE2CA      SSOLE2CA
      #define W2COLE      SSW2COLE
      #define OLE2CW      SSOLE2CW

    #endif // #ifdef _MFC_VER
  #endif // #ifndef USES_CONVERSION
#endif // #ifndef SS_NO_CONVERSION

// Define ostring - generic name for std::basic_string<OLECHAR>

#if !defined(ostring) && !defined(OSTRING_DEFINED)
  typedef std::basic_string<OLECHAR> ostring;
  #define OSTRING_DEFINED
#endif

// StdCodeCvt when there's no conversion to be done
template <typename T>
inline T* StdCodeCvt(T* pDst, int nDst, const T* pSrc, int nSrc)
{
  int nChars = SSMIN(nSrc, nDst);

  if ( nChars > 0 )
  {
    pDst[0]        = '\0';
    std::basic_string<T>::traits_type::copy(pDst, pSrc, nChars);
//    std::char_traits<T>::copy(pDst, pSrc, nChars);
    pDst[nChars]  = '\0';
  }

  return pDst;
}
inline PSTR StdCodeCvt(PSTR pDst, int nDst, PCUSTR pSrc, int nSrc)
{
  return StdCodeCvt(pDst, nDst, (PCSTR)pSrc, nSrc);
}
inline PUSTR StdCodeCvt(PUSTR pDst, int nDst, PCSTR pSrc, int nSrc)
{
  return (PUSTR)StdCodeCvt((PSTR)pDst, nDst, pSrc, nSrc);
}

// Define tstring -- generic name for std::basic_string<TCHAR>

#if !defined(tstring) && !defined(TSTRING_DEFINED)
  typedef std::basic_string<TCHAR> tstring;
  #define TSTRING_DEFINED
#endif

// a very shorthand way of applying the fix for KB problem Q172398
// (basic_string assignment bug)

#if defined ( _MSC_VER ) && ( _MSC_VER < 1200 )
  #define Q172398(x) (x).erase()
#else
  #define Q172398(x)
#endif

// =============================================================================
// INLINE FUNCTIONS ON WHICH CSTDSTRING RELIES
//
// Usually for generic text mapping, we rely on preprocessor macro definitions
// to map to string functions.  However the CStdStr<> template cannot use
// macro-based generic text mappings because its character types do not get
// resolved until template processing which comes AFTER macro processing.  In
// other words, the preprocessor macro UNICODE is of little help to us in the
// CStdStr template
//
// Therefore, to keep the CStdStr declaration simple, we have these inline
// functions.  The template calls them often.  Since they are inline (and NOT
// exported when this is built as a DLL), they will probably be resolved away
// to nothing.
//
// Without these functions, the CStdStr<> template would probably have to broken
// out into two, almost identical classes.  Either that or it would be a huge,
// convoluted mess, with tons of "if" statements all over the place checking the
// size of template parameter CT.
// =============================================================================

#ifdef SS_NO_LOCALE

  // --------------------------------------------------------------------------
  // Win32 GetStringTypeEx wrappers
  // --------------------------------------------------------------------------
  inline bool wsGetStringType(LCID lc, DWORD dwT, PCSTR pS, int nSize,
    WORD* pWd)
  {
    return FALSE != GetStringTypeExA(lc, dwT, pS, nSize, pWd);
  }
  inline bool wsGetStringType(LCID lc, DWORD dwT, PCWSTR pS, int nSize,
    WORD* pWd)
  {
    return FALSE != GetStringTypeExW(lc, dwT, pS, nSize, pWd);
  }


  template<typename CT>
    inline bool ssisspace (CT t)
  {
    WORD toYourMother;
    return  wsGetStringType(GetThreadLocale(), CT_CTYPE1, &t, 1, &toYourMother)
      && 0 != (C1_BLANK & toYourMother);
  }

#endif

// If they defined SS_NO_REFCOUNT, then we must convert all assignments

#if defined (_MSC_VER) && (_MSC_VER < 1300)
  #ifdef SS_NO_REFCOUNT
    #define SSREF(x) (x).c_str()
  #else
    #define SSREF(x) (x)
  #endif
#else
  #define SSREF(x) (x)
#endif

// -----------------------------------------------------------------------------
// sslen: strlen/wcslen wrappers
// -----------------------------------------------------------------------------
template<typename CT> inline int sslen(const CT* pT)
{
  return 0 == pT ? 0 : (int)std::basic_string<CT>::traits_type::length(pT);
//  return 0 == pT ? 0 : std::char_traits<CT>::length(pT);
}
inline SS_NOTHROW int sslen(const std::string& s)
{
  return static_cast<int>(s.length());
}
inline SS_NOTHROW int sslen(const std::wstring& s)
{
  return static_cast<int>(s.length());
}

// -----------------------------------------------------------------------------
// sstolower/sstoupper -- convert characters to upper/lower case
// -----------------------------------------------------------------------------

#ifdef SS_NO_LOCALE
  inline char sstoupper(char ch)    { return (char)::toupper(ch); }
  inline wchar_t sstoupper(wchar_t ch){ return (wchar_t)::towupper(ch); }
  inline char sstolower(char ch)    { return (char)::tolower(ch); }
  inline wchar_t sstolower(wchar_t ch){ return (wchar_t)::tolower(ch); }
#else
  template<typename CT>
  inline CT sstolower(const CT& t, const std::locale& loc = std::locale())
  {
    return std::tolower<CT>(t, loc);
  }
  template<typename CT>
  inline CT sstoupper(const CT& t, const std::locale& loc = std::locale())
  {
    return std::toupper<CT>(t, loc);
  }
#endif

// -----------------------------------------------------------------------------
// ssasn: assignment functions -- assign "sSrc" to "sDst"
// -----------------------------------------------------------------------------
typedef std::string::size_type    SS_SIZETYPE; // just for shorthand, really
typedef std::string::pointer    SS_PTRTYPE;
typedef std::wstring::size_type    SW_SIZETYPE;
typedef std::wstring::pointer    SW_PTRTYPE;


template <typename T>
inline void  ssasn(std::basic_string<T>& sDst, const std::basic_string<T>& sSrc)
{
  if ( sDst.c_str() != sSrc.c_str() )
  {
    sDst.erase();
    sDst.assign(SSREF(sSrc));
  }
}
template <typename T>
inline void  ssasn(std::basic_string<T>& sDst, const T *pA)
{
  // Watch out for NULLs, as always.

  if ( 0 == pA )
  {
    sDst.erase();
  }

  // If pA actually points to part of sDst, we must NOT erase(), but
  // rather take a substring

  else if ( pA >= sDst.c_str() && pA <= sDst.c_str() + sDst.size() )
  {
    sDst =sDst.substr(static_cast<typename std::basic_string<T>::size_type>(pA-sDst.c_str()));
  }

  // Otherwise (most cases) apply the assignment bug fix, if applicable
  // and do the assignment

  else
  {
    Q172398(sDst);
    sDst.assign(pA);
  }
}
inline void  ssasn(std::string& sDst, const std::wstring& sSrc)
{
  if ( sSrc.empty() )
  {
    sDst.erase();
  }
  else
  {
    int nDst  = static_cast<int>(sSrc.size());

    // In MBCS builds, pad the buffer to account for the possibility of
    // some 3 byte characters.  Not perfect but should get most cases.

#ifdef SS_MBCS
    // In MBCS builds, we don't know how long the destination string will be.
    nDst  = static_cast<int>(static_cast<double>(nDst) * 1.3);
    sDst.resize(nDst+1);
    PCSTR szCvt = StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()), nDst,
      sSrc.c_str(), static_cast<int>(sSrc.size()));
    sDst.resize(sslen(szCvt));
#else
    sDst.resize(nDst+1);
    StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()), nDst,
      sSrc.c_str(), static_cast<int>(sSrc.size()));
    sDst.resize(sSrc.size());
#endif
  }
}
inline void  ssasn(std::string& sDst, PCWSTR pW)
{
  int nSrc  = sslen(pW);
  if ( nSrc > 0 )
  {
    int nSrc  = sslen(pW);
    int nDst  = nSrc;

    // In MBCS builds, pad the buffer to account for the possibility of
    // some 3 byte characters.  Not perfect but should get most cases.

#ifdef SS_MBCS
    nDst  = static_cast<int>(static_cast<double>(nDst) * 1.3);
    // In MBCS builds, we don't know how long the destination string will be.
    sDst.resize(nDst + 1);
    PCSTR szCvt = StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()), nDst,
      pW, nSrc);
    sDst.resize(sslen(szCvt));
#else
    sDst.resize(nDst + 1);
    StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()), nDst, pW, nSrc);
    sDst.resize(nDst);
#endif
  }
  else
  {
    sDst.erase();
  }
}
inline void ssasn(std::string& sDst, const int nNull)
{
  //UNUSED(nNull);
  ASSERT(nNull==0);
  sDst.assign("");
}
#undef StrSizeType
inline void  ssasn(std::wstring& sDst, const std::string& sSrc)
{
  if ( sSrc.empty() )
  {
    sDst.erase();
  }
  else
  {
    int nSrc  = static_cast<int>(sSrc.size());
    int nDst  = nSrc;

    sDst.resize(nSrc+1);
    PCWSTR szCvt = StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()), nDst,
      sSrc.c_str(), nSrc);

    sDst.resize(sslen(szCvt));
  }
}
inline void  ssasn(std::wstring& sDst, PCSTR pA)
{
  int nSrc  = sslen(pA);

  if ( 0 == nSrc )
  {
    sDst.erase();
  }
  else
  {
    int nDst  = nSrc;
    sDst.resize(nDst+1);
    PCWSTR szCvt = StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()), nDst, pA,
      nSrc);

    sDst.resize(sslen(szCvt));
  }
}
inline void ssasn(std::wstring& sDst, const int nNull)
{
  //UNUSED(nNull);
  ASSERT(nNull==0);
  sDst.assign(L"");
}

// -----------------------------------------------------------------------------
// ssadd: string object concatenation -- add second argument to first
// -----------------------------------------------------------------------------
inline void  ssadd(std::string& sDst, const std::wstring& sSrc)
{
  int nSrc  = static_cast<int>(sSrc.size());

  if ( nSrc > 0 )
  {
    int nDst  = static_cast<int>(sDst.size());
    int nAdd  = nSrc;

    // In MBCS builds, pad the buffer to account for the possibility of
    // some 3 byte characters.  Not perfect but should get most cases.

#ifdef SS_MBCS
    nAdd    = static_cast<int>(static_cast<double>(nAdd) * 1.3);
    sDst.resize(nDst+nAdd+1);
    PCSTR szCvt = StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()+nDst),
      nAdd, sSrc.c_str(), nSrc);
    sDst.resize(nDst + sslen(szCvt));
#else
    sDst.resize(nDst+nAdd+1);
    StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()+nDst), nAdd, sSrc.c_str(), nSrc);
    sDst.resize(nDst + nAdd);
#endif
  }
}
template <typename T>
inline void  ssadd(typename std::basic_string<T>& sDst, const typename std::basic_string<T>& sSrc)
{
  sDst += sSrc;
}
inline void  ssadd(std::string& sDst, PCWSTR pW)
{
  int nSrc    = sslen(pW);
  if ( nSrc > 0 )
  {
    int nDst  = static_cast<int>(sDst.size());
    int nAdd  = nSrc;

#ifdef SS_MBCS
    nAdd  = static_cast<int>(static_cast<double>(nAdd) * 1.3);
    sDst.resize(nDst + nAdd + 1);
    PCSTR szCvt = StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()+nDst),
      nAdd, pW, nSrc);
    sDst.resize(nDst + sslen(szCvt));
#else
    sDst.resize(nDst + nAdd + 1);
    StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()+nDst), nAdd, pW, nSrc);
    sDst.resize(nDst + nSrc);
#endif
  }
}
template <typename T>
inline void  ssadd(typename std::basic_string<T>& sDst, const T *pA)
{
  if ( pA )
  {
    // If the string being added is our internal string or a part of our
    // internal string, then we must NOT do any reallocation without
    // first copying that string to another object (since we're using a
    // direct pointer)

    if ( pA >= sDst.c_str() && pA <= sDst.c_str()+sDst.length())
    {
      if ( sDst.capacity() <= sDst.size()+sslen(pA) )
        sDst.append(std::basic_string<T>(pA));
      else
        sDst.append(pA);
    }
    else
    {
      sDst.append(pA);
    }
  }
}
inline void  ssadd(std::wstring& sDst, const std::string& sSrc)
{
  if ( !sSrc.empty() )
  {
    int nSrc  = static_cast<int>(sSrc.size());
    int nDst  = static_cast<int>(sDst.size());

    sDst.resize(nDst + nSrc + 1);
#ifdef SS_MBCS
    PCWSTR szCvt = StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()+nDst),
      nSrc, sSrc.c_str(), nSrc+1);
    sDst.resize(nDst + sslen(szCvt));
#else
    StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()+nDst), nSrc, sSrc.c_str(), nSrc+1);
    sDst.resize(nDst + nSrc);
#endif
  }
}
inline void  ssadd(std::wstring& sDst, PCSTR pA)
{
  int nSrc    = sslen(pA);

  if ( nSrc > 0 )
  {
    int nDst  = static_cast<int>(sDst.size());

    sDst.resize(nDst + nSrc + 1);
#ifdef SS_MBCS
    PCWSTR szCvt = StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()+nDst),
      nSrc, pA, nSrc+1);
    sDst.resize(nDst + sslen(szCvt));
#else
    StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()+nDst), nSrc, pA, nSrc+1);
    sDst.resize(nDst + nSrc);
#endif
  }
}

// -----------------------------------------------------------------------------
// sscmp: comparison (case sensitive, not affected by locale)
// -----------------------------------------------------------------------------
template<typename CT>
inline int sscmp(const CT* pA1, const CT* pA2)
{
    CT f;
    CT l;

    do
    {
      f = *(pA1++);
      l = *(pA2++);
    } while ( (f) && (f == l) );

    return (int)(f - l);
}

// -----------------------------------------------------------------------------
// ssicmp: comparison (case INsensitive, not affected by locale)
// -----------------------------------------------------------------------------
template<typename CT>
inline int ssicmp(const CT* pA1, const CT* pA2)
{
  // Using the "C" locale = "not affected by locale"

  std::locale loc = std::locale::classic();
    const std::ctype<CT>& ct = SS_USE_FACET(loc, std::ctype<CT>);
    CT f;
    CT l;

    do
    {
      f = ct.tolower(*(pA1++));
      l = ct.tolower(*(pA2++));
    } while ( (f) && (f == l) );

    return (int)(f - l);
}

// -----------------------------------------------------------------------------
// ssupr/sslwr: Uppercase/Lowercase conversion functions
// -----------------------------------------------------------------------------

template<typename CT>
inline void sslwr(CT* pT, size_t nLen, const std::locale& loc=std::locale())
{
  SS_USE_FACET(loc, std::ctype<CT>).tolower(pT, pT+nLen);
}
template<typename CT>
inline void ssupr(CT* pT, size_t nLen, const std::locale& loc=std::locale())
{
  SS_USE_FACET(loc, std::ctype<CT>).toupper(pT, pT+nLen);
}

// -----------------------------------------------------------------------------
// vsprintf/vswprintf or _vsnprintf/_vsnwprintf equivalents.  In standard
// builds we can't use _vsnprintf/_vsnwsprintf because they're MS extensions.
//
// -----------------------------------------------------------------------------
// Borland's headers put some ANSI "C" functions in the 'std' namespace.
// Promote them to the global namespace so we can use them here.

#if defined(__BORLANDC__)
    using std::vsprintf;
    using std::vswprintf;
#endif

  // GNU is supposed to have vsnprintf and vsnwprintf.  But only the newer
  // distributions do.

#if defined(__GNUC__)

  inline int ssvsprintf(PSTR pA, size_t nCount, PCSTR pFmtA, va_list vl)
  {
    return vsnprintf(pA, nCount, pFmtA, vl);
  }
  inline int ssvsprintf(PWSTR pW, size_t nCount, PCWSTR pFmtW, va_list vl)
  {
    return vswprintf(pW, nCount, pFmtW, vl);
  }

  // Microsofties can use
#elif defined(_MSC_VER) && !defined(SS_ANSI)

  inline int  ssvsprintf(PSTR pA, size_t nCount, PCSTR pFmtA, va_list vl)
  {
    return _vsnprintf(pA, nCount, pFmtA, vl);
  }
  inline int  ssvsprintf(PWSTR pW, size_t nCount, PCWSTR pFmtW, va_list vl)
  {
    return _vsnwprintf(pW, nCount, pFmtW, vl);
  }

#elif defined (SS_DANGEROUS_FORMAT)  // ignore buffer size parameter if needed?

  inline int ssvsprintf(PSTR pA, size_t /*nCount*/, PCSTR pFmtA, va_list vl)
  {
    return vsprintf(pA, pFmtA, vl);
  }

  inline int ssvsprintf(PWSTR pW, size_t nCount, PCWSTR pFmtW, va_list vl)
  {
    // JMO: Some distributions of the "C" have a version of vswprintf that
        // takes 3 arguments (e.g. Microsoft, Borland, GNU).  Others have a
        // version which takes 4 arguments (an extra "count" argument in the
        // second position.  The best stab I can take at this so far is that if
        // you are NOT running with MS, Borland, or GNU, then I'll assume you
        // have the version that takes 4 arguments.
        //
        // I'm sure that these checks don't catch every platform correctly so if
        // you get compiler errors on one of the lines immediately below, it's
        // probably because your implemntation takes a different number of
        // arguments.  You can comment out the offending line (and use the
        // alternate version) or you can figure out what compiler flag to check
        // and add that preprocessor check in.  Regardless, if you get an error
        // on these lines, I'd sure like to hear from you about it.
        //
        // Thanks to Ronny Schulz for the SGI-specific checks here.

//  #if !defined(__MWERKS__) && !defined(__SUNPRO_CC_COMPAT) && !defined(__SUNPRO_CC)
    #if    !defined(_MSC_VER) \
        && !defined (__BORLANDC__) \
        && !defined(__GNUC__) \
        && !defined(__sgi)

        return vswprintf(pW, nCount, pFmtW, vl);

    // suddenly with the current SGI 7.3 compiler there is no such function as
    // vswprintf and the substitute needs explicit casts to compile

    #elif defined(__sgi)

        nCount;
        return vsprintf( (char *)pW, (char *)pFmtW, vl);

    #else

        nCount;
        return vswprintf(pW, pFmtW, vl);

    #endif

  }

#endif

  // GOT COMPILER PROBLEMS HERE?
  // ---------------------------
  // Does your compiler choke on one or more of the following 2 functions?  It
  // probably means that you don't have have either vsnprintf or vsnwprintf in
  // your version of the CRT.  This is understandable since neither is an ANSI
  // "C" function.  However it still leaves you in a dilemma.  In order to make
  // this code build, you're going to have to to use some non-length-checked
  // formatting functions that every CRT has:  vsprintf and vswprintf.
  //
  // This is very dangerous.  With the proper erroneous (or malicious) code, it
  // can lead to buffer overlows and crashing your PC.  Use at your own risk
  // In order to use them, just #define SS_DANGEROUS_FORMAT at the top of
  // this file.
  //
  // Even THEN you might not be all the way home due to some non-conforming
  // distributions.  More on this in the comments below.

  inline int  ssnprintf(PSTR pA, size_t nCount, PCSTR pFmtA, va_list vl)
  {
  #ifdef _MSC_VER
      return _vsnprintf(pA, nCount, pFmtA, vl);
  #else
      return vsnprintf(pA, nCount, pFmtA, vl);
  #endif
  }
  inline int  ssnprintf(PWSTR pW, size_t nCount, PCWSTR pFmtW, va_list vl)
  {
  #ifdef _MSC_VER
      return _vsnwprintf(pW, nCount, pFmtW, vl);
  #else
      return vswprintf(pW, nCount, pFmtW, vl);
  #endif
  }




// -----------------------------------------------------------------------------
// ssload: Type safe, overloaded ::LoadString wrappers
// There is no equivalent of these in non-Win32-specific builds.  However, I'm
// thinking that with the message facet, there might eventually be one
// -----------------------------------------------------------------------------
#if defined (SS_WIN32) && !defined(SS_ANSI)
  inline int ssload(HMODULE hInst, UINT uId, PSTR pBuf, int nMax)
  {
    return ::LoadStringA(hInst, uId, pBuf, nMax);
  }
  inline int ssload(HMODULE hInst, UINT uId, PWSTR pBuf, int nMax)
  {
    return ::LoadStringW(hInst, uId, pBuf, nMax);
  }
#if defined ( _MSC_VER ) && ( _MSC_VER >= 1500 )
  inline int ssload(HMODULE hInst, UINT uId, uint16_t *pBuf, int nMax)
  {
    return 0;
  }
  inline int ssload(HMODULE hInst, UINT uId, uint32_t *pBuf, int nMax)
  {
    return 0;
  }
#endif
#endif


// -----------------------------------------------------------------------------
// sscoll/ssicoll: Collation wrappers
//    Note -- with MSVC I have reversed the arguments order here because the
//    functions appear to return the opposite of what they should
// -----------------------------------------------------------------------------
#ifndef SS_NO_LOCALE
template <typename CT>
inline int sscoll(const CT* sz1, int nLen1, const CT* sz2, int nLen2)
{
  const std::collate<CT>& coll =
    SS_USE_FACET(std::locale(), std::collate<CT>);

  return coll.compare(sz2, sz2+nLen2, sz1, sz1+nLen1);
}
template <typename CT>
inline int ssicoll(const CT* sz1, int nLen1, const CT* sz2, int nLen2)
{
  const std::locale loc;
  const std::collate<CT>& coll = SS_USE_FACET(loc, std::collate<CT>);

  // Some implementations seem to have trouble using the collate<>
  // facet typedefs so we'll just default to basic_string and hope
  // that's what the collate facet uses (which it generally should)

//  std::collate<CT>::string_type s1(sz1);
//  std::collate<CT>::string_type s2(sz2);
  const std::basic_string<CT> sEmpty;
    std::basic_string<CT> s1(sz1 ? sz1 : sEmpty.c_str());
    std::basic_string<CT> s2(sz2 ? sz2 : sEmpty.c_str());

  sslwr(const_cast<CT*>(s1.c_str()), nLen1, loc);
  sslwr(const_cast<CT*>(s2.c_str()), nLen2, loc);
  return coll.compare(s2.c_str(), s2.c_str()+nLen2,
            s1.c_str(), s1.c_str()+nLen1);
}
#endif


// -----------------------------------------------------------------------------
// ssfmtmsg: FormatMessage equivalents.  Needed because I added a CString facade
// Again -- no equivalent of these on non-Win32 builds but their might one day
// be one if the message facet gets implemented
// -----------------------------------------------------------------------------
#if defined (SS_WIN32) && !defined(SS_ANSI)
  inline DWORD ssfmtmsg(DWORD dwFlags, LPCVOID pSrc, DWORD dwMsgId,
              DWORD dwLangId, PSTR pBuf, DWORD nSize,
              va_list* vlArgs)
  {
    return FormatMessageA(dwFlags, pSrc, dwMsgId, dwLangId,
                pBuf, nSize,vlArgs);
  }
  inline DWORD ssfmtmsg(DWORD dwFlags, LPCVOID pSrc, DWORD dwMsgId,
              DWORD dwLangId, PWSTR pBuf, DWORD nSize,
              va_list* vlArgs)
  {
    return FormatMessageW(dwFlags, pSrc, dwMsgId, dwLangId,
                pBuf, nSize,vlArgs);
  }
#else
#endif



// FUNCTION: sscpy.  Copies up to 'nMax' characters from pSrc to pDst.
// -----------------------------------------------------------------------------
// FUNCTION:  sscpy
//    inline int sscpy(PSTR pDst, PCSTR pSrc, int nMax=-1);
//    inline int sscpy(PUSTR pDst,  PCSTR pSrc, int nMax=-1)
//    inline int sscpy(PSTR pDst, PCWSTR pSrc, int nMax=-1);
//    inline int sscpy(PWSTR pDst, PCWSTR pSrc, int nMax=-1);
//    inline int sscpy(PWSTR pDst, PCSTR pSrc, int nMax=-1);
//
// DESCRIPTION:
//    This function is very much (but not exactly) like strcpy.  These
//    overloads simplify copying one C-style string into another by allowing
//    the caller to specify two different types of strings if necessary.
//
//    The strings must NOT overlap
//
//    "Character" is expressed in terms of the destination string, not
//    the source.  If no 'nMax' argument is supplied, then the number of
//    characters copied will be sslen(pSrc).  A NULL terminator will
//    also be added so pDst must actually be big enough to hold nMax+1
//    characters.  The return value is the number of characters copied,
//    not including the NULL terminator.
//
// PARAMETERS:
//    pSrc - the string to be copied FROM.  May be a char based string, an
//         MBCS string (in Win32 builds) or a wide string (wchar_t).
//    pSrc - the string to be copied TO.  Also may be either MBCS or wide
//    nMax - the maximum number of characters to be copied into szDest.  Note
//         that this is expressed in whatever a "character" means to pDst.
//         If pDst is a wchar_t type string than this will be the maximum
//         number of wchar_ts that my be copied.  The pDst string must be
//         large enough to hold least nMaxChars+1 characters.
//         If the caller supplies no argument for nMax this is a signal to
//         the routine to copy all the characters in pSrc, regardless of
//         how long it is.
//
// RETURN VALUE: none
// -----------------------------------------------------------------------------

template<typename CT1, typename CT2>
inline int sscpycvt(CT1* pDst, const CT2* pSrc, int nMax)
{
  // Note -- we assume pDst is big enough to hold pSrc.  If not, we're in
  // big trouble.  No bounds checking.  Caveat emptor.

  int nSrc = sslen(pSrc);

  const CT1* szCvt = StdCodeCvt(pDst, nMax, pSrc, nSrc);

  // If we're copying the same size characters, then all the "code convert"
  // just did was basically memcpy so the #of characters copied is the same
  // as the number requested.  I should probably specialize this function
  // template to achieve this purpose as it is silly to do a runtime check
  // of a fact known at compile time.  I'll get around to it.

  return sslen(szCvt);
}

template<typename T>
inline int sscpycvt(T* pDst, const T* pSrc, int nMax)
{
  int nCount = nMax;
  for (; nCount > 0 && *pSrc; ++pSrc, ++pDst, --nCount)
    std::basic_string<T>::traits_type::assign(*pDst, *pSrc);

  *pDst = 0;
  return nMax - nCount;
}

inline int sscpycvt(PWSTR pDst, PCSTR pSrc, int nMax)
{
  // Note -- we assume pDst is big enough to hold pSrc.  If not, we're in
  // big trouble.  No bounds checking.  Caveat emptor.

  const PWSTR szCvt = StdCodeCvt(pDst, nMax, pSrc, nMax);
  return sslen(szCvt);
}

template<typename CT1, typename CT2>
inline int sscpy(CT1* pDst, const CT2* pSrc, int nMax, int nLen)
{
  return sscpycvt(pDst, pSrc, SSMIN(nMax, nLen));
}
template<typename CT1, typename CT2>
inline int sscpy(CT1* pDst, const CT2* pSrc, int nMax)
{
  return sscpycvt(pDst, pSrc, SSMIN(nMax, sslen(pSrc)));
}
template<typename CT1, typename CT2>
inline int sscpy(CT1* pDst, const CT2* pSrc)
{
  return sscpycvt(pDst, pSrc, sslen(pSrc));
}
template<typename CT1, typename CT2>
inline int sscpy(CT1* pDst, const std::basic_string<CT2>& sSrc, int nMax)
{
  return sscpycvt(pDst, sSrc.c_str(), SSMIN(nMax, (int)sSrc.length()));
}
template<typename CT1, typename CT2>
inline int sscpy(CT1* pDst, const std::basic_string<CT2>& sSrc)
{
  return sscpycvt(pDst, sSrc.c_str(), (int)sSrc.length());
}

#ifdef SS_INC_COMDEF
  template<typename CT1>
  inline int sscpy(CT1* pDst, const _bstr_t& bs, int nMax)
  {
    return sscpycvt(pDst, static_cast<PCOLESTR>(bs),
            SSMIN(nMax, static_cast<int>(bs.length())));
  }
  template<typename CT1>
  inline int sscpy(CT1* pDst, const _bstr_t& bs)
  {
    return sscpy(pDst, bs, static_cast<int>(bs.length()));
  }
#endif


// -----------------------------------------------------------------------------
// Functional objects for changing case.  They also let you pass locales
// -----------------------------------------------------------------------------

#ifdef SS_NO_LOCALE
  template<typename CT>
  struct SSToUpper : public std::unary_function<CT, CT>
  {
    inline CT operator()(const CT& t) const
    {
      return sstoupper(t);
    }
  };
  template<typename CT>
  struct SSToLower : public std::unary_function<CT, CT>
  {
    inline CT operator()(const CT& t) const
    {
      return sstolower(t);
    }
  };
#else
  template<typename CT>
  struct SSToUpper : public std::binary_function<CT, std::locale, CT>
  {
    inline CT operator()(const CT& t, const std::locale& loc) const
    {
      return sstoupper<CT>(t, loc);
    }
  };
  template<typename CT>
  struct SSToLower : public std::binary_function<CT, std::locale, CT>
  {
    inline CT operator()(const CT& t, const std::locale& loc) const
    {
      return sstolower<CT>(t, loc);
    }
  };
#endif

// This struct is used for TrimRight() and TrimLeft() function implementations.
//template<typename CT>
//struct NotSpace : public std::unary_function<CT, bool>
//{
//  const std::locale& loc;
//  inline NotSpace(const std::locale& locArg) : loc(locArg) {}
//  inline bool operator() (CT t) { return !std::isspace(t, loc); }
//};
template<typename CT>
struct NotSpace : public std::unary_function<CT, bool>
{
  // DINKUMWARE BUG:
  // Note -- using std::isspace in a COM DLL gives us access violations
  // because it causes the dynamic addition of a function to be called
  // when the library shuts down.  Unfortunately the list is maintained
  // in DLL memory but the function is in static memory.  So the COM DLL
  // goes away along with the function that was supposed to be called,
  // and then later when the DLL CRT shuts down it unloads the list and
  // tries to call the long-gone function.
  // This is DinkumWare's implementation problem.  If you encounter this
  // problem, you may replace the calls here with good old isspace() and
  // iswspace() from the CRT unless they specify SS_ANSI

#ifdef SS_NO_LOCALE

  bool operator() (CT t) const { return !ssisspace(t); }

#else
  const std::locale loc;
  NotSpace(const std::locale& locArg=std::locale()) : loc(locArg) {}
  bool operator() (CT t) const { return !std::isspace(t, loc); }
#endif
};




//      Now we can define the template (finally!)
// =============================================================================
// TEMPLATE: CStdStr
//    template<typename CT> class CStdStr : public std::basic_string<CT>
//
// REMARKS:
//    This template derives from basic_string<CT> and adds some MFC CString-
//    like functionality
//
//    Basically, this is my attempt to make Standard C++ library strings as
//    easy to use as the MFC CString class.
//
//    Note that although this is a template, it makes the assumption that the
//    template argument (CT, the character type) is either char or wchar_t.
// =============================================================================

//#define CStdStr _SS  // avoid compiler warning 4786

//    template<typename ARG> ARG& FmtArg(ARG& arg)  { return arg; }
//    PCSTR  FmtArg(const std::string& arg)  { return arg.c_str(); }
//    PCWSTR FmtArg(const std::wstring& arg) { return arg.c_str(); }

template<typename ARG>
struct FmtArg
{
    explicit FmtArg(const ARG& arg) : a_(arg) {}
    const ARG& operator()() const { return a_; }
    const ARG& a_;
private:
    FmtArg& operator=(const FmtArg&) { return *this; }
};

template<typename CT>
class CStdStr : public std::basic_string<CT>
{
  // Typedefs for shorter names.  Using these names also appears to help
  // us avoid some ambiguities that otherwise arise on some platforms

  #define MYBASE std::basic_string<CT>         // my base class
  //typedef typename std::basic_string<CT>    MYBASE;   // my base class
  typedef CStdStr<CT>              MYTYPE;   // myself
  typedef typename MYBASE::const_pointer    PCMYSTR; // PCSTR or PCWSTR
  typedef typename MYBASE::pointer      PMYSTR;   // PSTR or PWSTR
  typedef typename MYBASE::iterator      MYITER;  // my iterator type
  typedef typename MYBASE::const_iterator    MYCITER; // you get the idea...
  typedef typename MYBASE::reverse_iterator  MYRITER;
  typedef typename MYBASE::size_type      MYSIZE;
  typedef typename MYBASE::value_type      MYVAL;
  typedef typename MYBASE::allocator_type    MYALLOC;

public:
  // shorthand conversion from PCTSTR to string resource ID
  #define SSRES(pctstr)  LOWORD(reinterpret_cast<unsigned long>(pctstr))

  bool TryLoad(const void* pT)
  {
    bool bLoaded = false;

#if defined(SS_WIN32) && !defined(SS_ANSI)
    if ( ( pT != NULL ) && SS_IS_INTRESOURCE(pT) )
    {
      UINT nId = LOWORD(reinterpret_cast<unsigned long>(pT));
      if ( !LoadString(nId) )
      {
        TRACE(_T("Can't load string %u\n"), SSRES(pT));
      }
      bLoaded = true;
    }
#endif

    return bLoaded;
  }


  // CStdStr inline constructors
  CStdStr()
  {
  }

  CStdStr(const MYTYPE& str) : MYBASE(SSREF(str))
  {
  }

  CStdStr(const std::string& str)
  {
    ssasn(*this, SSREF(str));
  }

  CStdStr(const std::wstring& str)
  {
    ssasn(*this, SSREF(str));
  }

  CStdStr(PCMYSTR pT, MYSIZE n) : MYBASE(pT, n)
  {
  }

#ifdef SS_UNSIGNED
  CStdStr(PCUSTR pU)
  {
    *this = reinterpret_cast<PCSTR>(pU);
  }
#endif

  CStdStr(PCSTR pA)
  {
  #ifdef SS_ANSI
    *this = pA;
  #else
    if ( !TryLoad(pA) )
      *this = pA;
  #endif
  }

  CStdStr(PCWSTR pW)
  {
  #ifdef SS_ANSI
    *this = pW;
  #else
    if ( !TryLoad(pW) )
      *this = pW;
  #endif
  }

  CStdStr(uint16_t* pW)
  {
  #ifdef SS_ANSI
    *this = pW;
  #else
    if ( !TryLoad(pW) )
      *this = pW;
  #endif
  }

  CStdStr(uint32_t* pW)
  {
  #ifdef SS_ANSI
    *this = pW;
  #else
    if ( !TryLoad(pW) )
      *this = pW;
  #endif
  }

  CStdStr(MYCITER first, MYCITER last)
    : MYBASE(first, last)
  {
  }

  CStdStr(MYSIZE nSize, MYVAL ch, const MYALLOC& al=MYALLOC())
    : MYBASE(nSize, ch, al)
  {
  }

  #ifdef SS_INC_COMDEF
    CStdStr(const _bstr_t& bstr)
    {
      if ( bstr.length() > 0 )
        this->append(static_cast<PCMYSTR>(bstr), bstr.length());
    }
  #endif

  // CStdStr inline assignment operators -- the ssasn function now takes care
  // of fixing  the MSVC assignment bug (see knowledge base article Q172398).
  MYTYPE& operator=(const MYTYPE& str)
  {
    ssasn(*this, str);
    return *this;
  }

  MYTYPE& operator=(const std::string& str)
  {
    ssasn(*this, str);
    return *this;
  }

  MYTYPE& operator=(const std::wstring& str)
  {
    ssasn(*this, str);
    return *this;
  }

  MYTYPE& operator=(PCSTR pA)
  {
    ssasn(*this, pA);
    return *this;
  }

  MYTYPE& operator=(PCWSTR pW)
  {
    ssasn(*this, pW);
    return *this;
  }

#ifdef SS_UNSIGNED
  MYTYPE& operator=(PCUSTR pU)
  {
    ssasn(*this, reinterpret_cast<PCSTR>(pU));
    return *this;
  }
#endif

  MYTYPE& operator=(uint16_t* pA)
  {
    ssasn(*this, pA);
    return *this;
  }

  MYTYPE& operator=(uint32_t* pA)
  {
    ssasn(*this, pA);
    return *this;
  }

  MYTYPE& operator=(CT t)
  {
    Q172398(*this);
    this->assign(1, t);
    return *this;
  }

  #ifdef SS_INC_COMDEF
    MYTYPE& operator=(const _bstr_t& bstr)
    {
      if ( bstr.length() > 0 )
      {
        this->assign(static_cast<PCMYSTR>(bstr), bstr.length());
        return *this;
      }
      else
      {
        this->erase();
        return *this;
      }
    }
  #endif


  // Overloads  also needed to fix the MSVC assignment bug (KB: Q172398)
  //  *** Thanks to Pete The Plumber for catching this one ***
  // They also are compiled if you have explicitly turned off refcounting
  #if ( defined(_MSC_VER) && ( _MSC_VER < 1200 ) ) || defined(SS_NO_REFCOUNT)

    MYTYPE& assign(const MYTYPE& str)
    {
      Q172398(*this);
      sscpy(GetBuffer(str.size()+1), SSREF(str));
      this->ReleaseBuffer(str.size());
      return *this;
    }

    MYTYPE& assign(const MYTYPE& str, MYSIZE nStart, MYSIZE nChars)
    {
      // This overload of basic_string::assign is supposed to assign up to
      // <nChars> or the NULL terminator, whichever comes first.  Since we
      // are about to call a less forgiving overload (in which <nChars>
      // must be a valid length), we must adjust the length here to a safe
      // value.  Thanks to Ullrich Poll�hne for catching this bug

      nChars    = SSMIN(nChars, str.length() - nStart);
      MYTYPE strTemp(str.c_str()+nStart, nChars);
      Q172398(*this);
      this->assign(strTemp);
      return *this;
    }

    MYTYPE& assign(const MYBASE& str)
    {
      ssasn(*this, str);
      return *this;
    }

    MYTYPE& assign(const MYBASE& str, MYSIZE nStart, MYSIZE nChars)
    {
      // This overload of basic_string::assign is supposed to assign up to
      // <nChars> or the NULL terminator, whichever comes first.  Since we
      // are about to call a less forgiving overload (in which <nChars>
      // must be a valid length), we must adjust the length here to a safe
      // value. Thanks to Ullrich Poll�hne for catching this bug

      nChars    = SSMIN(nChars, str.length() - nStart);

      // Watch out for assignment to self

      if ( this == &str )
      {
        MYTYPE strTemp(str.c_str() + nStart, nChars);
        static_cast<MYBASE*>(this)->assign(strTemp);
      }
      else
      {
        Q172398(*this);
        static_cast<MYBASE*>(this)->assign(str.c_str()+nStart, nChars);
      }
      return *this;
    }

    MYTYPE& assign(const CT* pC, MYSIZE nChars)
    {
      // Q172398 only fix -- erase before assigning, but not if we're
      // assigning from our own buffer

  #if defined ( _MSC_VER ) && ( _MSC_VER < 1200 )
      if ( !this->empty() &&
        ( pC < this->data() || pC > this->data() + this->capacity() ) )
      {
        this->erase();
      }
  #endif
      Q172398(*this);
      static_cast<MYBASE*>(this)->assign(pC, nChars);
      return *this;
    }

    MYTYPE& assign(MYSIZE nChars, MYVAL val)
    {
      Q172398(*this);
      static_cast<MYBASE*>(this)->assign(nChars, val);
      return *this;
    }

    MYTYPE& assign(const CT* pT)
    {
      return this->assign(pT, MYBASE::traits_type::length(pT));
    }

    MYTYPE& assign(MYCITER iterFirst, MYCITER iterLast)
    {
  #if defined ( _MSC_VER ) && ( _MSC_VER < 1200 )
      // Q172398 fix.  don't call erase() if we're assigning from ourself
      if ( iterFirst < this->begin() ||
                 iterFirst > this->begin() + this->size() )
            {
        this->erase()
            }
  #endif
      this->replace(this->begin(), this->end(), iterFirst, iterLast);
      return *this;
    }
  #endif


  // -------------------------------------------------------------------------
  // CStdStr inline concatenation.
  // -------------------------------------------------------------------------
  MYTYPE& operator+=(const MYTYPE& str)
  {
    ssadd(*this, str);
    return *this;
  }

  MYTYPE& operator+=(const std::string& str)
  {
    ssadd(*this, str);
    return *this;
  }

  MYTYPE& operator+=(const std::wstring& str)
  {
    ssadd(*this, str);
    return *this;
  }

  MYTYPE& operator+=(PCSTR pA)
  {
    ssadd(*this, pA);
    return *this;
  }

  MYTYPE& operator+=(PCWSTR pW)
  {
    ssadd(*this, pW);
    return *this;
  }

  MYTYPE& operator+=(uint16_t* pW)
  {
    ssadd(*this, pW);
    return *this;
  }

  MYTYPE& operator+=(uint32_t* pW)
  {
    ssadd(*this, pW);
    return *this;
  }

  MYTYPE& operator+=(CT t)
  {
    this->append(1, t);
    return *this;
  }
  #ifdef SS_INC_COMDEF  // if we have _bstr_t, define a += for it too.
    MYTYPE& operator+=(const _bstr_t& bstr)
    {
      return this->operator+=(static_cast<PCMYSTR>(bstr));
    }
  #endif


  // -------------------------------------------------------------------------
  // Case changing functions
  // -------------------------------------------------------------------------

    MYTYPE& ToUpper(const std::locale& loc=std::locale())
  {
    // Note -- if there are any MBCS character sets in which the lowercase
    // form a character takes up a different number of bytes than the
    // uppercase form, this would probably not work...

    std::transform(this->begin(),
             this->end(),
             this->begin(),
#ifdef SS_NO_LOCALE
             SSToUpper<CT>());
#else
             std::bind2nd(SSToUpper<CT>(), loc));
#endif

    // ...but if it were, this would probably work better.  Also, this way
    // seems to be a bit faster when anything other then the "C" locale is
    // used...

//    if ( !empty() )
//    {
//      ssupr(this->GetBuf(), this->size(), loc);
//      this->RelBuf();
//    }

    return *this;
  }

  MYTYPE& ToLower(const std::locale& loc=std::locale())
  {
    // Note -- if there are any MBCS character sets in which the lowercase
    // form a character takes up a different number of bytes than the
    // uppercase form, this would probably not work...

    std::transform(this->begin(),
             this->end(),
             this->begin(),
#ifdef SS_NO_LOCALE
             SSToLower<CT>());
#else
             std::bind2nd(SSToLower<CT>(), loc));
#endif

    // ...but if it were, this would probably work better.  Also, this way
    // seems to be a bit faster when anything other then the "C" locale is
    // used...

//    if ( !empty() )
//    {
//      sslwr(this->GetBuf(), this->size(), loc);
//      this->RelBuf();
//    }
    return *this;
  }


  MYTYPE& Normalize()
  {
    return Trim().ToLower();
  }


  // -------------------------------------------------------------------------
  // CStdStr -- Direct access to character buffer.  In the MS' implementation,
  // the at() function that we use here also calls _Freeze() providing us some
  // protection from multithreading problems associated with ref-counting.
    // In VC 7 and later, of course, the ref-counting stuff is gone.
  // -------------------------------------------------------------------------

  CT* GetBuf(int nMinLen=-1)
  {
    if ( static_cast<int>(this->size()) < nMinLen )
      this->resize(static_cast<MYSIZE>(nMinLen));

    return this->empty() ? const_cast<CT*>(this->data()) : &(this->at(0));
  }

  CT* SetBuf(int nLen)
  {
    nLen = ( nLen > 0 ? nLen : 0 );
    if ( this->capacity() < 1 && nLen == 0 )
      this->resize(1);

    this->resize(static_cast<MYSIZE>(nLen));
    return const_cast<CT*>(this->data());
  }
  void RelBuf(int nNewLen=-1)
  {
    this->resize(static_cast<MYSIZE>(nNewLen > -1 ? nNewLen :
                                                        sslen(this->c_str())));
  }

  void BufferRel()     { RelBuf(); }      // backwards compatability
  CT*  Buffer()       { return GetBuf(); }  // backwards compatability
  CT*  BufferSet(int nLen) { return SetBuf(nLen);}// backwards compatability

  bool Equals(const CT* pT, bool bUseCase=false) const
  {
    return  0 == (bUseCase ? this->compare(pT) : ssicmp(this->c_str(), pT));
  }

  // -------------------------------------------------------------------------
  // FUNCTION:  CStdStr::Load
  // REMARKS:
  //    Loads string from resource specified by nID
  //
  // PARAMETERS:
  //    nID - resource Identifier.  Purely a Win32 thing in this case
  //
  // RETURN VALUE:
  //    true if successful, false otherwise
  // -------------------------------------------------------------------------

#ifndef SS_ANSI

  bool Load(UINT nId, HMODULE hModule=NULL)
  {
    bool bLoaded    = false;  // set to true of we succeed.

  #ifdef _MFC_VER    // When in Rome (or MFC land)...

    // If they gave a resource handle, use it.  Note - this is archaic
    // and not really what I would recommend.  But then again, in MFC
    // land, you ought to be using CString for resources anyway since
    // it walks the resource chain for you.

    HMODULE hModuleOld = NULL;

    if ( NULL != hModule )
    {
      hModuleOld = AfxGetResourceHandle();
      AfxSetResourceHandle(hModule);
    }

    // ...load the string

    CString strRes;
    bLoaded        = FALSE != strRes.LoadString(nId);

    // ...and if we set the resource handle, restore it.

    if ( NULL != hModuleOld )
      AfxSetResourceHandle(hModule);

    if ( bLoaded )
      *this      = strRes;

  #else // otherwise make our own hackneyed version of CString's Load

    // Get the resource name and module handle

    if ( NULL == hModule )
      hModule      = GetResourceHandle();

    PCTSTR szName    = MAKEINTRESOURCE((nId>>4)+1); // lifted
    DWORD dwSize    = 0;

    // No sense continuing if we can't find the resource

    HRSRC hrsrc      = ::FindResource(hModule, szName, RT_STRING);

    if ( NULL == hrsrc )
    {
      TRACE(_T("Cannot find resource %d: 0x%X"), nId, ::GetLastError());
    }
    else if ( 0 == (dwSize = ::SizeofResource(hModule, hrsrc) / sizeof(CT)))
    {
      TRACE(_T("Cant get size of resource %d 0x%X\n"),nId,GetLastError());
    }
    else
    {
      bLoaded      = 0 != ssload(hModule, nId, GetBuf(dwSize), dwSize);
      ReleaseBuffer();
    }

  #endif  // #ifdef _MFC_VER

    if ( !bLoaded )
      TRACE(_T("String not loaded 0x%X\n"), ::GetLastError());

    return bLoaded;
  }

#endif  // #ifdef SS_ANSI

  // -------------------------------------------------------------------------
  // FUNCTION:  CStdStr::Format
  //    void _cdecl Formst(CStdStringA& PCSTR szFormat, ...)
  //    void _cdecl Format(PCSTR szFormat);
  //
  // DESCRIPTION:
  //    This function does sprintf/wsprintf style formatting on CStdStringA
  //    objects.  It looks a lot like MFC's CString::Format.  Some people
  //    might even call this identical.  Fortunately, these people are now
  //    dead... heh heh.
  //
  // PARAMETERS:
  //    nId - ID of string resource holding the format string
  //    szFormat - a PCSTR holding the format specifiers
  //    argList - a va_list holding the arguments for the format specifiers.
  //
  // RETURN VALUE:  None.
  // -------------------------------------------------------------------------
  // formatting (using wsprintf style formatting)

    // If they want a Format() function that safely handles string objects
    // without casting

#ifdef SS_SAFE_FORMAT

    // Question:  Joe, you wacky coder you, why do you have so many overloads
    //      of the Format() function
    // Answer:  One reason only - CString compatability.  In short, by making
    //      the Format() function a template this way, I can do strong typing
    //      and allow people to pass CStdString arguments as fillers for
    //      "%s" format specifiers without crashing their program!  The downside
    //      is that I need to overload on the number of arguments.   If you are
    //      passing more arguments than I have listed below in any of my
    //      overloads, just add another one.
    //
    //      Yes, yes, this is really ugly.  In essence what I am doing here is
    //      protecting people from a bad (and incorrect) programming practice
    //      that they should not be doing anyway.  I am protecting them from
    //      themselves.  Why am I doing this?  Well, if you had any idea the
    //      number of times I've been emailed by people about this
    //      "incompatability" in my code, you wouldn't ask.

  void Fmt(const CT* szFmt, ...)
  {
    va_list argList;
    va_start(argList, szFmt);
    FormatV(szFmt, argList);
    va_end(argList);
  }

#ifndef SS_ANSI

    void Format(UINT nId)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
            this->swap(strFmt);
    }
    template<class A1>
    void Format(UINT nId, const A1& v)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
            Fmt(strFmt, FmtArg<A1>(v)());
    }
    template<class A1, class A2>
    void Format(UINT nId, const A1& v1, const A2& v2)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
           Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)());
    }
    template<class A1, class A2, class A3>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)());
        }
    }
    template<class A1, class A2, class A3, class A4>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(),FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(),FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
           Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
                FmtArg<A9>(v9)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
                FmtArg<A9>(v9)(), FmtArg<A10>(v10)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
                FmtArg<A9>(v9)(),FmtArg<A10>(v10)(),FmtArg<A11>(v11)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
                FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
                FmtArg<A12>(v12)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
                FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
                FmtArg<A12>(v12)(), FmtArg<A13>(v13)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13, class A14>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13, const A14& v14)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
                FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
                FmtArg<A12>(v12)(), FmtArg<A13>(v13)(),FmtArg<A14>(v14)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13, class A14, class A15>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13, const A14& v14, const A15& v15)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
                FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
                FmtArg<A12>(v12)(),FmtArg<A13>(v13)(),FmtArg<A14>(v14)(),
                FmtArg<A15>(v15)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13, class A14, class A15, class A16>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13, const A14& v14, const A15& v15,
                const A16& v16)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
                FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
                FmtArg<A12>(v12)(),FmtArg<A13>(v13)(),FmtArg<A14>(v14)(),
                FmtArg<A15>(v15)(), FmtArg<A16>(v16)());
        }
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13, class A14, class A15, class A16, class A17>
    void Format(UINT nId, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13, const A14& v14, const A15& v15,
                const A16& v16, const A17& v17)
    {
    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
        {
            Fmt(strFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
                FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
                FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
                FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
                FmtArg<A12>(v12)(),FmtArg<A13>(v13)(),FmtArg<A14>(v14)(),
                FmtArg<A15>(v15)(),FmtArg<A16>(v16)(),FmtArg<A17>(v17)());
        }
    }

#endif // #ifndef SS_ANSI

    // ...now the other overload of Format: the one that takes a string literal

    void Format(const CT* szFmt)
    {
        *this = szFmt;
    }
    template<class A1>
    void Format(const CT* szFmt, const A1& v)
    {
        Fmt(szFmt, FmtArg<A1>(v)());
    }
    template<class A1, class A2>
    void Format(const CT* szFmt, const A1& v1, const A2& v2)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)());
    }
    template<class A1, class A2, class A3>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)());
    }
    template<class A1, class A2, class A3, class A4>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)());
    }
    template<class A1, class A2, class A3, class A4, class A5>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
            FmtArg<A9>(v9)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
            FmtArg<A9>(v9)(), FmtArg<A10>(v10)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
            FmtArg<A9>(v9)(),FmtArg<A10>(v10)(),FmtArg<A11>(v11)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
            FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
            FmtArg<A12>(v12)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
            FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
            FmtArg<A12>(v12)(), FmtArg<A13>(v13)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13, class A14>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13, const A14& v14)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
            FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
            FmtArg<A12>(v12)(), FmtArg<A13>(v13)(),FmtArg<A14>(v14)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13, class A14, class A15>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13, const A14& v14, const A15& v15)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
            FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
            FmtArg<A12>(v12)(),FmtArg<A13>(v13)(),FmtArg<A14>(v14)(),
            FmtArg<A15>(v15)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13, class A14, class A15, class A16>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13, const A14& v14, const A15& v15,
                const A16& v16)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
            FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
            FmtArg<A12>(v12)(),FmtArg<A13>(v13)(),FmtArg<A14>(v14)(),
            FmtArg<A15>(v15)(), FmtArg<A16>(v16)());
    }
    template<class A1, class A2, class A3, class A4, class A5, class A6,
        class A7, class A8, class A9, class A10, class A11, class A12,
        class A13, class A14, class A15, class A16, class A17>
    void Format(const CT* szFmt, const A1& v1, const A2& v2, const A3& v3,
                const A4& v4, const A5& v5, const A6& v6, const A7& v7,
                const A8& v8, const A9& v9, const A10& v10, const A11& v11,
                const A12& v12, const A13& v13, const A14& v14, const A15& v15,
                const A16& v16, const A17& v17)
    {
        Fmt(szFmt, FmtArg<A1>(v1)(), FmtArg<A2>(v2)(),
            FmtArg<A3>(v3)(), FmtArg<A4>(v4)(), FmtArg<A5>(v5)(),
            FmtArg<A6>(v6)(), FmtArg<A7>(v7)(), FmtArg<A8>(v8)(),
            FmtArg<A9>(v9)(), FmtArg<A10>(v10)(),FmtArg<A11>(v11)(),
            FmtArg<A12>(v12)(),FmtArg<A13>(v13)(),FmtArg<A14>(v14)(),
            FmtArg<A15>(v15)(),FmtArg<A16>(v16)(),FmtArg<A17>(v17)());
    }

#else  // #ifdef SS_SAFE_FORMAT


#ifndef SS_ANSI

  void Format(UINT nId, ...)
  {
    va_list argList;
    va_start(argList, nId);

    MYTYPE strFmt;
    if ( strFmt.Load(nId) )
      FormatV(strFmt, argList);

    va_end(argList);
  }

#endif  // #ifdef SS_ANSI

  void Format(const CT* szFmt, ...)
  {
    va_list argList;
    va_start(argList, szFmt);
    FormatV(szFmt, argList);
    va_end(argList);
  }

#endif // #ifdef SS_SAFE_FORMAT

  void AppendFormat(const CT* szFmt, ...)
  {
    va_list argList;
    va_start(argList, szFmt);
    AppendFormatV(szFmt, argList);
    va_end(argList);
  }

  #define MAX_FMT_TRIES    5   // #of times we try
  #define FMT_BLOCK_SIZE    2048 // # of bytes to increment per try
  #define BUFSIZE_1ST  256
  #define BUFSIZE_2ND 512
  #define STD_BUF_SIZE    1024

  // an efficient way to add formatted characters to the string.  You may only
  // add up to STD_BUF_SIZE characters at a time, though
  void AppendFormatV(const CT* szFmt, va_list argList)
  {
    CT szBuf[STD_BUF_SIZE];
    int nLen = ssnprintf(szBuf, STD_BUF_SIZE-1, szFmt, argList);

    if ( 0 < nLen )
      this->append(szBuf, nLen);
  }

  // -------------------------------------------------------------------------
  // FUNCTION:  FormatV
  //    void FormatV(PCSTR szFormat, va_list, argList);
  //
  // DESCRIPTION:
  //    This function formats the string with sprintf style format-specs.
  //    It makes a general guess at required buffer size and then tries
  //    successively larger buffers until it finds one big enough or a
  //    threshold (MAX_FMT_TRIES) is exceeded.
  //
  // PARAMETERS:
  //    szFormat - a PCSTR holding the format of the output
  //    argList - a Microsoft specific va_list for variable argument lists
  //
  // RETURN VALUE:
  // -------------------------------------------------------------------------

  // NOTE: Changed by JM to actually function under non-win32,
  //       and to remove the upper limit on size.
  void FormatV(const CT* szFormat, va_list argList)
  {
    // try and grab a sufficient buffersize
    int nChars = FMT_BLOCK_SIZE;
    va_list argCopy;

    CT *p = reinterpret_cast<CT*>(malloc(sizeof(CT)*nChars));
    if (!p) return;

    while (1)
    {
      va_copy(argCopy, argList);

      int nActual = ssvsprintf(p, nChars, szFormat, argCopy);
      /* If that worked, return the string. */
      if (nActual > -1 && nActual < nChars)
      { /* make sure it's NULL terminated */
        p[nActual] = '\0';
        this->assign(p, nActual);
        free(p);
        va_end(argCopy);
        return;
      }
      /* Else try again with more space. */
      if (nActual > -1)        /* glibc 2.1 */
        nChars = nActual + 1;  /* precisely what is needed */
      else                     /* glibc 2.0 */
        nChars *= 2;           /* twice the old size */

      CT *np = reinterpret_cast<CT*>(realloc(p, sizeof(CT)*nChars));
      if (np == NULL)
      {
        free(p);
        va_end(argCopy);
        return;   // failed :(
      }
      p = np;
      va_end(argCopy);
    }
  }

  // -------------------------------------------------------------------------
  // CString Facade Functions:
  //
  // The following methods are intended to allow you to use this class as a
  // near drop-in replacement for CString.
  // -------------------------------------------------------------------------
  #ifdef SS_WIN32
    BSTR AllocSysString() const
    {
      ostring os;
      ssasn(os, *this);
      return ::SysAllocString(os.c_str());
    }
  #endif

#ifndef SS_NO_LOCALE
  int Collate(PCMYSTR szThat) const
  {
    return sscoll(this->c_str(), this->length(), szThat, sslen(szThat));
  }

  int CollateNoCase(PCMYSTR szThat) const
  {
    return ssicoll(this->c_str(), this->length(), szThat, sslen(szThat));
  }
#endif
  int Compare(PCMYSTR szThat) const
  {
    return this->compare(szThat);
  }

  int CompareNoCase(PCMYSTR szThat)  const
  {
    return ssicmp(this->c_str(), szThat);
  }

  int Delete(int nIdx, int nCount=1)
  {
        if ( nIdx < 0 )
      nIdx = 0;

    if ( nIdx < this->GetLength() )
      this->erase(static_cast<MYSIZE>(nIdx), static_cast<MYSIZE>(nCount));

    return GetLength();
  }

  void Empty()
  {
    this->erase();
  }

  int Find(CT ch) const
  {
    MYSIZE nIdx  = this->find_first_of(ch);
    return static_cast<int>(MYBASE::npos == nIdx  ? -1 : nIdx);
  }

  int Find(PCMYSTR szSub) const
  {
    MYSIZE nIdx  = this->find(szSub);
    return static_cast<int>(MYBASE::npos == nIdx ? -1 : nIdx);
  }

  int Find(CT ch, int nStart) const
  {
    // CString::Find docs say add 1 to nStart when it's not zero
    // CString::Find code doesn't do that however.  We'll stick
    // with what the code does

    MYSIZE nIdx  = this->find_first_of(ch, static_cast<MYSIZE>(nStart));
    return static_cast<int>(MYBASE::npos == nIdx ? -1 : nIdx);
  }

  int Find(PCMYSTR szSub, int nStart) const
  {
    // CString::Find docs say add 1 to nStart when it's not zero
    // CString::Find code doesn't do that however.  We'll stick
    // with what the code does

    MYSIZE nIdx  = this->find(szSub, static_cast<MYSIZE>(nStart));
    return static_cast<int>(MYBASE::npos == nIdx ? -1 : nIdx);
  }

  int FindOneOf(PCMYSTR szCharSet) const
  {
    MYSIZE nIdx = this->find_first_of(szCharSet);
    return static_cast<int>(MYBASE::npos == nIdx ? -1 : nIdx);
  }

#ifndef SS_ANSI
  void FormatMessage(PCMYSTR szFormat, ...) throw(std::exception)
  {
    va_list argList;
    va_start(argList, szFormat);
    PMYSTR szTemp;
    if ( ssfmtmsg(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
             szFormat, 0, 0,
             reinterpret_cast<PMYSTR>(&szTemp), 0, &argList) == 0 ||
       szTemp == 0 )
    {
      throw std::runtime_error("out of memory");
    }
    *this = szTemp;
    LocalFree(szTemp);
    va_end(argList);
  }

  void FormatMessage(UINT nFormatId, ...) throw(std::exception)
  {
    MYTYPE sFormat;
    VERIFY(sFormat.LoadString(nFormatId));
    va_list argList;
    va_start(argList, nFormatId);
    PMYSTR szTemp;
    if ( ssfmtmsg(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
             sFormat, 0, 0,
             reinterpret_cast<PMYSTR>(&szTemp), 0, &argList) == 0 ||
      szTemp == 0)
    {
      throw std::runtime_error("out of memory");
    }
    *this = szTemp;
    LocalFree(szTemp);
    va_end(argList);
  }
#endif

  // GetAllocLength -- an MSVC7 function but it costs us nothing to add it.

  int GetAllocLength()
  {
    return static_cast<int>(this->capacity());
  }

  // -------------------------------------------------------------------------
  // GetXXXX -- Direct access to character buffer
  // -------------------------------------------------------------------------
  CT GetAt(int nIdx) const
  {
    return this->at(static_cast<MYSIZE>(nIdx));
  }

  CT* GetBuffer(int nMinLen=-1)
  {
    return GetBuf(nMinLen);
  }

  CT* GetBufferSetLength(int nLen)
  {
    return BufferSet(nLen);
  }

  // GetLength() -- MFC docs say this is the # of BYTES but
  // in truth it is the number of CHARACTERs (chars or wchar_ts)
  int GetLength() const
  {
    return static_cast<int>(this->length());
  }

  int Insert(int nIdx, CT ch)
  {
    if ( static_cast<MYSIZE>(nIdx) > this->size()-1 )
      this->append(1, ch);
    else
      this->insert(static_cast<MYSIZE>(nIdx), 1, ch);

    return GetLength();
  }
  int Insert(int nIdx, PCMYSTR sz)
  {
    if ( static_cast<MYSIZE>(nIdx) >= this->size() )
      this->append(sz, static_cast<MYSIZE>(sslen(sz)));
    else
      this->insert(static_cast<MYSIZE>(nIdx), sz);

    return GetLength();
  }

  bool IsEmpty() const
  {
    return this->empty();
  }

  MYTYPE Left(int nCount) const
  {
        // Range check the count.

    nCount = SSMAX(0, SSMIN(nCount, static_cast<int>(this->size())));
    return this->substr(0, static_cast<MYSIZE>(nCount));
  }

#ifndef SS_ANSI
  bool LoadString(UINT nId)
  {
    return this->Load(nId);
  }
#endif

  void MakeLower()
  {
    ToLower();
  }

  void MakeReverse()
  {
    std::reverse(this->begin(), this->end());
  }

  void MakeUpper()
  {
    ToUpper();
  }

  MYTYPE Mid(int nFirst) const
  {
    return Mid(nFirst, this->GetLength()-nFirst);
  }

  MYTYPE Mid(int nFirst, int nCount) const
  {
    // CString does range checking here.  Since we're trying to emulate it,
    // we must check too.

    if ( nFirst < 0 )
      nFirst = 0;
    if ( nCount < 0 )
      nCount = 0;

    int nSize = static_cast<int>(this->size());

    if ( nFirst + nCount > nSize )
      nCount = nSize - nFirst;

    if ( nFirst > nSize )
      return MYTYPE();

    ASSERT(nFirst >= 0);
    ASSERT(nFirst + nCount <= nSize);

    return this->substr(static_cast<MYSIZE>(nFirst),
              static_cast<MYSIZE>(nCount));
  }

  void ReleaseBuffer(int nNewLen=-1)
  {
    RelBuf(nNewLen);
  }

  int Remove(CT ch)
  {
    MYSIZE nIdx    = 0;
    int nRemoved  = 0;
    while ( (nIdx=this->find_first_of(ch)) != MYBASE::npos )
    {
      this->erase(nIdx, 1);
      nRemoved++;
    }
    return nRemoved;
  }

  int Replace(CT chOld, CT chNew)
  {
    int nReplaced  = 0;

    for ( MYITER iter=this->begin(); iter != this->end(); iter++ )
    {
      if ( *iter == chOld )
      {
        *iter = chNew;
        nReplaced++;
      }
    }

    return nReplaced;
  }

  int Replace(PCMYSTR szOld, PCMYSTR szNew)
  {
    int nReplaced    = 0;
    MYSIZE nIdx      = 0;
    MYSIZE nOldLen    = sslen(szOld);

    if ( 0 != nOldLen )
    {
      // If the replacement string is longer than the one it replaces, this
      // string is going to have to grow in size,  Figure out how much
      // and grow it all the way now, rather than incrementally

      MYSIZE nNewLen    = sslen(szNew);
      if ( nNewLen > nOldLen )
      {
        int nFound      = 0;
        while ( nIdx < this->length() &&
          (nIdx=this->find(szOld, nIdx)) != MYBASE::npos )
        {
          nFound++;
          nIdx += nOldLen;
        }
        this->reserve(this->size() + nFound * (nNewLen - nOldLen));
      }


      static const CT ch  = CT(0);
      PCMYSTR szRealNew  = szNew == 0 ? &ch : szNew;
      nIdx        = 0;

      while ( nIdx < this->length() &&
        (nIdx=this->find(szOld, nIdx)) != MYBASE::npos )
      {
        this->replace(this->begin()+nIdx, this->begin()+nIdx+nOldLen,
          szRealNew);

        nReplaced++;
        nIdx += nNewLen;
      }
    }

    return nReplaced;
  }

  int ReverseFind(CT ch) const
  {
    MYSIZE nIdx  = this->find_last_of(ch);
    return static_cast<int>(MYBASE::npos == nIdx ? -1 : nIdx);
  }

  // ReverseFind overload that's not in CString but might be useful
  int ReverseFind(PCMYSTR szFind, MYSIZE pos=MYBASE::npos) const
  {
    //yuvalt - this does not compile with g++ since MYTTYPE() is different type
    //MYSIZE nIdx  = this->rfind(0 == szFind ? MYTYPE() : szFind, pos);
    MYSIZE nIdx  = this->rfind(0 == szFind ? "" : szFind, pos);
    return static_cast<int>(MYBASE::npos == nIdx ? -1 : nIdx);
  }

  MYTYPE Right(int nCount) const
  {
        // Range check the count.

    nCount = SSMAX(0, SSMIN(nCount, static_cast<int>(this->size())));
    return this->substr(this->size()-static_cast<MYSIZE>(nCount));
  }

  void SetAt(int nIndex, CT ch)
  {
    ASSERT(this->size() > static_cast<MYSIZE>(nIndex));
    this->at(static_cast<MYSIZE>(nIndex))    = ch;
  }

#ifndef SS_ANSI
  BSTR SetSysString(BSTR* pbstr) const
  {
    ostring os;
    ssasn(os, *this);
    if ( !::SysReAllocStringLen(pbstr, os.c_str(), os.length()) )
      throw std::runtime_error("out of memory");

    ASSERT(*pbstr != 0);
    return *pbstr;
  }
#endif

  MYTYPE SpanExcluding(PCMYSTR szCharSet) const
  {
        MYSIZE pos = this->find_first_of(szCharSet);
        return pos == MYBASE::npos ? *this : Left(pos);
  }

  MYTYPE SpanIncluding(PCMYSTR szCharSet) const
  {
        MYSIZE pos = this->find_first_not_of(szCharSet);
        return pos == MYBASE::npos ? *this : Left(pos);
  }

#if defined SS_WIN32 && !defined(UNICODE) && !defined(SS_ANSI)

  // CString's OemToAnsi and AnsiToOem functions are available only in
  // Unicode builds.  However since we're a template we also need a
  // runtime check of CT and a reinterpret_cast to account for the fact
  // that CStdStringW gets instantiated even in non-Unicode builds.

  void AnsiToOem()
  {
    if ( sizeof(CT) == sizeof(char) && !empty() )
    {
      ::CharToOem(reinterpret_cast<PCSTR>(this->c_str()),
            reinterpret_cast<PSTR>(GetBuf()));
    }
    else
    {
      ASSERT(false);
    }
  }

  void OemToAnsi()
  {
    if ( sizeof(CT) == sizeof(char) && !empty() )
    {
      ::OemToChar(reinterpret_cast<PCSTR>(this->c_str()),
            reinterpret_cast<PSTR>(GetBuf()));
    }
    else
    {
      ASSERT(false);
    }
  }

#endif


  // -------------------------------------------------------------------------
  // Trim and its variants
  // -------------------------------------------------------------------------
  MYTYPE& Trim()
  {
    return TrimLeft().TrimRight();
  }

  MYTYPE& TrimLeft()
  {
    this->erase(this->begin(),
      std::find_if(this->begin(), this->end(), NotSpace<CT>()));

    return *this;
  }

  MYTYPE&  TrimLeft(CT tTrim)
  {
    this->erase(0, this->find_first_not_of(tTrim));
    return *this;
  }

  MYTYPE&  TrimLeft(PCMYSTR szTrimChars)
  {
    this->erase(0, this->find_first_not_of(szTrimChars));
    return *this;
  }

  MYTYPE& TrimRight()
  {
    // NOTE:  When comparing reverse_iterators here (MYRITER), I avoid using
    // operator!=.  This is because namespace rel_ops also has a template
    // operator!= which conflicts with the global operator!= already defined
    // for reverse_iterator in the header <utility>.
    // Thanks to John James for alerting me to this.

    MYRITER it = std::find_if(this->rbegin(), this->rend(), NotSpace<CT>());
    if ( !(this->rend() == it) )
      this->erase(this->rend() - it);

    this->erase(!(it == this->rend()) ? this->find_last_of(*it) + 1 : 0);
    return *this;
  }

  MYTYPE&  TrimRight(CT tTrim)
  {
    MYSIZE nIdx  = this->find_last_not_of(tTrim);
    this->erase(MYBASE::npos == nIdx ? 0 : ++nIdx);
    return *this;
  }

  MYTYPE&  TrimRight(PCMYSTR szTrimChars)
  {
    MYSIZE nIdx  = this->find_last_not_of(szTrimChars);
    this->erase(MYBASE::npos == nIdx ? 0 : ++nIdx);
    return *this;
  }

  void      FreeExtra()
  {
    MYTYPE mt;
    this->swap(mt);
    if ( !mt.empty() )
      this->assign(mt.c_str(), mt.size());
  }

  // I have intentionally not implemented the following CString
  // functions.   You cannot make them work without taking advantage
  // of implementation specific behavior.  However if you absolutely
  // MUST have them, uncomment out these lines for "sort-of-like"
  // their behavior.  You're on your own.

//  CT*        LockBuffer()  { return GetBuf(); }// won't really lock
//  void      UnlockBuffer(); { }  // why have UnlockBuffer w/o LockBuffer?

  // Array-indexing operators.  Required because we defined an implicit cast
  // to operator const CT* (Thanks to Julian Selman for pointing this out)

  CT& operator[](int nIdx)
  {
    return static_cast<MYBASE*>(this)->operator[](static_cast<MYSIZE>(nIdx));
  }

  const CT& operator[](int nIdx) const
  {
    return static_cast<const MYBASE*>(this)->operator[](static_cast<MYSIZE>(nIdx));
  }

  CT& operator[](unsigned int nIdx)
  {
    return static_cast<MYBASE*>(this)->operator[](static_cast<MYSIZE>(nIdx));
  }

  const CT& operator[](unsigned int nIdx) const
  {
    return static_cast<const MYBASE*>(this)->operator[](static_cast<MYSIZE>(nIdx));
  }

  CT& operator[](unsigned long nIdx)
  {
    return static_cast<MYBASE*>(this)->operator[](static_cast<MYSIZE>(nIdx));
  }

  const CT& operator[](unsigned long nIdx) const
  {
    return static_cast<const MYBASE*>(this)->operator[](static_cast<MYSIZE>(nIdx));
  }

#ifndef SS_NO_IMPLICIT_CAST
  operator const CT*() const
  {
    return this->c_str();
  }
#endif

  // IStream related functions.  Useful in IPersistStream implementations

#ifdef SS_INC_COMDEF

  // struct SSSHDR - useful for non Std C++ persistence schemes.
  typedef struct SSSHDR
  {
    BYTE  byCtrl;
    ULONG  nChars;
  } SSSHDR;  // as in "Standard String Stream Header"

  #define SSSO_UNICODE  0x01  // the string is a wide string
  #define SSSO_COMPRESS  0x02  // the string is compressed

  // -------------------------------------------------------------------------
  // FUNCTION: StreamSize
  // REMARKS:
  //    Returns how many bytes it will take to StreamSave() this CStdString
  //    object to an IStream.
  // -------------------------------------------------------------------------
  ULONG StreamSize() const
  {
    // Control header plus string
    ASSERT(this->size()*sizeof(CT) < 0xffffffffUL - sizeof(SSSHDR));
    return (this->size() * sizeof(CT)) + sizeof(SSSHDR);
  }

  // -------------------------------------------------------------------------
  // FUNCTION: StreamSave
  // REMARKS:
  //    Saves this CStdString object to a COM IStream.
  // -------------------------------------------------------------------------
  HRESULT StreamSave(IStream* pStream) const
  {
    ASSERT(this->size()*sizeof(CT) < 0xffffffffUL - sizeof(SSSHDR));
    HRESULT hr    = E_FAIL;
    ASSERT(pStream != 0);
    SSSHDR hdr;
    hdr.byCtrl    = sizeof(CT) == 2 ? SSSO_UNICODE : 0;
    hdr.nChars    = this->size();


    if ( FAILED(hr=pStream->Write(&hdr, sizeof(SSSHDR), 0)) )
    {
      TRACE(_T("StreamSave: Cannot write control header, ERR=0x%X\n"),hr);
    }
    else if ( empty() )
    {
      ;    // nothing to write
    }
    else if ( FAILED(hr=pStream->Write(this->c_str(),
      this->size()*sizeof(CT), 0)) )
    {
      TRACE(_T("StreamSave: Cannot write string to stream 0x%X\n"), hr);
    }

    return hr;
  }


  // -------------------------------------------------------------------------
  // FUNCTION: StreamLoad
  // REMARKS:
  //    This method loads the object from an IStream.
  // -------------------------------------------------------------------------
  HRESULT StreamLoad(IStream* pStream)
  {
    ASSERT(pStream != 0);
    SSSHDR hdr;
    HRESULT hr      = E_FAIL;

    if ( FAILED(hr=pStream->Read(&hdr, sizeof(SSSHDR), 0)) )
    {
      TRACE(_T("StreamLoad: Cant read control header, ERR=0x%X\n"), hr);
    }
    else if ( hdr.nChars > 0 )
    {
      ULONG nRead    = 0;
      PMYSTR pMyBuf  = BufferSet(hdr.nChars);

      // If our character size matches the character size of the string
      // we're trying to read, then we can read it directly into our
      // buffer. Otherwise, we have to read into an intermediate buffer
      // and convert.

      if ( (hdr.byCtrl & SSSO_UNICODE) != 0 )
      {
        ULONG nBytes  = hdr.nChars * sizeof(wchar_t);
        if ( sizeof(CT) == sizeof(wchar_t) )
        {
          if ( FAILED(hr=pStream->Read(pMyBuf, nBytes, &nRead)) )
            TRACE(_T("StreamLoad: Cannot read string: 0x%X\n"), hr);
        }
        else
        {
          PWSTR pBufW = reinterpret_cast<PWSTR>(_alloca((nBytes)+1));
          if ( FAILED(hr=pStream->Read(pBufW, nBytes, &nRead)) )
            TRACE(_T("StreamLoad: Cannot read string: 0x%X\n"), hr);
          else
            sscpy(pMyBuf, pBufW, hdr.nChars);
        }
      }
      else
      {
        ULONG nBytes  = hdr.nChars * sizeof(char);
        if ( sizeof(CT) == sizeof(char) )
        {
          if ( FAILED(hr=pStream->Read(pMyBuf, nBytes, &nRead)) )
            TRACE(_T("StreamLoad: Cannot read string: 0x%X\n"), hr);
        }
        else
        {
          PSTR pBufA = reinterpret_cast<PSTR>(_alloca(nBytes));
          if ( FAILED(hr=pStream->Read(pBufA, hdr.nChars, &nRead)) )
            TRACE(_T("StreamLoad: Cannot read string: 0x%X\n"), hr);
          else
            sscpy(pMyBuf, pBufA, hdr.nChars);
        }
      }
    }
    else
    {
      this->erase();
    }
    return hr;
  }
#endif // #ifdef SS_INC_COMDEF

#ifndef SS_ANSI

  // SetResourceHandle/GetResourceHandle.  In MFC builds, these map directly
  // to AfxSetResourceHandle and AfxGetResourceHandle.  In non-MFC builds they
  // point to a single static HINST so that those who call the member
  // functions that take resource IDs can provide an alternate HINST of a DLL
  // to search.  This is not exactly the list of HMODULES that MFC provides
  // but it's better than nothing.

  #ifdef _MFC_VER
    static void SetResourceHandle(HMODULE hNew)
    {
      AfxSetResourceHandle(hNew);
    }
    static HMODULE GetResourceHandle()
    {
      return AfxGetResourceHandle();
    }
  #else
    static void SetResourceHandle(HMODULE hNew)
    {
      SSResourceHandle() = hNew;
    }
    static HMODULE GetResourceHandle()
    {
      return SSResourceHandle();
    }
  #endif

#endif
};

// -----------------------------------------------------------------------------
// MSVC USERS: HOW TO EXPORT CSTDSTRING FROM A DLL
//
// If you are using MS Visual C++ and you want to export CStdStringA and
// CStdStringW from a DLL, then all you need to
//
//    1.  make sure that all components link to the same DLL version
//      of the CRT (not the static one).
//    2.  Uncomment the 3 lines of code below
//    3.  #define 2 macros per the instructions in MS KnowledgeBase
//      article Q168958.  The macros are:
//
//    MACRO    DEFINTION WHEN EXPORTING    DEFINITION WHEN IMPORTING
//    -----    ------------------------    -------------------------
//    SSDLLEXP  (nothing, just #define it)    extern
//    SSDLLSPEC  __declspec(dllexport)      __declspec(dllimport)
//
//    Note that these macros must be available to ALL clients who want to
//    link to the DLL and use the class.  If they
//
// A word of advice: Don't bother.
//
// Really, it is not necessary to export CStdString functions from a DLL.  I
// never do.  In my projects, I do generally link to the DLL version of the
// Standard C++ Library, but I do NOT attempt to export CStdString functions.
// I simply include the header where it is needed and allow for the code
// redundancy.
//
// That redundancy is a lot less than you think.  This class does most of its
// work via the Standard C++ Library, particularly the base_class basic_string<>
// member functions.  Most of the functions here are small enough to be inlined
// anyway.  Besides, you'll find that in actual practice you use less than 1/2
// of the code here, even in big projects and different modules will use as
// little as 10% of it.  That means a lot less functions actually get linked
// your binaries.  If you export this code from a DLL, it ALL gets linked in.
//
// I've compared the size of the binaries from exporting vs NOT exporting.  Take
// my word for it -- exporting this code is not worth the hassle.
//
// -----------------------------------------------------------------------------
//#pragma warning(disable:4231) // non-standard extension ("extern template")
//  SSDLLEXP template class SSDLLSPEC CStdStr<char>;
//  SSDLLEXP template class SSDLLSPEC CStdStr<wchar_t>;


// =============================================================================
//            END OF CStdStr INLINE FUNCTION DEFINITIONS
// =============================================================================

//  Now typedef our class names based upon this humongous template

typedef CStdStr<char>    CStdStringA;  // a better std::string
typedef CStdStr<wchar_t>  CStdStringW;  // a better std::wstring
typedef CStdStr<uint16_t>  CStdString16;  // a 16bit char string
typedef CStdStr<uint32_t>  CStdString32;  // a 32bit char string
typedef CStdStr<OLECHAR>  CStdStringO;  // almost always CStdStringW

// -----------------------------------------------------------------------------
// CStdStr addition functions defined as inline
// -----------------------------------------------------------------------------


inline CStdStringA operator+(const CStdStringA& s1, const CStdStringA& s2)
{
  CStdStringA sRet(SSREF(s1));
  sRet.append(s2);
  return sRet;
}
inline CStdStringA operator+(const CStdStringA& s1, CStdStringA::value_type t)
{
  CStdStringA sRet(SSREF(s1));
  sRet.append(1, t);
  return sRet;
}
inline CStdStringA operator+(const CStdStringA& s1, PCSTR pA)
{
  CStdStringA sRet(SSREF(s1));
  sRet.append(pA);
  return sRet;
}
inline CStdStringA operator+(PCSTR pA, const CStdStringA& sA)
{
  CStdStringA sRet;
  CStdStringA::size_type nObjSize = sA.size();
  CStdStringA::size_type nLitSize =
    static_cast<CStdStringA::size_type>(sslen(pA));

  sRet.reserve(nLitSize + nObjSize);
  sRet.assign(pA);
  sRet.append(sA);
  return sRet;
}


inline CStdStringA operator+(const CStdStringA& s1, const CStdStringW& s2)
{
  return s1 + CStdStringA(s2);
}
inline CStdStringW operator+(const CStdStringW& s1, const CStdStringW& s2)
{
  CStdStringW sRet(SSREF(s1));
  sRet.append(s2);
  return sRet;
}
inline CStdStringA operator+(const CStdStringA& s1, PCWSTR pW)
{
  return s1 + CStdStringA(pW);
}

#ifdef UNICODE
  inline CStdStringW operator+(PCWSTR pW, const CStdStringA& sA)
  {
    return CStdStringW(pW) + CStdStringW(SSREF(sA));
  }
  inline CStdStringW operator+(PCSTR pA, const CStdStringW& sW)
  {
    return CStdStringW(pA) + sW;
  }
#else
  inline CStdStringA operator+(PCWSTR pW, const CStdStringA& sA)
  {
    return CStdStringA(pW) + sA;
  }
  inline CStdStringA operator+(PCSTR pA, const CStdStringW& sW)
  {
    return pA + CStdStringA(sW);
  }
#endif

// ...Now the wide string versions.
inline CStdStringW operator+(const CStdStringW& s1, CStdStringW::value_type t)
{
  CStdStringW sRet(SSREF(s1));
  sRet.append(1, t);
  return sRet;
}
inline CStdStringW operator+(const CStdStringW& s1, PCWSTR pW)
{
  CStdStringW sRet(SSREF(s1));
  sRet.append(pW);
  return sRet;
}
inline CStdStringW operator+(PCWSTR pW, const CStdStringW& sW)
{
  CStdStringW sRet;
  CStdStringW::size_type nObjSize = sW.size();
  CStdStringA::size_type nLitSize =
    static_cast<CStdStringW::size_type>(sslen(pW));

  sRet.reserve(nLitSize + nObjSize);
  sRet.assign(pW);
  sRet.append(sW);
  return sRet;
}

inline CStdStringW operator+(const CStdStringW& s1, const CStdStringA& s2)
{
  return s1 + CStdStringW(s2);
}
inline CStdStringW operator+(const CStdStringW& s1, PCSTR pA)
{
  return s1 + CStdStringW(pA);
}


// New-style format function is a template

#ifdef SS_SAFE_FORMAT

template<>
struct FmtArg<CStdStringA>
{
    explicit FmtArg(const CStdStringA& arg) : a_(arg) {}
    PCSTR operator()() const { return a_.c_str(); }
    const CStdStringA& a_;
private:
    FmtArg<CStdStringA>& operator=(const FmtArg<CStdStringA>&) { return *this; }
};
template<>
struct FmtArg<CStdStringW>
{
    explicit FmtArg(const CStdStringW& arg) : a_(arg) {}
    PCWSTR operator()() const { return a_.c_str(); }
    const CStdStringW& a_;
private:
    FmtArg<CStdStringW>& operator=(const FmtArg<CStdStringW>&) { return *this; }
};

template<>
struct FmtArg<std::string>
{
    explicit FmtArg(const std::string& arg) : a_(arg) {}
    PCSTR operator()() const { return a_.c_str(); }
    const std::string& a_;
private:
    FmtArg<std::string>& operator=(const FmtArg<std::string>&) { return *this; }
};
template<>
struct FmtArg<std::wstring>
{
    explicit FmtArg(const std::wstring& arg) : a_(arg) {}
    PCWSTR operator()() const { return a_.c_str(); }
    const std::wstring& a_;
private:
    FmtArg<std::wstring>& operator=(const FmtArg<std::wstring>&) {return *this;}
};
#endif // #ifdef SS_SAFEFORMAT

#ifndef SS_ANSI
  // SSResourceHandle: our MFC-like resource handle
  inline HMODULE& SSResourceHandle()
  {
    static HMODULE hModuleSS  = GetModuleHandle(0);
    return hModuleSS;
  }
#endif


// In MFC builds, define some global serialization operators
// Special operators that allow us to serialize CStdStrings to CArchives.
// Note that we use an intermediate CString object in order to ensure that
// we use the exact same format.

#ifdef _MFC_VER
  inline CArchive& AFXAPI operator<<(CArchive& ar, const CStdStringA& strA)
  {
    CString strTemp  = strA;
    return ar << strTemp;
  }
  inline CArchive& AFXAPI operator<<(CArchive& ar, const CStdStringW& strW)
  {
    CString strTemp  = strW;
    return ar << strTemp;
  }

  inline CArchive& AFXAPI operator>>(CArchive& ar, CStdStringA& strA)
  {
    CString strTemp;
    ar >> strTemp;
    strA = strTemp;
    return ar;
  }
  inline CArchive& AFXAPI operator>>(CArchive& ar, CStdStringW& strW)
  {
    CString strTemp;
    ar >> strTemp;
    strW = strTemp;
    return ar;
  }
#endif  // #ifdef _MFC_VER -- (i.e. is this MFC?)



// -----------------------------------------------------------------------------
// GLOBAL FUNCTION:  WUFormat
//    CStdStringA WUFormat(UINT nId, ...);
//    CStdStringA WUFormat(PCSTR szFormat, ...);
//
// REMARKS:
//    This function allows the caller for format and return a CStdStringA
//    object with a single line of code.
// -----------------------------------------------------------------------------
#ifdef SS_ANSI
#else
  inline CStdStringA WUFormatA(UINT nId, ...)
  {
    va_list argList;
    va_start(argList, nId);

    CStdStringA strFmt;
    CStdStringA strOut;
    if ( strFmt.Load(nId) )
      strOut.FormatV(strFmt, argList);

    va_end(argList);
    return strOut;
  }
  inline CStdStringA WUFormatA(PCSTR szFormat, ...)
  {
    va_list argList;
    va_start(argList, szFormat);
    CStdStringA strOut;
    strOut.FormatV(szFormat, argList);
    va_end(argList);
    return strOut;
  }
  inline CStdStringW WUFormatW(UINT nId, ...)
  {
    va_list argList;
    va_start(argList, nId);

    CStdStringW strFmt;
    CStdStringW strOut;
    if ( strFmt.Load(nId) )
      strOut.FormatV(strFmt, argList);

    va_end(argList);
    return strOut;
  }
  inline CStdStringW WUFormatW(PCWSTR szwFormat, ...)
  {
    va_list argList;
    va_start(argList, szwFormat);
    CStdStringW strOut;
    strOut.FormatV(szwFormat, argList);
    va_end(argList);
    return strOut;
  }
#endif // #ifdef SS_ANSI



#if defined(SS_WIN32) && !defined (SS_ANSI)
  // -------------------------------------------------------------------------
  // FUNCTION: WUSysMessage
  //   CStdStringA WUSysMessageA(DWORD dwError, DWORD dwLangId=SS_DEFLANGID);
  //   CStdStringW WUSysMessageW(DWORD dwError, DWORD dwLangId=SS_DEFLANGID);
  //
  // DESCRIPTION:
  //   This function simplifies the process of obtaining a string equivalent
  //   of a system error code returned from GetLastError().  You simply
  //   supply the value returned by GetLastError() to this function and the
  //   corresponding system string is returned in the form of a CStdStringA.
  //
  // PARAMETERS:
  //   dwError - a DWORD value representing the error code to be translated
  //   dwLangId - the language id to use.  defaults to english.
  //
  // RETURN VALUE:
  //   a CStdStringA equivalent of the error code.  Currently, this function
  //   only returns either English of the system default language strings.
  // -------------------------------------------------------------------------
  #define SS_DEFLANGID MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
  inline CStdStringA WUSysMessageA(DWORD dwError, DWORD dwLangId=SS_DEFLANGID)
  {
    CHAR szBuf[512];

    if ( 0 != ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
                   dwLangId, szBuf, 511, NULL) )
      return WUFormatA("%s (0x%X)", szBuf, dwError);
    else
       return WUFormatA("Unknown error (0x%X)", dwError);
  }
  inline CStdStringW WUSysMessageW(DWORD dwError, DWORD dwLangId=SS_DEFLANGID)
  {
    WCHAR szBuf[512];

    if ( 0 != ::FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
                   dwLangId, szBuf, 511, NULL) )
      return WUFormatW(L"%s (0x%X)", szBuf, dwError);
    else
       return WUFormatW(L"Unknown error (0x%X)", dwError);
  }
#endif

// Define TCHAR based friendly names for some of these functions

#ifdef UNICODE
  //#define CStdString        CStdStringW
  typedef CStdStringW        CStdString;
  #define WUSysMessage      WUSysMessageW
  #define WUFormat        WUFormatW
#else
  //#define CStdString        CStdStringA
  typedef CStdStringA        CStdString;
  #define WUSysMessage      WUSysMessageA
  #define WUFormat        WUFormatA
#endif

// ...and some shorter names for the space-efficient

#define WUSysMsg          WUSysMessage
#define WUSysMsgA          WUSysMessageA
#define WUSysMsgW          WUSysMessageW
#define WUFmtA            WUFormatA
#define  WUFmtW            WUFormatW
#define WUFmt            WUFormat
#define WULastErrMsg()        WUSysMessage(::GetLastError())
#define WULastErrMsgA()        WUSysMessageA(::GetLastError())
#define WULastErrMsgW()        WUSysMessageW(::GetLastError())


// -----------------------------------------------------------------------------
// FUNCTIONAL COMPARATORS:
// REMARKS:
//    These structs are derived from the std::binary_function template.  They
//    give us functional classes (which may be used in Standard C++ Library
//    collections and algorithms) that perform case-insensitive comparisons of
//    CStdString objects.  This is useful for maps in which the key may be the
//     proper string but in the wrong case.
// -----------------------------------------------------------------------------
#define StdStringLessNoCaseW    SSLNCW  // avoid VC compiler warning 4786
#define StdStringEqualsNoCaseW    SSENCW
#define StdStringLessNoCaseA    SSLNCA
#define StdStringEqualsNoCaseA    SSENCA

#ifdef UNICODE
  #define StdStringLessNoCase    SSLNCW
  #define StdStringEqualsNoCase  SSENCW
#else
  #define StdStringLessNoCase    SSLNCA
  #define StdStringEqualsNoCase  SSENCA
#endif

struct StdStringLessNoCaseW
  : std::binary_function<CStdStringW, CStdStringW, bool>
{
  inline
  bool operator()(const CStdStringW& sLeft, const CStdStringW& sRight) const
  { return ssicmp(sLeft.c_str(), sRight.c_str()) < 0; }
};
struct StdStringEqualsNoCaseW
  : std::binary_function<CStdStringW, CStdStringW, bool>
{
  inline
  bool operator()(const CStdStringW& sLeft, const CStdStringW& sRight) const
  { return ssicmp(sLeft.c_str(), sRight.c_str()) == 0; }
};
struct StdStringLessNoCaseA
  : std::binary_function<CStdStringA, CStdStringA, bool>
{
  inline
  bool operator()(const CStdStringA& sLeft, const CStdStringA& sRight) const
  { return ssicmp(sLeft.c_str(), sRight.c_str()) < 0; }
};
struct StdStringEqualsNoCaseA
  : std::binary_function<CStdStringA, CStdStringA, bool>
{
  inline
  bool operator()(const CStdStringA& sLeft, const CStdStringA& sRight) const
  { return ssicmp(sLeft.c_str(), sRight.c_str()) == 0; }
};

// If we had to define our own version of TRACE above, get rid of it now

#ifdef TRACE_DEFINED_HERE
  #undef TRACE
  #undef TRACE_DEFINED_HERE
#endif


// These std::swap specializations come courtesy of Mike Crusader.

//namespace std
//{
//  inline void swap(CStdStringA& s1, CStdStringA& s2) throw()
//  {
//    s1.swap(s2);
//  }
//  template<>
//  inline void swap(CStdStringW& s1, CStdStringW& s2) throw()
//  {
//    s1.swap(s2);
//  }
//}

// Turn back on any Borland warnings we turned off.

#ifdef __BORLANDC__
    #pragma option pop  // Turn back on inline function warnings
//  #pragma warn +inl   // Turn back on inline function warnings
#endif

typedef std::vector<CStdString> CStdStringArray;

#endif  // #ifndef STDSTRING_H