about summary refs log tree commit diff
path: root/src/libcore/str.rs
blob: 479776d7fd540f0cdaf30f0beaa4b0e87d677dc3 (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
/*!
 * String manipulation
 *
 * Strings are a packed UTF-8 representation of text, stored as null
 * terminated buffers of u8 bytes.  Strings should be indexed in bytes,
 * for efficiency, but UTF-8 unsafe operations should be avoided.  For
 * some heavy-duty uses, try std::rope.
 */

use cmp::{Eq, Ord};
use libc::size_t;
use io::WriterUtil;

export
   // Creating a string
   from_bytes,
   from_byte,
   from_slice,
   from_char,
   from_chars,
   append,
   concat,
   connect,

   // Reinterpretation
   as_bytes,
   as_bytes_slice,
   as_buf,
   as_c_str,

   // Adding things to and removing things from a string
   push_str_no_overallocate,
   push_str,
   push_char,
   pop_char,
   shift_char,
   view_shift_char,
   unshift_char,
   trim_left,
   trim_right,
   trim,
   trim_left_chars,
   trim_right_chars,
   trim_chars,

   // Transforming strings
   to_bytes,
   byte_slice,
   chars,
   substr,
   slice,
   view,
   split, splitn, split_nonempty,
   split_char, splitn_char, split_char_nonempty,
   split_str, split_str_nonempty,
   lines,
   lines_any,
   words,
   to_lower,
   to_upper,
   replace,

   // Comparing strings
   eq,
   eq_slice,
   le,
   hash,

   // Iterating through strings
   all, any,
   all_between, any_between,
   map,
   each, eachi,
   each_char, each_chari,
   bytes_each,
   chars_each,
   split_char_each,
   splitn_char_each,
   words_each,
   lines_each,

   // Searching
   find, find_from, find_between,
   rfind, rfind_from, rfind_between,
   find_char, find_char_from, find_char_between,
   rfind_char, rfind_char_from, rfind_char_between,
   find_str, find_str_from, find_str_between,
   contains, contains_char,
   starts_with,
   ends_with,

   // String properties
   is_ascii,
   is_empty,
   is_not_empty,
   is_whitespace,
   len,
   char_len,

   // Misc
   is_utf8,
   is_utf16,
   to_utf16,
   from_utf16,
   utf16_chars,
   count_chars, count_bytes,
   utf8_char_width,
   char_range_at,
   is_char_boundary,
   char_at,
   reserve,
   reserve_at_least,
   capacity,
   escape_default,
   escape_unicode,

   raw,
   extensions,
   StrSlice,
   UniqueStr,
   traits;

/*
Section: Creating a string
*/

/**
 * Convert a vector of bytes to a UTF-8 string
 *
 * # Failure
 *
 * Fails if invalid UTF-8
 */
pure fn from_bytes(vv: &[const u8]) -> ~str {
    assert is_utf8(vv);
    return unsafe { raw::from_bytes(vv) };
}

/// Copy a slice into a new unique str
pure fn from_slice(s: &str) -> ~str {
    unsafe { raw::slice_bytes(s, 0, len(s)) }
}

/**
 * Convert a byte to a UTF-8 string
 *
 * # Failure
 *
 * Fails if invalid UTF-8
 */
pure fn from_byte(b: u8) -> ~str {
    assert b < 128u8;
    unsafe { ::cast::transmute(~[b, 0u8]) }
}

/// Appends a character at the end of a string
fn push_char(&s: ~str, ch: char) {
    unsafe {
        let code = ch as uint;
        let nb = if code < max_one_b { 1u }
        else if code < max_two_b { 2u }
        else if code < max_three_b { 3u }
        else if code < max_four_b { 4u }
        else if code < max_five_b { 5u }
        else { 6u };
        let len = len(s);
        let new_len = len + nb;
        reserve_at_least(s, new_len);
        let off = len;
        do as_buf(s) |buf, _len| {
            let buf: *mut u8 = ::cast::reinterpret_cast(&buf);
            if nb == 1u {
                *ptr::mut_offset(buf, off) =
                    code as u8;
            } else if nb == 2u {
                *ptr::mut_offset(buf, off) =
                    (code >> 6u & 31u | tag_two_b) as u8;
                *ptr::mut_offset(buf, off + 1u) =
                    (code & 63u | tag_cont) as u8;
            } else if nb == 3u {
                *ptr::mut_offset(buf, off) =
                    (code >> 12u & 15u | tag_three_b) as u8;
                *ptr::mut_offset(buf, off + 1u) =
                    (code >> 6u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 2u) =
                    (code & 63u | tag_cont) as u8;
            } else if nb == 4u {
                *ptr::mut_offset(buf, off) =
                    (code >> 18u & 7u | tag_four_b) as u8;
                *ptr::mut_offset(buf, off + 1u) =
                    (code >> 12u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 2u) =
                    (code >> 6u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 3u) =
                    (code & 63u | tag_cont) as u8;
            } else if nb == 5u {
                *ptr::mut_offset(buf, off) =
                    (code >> 24u & 3u | tag_five_b) as u8;
                *ptr::mut_offset(buf, off + 1u) =
                    (code >> 18u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 2u) =
                    (code >> 12u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 3u) =
                    (code >> 6u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 4u) =
                    (code & 63u | tag_cont) as u8;
            } else if nb == 6u {
                *ptr::mut_offset(buf, off) =
                    (code >> 30u & 1u | tag_six_b) as u8;
                *ptr::mut_offset(buf, off + 1u) =
                    (code >> 24u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 2u) =
                    (code >> 18u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 3u) =
                    (code >> 12u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 4u) =
                    (code >> 6u & 63u | tag_cont) as u8;
                *ptr::mut_offset(buf, off + 5u) =
                    (code & 63u | tag_cont) as u8;
            }
        }

        raw::set_len(s, new_len);
    }
}

/// Convert a char to a string
pure fn from_char(ch: char) -> ~str {
    let mut buf = ~"";
    unsafe { push_char(buf, ch); }
    move buf
}

/// Convert a vector of chars to a string
pure fn from_chars(chs: &[char]) -> ~str {
    let mut buf = ~"";
    unsafe {
        reserve(buf, chs.len());
        for vec::each(chs) |ch| {
            push_char(buf, *ch);
        }
    }
    move buf
}

/// Appends a string slice to the back of a string, without overallocating
#[inline(always)]
fn push_str_no_overallocate(&lhs: ~str, rhs: &str) {
    unsafe {
        let llen = lhs.len();
        let rlen = rhs.len();
        reserve(lhs, llen + rlen);
        do as_buf(lhs) |lbuf, _llen| {
            do as_buf(rhs) |rbuf, _rlen| {
                let dst = ptr::offset(lbuf, llen);
                let dst = ::cast::transmute_mut_unsafe(dst);
                ptr::memcpy(dst, rbuf, rlen);
            }
        }
        raw::set_len(lhs, llen + rlen);
    }
}
/// Appends a string slice to the back of a string
#[inline(always)]
fn push_str(&lhs: ~str, rhs: &str) {
    unsafe {
        let llen = lhs.len();
        let rlen = rhs.len();
        reserve_at_least(lhs, llen + rlen);
        do as_buf(lhs) |lbuf, _llen| {
            do as_buf(rhs) |rbuf, _rlen| {
                let dst = ptr::offset(lbuf, llen);
                let dst = ::cast::transmute_mut_unsafe(dst);
                ptr::memcpy(dst, rbuf, rlen);
            }
        }
        raw::set_len(lhs, llen + rlen);
    }
}

/// Concatenate two strings together
#[inline(always)]
pure fn append(+lhs: ~str, rhs: &str) -> ~str {
    let mut v <- lhs;
    unsafe {
        push_str_no_overallocate(v, rhs);
    }
    move v
}


/// Concatenate a vector of strings
pure fn concat(v: &[~str]) -> ~str {
    let mut s: ~str = ~"";
    for vec::each(v) |ss| {
        unsafe { push_str(s, *ss) };
    }
    move s
}

/// Concatenate a vector of strings, placing a given separator between each
pure fn connect(v: &[~str], sep: &str) -> ~str {
    let mut s = ~"", first = true;
    for vec::each(v) |ss| {
        if first { first = false; } else { unsafe { push_str(s, sep); } }
        unsafe { push_str(s, *ss) };
    }
    move s
}

/*
Section: Adding to and removing from a string
*/

/**
 * Remove the final character from a string and return it
 *
 * # Failure
 *
 * If the string does not contain any characters
 */
fn pop_char(&s: ~str) -> char {
    let end = len(s);
    assert end > 0u;
    let {ch, prev} = char_range_at_reverse(s, end);
    unsafe { raw::set_len(s, prev); }
    return ch;
}

/**
 * Remove the first character from a string and return it
 *
 * # Failure
 *
 * If the string does not contain any characters
 */
fn shift_char(&s: ~str) -> char {
    let {ch, next} = char_range_at(s, 0u);
    s = unsafe { raw::slice_bytes(s, next, len(s)) };
    return ch;
}

/**
 * Removes the first character from a string slice and returns it. This does
 * not allocate a new string; instead, it mutates a slice to point one
 * character beyond the character that was shifted.
 *
 * # Failure
 *
 * If the string does not contain any characters
 */
#[inline]
fn view_shift_char(s: &a/str) -> (char, &a/str) {
    let {ch, next} = char_range_at(s, 0u);
    let next_s = unsafe { raw::view_bytes(s, next, len(s)) };
    return (ch, next_s);
}

/// Prepend a char to a string
fn unshift_char(&s: ~str, ch: char) { s = from_char(ch) + s; }

/**
 * Returns a string with leading `chars_to_trim` removed.
 *
 * # Arguments
 *
 * * s - A string
 * * chars_to_trim - A vector of chars
 *
 */
pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
    if chars_to_trim.is_empty() { return from_slice(s); }

    match find(s, |c| !chars_to_trim.contains(c)) {
      None => ~"",
      Some(first) => unsafe { raw::slice_bytes(s, first, s.len()) }
    }
}

/**
 * Returns a string with trailing `chars_to_trim` removed.
 *
 * # Arguments
 *
 * * s - A string
 * * chars_to_trim - A vector of chars
 *
 */
pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
    if chars_to_trim.is_empty() { return str::from_slice(s); }

    match rfind(s, |c| !chars_to_trim.contains(c)) {
      None => ~"",
      Some(last) => {
        let {next, _} = char_range_at(s, last);
        unsafe { raw::slice_bytes(s, 0u, next) }
      }
    }
}

/**
 * Returns a string with leading and trailing `chars_to_trim` removed.
 *
 * # Arguments
 *
 * * s - A string
 * * chars_to_trim - A vector of chars
 *
 */
pure fn trim_chars(s: &str, chars_to_trim: &[char]) -> ~str {
    trim_left_chars(trim_right_chars(s, chars_to_trim), chars_to_trim)
}

/// Returns a string with leading whitespace removed
pure fn trim_left(s: &str) -> ~str {
    match find(s, |c| !char::is_whitespace(c)) {
      None => ~"",
      Some(first) => unsafe { raw::slice_bytes(s, first, len(s)) }
    }
}

/// Returns a string with trailing whitespace removed
pure fn trim_right(s: &str) -> ~str {
    match rfind(s, |c| !char::is_whitespace(c)) {
      None => ~"",
      Some(last) => {
        let {next, _} = char_range_at(s, last);
        unsafe { raw::slice_bytes(s, 0u, next) }
      }
    }
}

/// Returns a string with leading and trailing whitespace removed
pure fn trim(s: &str) -> ~str { trim_left(trim_right(s)) }

/*
Section: Transforming strings
*/

/**
 * Converts a string to a vector of bytes
 *
 * The result vector is not null-terminated.
 */
pure fn to_bytes(s: &str) -> ~[u8] unsafe {
    let mut v: ~[u8] = ::cast::transmute(from_slice(s));
    vec::raw::set_len(v, len(s));
    move v
}

/// Work with the string as a byte slice, not including trailing null.
#[inline(always)]
pure fn byte_slice<T>(s: &str, f: fn(v: &[u8]) -> T) -> T {
    do as_buf(s) |p,n| {
        unsafe { vec::raw::form_slice(p, n-1u, f) }
    }
}

/// Convert a string to a vector of characters
pure fn chars(s: &str) -> ~[char] {
    let mut buf = ~[], i = 0u;
    let len = len(s);
    while i < len {
        let {ch, next} = char_range_at(s, i);
        unsafe { vec::push(buf, ch); }
        i = next;
    }
    move buf
}

/**
 * Take a substring of another.
 *
 * Returns a string containing `n` characters starting at byte offset
 * `begin`.
 */
pure fn substr(s: &str, begin: uint, n: uint) -> ~str {
    slice(s, begin, begin + count_bytes(s, begin, n))
}

/**
 * Returns a slice of the given string from the byte range [`begin`..`end`)
 *
 * Fails when `begin` and `end` do not point to valid characters or
 * beyond the last character of the string
 */
pure fn slice(s: &str, begin: uint, end: uint) -> ~str {
    assert is_char_boundary(s, begin);
    assert is_char_boundary(s, end);
    unsafe { raw::slice_bytes(s, begin, end) }
}

/**
 * Returns a view of the given string from the byte range [`begin`..`end`)
 *
 * Fails when `begin` and `end` do not point to valid characters or beyond
 * the last character of the string
 */
pure fn view(s: &a/str, begin: uint, end: uint) -> &a/str {
    assert is_char_boundary(s, begin);
    assert is_char_boundary(s, end);
    unsafe { raw::view_bytes(s, begin, end) }
}

/// Splits a string into substrings at each occurrence of a given character
pure fn split_char(s: &str, sep: char) -> ~[~str] {
    split_char_inner(s, sep, len(s), true)
}

/**
 * Splits a string into substrings at each occurrence of a given
 * character up to 'count' times
 *
 * The byte must be a valid UTF-8/ASCII byte
 */
pure fn splitn_char(s: &str, sep: char, count: uint) -> ~[~str] {
    split_char_inner(s, sep, count, true)
}

/// Like `split_char`, but omits empty strings from the returned vector
pure fn split_char_nonempty(s: &str, sep: char) -> ~[~str] {
    split_char_inner(s, sep, len(s), false)
}

pure fn split_char_inner(s: &str, sep: char, count: uint, allow_empty: bool)
    -> ~[~str] {
    if sep < 128u as char {
        let b = sep as u8, l = len(s);
        let mut result = ~[], done = 0u;
        let mut i = 0u, start = 0u;
        while i < l && done < count {
            if s[i] == b {
                if allow_empty || start < i unsafe {
                    vec::push(result,
                              unsafe { raw::slice_bytes(s, start, i) });
                }
                start = i + 1u;
                done += 1u;
            }
            i += 1u;
        }
        if allow_empty || start < l {
            unsafe { vec::push(result, raw::slice_bytes(s, start, l) ) };
        }
        move result
    } else {
        splitn(s, |cur| cur == sep, count)
    }
}


/// Splits a string into substrings using a character function
pure fn split(s: &str, sepfn: fn(char) -> bool) -> ~[~str] {
    split_inner(s, sepfn, len(s), true)
}

/**
 * Splits a string into substrings using a character function, cutting at
 * most `count` times.
 */
pure fn splitn(s: &str, sepfn: fn(char) -> bool, count: uint) -> ~[~str] {
    split_inner(s, sepfn, count, true)
}

/// Like `split`, but omits empty strings from the returned vector
pure fn split_nonempty(s: &str, sepfn: fn(char) -> bool) -> ~[~str] {
    split_inner(s, sepfn, len(s), false)
}

pure fn split_inner(s: &str, sepfn: fn(cc: char) -> bool, count: uint,
               allow_empty: bool) -> ~[~str] {
    let l = len(s);
    let mut result = ~[], i = 0u, start = 0u, done = 0u;
    while i < l && done < count {
        let {ch, next} = char_range_at(s, i);
        if sepfn(ch) {
            if allow_empty || start < i unsafe {
                vec::push(result, unsafe { raw::slice_bytes(s, start, i)});
            }
            start = next;
            done += 1u;
        }
        i = next;
    }
    if allow_empty || start < l unsafe {
        vec::push(result, unsafe { raw::slice_bytes(s, start, l) });
    }
    move result
}

// See Issue #1932 for why this is a naive search
pure fn iter_matches(s: &a/str, sep: &b/str, f: fn(uint, uint)) {
    let sep_len = len(sep), l = len(s);
    assert sep_len > 0u;
    let mut i = 0u, match_start = 0u, match_i = 0u;

    while i < l {
        if s[i] == sep[match_i] {
            if match_i == 0u { match_start = i; }
            match_i += 1u;
            // Found a match
            if match_i == sep_len {
                f(match_start, i + 1u);
                match_i = 0u;
            }
            i += 1u;
        } else {
            // Failed match, backtrack
            if match_i > 0u {
                match_i = 0u;
                i = match_start + 1u;
            } else {
                i += 1u;
            }
        }
    }
}

pure fn iter_between_matches(s: &a/str, sep: &b/str, f: fn(uint, uint)) {
    let mut last_end = 0u;
    do iter_matches(s, sep) |from, to| {
        f(last_end, from);
        last_end = to;
    }
    f(last_end, len(s));
}

/**
 * Splits a string into a vector of the substrings separated by a given string
 *
 * # Example
 *
 * ~~~
 * assert ["", "XXX", "YYY", ""] == split_str(".XXX.YYY.", ".")
 * ~~~
 */
pure fn split_str(s: &a/str, sep: &b/str) -> ~[~str] {
    let mut result = ~[];
    do iter_between_matches(s, sep) |from, to| {
        unsafe { vec::push(result, raw::slice_bytes(s, from, to)); }
    }
    move result
}

pure fn split_str_nonempty(s: &a/str, sep: &b/str) -> ~[~str] {
    let mut result = ~[];
    do iter_between_matches(s, sep) |from, to| {
        if to > from {
            unsafe { vec::push(result, raw::slice_bytes(s, from, to)); }
        }
    }
    move result
}

/**
 * Splits a string into a vector of the substrings separated by LF ('\n')
 */
pure fn lines(s: &str) -> ~[~str] { split_char(s, '\n') }

/**
 * Splits a string into a vector of the substrings separated by LF ('\n')
 * and/or CR LF ("\r\n")
 */
pure fn lines_any(s: &str) -> ~[~str] {
    vec::map(lines(s), |s| {
        let l = len(s);
        let mut cp = copy s;
        if l > 0u && s[l - 1u] == '\r' as u8 {
            unsafe { raw::set_len(cp, l - 1u); }
        }
        move cp
    })
}

/// Splits a string into a vector of the substrings separated by whitespace
pure fn words(s: &str) -> ~[~str] {
    split_nonempty(s, |c| char::is_whitespace(c))
}

/// Convert a string to lowercase. ASCII only
pure fn to_lower(s: &str) -> ~str {
    map(s,
        |c| unsafe{(libc::tolower(c as libc::c_char)) as char}
    )
}

/// Convert a string to uppercase. ASCII only
pure fn to_upper(s: &str) -> ~str {
    map(s,
        |c| unsafe{(libc::toupper(c as libc::c_char)) as char}
    )
}

/**
 * Replace all occurrences of one string with another
 *
 * # Arguments
 *
 * * s - The string containing substrings to replace
 * * from - The string to replace
 * * to - The replacement string
 *
 * # Return value
 *
 * The original string with all occurances of `from` replaced with `to`
 */
pure fn replace(s: &str, from: &str, to: &str) -> ~str {
    let mut result = ~"", first = true;
    do iter_between_matches(s, from) |start, end| {
        if first { first = false; } else { unsafe {push_str(result, to); }}
        unsafe { push_str(result, raw::slice_bytes(s, start, end)); }
    }
    move result
}

/*
Section: Comparing strings
*/

/// Bytewise slice equality
#[cfg(notest)]
#[lang="str_eq"]
pure fn eq_slice(a: &str, b: &str) -> bool {
    do as_buf(a) |ap, alen| {
        do as_buf(b) |bp, blen| {
            if (alen != blen) { false }
            else {
                unsafe {
                    libc::memcmp(ap as *libc::c_void,
                                 bp as *libc::c_void,
                                 (alen - 1) as libc::size_t) == 0
                }
            }
        }
    }
}

#[cfg(test)]
pure fn eq_slice(a: &str, b: &str) -> bool {
    do as_buf(a) |ap, alen| {
        do as_buf(b) |bp, blen| {
            if (alen != blen) { false }
            else {
                unsafe {
                    libc::memcmp(ap as *libc::c_void,
                                 bp as *libc::c_void,
                                 (alen - 1) as libc::size_t) == 0
                }
            }
        }
    }
}

/// Bytewise string equality
#[cfg(notest)]
#[lang="uniq_str_eq"]
pure fn eq(a: &~str, b: &~str) -> bool {
    eq_slice(*a, *b)
}

#[cfg(test)]
pure fn eq(a: &~str, b: &~str) -> bool {
    eq_slice(*a, *b)
}

/// Bytewise slice less than
pure fn lt(a: &str, b: &str) -> bool {
    let (a_len, b_len) = (a.len(), b.len());
    let mut end = uint::min(a_len, b_len);

    let mut i = 0;
    while i < end {
        let (c_a, c_b) = (a[i], b[i]);
        if c_a < c_b { return true; }
        if c_a > c_b { return false; }
        i += 1;
    }

    return a_len < b_len;
}

/// Bytewise less than or equal
pure fn le(a: &str, b: &str) -> bool {
    !lt(b, a)
}

/// Bytewise greater than or equal
pure fn ge(a: &str, b: &str) -> bool {
    !lt(a, b)
}

/// Bytewise greater than
pure fn gt(a: &str, b: &str) -> bool {
    !le(a, b)
}

#[cfg(stage0)]
impl &str: Eq {
    #[inline(always)]
    pure fn eq(&&other: &str) -> bool {
        eq_slice(self, other)
    }
    #[inline(always)]
    pure fn ne(&&other: &str) -> bool { !self.eq(other) }
}
#[cfg(stage1)]
#[cfg(stage2)]
impl &str : Eq {
    #[inline(always)]
    pure fn eq(other: & &str) -> bool {
        eq_slice(self, (*other))
    }
    #[inline(always)]
    pure fn ne(other: & &str) -> bool { !self.eq(other) }
}

#[cfg(stage0)]
impl ~str: Eq {
    #[inline(always)]
    pure fn eq(&&other: ~str) -> bool {
        eq_slice(self, other)
    }
    #[inline(always)]
    pure fn ne(&&other: ~str) -> bool { !self.eq(other) }
}
#[cfg(stage1)]
#[cfg(stage2)]
impl ~str : Eq {
    #[inline(always)]
    pure fn eq(other: &~str) -> bool {
        eq_slice(self, (*other))
    }
    #[inline(always)]
    pure fn ne(other: &~str) -> bool { !self.eq(other) }
}

#[cfg(stage0)]
impl @str: Eq {
    #[inline(always)]
    pure fn eq(&&other: @str) -> bool {
        eq_slice(self, other)
    }
    #[inline(always)]
    pure fn ne(&&other: @str) -> bool { !self.eq(other) }
}
#[cfg(stage1)]
#[cfg(stage2)]
impl @str : Eq {
    #[inline(always)]
    pure fn eq(other: &@str) -> bool {
        eq_slice(self, (*other))
    }
    #[inline(always)]
    pure fn ne(other: &@str) -> bool { !self.eq(other) }
}

#[cfg(stage0)]
impl ~str : Ord {
    #[inline(always)]
    pure fn lt(&&other: ~str) -> bool { lt(self, other) }
    #[inline(always)]
    pure fn le(&&other: ~str) -> bool { le(self, other) }
    #[inline(always)]
    pure fn ge(&&other: ~str) -> bool { ge(self, other) }
    #[inline(always)]
    pure fn gt(&&other: ~str) -> bool { gt(self, other) }
}
#[cfg(stage1)]
#[cfg(stage2)]
impl ~str : Ord {
    #[inline(always)]
    pure fn lt(other: &~str) -> bool { lt(self, (*other)) }
    #[inline(always)]
    pure fn le(other: &~str) -> bool { le(self, (*other)) }
    #[inline(always)]
    pure fn ge(other: &~str) -> bool { ge(self, (*other)) }
    #[inline(always)]
    pure fn gt(other: &~str) -> bool { gt(self, (*other)) }
}

#[cfg(stage0)]
impl &str : Ord {
    #[inline(always)]
    pure fn lt(&&other: &str) -> bool { lt(self, other) }
    #[inline(always)]
    pure fn le(&&other: &str) -> bool { le(self, other) }
    #[inline(always)]
    pure fn ge(&&other: &str) -> bool { ge(self, other) }
    #[inline(always)]
    pure fn gt(&&other: &str) -> bool { gt(self, other) }
}
#[cfg(stage1)]
#[cfg(stage2)]
impl &str : Ord {
    #[inline(always)]
    pure fn lt(other: & &str) -> bool { lt(self, (*other)) }
    #[inline(always)]
    pure fn le(other: & &str) -> bool { le(self, (*other)) }
    #[inline(always)]
    pure fn ge(other: & &str) -> bool { ge(self, (*other)) }
    #[inline(always)]
    pure fn gt(other: & &str) -> bool { gt(self, (*other)) }
}

#[cfg(stage0)]
impl @str : Ord {
    #[inline(always)]
    pure fn lt(&&other: @str) -> bool { lt(self, other) }
    #[inline(always)]
    pure fn le(&&other: @str) -> bool { le(self, other) }
    #[inline(always)]
    pure fn ge(&&other: @str) -> bool { ge(self, other) }
    #[inline(always)]
    pure fn gt(&&other: @str) -> bool { gt(self, other) }
}
#[cfg(stage1)]
#[cfg(stage2)]
impl @str : Ord {
    #[inline(always)]
    pure fn lt(other: &@str) -> bool { lt(self, (*other)) }
    #[inline(always)]
    pure fn le(other: &@str) -> bool { le(self, (*other)) }
    #[inline(always)]
    pure fn ge(other: &@str) -> bool { ge(self, (*other)) }
    #[inline(always)]
    pure fn gt(other: &@str) -> bool { gt(self, (*other)) }
}

/*
Section: Iterating through strings
*/

/**
 * Return true if a predicate matches all characters or if the string
 * contains no characters
 */
pure fn all(s: &str, it: fn(char) -> bool) -> bool {
    all_between(s, 0u, len(s), it)
}

/**
 * Return true if a predicate matches any character (and false if it
 * matches none or there are no characters)
 */
pure fn any(ss: &str, pred: fn(char) -> bool) -> bool {
    !all(ss, |cc| !pred(cc))
}

/// Apply a function to each character
pure fn map(ss: &str, ff: fn(char) -> char) -> ~str {
    let mut result = ~"";
    unsafe {
        reserve(result, len(ss));
        for chars_each(ss) |cc| {
            str::push_char(result, ff(cc));
        }
    }
    move result
}

/// Iterate over the bytes in a string
pure fn bytes_each(ss: &str, it: fn(u8) -> bool) {
    let mut pos = 0u;
    let len = len(ss);

    while (pos < len) {
        if !it(ss[pos]) { return; }
        pos += 1u;
    }
}

/// Iterate over the bytes in a string
#[inline(always)]
pure fn each(s: &str, it: fn(u8) -> bool) {
    eachi(s, |_i, b| it(b) )
}

/// Iterate over the bytes in a string, with indices
#[inline(always)]
pure fn eachi(s: &str, it: fn(uint, u8) -> bool) {
    let mut i = 0u, l = len(s);
    while (i < l) {
        if !it(i, s[i]) { break; }
        i += 1u;
    }
}

/// Iterates over the chars in a string
#[inline(always)]
pure fn each_char(s: &str, it: fn(char) -> bool) {
    each_chari(s, |_i, c| it(c))
}

/// Iterates over the chars in a string, with indices
#[inline(always)]
pure fn each_chari(s: &str, it: fn(uint, char) -> bool) {
    let mut pos = 0u, ch_pos = 0u;
    let len = len(s);
    while pos < len {
        let {ch, next} = char_range_at(s, pos);
        pos = next;
        if !it(ch_pos, ch) { break; }
        ch_pos += 1u;
    }
}

/// Iterate over the characters in a string
pure fn chars_each(s: &str, it: fn(char) -> bool) {
    let mut pos = 0u;
    let len = len(s);
    while (pos < len) {
        let {ch, next} = char_range_at(s, pos);
        pos = next;
        if !it(ch) { return; }
    }
}

/// Apply a function to each substring after splitting by character
pure fn split_char_each(ss: &str, cc: char, ff: fn(v: &str) -> bool) {
    vec::each(split_char(ss, cc), |s| ff(*s))
}

/**
 * Apply a function to each substring after splitting by character, up to
 * `count` times
 */
pure fn splitn_char_each(ss: &str, sep: char, count: uint,
                         ff: fn(v: &str) -> bool) {
    vec::each(splitn_char(ss, sep, count), |s| ff(*s))
}

/// Apply a function to each word
pure fn words_each(ss: &str, ff: fn(v: &str) -> bool) {
    vec::each(words(ss), |s| ff(*s))
}

/**
 * Apply a function to each line (by '\n')
 */
pure fn lines_each(ss: &str, ff: fn(v: &str) -> bool) {
    vec::each(lines(ss), |s| ff(*s))
}

/*
Section: Searching
*/

/**
 * Returns the byte index of the first matching character
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `c` - The character to search for
 *
 * # Return value
 *
 * An `option` containing the byte index of the first matching character
 * or `none` if there is no match
 */
pure fn find_char(s: &str, c: char) -> Option<uint> {
    find_char_between(s, c, 0u, len(s))
}

/**
 * Returns the byte index of the first matching character beginning
 * from a given byte offset
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `c` - The character to search for
 * * `start` - The byte index to begin searching at, inclusive
 *
 * # Return value
 *
 * An `option` containing the byte index of the first matching character
 * or `none` if there is no match
 *
 * # Failure
 *
 * `start` must be less than or equal to `len(s)`. `start` must be the
 * index of a character boundary, as defined by `is_char_boundary`.
 */
pure fn find_char_from(s: &str, c: char, start: uint) -> Option<uint> {
    find_char_between(s, c, start, len(s))
}

/**
 * Returns the byte index of the first matching character within a given range
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `c` - The character to search for
 * * `start` - The byte index to begin searching at, inclusive
 * * `end` - The byte index to end searching at, exclusive
 *
 * # Return value
 *
 * An `option` containing the byte index of the first matching character
 * or `none` if there is no match
 *
 * # Failure
 *
 * `start` must be less than or equal to `end` and `end` must be less than
 * or equal to `len(s)`. `start` must be the index of a character boundary,
 * as defined by `is_char_boundary`.
 */
pure fn find_char_between(s: &str, c: char, start: uint, end: uint)
    -> Option<uint> {
    if c < 128u as char {
        assert start <= end;
        assert end <= len(s);
        let mut i = start;
        let b = c as u8;
        while i < end {
            if s[i] == b { return Some(i); }
            i += 1u;
        }
        return None;
    } else {
        find_between(s, start, end, |x| x == c)
    }
}

/**
 * Returns the byte index of the last matching character
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `c` - The character to search for
 *
 * # Return value
 *
 * An `option` containing the byte index of the last matching character
 * or `none` if there is no match
 */
pure fn rfind_char(s: &str, c: char) -> Option<uint> {
    rfind_char_between(s, c, len(s), 0u)
}

/**
 * Returns the byte index of the last matching character beginning
 * from a given byte offset
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `c` - The character to search for
 * * `start` - The byte index to begin searching at, exclusive
 *
 * # Return value
 *
 * An `option` containing the byte index of the last matching character
 * or `none` if there is no match
 *
 * # Failure
 *
 * `start` must be less than or equal to `len(s)`. `start` must be
 * the index of a character boundary, as defined by `is_char_boundary`.
 */
pure fn rfind_char_from(s: &str, c: char, start: uint) -> Option<uint> {
    rfind_char_between(s, c, start, 0u)
}

/**
 * Returns the byte index of the last matching character within a given range
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `c` - The character to search for
 * * `start` - The byte index to begin searching at, exclusive
 * * `end` - The byte index to end searching at, inclusive
 *
 * # Return value
 *
 * An `option` containing the byte index of the last matching character
 * or `none` if there is no match
 *
 * # Failure
 *
 * `end` must be less than or equal to `start` and `start` must be less than
 * or equal to `len(s)`. `start` must be the index of a character boundary,
 * as defined by `is_char_boundary`.
 */
pure fn rfind_char_between(s: &str, c: char, start: uint, end: uint)
    -> Option<uint> {
    if c < 128u as char {
        assert start >= end;
        assert start <= len(s);
        let mut i = start;
        let b = c as u8;
        while i > end {
            i -= 1u;
            if s[i] == b { return Some(i); }
        }
        return None;
    } else {
        rfind_between(s, start, end, |x| x == c)
    }
}

/**
 * Returns the byte index of the first character that satisfies
 * the given predicate
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `f` - The predicate to satisfy
 *
 * # Return value
 *
 * An `option` containing the byte index of the first matching character
 * or `none` if there is no match
 */
pure fn find(s: &str, f: fn(char) -> bool) -> Option<uint> {
    find_between(s, 0u, len(s), f)
}

/**
 * Returns the byte index of the first character that satisfies
 * the given predicate, beginning from a given byte offset
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `start` - The byte index to begin searching at, inclusive
 * * `f` - The predicate to satisfy
 *
 * # Return value
 *
 * An `option` containing the byte index of the first matching charactor
 * or `none` if there is no match
 *
 * # Failure
 *
 * `start` must be less than or equal to `len(s)`. `start` must be the
 * index of a character boundary, as defined by `is_char_boundary`.
 */
pure fn find_from(s: &str, start: uint, f: fn(char)
    -> bool) -> Option<uint> {
    find_between(s, start, len(s), f)
}

/**
 * Returns the byte index of the first character that satisfies
 * the given predicate, within a given range
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `start` - The byte index to begin searching at, inclusive
 * * `end` - The byte index to end searching at, exclusive
 * * `f` - The predicate to satisfy
 *
 * # Return value
 *
 * An `option` containing the byte index of the first matching character
 * or `none` if there is no match
 *
 * # Failure
 *
 * `start` must be less than or equal to `end` and `end` must be less than
 * or equal to `len(s)`. `start` must be the index of a character
 * boundary, as defined by `is_char_boundary`.
 */
pure fn find_between(s: &str, start: uint, end: uint, f: fn(char) -> bool)
    -> Option<uint> {
    assert start <= end;
    assert end <= len(s);
    assert is_char_boundary(s, start);
    let mut i = start;
    while i < end {
        let {ch, next} = char_range_at(s, i);
        if f(ch) { return Some(i); }
        i = next;
    }
    return None;
}

/**
 * Returns the byte index of the last character that satisfies
 * the given predicate
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `f` - The predicate to satisfy
 *
 * # Return value
 *
 * An option containing the byte index of the last matching character
 * or `none` if there is no match
 */
pure fn rfind(s: &str, f: fn(char) -> bool) -> Option<uint> {
    rfind_between(s, len(s), 0u, f)
}

/**
 * Returns the byte index of the last character that satisfies
 * the given predicate, beginning from a given byte offset
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `start` - The byte index to begin searching at, exclusive
 * * `f` - The predicate to satisfy
 *
 * # Return value
 *
 * An `option` containing the byte index of the last matching character
 * or `none` if there is no match
 *
 * # Failure
 *
 * `start` must be less than or equal to `len(s)', `start` must be the
 * index of a character boundary, as defined by `is_char_boundary`
 */
pure fn rfind_from(s: &str, start: uint, f: fn(char) -> bool)
    -> Option<uint> {
    rfind_between(s, start, 0u, f)
}

/**
 * Returns the byte index of the last character that satisfies
 * the given predicate, within a given range
 *
 * # Arguments
 *
 * * `s` - The string to search
 * * `start` - The byte index to begin searching at, exclusive
 * * `end` - The byte index to end searching at, inclusive
 * * `f` - The predicate to satisfy
 *
 * # Return value
 *
 * An `option` containing the byte index of the last matching character
 * or `none` if there is no match
 *
 * # Failure
 *
 * `end` must be less than or equal to `start` and `start` must be less
 * than or equal to `len(s)`. `start` must be the index of a character
 * boundary, as defined by `is_char_boundary`
 */
pure fn rfind_between(s: &str, start: uint, end: uint, f: fn(char) -> bool)
    -> Option<uint> {
    assert start >= end;
    assert start <= len(s);
    assert is_char_boundary(s, start);
    let mut i = start;
    while i > end {
        let {ch, prev} = char_range_at_reverse(s, i);
        if f(ch) { return Some(prev); }
        i = prev;
    }
    return None;
}

// Utility used by various searching functions
pure fn match_at(haystack: &a/str, needle: &b/str, at: uint) -> bool {
    let mut i = at;
    for each(needle) |c| { if haystack[i] != c { return false; } i += 1u; }
    return true;
}

/**
 * Returns the byte index of the first matching substring
 *
 * # Arguments
 *
 * * `haystack` - The string to search
 * * `needle` - The string to search for
 *
 * # Return value
 *
 * An `option` containing the byte index of the first matching substring
 * or `none` if there is no match
 */
pure fn find_str(haystack: &a/str, needle: &b/str) -> Option<uint> {
    find_str_between(haystack, needle, 0u, len(haystack))
}

/**
 * Returns the byte index of the first matching substring beginning
 * from a given byte offset
 *
 * # Arguments
 *
 * * `haystack` - The string to search
 * * `needle` - The string to search for
 * * `start` - The byte index to begin searching at, inclusive
 *
 * # Return value
 *
 * An `option` containing the byte index of the last matching character
 * or `none` if there is no match
 *
 * # Failure
 *
 * `start` must be less than or equal to `len(s)`
 */
pure fn find_str_from(haystack: &a/str, needle: &b/str, start: uint)
  -> Option<uint> {
    find_str_between(haystack, needle, start, len(haystack))
}

/**
 * Returns the byte index of the first matching substring within a given range
 *
 * # Arguments
 *
 * * `haystack` - The string to search
 * * `needle` - The string to search for
 * * `start` - The byte index to begin searching at, inclusive
 * * `end` - The byte index to end searching at, exclusive
 *
 * # Return value
 *
 * An `option` containing the byte index of the first matching character
 * or `none` if there is no match
 *
 * # Failure
 *
 * `start` must be less than or equal to `end` and `end` must be less than
 * or equal to `len(s)`.
 */
pure fn find_str_between(haystack: &a/str, needle: &b/str, start: uint,
                         end:uint)
  -> Option<uint> {
    // See Issue #1932 for why this is a naive search
    assert end <= len(haystack);
    let needle_len = len(needle);
    if needle_len == 0u { return Some(start); }
    if needle_len > end { return None; }

    let mut i = start;
    let e = end - needle_len;
    while i <= e {
        if match_at(haystack, needle, i) { return Some(i); }
        i += 1u;
    }
    return None;
}

/**
 * Returns true if one string contains another
 *
 * # Arguments
 *
 * * haystack - The string to look in
 * * needle - The string to look for
 */
pure fn contains(haystack: &a/str, needle: &b/str) -> bool {
    option::is_some(find_str(haystack, needle))
}

/**
 * Returns true if a string contains a char.
 *
 * # Arguments
 *
 * * haystack - The string to look in
 * * needle - The char to look for
 */
pure fn contains_char(haystack: &str, needle: char) -> bool {
    option::is_some(find_char(haystack, needle))
}

/**
 * Returns true if one string starts with another
 *
 * # Arguments
 *
 * * haystack - The string to look in
 * * needle - The string to look for
 */
pure fn starts_with(haystack: &a/str, needle: &b/str) -> bool {
    let haystack_len = len(haystack), needle_len = len(needle);
    if needle_len == 0u { true }
    else if needle_len > haystack_len { false }
    else { match_at(haystack, needle, 0u) }
}

/**
 * Returns true if one string ends with another
 *
 * # Arguments
 *
 * * haystack - The string to look in
 * * needle - The string to look for
 */
pure fn ends_with(haystack: &a/str, needle: &b/str) -> bool {
    let haystack_len = len(haystack), needle_len = len(needle);
    if needle_len == 0u { true }
    else if needle_len > haystack_len { false }
    else { match_at(haystack, needle, haystack_len - needle_len) }
}

/*
Section: String properties
*/

/// Determines if a string contains only ASCII characters
pure fn is_ascii(s: &str) -> bool {
    let mut i: uint = len(s);
    while i > 0u { i -= 1u; if !u8::is_ascii(s[i]) { return false; } }
    return true;
}

/// Returns true if the string has length 0
pure fn is_empty(s: &str) -> bool { len(s) == 0u }

/// Returns true if the string has length greater than 0
pure fn is_not_empty(s: &str) -> bool { !is_empty(s) }

/**
 * Returns true if the string contains only whitespace
 *
 * Whitespace characters are determined by `char::is_whitespace`
 */
pure fn is_whitespace(s: &str) -> bool {
    return all(s, char::is_whitespace);
}

/**
 * Returns true if the string contains only alphanumerics
 *
 * Alphanumeric characters are determined by `char::is_alphanumeric`
 */
fn is_alphanumeric(s: &str) -> bool {
    return all(s, char::is_alphanumeric);
}

/// Returns the string length/size in bytes not counting the null terminator
pure fn len(s: &str) -> uint {
    do as_buf(s) |_p, n| { n - 1u }
}

/// Returns the number of characters that a string holds
pure fn char_len(s: &str) -> uint { count_chars(s, 0u, len(s)) }

/*
Section: Misc
*/

/// Determines if a vector of bytes contains valid UTF-8
pure fn is_utf8(v: &[const u8]) -> bool {
    let mut i = 0u;
    let total = vec::len::<u8>(v);
    while i < total {
        let mut chsize = utf8_char_width(v[i]);
        if chsize == 0u { return false; }
        if i + chsize > total { return false; }
        i += 1u;
        while chsize > 1u {
            if v[i] & 192u8 != tag_cont_u8 { return false; }
            i += 1u;
            chsize -= 1u;
        }
    }
    return true;
}

/// Determines if a vector of `u16` contains valid UTF-16
pure fn is_utf16(v: &[u16]) -> bool {
    let len = vec::len(v);
    let mut i = 0u;
    while (i < len) {
        let u = v[i];

        if  u <= 0xD7FF_u16 || u >= 0xE000_u16 {
            i += 1u;

        } else {
            if i+1u < len { return false; }
            let u2 = v[i+1u];
            if u < 0xD7FF_u16 || u > 0xDBFF_u16 { return false; }
            if u2 < 0xDC00_u16 || u2 > 0xDFFF_u16 { return false; }
            i += 2u;
        }
    }
    return true;
}

/// Converts to a vector of `u16` encoded as UTF-16
pure fn to_utf16(s: &str) -> ~[u16] {
    let mut u = ~[];
    for chars_each(s) |cch| {
        // Arithmetic with u32 literals is easier on the eyes than chars.
        let mut ch = cch as u32;

        if (ch & 0xFFFF_u32) == ch unsafe {
            // The BMP falls through (assuming non-surrogate, as it should)
            assert ch <= 0xD7FF_u32 || ch >= 0xE000_u32;
            vec::push(u, ch as u16)
        } else unsafe {
            // Supplementary planes break into surrogates.
            assert ch >= 0x1_0000_u32 && ch <= 0x10_FFFF_u32;
            ch -= 0x1_0000_u32;
            let w1 = 0xD800_u16 | ((ch >> 10) as u16);
            let w2 = 0xDC00_u16 | ((ch as u16) & 0x3FF_u16);
            vec::push_all(u, ~[w1, w2])
        }
    }
    move u
}

pure fn utf16_chars(v: &[u16], f: fn(char)) {
    let len = vec::len(v);
    let mut i = 0u;
    while (i < len && v[i] != 0u16) {
        let mut u = v[i];

        if  u <= 0xD7FF_u16 || u >= 0xE000_u16 {
            f(u as char);
            i += 1u;

        } else {
            let u2 = v[i+1u];
            assert u >= 0xD800_u16 && u <= 0xDBFF_u16;
            assert u2 >= 0xDC00_u16 && u2 <= 0xDFFF_u16;
            let mut c = (u - 0xD800_u16) as char;
            c = c << 10;
            c |= (u2 - 0xDC00_u16) as char;
            c |= 0x1_0000_u32 as char;
            f(c);
            i += 2u;
        }
    }
}


pure fn from_utf16(v: &[u16]) -> ~str {
    let mut buf = ~"";
    unsafe {
        reserve(buf, vec::len(v));
        utf16_chars(v, |ch| push_char(buf, ch));
    }
    move buf
}


/**
 * As char_len but for a slice of a string
 *
 * # Arguments
 *
 * * s - A valid string
 * * start - The position inside `s` where to start counting in bytes
 * * end - The position where to stop counting
 *
 * # Return value
 *
 * The number of Unicode characters in `s` between the given indices.
 */
pure fn count_chars(s: &str, start: uint, end: uint) -> uint {
    assert is_char_boundary(s, start);
    assert is_char_boundary(s, end);
    let mut i = start, len = 0u;
    while i < end {
        let {next, _} = char_range_at(s, i);
        len += 1u;
        i = next;
    }
    return len;
}

/// Counts the number of bytes taken by the `n` in `s` starting from `start`.
pure fn count_bytes(s: &b/str, start: uint, n: uint) -> uint {
    assert is_char_boundary(s, start);
    let mut end = start, cnt = n;
    let l = len(s);
    while cnt > 0u {
        assert end < l;
        let {next, _} = char_range_at(s, end);
        cnt -= 1u;
        end = next;
    }
    end - start
}

/// Given a first byte, determine how many bytes are in this UTF-8 character
pure fn utf8_char_width(b: u8) -> uint {
    let byte: uint = b as uint;
    if byte < 128u { return 1u; }
    // Not a valid start byte
    if byte < 192u { return 0u; }
    if byte < 224u { return 2u; }
    if byte < 240u { return 3u; }
    if byte < 248u { return 4u; }
    if byte < 252u { return 5u; }
    return 6u;
}

/**
 * Returns false if the index points into the middle of a multi-byte
 * character sequence.
 */
pure fn is_char_boundary(s: &str, index: uint) -> bool {
    if index == len(s) { return true; }
    let b = s[index];
    return b < 128u8 || b >= 192u8;
}

/**
 * Pluck a character out of a string and return the index of the next
 * character.
 *
 * This function can be used to iterate over the unicode characters of a
 * string.
 *
 * # Example
 *
 * ~~~
 * let s = "中华Việt Nam";
 * let i = 0u;
 * while i < str::len(s) {
 *     let {ch, next} = str::char_range_at(s, i);
 *     std::io::println(fmt!("%u: %c",i,ch));
 *     i = next;
 * }
 * ~~~
 *
 * # Example output
 *
 * ~~~
 * 0: 中
 * 3: 华
 * 6: V
 * 7: i
 * 8: ệ
 * 11: t
 * 12:
 * 13: N
 * 14: a
 * 15: m
 * ~~~
 *
 * # Arguments
 *
 * * s - The string
 * * i - The byte offset of the char to extract
 *
 * # Return value
 *
 * A record {ch: char, next: uint} containing the char value and the byte
 * index of the next unicode character.
 *
 * # Failure
 *
 * If `i` is greater than or equal to the length of the string.
 * If `i` is not the index of the beginning of a valid UTF-8 character.
 */
pure fn char_range_at(s: &str, i: uint) -> {ch: char, next: uint} {
    let b0 = s[i];
    let w = utf8_char_width(b0);
    assert (w != 0u);
    if w == 1u { return {ch: b0 as char, next: i + 1u}; }
    let mut val = 0u;
    let end = i + w;
    let mut i = i + 1u;
    while i < end {
        let byte = s[i];
        assert (byte & 192u8 == tag_cont_u8);
        val <<= 6u;
        val += (byte & 63u8) as uint;
        i += 1u;
    }
    // Clunky way to get the right bits from the first byte. Uses two shifts,
    // the first to clip off the marker bits at the left of the byte, and then
    // a second (as uint) to get it to the right position.
    val += ((b0 << ((w + 1u) as u8)) as uint) << ((w - 1u) * 6u - w - 1u);
    return {ch: val as char, next: i};
}

/// Pluck a character out of a string
pure fn char_at(s: &str, i: uint) -> char { return char_range_at(s, i).ch; }

/**
 * Given a byte position and a str, return the previous char and its position
 *
 * This function can be used to iterate over a unicode string in reverse.
 */
pure fn char_range_at_reverse(ss: &str, start: uint)
    -> {ch: char, prev: uint} {

    let mut prev = start;

    // while there is a previous byte == 10......
    while prev > 0u && ss[prev - 1u] & 192u8 == tag_cont_u8 {
        prev -= 1u;
    }

    // now refer to the initial byte of previous char
    prev -= 1u;

    let ch = char_at(ss, prev);
    return {ch:ch, prev:prev};
}

/**
 * Loop through a substring, char by char
 *
 * # Safety note
 *
 * * This function does not check whether the substring is valid.
 * * This function fails if `start` or `end` do not
 *   represent valid positions inside `s`
 *
 * # Arguments
 *
 * * s - A string to traverse. It may be empty.
 * * start - The byte offset at which to start in the string.
 * * end - The end of the range to traverse
 * * it - A block to execute with each consecutive character of `s`.
 *        Return `true` to continue, `false` to stop.
 *
 * # Return value
 *
 * `true` If execution proceeded correctly, `false` if it was interrupted,
 * that is if `it` returned `false` at any point.
 */
pure fn all_between(s: &str, start: uint, end: uint,
                    it: fn(char) -> bool) -> bool {
    assert is_char_boundary(s, start);
    let mut i = start;
    while i < end {
        let {ch, next} = char_range_at(s, i);
        if !it(ch) { return false; }
        i = next;
    }
    return true;
}

/**
 * Loop through a substring, char by char
 *
 * # Safety note
 *
 * * This function does not check whether the substring is valid.
 * * This function fails if `start` or `end` do not
 *   represent valid positions inside `s`
 *
 * # Arguments
 *
 * * s - A string to traverse. It may be empty.
 * * start - The byte offset at which to start in the string.
 * * end - The end of the range to traverse
 * * it - A block to execute with each consecutive character of `s`.
 *        Return `true` to continue, `false` to stop.
 *
 * # Return value
 *
 * `true` if `it` returns `true` for any character
 */
pure fn any_between(s: &str, start: uint, end: uint,
                    it: fn(char) -> bool) -> bool {
    !all_between(s, start, end, |c| !it(c))
}

// UTF-8 tags and ranges
const tag_cont_u8: u8 = 128u8;
const tag_cont: uint = 128u;
const max_one_b: uint = 128u;
const tag_two_b: uint = 192u;
const max_two_b: uint = 2048u;
const tag_three_b: uint = 224u;
const max_three_b: uint = 65536u;
const tag_four_b: uint = 240u;
const max_four_b: uint = 2097152u;
const tag_five_b: uint = 248u;
const max_five_b: uint = 67108864u;
const tag_six_b: uint = 252u;


/**
 * Work with the byte buffer of a string.
 *
 * Allows for unsafe manipulation of strings, which is useful for foreign
 * interop.
 *
 * # Example
 *
 * ~~~
 * let i = str::as_bytes("Hello World") { |bytes| vec::len(bytes) };
 * ~~~
 */
pure fn as_bytes<T>(s: ~str, f: fn(~[u8]) -> T) -> T {
    unsafe {
        let v: *~[u8] = ::cast::reinterpret_cast(&ptr::addr_of(s));
        f(*v)
    }
}

/**
 * Work with the byte buffer of a string as a byte slice.
 *
 * The byte slice does not include the null terminator.
 */
pure fn as_bytes_slice(s: &a/str) -> &a/[u8] {
    unsafe {
        let (ptr, len): (*u8, uint) = ::cast::reinterpret_cast(&s);
        let outgoing_tuple: (*u8, uint) = (ptr, len - 1);
        return ::cast::reinterpret_cast(&outgoing_tuple);
    }
}

/**
 * Work with the byte buffer of a string as a null-terminated C string.
 *
 * Allows for unsafe manipulation of strings, which is useful for foreign
 * interop. This is similar to `str::as_buf`, but guarantees null-termination.
 * If the given slice is not already null-terminated, this function will
 * allocate a temporary, copy the slice, null terminate it, and pass
 * that instead.
 *
 * # Example
 *
 * ~~~
 * let s = str::as_c_str("PATH", { |path| libc::getenv(path) });
 * ~~~
 */
pure fn as_c_str<T>(s: &str, f: fn(*libc::c_char) -> T) -> T {
    do as_buf(s) |buf, len| {
        // NB: len includes the trailing null.
        assert len > 0;
        if unsafe { *(ptr::offset(buf,len-1)) != 0 } {
            as_c_str(from_slice(s), f)
        } else {
            f(buf as *libc::c_char)
        }
    }
}


/**
 * Work with the byte buffer and length of a slice.
 *
 * The given length is one byte longer than the 'official' indexable
 * length of the string. This is to permit probing the byte past the
 * indexable area for a null byte, as is the case in slices pointing
 * to full strings, or suffixes of them.
 */
#[inline(always)]
pure fn as_buf<T>(s: &str, f: fn(*u8, uint) -> T) -> T {
    unsafe {
        let v : *(*u8,uint) = ::cast::reinterpret_cast(&ptr::addr_of(s));
        let (buf,len) = *v;
        f(buf, len)
    }
}

/**
 * Reserves capacity for exactly `n` bytes in the given string, not including
 * the null terminator.
 *
 * Assuming single-byte characters, the resulting string will be large
 * enough to hold a string of length `n`. To account for the null terminator,
 * the underlying buffer will have the size `n` + 1.
 *
 * If the capacity for `s` is already equal to or greater than the requested
 * capacity, then no action is taken.
 *
 * # Arguments
 *
 * * s - A string
 * * n - The number of bytes to reserve space for
 */
fn reserve(&s: ~str, n: uint) {
    unsafe {
        let v: *mut ~[u8] = ::cast::reinterpret_cast(&ptr::addr_of(s));
        vec::reserve(*v, n + 1);
    }
}

/**
 * Reserves capacity for at least `n` bytes in the given string, not including
 * the null terminator.
 *
 * Assuming single-byte characters, the resulting string will be large
 * enough to hold a string of length `n`. To account for the null terminator,
 * the underlying buffer will have the size `n` + 1.
 *
 * This function will over-allocate in order to amortize the allocation costs
 * in scenarios where the caller may need to repeatedly reserve additional
 * space.
 *
 * If the capacity for `s` is already equal to or greater than the requested
 * capacity, then no action is taken.
 *
 * # Arguments
 *
 * * s - A string
 * * n - The number of bytes to reserve space for
 */
fn reserve_at_least(&s: ~str, n: uint) {
    reserve(s, uint::next_power_of_two(n + 1u) - 1u)
}

/**
 * Returns the number of single-byte characters the string can hold without
 * reallocating
 */
pure fn capacity(&&s: ~str) -> uint {
    do as_bytes(s) |buf| {
        let vcap = vec::capacity(buf);
        assert vcap > 0u;
        vcap - 1u
    }
}

/// Escape each char in `s` with char::escape_default.
pure fn escape_default(s: &str) -> ~str {
    let mut out: ~str = ~"";
    unsafe {
        reserve_at_least(out, str::len(s));
        for chars_each(s) |c| {
            push_str(out, char::escape_default(c));
        }
    }
    move out
}

/// Escape each char in `s` with char::escape_unicode.
pure fn escape_unicode(s: &str) -> ~str {
    let mut out: ~str = ~"";
    unsafe {
        reserve_at_least(out, str::len(s));
        for chars_each(s) |c| {
            push_str(out, char::escape_unicode(c));
        }
    }
    move out
}

/// Unsafe operations
mod raw {
   export
      from_buf,
      from_buf_len,
      from_c_str,
      from_c_str_len,
      from_bytes,
      buf_as_slice,
      slice_bytes,
      view_bytes,
      push_byte,
      pop_byte,
      shift_byte,
      set_len;

    /// Create a Rust string from a null-terminated *u8 buffer
    unsafe fn from_buf(buf: *u8) -> ~str {
        let mut curr = buf, i = 0u;
        while *curr != 0u8 {
            i += 1u;
            curr = ptr::offset(buf, i);
        }
        return from_buf_len(buf, i);
    }

    /// Create a Rust string from a *u8 buffer of the given length
    unsafe fn from_buf_len(buf: *const u8, len: uint) -> ~str {
        let mut v: ~[mut u8] = ~[mut];
        vec::reserve(v, len + 1u);
        vec::as_imm_buf(v, |vbuf, _len| {
            let vbuf = ::cast::transmute_mut_unsafe(vbuf);
            ptr::memcpy(vbuf, buf as *u8, len)
        });
        vec::raw::set_len(v, len);
        vec::push(v, 0u8);

        assert is_utf8(v);
        return ::cast::transmute(move v);
    }

    /// Create a Rust string from a null-terminated C string
    unsafe fn from_c_str(c_str: *libc::c_char) -> ~str {
        from_buf(::cast::reinterpret_cast(&c_str))
    }

    /// Create a Rust string from a `*c_char` buffer of the given length
    unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> ~str {
        from_buf_len(::cast::reinterpret_cast(&c_str), len)
    }

    /// Converts a vector of bytes to a string.
    unsafe fn from_bytes(v: &[const u8]) -> ~str {
        do vec::as_const_buf(v) |buf, len| {
            from_buf_len(buf, len)
        }
    }

    /// Converts a byte to a string.
    unsafe fn from_byte(u: u8) -> ~str { raw::from_bytes([u]) }

    /// Form a slice from a *u8 buffer of the given length without copying.
    unsafe fn buf_as_slice<T>(buf: *u8, len: uint, f: fn(&& &str) -> T) -> T {
        let v = (buf, len + 1);
        assert is_utf8(::cast::reinterpret_cast(&v));
        f(::cast::transmute(move v))
    }

    /**
     * Takes a bytewise (not UTF-8) slice from a string.
     *
     * Returns the substring from [`begin`..`end`).
     *
     * # Failure
     *
     * If begin is greater than end.
     * If end is greater than the length of the string.
     */
    unsafe fn slice_bytes(s: &str, begin: uint, end: uint) -> ~str {
        do as_buf(s) |sbuf, n| {
            assert (begin <= end);
            assert (end <= n);

            let mut v = ~[];
            vec::reserve(v, end - begin + 1u);
            unsafe {
                do vec::as_imm_buf(v) |vbuf, _vlen| {
                    let vbuf = ::cast::transmute_mut_unsafe(vbuf);
                    let src = ptr::offset(sbuf, begin);
                    ptr::memcpy(vbuf, src, end - begin);
                }
                vec::raw::set_len(v, end - begin);
                vec::push(v, 0u8);
                ::cast::transmute(move v)
            }
        }
    }

    /**
     * Takes a bytewise (not UTF-8) view from a string.
     *
     * Returns the substring from [`begin`..`end`).
     *
     * # Failure
     *
     * If begin is greater than end.
     * If end is greater than the length of the string.
     */
    #[inline]
    unsafe fn view_bytes(s: &str, begin: uint, end: uint) -> &str {
        do as_buf(s) |sbuf, n| {
             assert (begin <= end);
             assert (end <= n);

             let tuple = (ptr::offset(sbuf, begin), end - begin + 1);
             ::cast::reinterpret_cast(&tuple)
        }
    }

    /// Appends a byte to a string. (Not UTF-8 safe).
    unsafe fn push_byte(&s: ~str, b: u8) {
        reserve_at_least(s, s.len() + 1);
        do as_buf(s) |buf, len| {
            let buf: *mut u8 = ::cast::reinterpret_cast(&buf);
            *ptr::mut_offset(buf, len) = b;
        }
        set_len(s, s.len() + 1);
    }

    /// Appends a vector of bytes to a string. (Not UTF-8 safe).
    unsafe fn push_bytes(&s: ~str, bytes: ~[u8]) {
        reserve_at_least(s, s.len() + bytes.len());
        for vec::each(bytes) |byte| { push_byte(s, *byte); }
    }

    /// Removes the last byte from a string and returns it. (Not UTF-8 safe).
    unsafe fn pop_byte(&s: ~str) -> u8 {
        let len = len(s);
        assert (len > 0u);
        let b = s[len - 1u];
        unsafe { set_len(s, len - 1u) };
        return b;
    }

    /// Removes the first byte from a string and returns it. (Not UTF-8 safe).
    unsafe fn shift_byte(&s: ~str) -> u8 {
        let len = len(s);
        assert (len > 0u);
        let b = s[0];
        s = unsafe { raw::slice_bytes(s, 1u, len) };
        return b;
    }

    /// Sets the length of the string and adds the null terminator
    unsafe fn set_len(&v: ~str, new_len: uint) {
        let repr: *vec::raw::VecRepr = ::cast::reinterpret_cast(&v);
        (*repr).unboxed.fill = new_len + 1u;
        let null = ptr::mut_offset(ptr::mut_addr_of((*repr).unboxed.data),
                                   new_len);
        *null = 0u8;
    }

    #[test]
    fn test_from_buf_len() {
        unsafe {
            let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
            let b = vec::raw::to_ptr(a);
            let c = from_buf_len(b, 3u);
            assert (c == ~"AAA");
        }
    }

}

trait UniqueStr {
    fn trim() -> self;
    fn trim_left() -> self;
    fn trim_right() -> self;
}

/// Extension methods for strings
impl ~str: UniqueStr {
    /// Returns a string with leading and trailing whitespace removed
    #[inline]
    fn trim() -> ~str { trim(self) }
    /// Returns a string with leading whitespace removed
    #[inline]
    fn trim_left() -> ~str { trim_left(self) }
    /// Returns a string with trailing whitespace removed
    #[inline]
    fn trim_right() -> ~str { trim_right(self) }
}

#[cfg(notest)]
mod traits {
    #[cfg(stage0)]
    impl ~str: Add<&str,~str> {
        #[inline(always)]
        pure fn add(rhs: &str) -> ~str {
            append(copy self, rhs)
        }
    }
    #[cfg(stage1)]
    #[cfg(stage2)]
    impl ~str : Add<&str,~str> {
        #[inline(always)]
        pure fn add(rhs: & &str) -> ~str {
            append(copy self, (*rhs))
        }
    }
}

#[cfg(test)]
mod traits {}

trait StrSlice {
    fn all(it: fn(char) -> bool) -> bool;
    fn any(it: fn(char) -> bool) -> bool;
    fn contains(needle: &a/str) -> bool;
    fn contains_char(needle: char) -> bool;
    fn each(it: fn(u8) -> bool);
    fn eachi(it: fn(uint, u8) -> bool);
    fn each_char(it: fn(char) -> bool);
    fn each_chari(it: fn(uint, char) -> bool);
    fn ends_with(needle: &str) -> bool;
    fn is_empty() -> bool;
    fn is_not_empty() -> bool;
    fn is_whitespace() -> bool;
    fn is_alphanumeric() -> bool;
    pure fn len() -> uint;
    pure fn slice(begin: uint, end: uint) -> ~str;
    fn split(sepfn: fn(char) -> bool) -> ~[~str];
    fn split_char(sep: char) -> ~[~str];
    fn split_str(sep: &a/str) -> ~[~str];
    fn starts_with(needle: &a/str) -> bool;
    fn substr(begin: uint, n: uint) -> ~str;
    pure fn to_lower() -> ~str;
    pure fn to_upper() -> ~str;
    fn escape_default() -> ~str;
    fn escape_unicode() -> ~str;
    pure fn to_unique() -> ~str;
    pure fn char_at(i: uint) -> char;
}

/// Extension methods for strings
impl &str: StrSlice {
    /**
     * Return true if a predicate matches all characters or if the string
     * contains no characters
     */
    #[inline]
    fn all(it: fn(char) -> bool) -> bool { all(self, it) }
    /**
     * Return true if a predicate matches any character (and false if it
     * matches none or there are no characters)
     */
    #[inline]
    fn any(it: fn(char) -> bool) -> bool { any(self, it) }
    /// Returns true if one string contains another
    #[inline]
    fn contains(needle: &a/str) -> bool { contains(self, needle) }
    /// Returns true if a string contains a char
    #[inline]
    fn contains_char(needle: char) -> bool { contains_char(self, needle) }
    /// Iterate over the bytes in a string
    #[inline]
    fn each(it: fn(u8) -> bool) { each(self, it) }
    /// Iterate over the bytes in a string, with indices
    #[inline]
    fn eachi(it: fn(uint, u8) -> bool) { eachi(self, it) }
    /// Iterate over the chars in a string
    #[inline]
    fn each_char(it: fn(char) -> bool) { each_char(self, it) }
    /// Iterate over the chars in a string, with indices
    #[inline]
    fn each_chari(it: fn(uint, char) -> bool) { each_chari(self, it) }
    /// Returns true if one string ends with another
    #[inline]
    fn ends_with(needle: &str) -> bool { ends_with(self, needle) }
    /// Returns true if the string has length 0
    #[inline]
    fn is_empty() -> bool { is_empty(self) }
    /// Returns true if the string has length greater than 0
    #[inline]
    fn is_not_empty() -> bool { is_not_empty(self) }
    /**
     * Returns true if the string contains only whitespace
     *
     * Whitespace characters are determined by `char::is_whitespace`
     */
    #[inline]
    fn is_whitespace() -> bool { is_whitespace(self) }
    /**
     * Returns true if the string contains only alphanumerics
     *
     * Alphanumeric characters are determined by `char::is_alphanumeric`
     */
    #[inline]
    fn is_alphanumeric() -> bool { is_alphanumeric(self) }
    #[inline]
    /// Returns the size in bytes not counting the null terminator
    pure fn len() -> uint { len(self) }
    /**
     * Returns a slice of the given string from the byte range
     * [`begin`..`end`)
     *
     * Fails when `begin` and `end` do not point to valid characters or
     * beyond the last character of the string
     */
    #[inline]
    pure fn slice(begin: uint, end: uint) -> ~str { slice(self, begin, end) }
    /// Splits a string into substrings using a character function
    #[inline]
    fn split(sepfn: fn(char) -> bool) -> ~[~str] { split(self, sepfn) }
    /**
     * Splits a string into substrings at each occurrence of a given character
     */
    #[inline]
    fn split_char(sep: char) -> ~[~str] { split_char(self, sep) }
    /**
     * Splits a string into a vector of the substrings separated by a given
     * string
     */
    #[inline]
    fn split_str(sep: &a/str) -> ~[~str] { split_str(self, sep) }
    /// Returns true if one string starts with another
    #[inline]
    fn starts_with(needle: &a/str) -> bool { starts_with(self, needle) }
    /**
     * Take a substring of another.
     *
     * Returns a string containing `n` characters starting at byte offset
     * `begin`.
     */
    #[inline]
    fn substr(begin: uint, n: uint) -> ~str { substr(self, begin, n) }
    /// Convert a string to lowercase
    #[inline]
    pure fn to_lower() -> ~str { to_lower(self) }
    /// Convert a string to uppercase
    #[inline]
    pure fn to_upper() -> ~str { to_upper(self) }
    /// Escape each char in `s` with char::escape_default.
    #[inline]
    fn escape_default() -> ~str { escape_default(self) }
    /// Escape each char in `s` with char::escape_unicode.
    #[inline]
    fn escape_unicode() -> ~str { escape_unicode(self) }

    #[inline]
    pure fn to_unique() -> ~str { self.slice(0, self.len()) }

    #[inline]
    pure fn char_at(i: uint) -> char { char_at(self, i) }
}

#[cfg(test)]
mod tests {

    use libc::c_char;

    #[test]
    fn test_eq() {
        assert (eq(&~"", &~""));
        assert (eq(&~"foo", &~"foo"));
        assert (!eq(&~"foo", &~"bar"));
    }

    #[test]
    fn test_eq_slice() {
        assert (eq_slice(view("foobar", 0, 3), "foo"));
        assert (eq_slice(view("barfoo", 3, 6), "foo"));
        assert (!eq_slice("foo1", "foo2"));
    }

    #[test]
    fn test_le() {
        assert (le(&"", &""));
        assert (le(&"", &"foo"));
        assert (le(&"foo", &"foo"));
        assert (!eq(&~"foo", &~"bar"));
    }

    #[test]
    fn test_len() {
        assert (len(~"") == 0u);
        assert (len(~"hello world") == 11u);
        assert (len(~"\x63") == 1u);
        assert (len(~"\xa2") == 2u);
        assert (len(~"\u03c0") == 2u);
        assert (len(~"\u2620") == 3u);
        assert (len(~"\U0001d11e") == 4u);

        assert (char_len(~"") == 0u);
        assert (char_len(~"hello world") == 11u);
        assert (char_len(~"\x63") == 1u);
        assert (char_len(~"\xa2") == 1u);
        assert (char_len(~"\u03c0") == 1u);
        assert (char_len(~"\u2620") == 1u);
        assert (char_len(~"\U0001d11e") == 1u);
        assert (char_len(~"ประเทศไทย中华Việt Nam") == 19u);
    }

    #[test]
    fn test_rfind_char() {
        assert rfind_char(~"hello", 'l') == Some(3u);
        assert rfind_char(~"hello", 'o') == Some(4u);
        assert rfind_char(~"hello", 'h') == Some(0u);
        assert rfind_char(~"hello", 'z').is_none();
        assert rfind_char(~"ประเทศไทย中华Việt Nam", '华') == Some(30u);
    }

    #[test]
    fn test_pop_char() {
        let mut data = ~"ประเทศไทย中华";
        let cc = pop_char(data);
        assert ~"ประเทศไทย中" == data;
        assert '华' == cc;
    }

    #[test]
    fn test_pop_char_2() {
        let mut data2 = ~"华";
        let cc2 = pop_char(data2);
        assert ~"" == data2;
        assert '华' == cc2;
    }

    #[test]
    #[should_fail]
    #[ignore(cfg(windows))]
    fn test_pop_char_fail() {
        let mut data = ~"";
        let _cc3 = pop_char(data);
    }

    #[test]
    fn test_split_char() {
        fn t(s: ~str, c: char, u: ~[~str]) {
            log(debug, ~"split_byte: " + s);
            let v = split_char(s, c);
            debug!("split_byte to: %?", v);
            assert vec::all2(v, u, |a,b| a == b);
        }
        t(~"abc.hello.there", '.', ~[~"abc", ~"hello", ~"there"]);
        t(~".hello.there", '.', ~[~"", ~"hello", ~"there"]);
        t(~"...hello.there.", '.', ~[~"", ~"", ~"", ~"hello", ~"there", ~""]);

        assert ~[~"", ~"", ~"", ~"hello", ~"there", ~""]
            == split_char(~"...hello.there.", '.');

        assert ~[~""] == split_char(~"", 'z');
        assert ~[~"",~""] == split_char(~"z", 'z');
        assert ~[~"ok"] == split_char(~"ok", 'z');
    }

    #[test]
    fn test_split_char_2() {
        let data = ~"ประเทศไทย中华Việt Nam";
        assert ~[~"ประเทศไทย中华", ~"iệt Nam"]
            == split_char(data, 'V');
        assert ~[~"ประเ", ~"ศไ", ~"ย中华Việt Nam"]
            == split_char(data, 'ท');
    }

    #[test]
    fn test_splitn_char() {
        fn t(s: ~str, c: char, n: uint, u: ~[~str]) {
            log(debug, ~"splitn_byte: " + s);
            let v = splitn_char(s, c, n);
            debug!("split_byte to: %?", v);
            debug!("comparing vs. %?", u);
            assert vec::all2(v, u, |a,b| a == b);
        }
        t(~"abc.hello.there", '.', 0u, ~[~"abc.hello.there"]);
        t(~"abc.hello.there", '.', 1u, ~[~"abc", ~"hello.there"]);
        t(~"abc.hello.there", '.', 2u, ~[~"abc", ~"hello", ~"there"]);
        t(~"abc.hello.there", '.', 3u, ~[~"abc", ~"hello", ~"there"]);
        t(~".hello.there", '.', 0u, ~[~".hello.there"]);
        t(~".hello.there", '.', 1u, ~[~"", ~"hello.there"]);
        t(~"...hello.there.", '.', 3u, ~[~"", ~"", ~"", ~"hello.there."]);
        t(~"...hello.there.", '.', 5u,
          ~[~"", ~"", ~"", ~"hello", ~"there", ~""]);

        assert ~[~""] == splitn_char(~"", 'z', 5u);
        assert ~[~"",~""] == splitn_char(~"z", 'z', 5u);
        assert ~[~"ok"] == splitn_char(~"ok", 'z', 5u);
        assert ~[~"z"] == splitn_char(~"z", 'z', 0u);
        assert ~[~"w.x.y"] == splitn_char(~"w.x.y", '.', 0u);
        assert ~[~"w",~"x.y"] == splitn_char(~"w.x.y", '.', 1u);
    }

    #[test]
    fn test_splitn_char_2 () {
        let data = ~"ประเทศไทย中华Việt Nam";
        assert ~[~"ประเทศไทย中", ~"Việt Nam"]
            == splitn_char(data, '华', 1u);

        assert ~[~"", ~"", ~"XXX", ~"YYYzWWWz"]
            == splitn_char(~"zzXXXzYYYzWWWz", 'z', 3u);
        assert ~[~"",~""] == splitn_char(~"z", 'z', 5u);
        assert ~[~""] == splitn_char(~"", 'z', 5u);
        assert ~[~"ok"] == splitn_char(~"ok", 'z', 5u);
    }


    #[test]
    fn test_splitn_char_3() {
        let data = ~"ประเทศไทย中华Việt Nam";
        assert ~[~"ประเทศไทย中华", ~"iệt Nam"]
            == splitn_char(data, 'V', 1u);
        assert ~[~"ประเ", ~"ศไทย中华Việt Nam"]
            == splitn_char(data, 'ท', 1u);

    }

    #[test]
    fn test_split_str() {
        fn t(s: ~str, sep: &a/str, i: int, k: ~str) {
            let v = split_str(s, sep);
            assert v[i] == k;
        }

        t(~"--1233345--", ~"12345", 0, ~"--1233345--");
        t(~"abc::hello::there", ~"::", 0, ~"abc");
        t(~"abc::hello::there", ~"::", 1, ~"hello");
        t(~"abc::hello::there", ~"::", 2, ~"there");
        t(~"::hello::there", ~"::", 0, ~"");
        t(~"hello::there::", ~"::", 2, ~"");
        t(~"::hello::there::", ~"::", 3, ~"");

        let data = ~"ประเทศไทย中华Việt Nam";
        assert ~[~"ประเทศไทย", ~"Việt Nam"]
            == split_str (data, ~"中华");

        assert ~[~"", ~"XXX", ~"YYY", ~""]
            == split_str(~"zzXXXzzYYYzz", ~"zz");

        assert ~[~"zz", ~"zYYYz"]
            == split_str(~"zzXXXzYYYz", ~"XXX");


        assert ~[~"", ~"XXX", ~"YYY", ~""] == split_str(~".XXX.YYY.", ~".");
        assert ~[~""] == split_str(~"", ~".");
        assert ~[~"",~""] == split_str(~"zz", ~"zz");
        assert ~[~"ok"] == split_str(~"ok", ~"z");
        assert ~[~"",~"z"] == split_str(~"zzz", ~"zz");
        assert ~[~"",~"",~"z"] == split_str(~"zzzzz", ~"zz");
    }


    #[test]
    fn test_split() {
        let data = ~"ประเทศไทย中华Việt Nam";
        assert ~[~"ประเทศไทย中", ~"Việt Nam"]
            == split (data, |cc| cc == '华');

        assert ~[~"", ~"", ~"XXX", ~"YYY", ~""]
            == split(~"zzXXXzYYYz", char::is_lowercase);

        assert ~[~"zz", ~"", ~"", ~"z", ~"", ~"", ~"z"]
            == split(~"zzXXXzYYYz", char::is_uppercase);

        assert ~[~"",~""] == split(~"z", |cc| cc == 'z');
        assert ~[~""] == split(~"", |cc| cc == 'z');
        assert ~[~"ok"] == split(~"ok", |cc| cc == 'z');
    }

    #[test]
    fn test_lines() {
        let lf = ~"\nMary had a little lamb\nLittle lamb\n";
        let crlf = ~"\r\nMary had a little lamb\r\nLittle lamb\r\n";

        assert ~[~"", ~"Mary had a little lamb", ~"Little lamb", ~""]
            == lines(lf);

        assert ~[~"", ~"Mary had a little lamb", ~"Little lamb", ~""]
            == lines_any(lf);

        assert ~[~"\r", ~"Mary had a little lamb\r", ~"Little lamb\r", ~""]
            == lines(crlf);

        assert ~[~"", ~"Mary had a little lamb", ~"Little lamb", ~""]
            == lines_any(crlf);

        assert ~[~""] == lines    (~"");
        assert ~[~""] == lines_any(~"");
        assert ~[~"",~""] == lines    (~"\n");
        assert ~[~"",~""] == lines_any(~"\n");
        assert ~[~"banana"] == lines    (~"banana");
        assert ~[~"banana"] == lines_any(~"banana");
    }

    #[test]
    fn test_words () {
        let data = ~"\nMary had a little lamb\nLittle lamb\n";
        assert ~[~"Mary",~"had",~"a",~"little",~"lamb",~"Little",~"lamb"]
            == words(data);

        assert ~[~"ok"] == words(~"ok");
        assert ~[] == words(~"");
    }

    #[test]
    fn test_find_str() {
        // byte positions
        assert find_str(~"banana", ~"apple pie").is_none();
        assert find_str(~"", ~"") == Some(0u);

        let data = ~"ประเทศไทย中华Việt Nam";
        assert find_str(data, ~"")     == Some(0u);
        assert find_str(data, ~"ประเ") == Some( 0u);
        assert find_str(data, ~"ะเ")   == Some( 6u);
        assert find_str(data, ~"中华") == Some(27u);
        assert find_str(data, ~"ไท华").is_none();
    }

    #[test]
    fn test_find_str_between() {
        // byte positions
        assert find_str_between(~"", ~"", 0u, 0u) == Some(0u);

        let data = ~"abcabc";
        assert find_str_between(data, ~"ab", 0u, 6u) == Some(0u);
        assert find_str_between(data, ~"ab", 2u, 6u) == Some(3u);
        assert find_str_between(data, ~"ab", 2u, 4u).is_none();

        let mut data = ~"ประเทศไทย中华Việt Nam";
        data += data;
        assert find_str_between(data, ~"", 0u, 43u) == Some(0u);
        assert find_str_between(data, ~"", 6u, 43u) == Some(6u);

        assert find_str_between(data, ~"ประ", 0u, 43u) == Some( 0u);
        assert find_str_between(data, ~"ทศไ", 0u, 43u) == Some(12u);
        assert find_str_between(data, ~"ย中", 0u, 43u) == Some(24u);
        assert find_str_between(data, ~"iệt", 0u, 43u) == Some(34u);
        assert find_str_between(data, ~"Nam", 0u, 43u) == Some(40u);

        assert find_str_between(data, ~"ประ", 43u, 86u) == Some(43u);
        assert find_str_between(data, ~"ทศไ", 43u, 86u) == Some(55u);
        assert find_str_between(data, ~"ย中", 43u, 86u) == Some(67u);
        assert find_str_between(data, ~"iệt", 43u, 86u) == Some(77u);
        assert find_str_between(data, ~"Nam", 43u, 86u) == Some(83u);
    }

    #[test]
    fn test_substr() {
        fn t(a: ~str, b: ~str, start: int) {
            assert substr(a, start as uint, len(b)) == b;
        }
        t(~"hello", ~"llo", 2);
        t(~"hello", ~"el", 1);
        assert ~"ะเทศไท" == substr(~"ประเทศไทย中华Việt Nam", 6u, 6u);
    }

    #[test]
    fn test_concat() {
        fn t(v: ~[~str], s: ~str) { assert concat(v) == s; }
        t(~[~"you", ~"know", ~"I'm", ~"no", ~"good"], ~"youknowI'mnogood");
        let v: ~[~str] = ~[];
        t(v, ~"");
        t(~[~"hi"], ~"hi");
    }

    #[test]
    fn test_connect() {
        fn t(v: ~[~str], sep: ~str, s: ~str) {
            assert connect(v, sep) == s;
        }
        t(~[~"you", ~"know", ~"I'm", ~"no", ~"good"],
          ~" ", ~"you know I'm no good");
        let v: ~[~str] = ~[];
        t(v, ~" ", ~"");
        t(~[~"hi"], ~" ", ~"hi");
    }

    #[test]
    fn test_to_upper() {
        // libc::toupper, and hence str::to_upper
        // are culturally insensitive: they only work for ASCII
        // (see Issue #1347)
        let unicode = ~""; //"\u65e5\u672c"; // uncomment once non-ASCII works
        let input = ~"abcDEF" + unicode + ~"xyz:.;";
        let expected = ~"ABCDEF" + unicode + ~"XYZ:.;";
        let actual = to_upper(input);
        assert expected == actual;
    }

    #[test]
    fn test_to_lower() {
        assert ~"" == map(~"", |c| libc::tolower(c as c_char) as char);
        assert ~"ymca" == map(~"YMCA",
                             |c| libc::tolower(c as c_char) as char);
    }

    #[test]
    fn test_unsafe_slice() {
        unsafe {
            assert ~"ab" == raw::slice_bytes(~"abc", 0u, 2u);
            assert ~"bc" == raw::slice_bytes(~"abc", 1u, 3u);
            assert ~"" == raw::slice_bytes(~"abc", 1u, 1u);
            fn a_million_letter_a() -> ~str {
                let mut i = 0;
                let mut rs = ~"";
                while i < 100000 { push_str(rs, ~"aaaaaaaaaa"); i += 1; }
                return rs;
            }
            fn half_a_million_letter_a() -> ~str {
                let mut i = 0;
                let mut rs = ~"";
                while i < 100000 { push_str(rs, ~"aaaaa"); i += 1; }
                return rs;
            }
            assert half_a_million_letter_a() ==
                raw::slice_bytes(a_million_letter_a(), 0u, 500000u);
        }
    }

    #[test]
    fn test_starts_with() {
        assert (starts_with(~"", ~""));
        assert (starts_with(~"abc", ~""));
        assert (starts_with(~"abc", ~"a"));
        assert (!starts_with(~"a", ~"abc"));
        assert (!starts_with(~"", ~"abc"));
    }

    #[test]
    fn test_ends_with() {
        assert (ends_with(~"", ~""));
        assert (ends_with(~"abc", ~""));
        assert (ends_with(~"abc", ~"c"));
        assert (!ends_with(~"a", ~"abc"));
        assert (!ends_with(~"", ~"abc"));
    }

    #[test]
    fn test_is_empty() {
        assert (is_empty(~""));
        assert (!is_empty(~"a"));
    }

    #[test]
    fn test_is_not_empty() {
        assert (is_not_empty(~"a"));
        assert (!is_not_empty(~""));
    }

    #[test]
    fn test_replace() {
        let a = ~"a";
        assert replace(~"", a, ~"b") == ~"";
        assert replace(~"a", a, ~"b") == ~"b";
        assert replace(~"ab", a, ~"b") == ~"bb";
        let test = ~"test";
        assert replace(~" test test ", test, ~"toast") == ~" toast toast ";
        assert replace(~" test test ", test, ~"") == ~"   ";
    }

    #[test]
    fn test_replace_2a() {
        let data = ~"ประเทศไทย中华";
        let repl = ~"دولة الكويت";

        let a = ~"ประเ";
        let A = ~"دولة الكويتทศไทย中华";
        assert (replace(data, a, repl) ==  A);
    }

    #[test]
    fn test_replace_2b() {
        let data = ~"ประเทศไทย中华";
        let repl = ~"دولة الكويت";

        let b = ~"ะเ";
        let B = ~"ปรدولة الكويتทศไทย中华";
        assert (replace(data, b,   repl) ==  B);
    }

    #[test]
    fn test_replace_2c() {
        let data = ~"ประเทศไทย中华";
        let repl = ~"دولة الكويت";

        let c = ~"中华";
        let C = ~"ประเทศไทยدولة الكويت";
        assert (replace(data, c, repl) ==  C);
    }

    #[test]
    fn test_replace_2d() {
        let data = ~"ประเทศไทย中华";
        let repl = ~"دولة الكويت";

        let d = ~"ไท华";
        assert (replace(data, d, repl) == data);
    }

    #[test]
    fn test_slice() {
        assert ~"ab" == slice(~"abc", 0u, 2u);
        assert ~"bc" == slice(~"abc", 1u, 3u);
        assert ~"" == slice(~"abc", 1u, 1u);
        assert ~"\u65e5" == slice(~"\u65e5\u672c", 0u, 3u);

        let data = ~"ประเทศไทย中华";
        assert ~"ป" == slice(data, 0u, 3u);
        assert ~"ร" == slice(data, 3u, 6u);
        assert ~"" == slice(data, 3u, 3u);
        assert ~"华" == slice(data, 30u, 33u);

        fn a_million_letter_X() -> ~str {
            let mut i = 0;
            let mut rs = ~"";
            while i < 100000 { push_str(rs, ~"华华华华华华华华华华"); i += 1; }
            return rs;
        }
        fn half_a_million_letter_X() -> ~str {
            let mut i = 0;
            let mut rs = ~"";
            while i < 100000 { push_str(rs, ~"华华华华华"); i += 1; }
            return rs;
        }
        assert half_a_million_letter_X() ==
            slice(a_million_letter_X(), 0u, 3u * 500000u);
    }

    #[test]
    fn test_slice_2() {
        let ss = ~"中华Việt Nam";

        assert ~"华" == slice(ss, 3u, 6u);
        assert ~"Việt Nam" == slice(ss, 6u, 16u);

        assert ~"ab" == slice(~"abc", 0u, 2u);
        assert ~"bc" == slice(~"abc", 1u, 3u);
        assert ~"" == slice(~"abc", 1u, 1u);

        assert ~"中" == slice(ss, 0u, 3u);
        assert ~"华V" == slice(ss, 3u, 7u);
        assert ~"" == slice(ss, 3u, 3u);
        /*0: 中
          3: 华
          6: V
          7: i
          8: ệ
         11: t
         12:
         13: N
         14: a
         15: m */
    }

    #[test]
    #[should_fail]
    #[ignore(cfg(windows))]
    fn test_slice_fail() {
        slice(~"中华Việt Nam", 0u, 2u);
    }

    #[test]
    fn test_trim_left_chars() {
        assert trim_left_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ";
        assert trim_left_chars(~" *** foo *** ", ~['*', ' ']) == ~"foo *** ";
        assert trim_left_chars(~" ***  *** ", ~['*', ' ']) == ~"";
        assert trim_left_chars(~"foo *** ", ~['*', ' ']) == ~"foo *** ";
    }

    #[test]
    fn test_trim_right_chars() {
        assert trim_right_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ";
        assert trim_right_chars(~" *** foo *** ", ~['*', ' ']) == ~" *** foo";
        assert trim_right_chars(~" ***  *** ", ~['*', ' ']) == ~"";
        assert trim_right_chars(~" *** foo", ~['*', ' ']) == ~" *** foo";
    }

    #[test]
    fn test_trim_chars() {
        assert trim_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ";
        assert trim_chars(~" *** foo *** ", ~['*', ' ']) == ~"foo";
        assert trim_chars(~" ***  *** ", ~['*', ' ']) == ~"";
        assert trim_chars(~"foo", ~['*', ' ']) == ~"foo";
    }

    #[test]
    fn test_trim_left() {
        assert (trim_left(~"") == ~"");
        assert (trim_left(~"a") == ~"a");
        assert (trim_left(~"    ") == ~"");
        assert (trim_left(~"     blah") == ~"blah");
        assert (trim_left(~"   \u3000  wut") == ~"wut");
        assert (trim_left(~"hey ") == ~"hey ");
    }

    #[test]
    fn test_trim_right() {
        assert (trim_right(~"") == ~"");
        assert (trim_right(~"a") == ~"a");
        assert (trim_right(~"    ") == ~"");
        assert (trim_right(~"blah     ") == ~"blah");
        assert (trim_right(~"wut   \u3000  ") == ~"wut");
        assert (trim_right(~" hey") == ~" hey");
    }

    #[test]
    fn test_trim() {
        assert (trim(~"") == ~"");
        assert (trim(~"a") == ~"a");
        assert (trim(~"    ") == ~"");
        assert (trim(~"    blah     ") == ~"blah");
        assert (trim(~"\nwut   \u3000  ") == ~"wut");
        assert (trim(~" hey dude ") == ~"hey dude");
    }

    #[test]
    fn test_is_whitespace() {
        assert (is_whitespace(~""));
        assert (is_whitespace(~" "));
        assert (is_whitespace(~"\u2009")); // Thin space
        assert (is_whitespace(~"  \n\t   "));
        assert (!is_whitespace(~"   _   "));
    }

    #[test]
    fn test_is_ascii() {
        assert (is_ascii(~""));
        assert (is_ascii(~"a"));
        assert (!is_ascii(~"\u2009"));
    }

    #[test]
    fn test_shift_byte() {
        let mut s = ~"ABC";
        let b = unsafe { raw::shift_byte(s) };
        assert (s == ~"BC");
        assert (b == 65u8);
    }

    #[test]
    fn test_pop_byte() {
        let mut s = ~"ABC";
        let b = unsafe { raw::pop_byte(s) };
        assert (s == ~"AB");
        assert (b == 67u8);
    }

    #[test]
    fn test_unsafe_from_bytes() {
        let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8];
        let b = unsafe { raw::from_bytes(a) };
        assert (b == ~"AAAAAAA");
    }

    #[test]
    fn test_from_bytes() {
        let ss = ~"ศไทย中华Việt Nam";
        let bb = ~[0xe0_u8, 0xb8_u8, 0xa8_u8,
                  0xe0_u8, 0xb9_u8, 0x84_u8,
                  0xe0_u8, 0xb8_u8, 0x97_u8,
                  0xe0_u8, 0xb8_u8, 0xa2_u8,
                  0xe4_u8, 0xb8_u8, 0xad_u8,
                  0xe5_u8, 0x8d_u8, 0x8e_u8,
                  0x56_u8, 0x69_u8, 0xe1_u8,
                  0xbb_u8, 0x87_u8, 0x74_u8,
                  0x20_u8, 0x4e_u8, 0x61_u8,
                  0x6d_u8];

        assert ss == from_bytes(bb);
    }

    #[test]
    #[should_fail]
    #[ignore(cfg(windows))]
    fn test_from_bytes_fail() {
        let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
                  0xe0_u8, 0xb9_u8, 0x84_u8,
                  0xe0_u8, 0xb8_u8, 0x97_u8,
                  0xe0_u8, 0xb8_u8, 0xa2_u8,
                  0xe4_u8, 0xb8_u8, 0xad_u8,
                  0xe5_u8, 0x8d_u8, 0x8e_u8,
                  0x56_u8, 0x69_u8, 0xe1_u8,
                  0xbb_u8, 0x87_u8, 0x74_u8,
                  0x20_u8, 0x4e_u8, 0x61_u8,
                  0x6d_u8];

         let _x = from_bytes(bb);
    }

    #[test]
    fn test_from_buf() {
        unsafe {
            let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
            let b = vec::raw::to_ptr(a);
            let c = raw::from_buf(b);
            assert (c == ~"AAAAAAA");
        }
    }

    #[test]
    #[ignore(cfg(windows))]
    #[should_fail]
    fn test_as_bytes_fail() {
        // Don't double free
        as_bytes::<()>(~"", |_bytes| fail );
    }

    #[test]
    fn test_as_buf() {
        let a = ~"Abcdefg";
        let b = as_buf(a, |buf, _l| {
            assert unsafe { *buf } == 65u8;
            100
        });
        assert (b == 100);
    }

    #[test]
    fn test_as_buf_small() {
        let a = ~"A";
        let b = as_buf(a, |buf, _l| {
            assert unsafe { *buf } == 65u8;
            100
        });
        assert (b == 100);
    }

    #[test]
    fn test_as_buf2() {
        unsafe {
            let s = ~"hello";
            let sb = as_buf(s, |b, _l| b);
            let s_cstr = raw::from_buf(sb);
            assert s_cstr == s;
        }
    }

    #[test]
    fn test_as_buf_3() {
        let a = ~"hello";
        do as_buf(a) |buf, len| {
            unsafe {
                assert a[0] == 'h' as u8;
                assert *buf == 'h' as u8;
                assert len == 6u;
                assert *ptr::offset(buf,4u) == 'o' as u8;
                assert *ptr::offset(buf,5u) == 0u8;
            }
        }
    }

    #[test]
    fn vec_str_conversions() {
        let s1: ~str = ~"All mimsy were the borogoves";

        let v: ~[u8] = to_bytes(s1);
        let s2: ~str = from_bytes(v);
        let mut i: uint = 0u;
        let n1: uint = len(s1);
        let n2: uint = vec::len::<u8>(v);
        assert (n1 == n2);
        while i < n1 {
            let a: u8 = s1[i];
            let b: u8 = s2[i];
            log(debug, a);
            log(debug, b);
            assert (a == b);
            i += 1u;
        }
    }

    #[test]
    fn test_contains() {
        assert contains(~"abcde", ~"bcd");
        assert contains(~"abcde", ~"abcd");
        assert contains(~"abcde", ~"bcde");
        assert contains(~"abcde", ~"");
        assert contains(~"", ~"");
        assert !contains(~"abcde", ~"def");
        assert !contains(~"", ~"a");

        let data = ~"ประเทศไทย中华Việt Nam";
        assert  contains(data, ~"ประเ");
        assert  contains(data, ~"ะเ");
        assert  contains(data, ~"中华");
        assert !contains(data, ~"ไท华");
    }

    #[test]
    fn test_contains_char() {
        assert contains_char(~"abc", 'b');
        assert contains_char(~"a", 'a');
        assert !contains_char(~"abc", 'd');
        assert !contains_char(~"", 'a');
    }

    #[test]
    fn test_chars_each() {
        let mut i = 0;
        for chars_each(~"x\u03c0y") |ch| {
            match i {
              0 => assert ch == 'x',
              1 => assert ch == '\u03c0',
              2 => assert ch == 'y',
              _ => fail ~"test_chars_each failed"
            }
            i += 1;
        }

        chars_each(~"", |_ch| fail ); // should not fail
    }

    #[test]
    fn test_bytes_each() {
        let mut i = 0;

        for bytes_each(~"xyz") |bb| {
            match i {
              0 => assert bb == 'x' as u8,
              1 => assert bb == 'y' as u8,
              2 => assert bb == 'z' as u8,
              _ => fail ~"test_bytes_each failed"
            }
            i += 1;
        }

        for bytes_each(~"") |bb| {
            assert bb == 0u8;
        }
    }

    #[test]
    fn test_split_char_each() {
        let data = ~"\nMary had a little lamb\nLittle lamb\n";

        let mut ii = 0;

        for split_char_each(data, ' ') |xx| {
            match ii {
              0 => assert "\nMary" == xx,
              1 => assert "had"    == xx,
              2 => assert "a"      == xx,
              3 => assert "little" == xx,
              _ => ()
            }
            ii += 1;
        }
    }

    #[test]
    fn test_splitn_char_each() {
        let data = ~"\nMary had a little lamb\nLittle lamb\n";

        let mut ii = 0;

        for splitn_char_each(data, ' ', 2u) |xx| {
            match ii {
              0 => assert "\nMary" == xx,
              1 => assert "had"    == xx,
              2 => assert "a little lamb\nLittle lamb\n" == xx,
              _ => ()
            }
            ii += 1;
        }
    }

    #[test]
    fn test_words_each() {
        let data = ~"\nMary had a little lamb\nLittle lamb\n";

        let mut ii = 0;

        for words_each(data) |ww| {
            match ii {
              0 => assert "Mary"   == ww,
              1 => assert "had"    == ww,
              2 => assert "a"      == ww,
              3 => assert "little" == ww,
              _ => ()
            }
            ii += 1;
        }

        words_each(~"", |_x| fail); // should not fail
    }

    #[test]
    fn test_lines_each () {
        let lf = ~"\nMary had a little lamb\nLittle lamb\n";

        let mut ii = 0;

        for lines_each(lf) |x| {
            match ii {
                0 => assert "" == x,
                1 => assert "Mary had a little lamb" == x,
                2 => assert "Little lamb" == x,
                3 => assert "" == x,
                _ => ()
            }
            ii += 1;
        }
    }

    #[test]
    fn test_map() {
        assert ~"" == map(~"", |c| libc::toupper(c as c_char) as char);
        assert ~"YMCA" == map(~"ymca",
                              |c| libc::toupper(c as c_char) as char);
    }

    #[test]
    fn test_all() {
        assert true  == all(~"", char::is_uppercase);
        assert false == all(~"ymca", char::is_uppercase);
        assert true  == all(~"YMCA", char::is_uppercase);
        assert false == all(~"yMCA", char::is_uppercase);
        assert false == all(~"YMCy", char::is_uppercase);
    }

    #[test]
    fn test_any() {
        assert false  == any(~"", char::is_uppercase);
        assert false == any(~"ymca", char::is_uppercase);
        assert true  == any(~"YMCA", char::is_uppercase);
        assert true == any(~"yMCA", char::is_uppercase);
        assert true == any(~"Ymcy", char::is_uppercase);
    }

    #[test]
    fn test_chars() {
        let ss = ~"ศไทย中华Việt Nam";
        assert ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']
            == chars(ss);
    }

    #[test]
    fn test_utf16() {
        let pairs =
            ~[(~"𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n",
              ~[0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16,
               0xd800_u16, 0xdf3b_u16, 0xd800_u16, 0xdf46_u16,
               0xd800_u16, 0xdf39_u16, 0xd800_u16, 0xdf3b_u16,
               0xd800_u16, 0xdf30_u16, 0x000a_u16]),

             (~"𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n",
              ~[0xd801_u16, 0xdc12_u16, 0xd801_u16,
               0xdc49_u16, 0xd801_u16, 0xdc2e_u16, 0xd801_u16,
               0xdc40_u16, 0xd801_u16, 0xdc32_u16, 0xd801_u16,
               0xdc4b_u16, 0x0020_u16, 0xd801_u16, 0xdc0f_u16,
               0xd801_u16, 0xdc32_u16, 0xd801_u16, 0xdc4d_u16,
               0x000a_u16]),

             (~"𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n",
              ~[0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16,
               0xd800_u16, 0xdf0b_u16, 0xd800_u16, 0xdf04_u16,
               0xd800_u16, 0xdf11_u16, 0xd800_u16, 0xdf09_u16,
               0x00b7_u16, 0xd800_u16, 0xdf0c_u16, 0xd800_u16,
               0xdf04_u16, 0xd800_u16, 0xdf15_u16, 0xd800_u16,
               0xdf04_u16, 0xd800_u16, 0xdf0b_u16, 0xd800_u16,
               0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]),

             (~"𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n",
              ~[0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16,
               0xd801_u16, 0xdc88_u16, 0xd801_u16, 0xdc91_u16,
               0xd801_u16, 0xdc9b_u16, 0xd801_u16, 0xdc92_u16,
               0x0020_u16, 0xd801_u16, 0xdc95_u16, 0xd801_u16,
               0xdc93_u16, 0x0020_u16, 0xd801_u16, 0xdc88_u16,
               0xd801_u16, 0xdc9a_u16, 0xd801_u16, 0xdc8d_u16,
               0x0020_u16, 0xd801_u16, 0xdc8f_u16, 0xd801_u16,
               0xdc9c_u16, 0xd801_u16, 0xdc92_u16, 0xd801_u16,
               0xdc96_u16, 0xd801_u16, 0xdc86_u16, 0x0020_u16,
               0xd801_u16, 0xdc95_u16, 0xd801_u16, 0xdc86_u16,
               0x000a_u16 ]) ];

        for vec::each(pairs) |p| {
            let (s, u) = copy *p;
            assert to_utf16(s) == u;
            assert from_utf16(u) == s;
            assert from_utf16(to_utf16(s)) == s;
            assert to_utf16(from_utf16(u)) == u;
        }
    }

    #[test]
    fn test_each_char() {
        let s = ~"abc";
        let mut found_b = false;
        for each_char(s) |ch| {
            if ch == 'b' {
                found_b = true;
                break;
            }
        }
        assert found_b;
    }

    #[test]
    fn test_escape_unicode() {
        assert escape_unicode(~"abc") == ~"\\x61\\x62\\x63";
        assert escape_unicode(~"a c") == ~"\\x61\\x20\\x63";
        assert escape_unicode(~"\r\n\t") == ~"\\x0d\\x0a\\x09";
        assert escape_unicode(~"'\"\\") == ~"\\x27\\x22\\x5c";
        assert escape_unicode(~"\x00\x01\xfe\xff") == ~"\\x00\\x01\\xfe\\xff";
        assert escape_unicode(~"\u0100\uffff") == ~"\\u0100\\uffff";
        assert escape_unicode(~"\U00010000\U0010ffff") ==
            ~"\\U00010000\\U0010ffff";
        assert escape_unicode(~"ab\ufb00") == ~"\\x61\\x62\\ufb00";
        assert escape_unicode(~"\U0001d4ea\r") == ~"\\U0001d4ea\\x0d";
    }

    #[test]
    fn test_escape_default() {
        assert escape_default(~"abc") == ~"abc";
        assert escape_default(~"a c") == ~"a c";
        assert escape_default(~"\r\n\t") == ~"\\r\\n\\t";
        assert escape_default(~"'\"\\") == ~"\\'\\\"\\\\";
        assert escape_default(~"\u0100\uffff") == ~"\\u0100\\uffff";
        assert escape_default(~"\U00010000\U0010ffff") ==
            ~"\\U00010000\\U0010ffff";
        assert escape_default(~"ab\ufb00") == ~"ab\\ufb00";
        assert escape_default(~"\U0001d4ea\r") == ~"\\U0001d4ea\\r";
    }

}