summaryrefslogtreecommitdiff
path: root/srfi/srfi-13.c
blob: 3d32cf7444bd781677ef9030af274f773de034ba (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
/* srfi-13.c --- SRFI-13 procedures for Guile
 *
 * Copyright (C) 2001 Free Software Foundation, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 * As a special exception, the Free Software Foundation gives
 * permission for additional uses of the text contained in its release
 * of GUILE.
 *
 * The exception is that, if you link the GUILE library with other
 * files to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public
 * License.  Your use of that executable is in no way restricted on
 * account of linking the GUILE library code into it.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU General Public
 * License.
 *
 * This exception applies only to the code released by the Free
 * Software Foundation under the name GUILE.  If you copy code from
 * other Free Software Foundation releases into a copy of GUILE, as
 * the General Public License permits, the exception does not apply to
 * the code that you add in this way.  To avoid misleading anyone as
 * to the status of such modified files, you must delete this
 * exception notice from them.
 *
 * If you write modifications of your own for GUILE, it is your choice
 * whether to permit this exception to apply to your modifications.
 * If you do not wish that, delete this exception notice.  */


#include <string.h>
#include <ctype.h>

#include <libguile.h>

#include "srfi-13.h"
#include "srfi-14.h"

SCM_DEFINE (scm_string_any, "string-any", 2, 2, 0,
            (SCM pred, SCM s, SCM start, SCM end),
	    "Check if the predicate @var{pred} is true for any character in\n"
	    "the string @var{s}, proceeding from left (index @var{start}) to\n"
	    "right (index @var{end}).  If @code{string-any} returns true,\n"
	    "the returned true value is the one produced by the first\n"
	    "successful application of @var{pred}.")
#define FUNC_NAME s_scm_string_any
{
  char * cstr;
  int cstart, cend;
  SCM res;

  SCM_VALIDATE_PROC (1, pred);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  cstr += cstart;
  while (cstart < cend)
    {
      res = scm_call_1 (pred, SCM_MAKE_CHAR (*cstr));
      if (!SCM_FALSEP (res))
	return res;
      cstr++;
      cstart++;
    }
  return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_every, "string-every", 2, 2, 0,
            (SCM pred, SCM s, SCM start, SCM end),
	    "Check if the predicate @var{pred} is true for every character\n"
	    "in the string @var{s}, proceeding from left (index @var{start})\n"
	    "to right (index @var{end}).  If @code{string-every} returns\n"
	    "true, the returned true value is the one produced by the final\n"
	    "application of @var{pred} to the last character of @var{s}.")
#define FUNC_NAME s_scm_string_every
{
  char * cstr;
  int cstart, cend;
  SCM res;

  SCM_VALIDATE_PROC (1, pred);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  res = SCM_BOOL_F;
  cstr += cstart;
  while (cstart < cend)
    {
      res = scm_call_1 (pred, SCM_MAKE_CHAR (*cstr));
      if (SCM_FALSEP (res))
	return res;
      cstr++;
      cstart++;
    }
  return res;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_tabulate, "string-tabulate", 2, 0, 0,
            (SCM proc, SCM len),
	    "@var{proc} is an integer->char procedure.  Construct a string\n"
	    "of size @var{len} by applying @var{proc} to each index to\n"
	    "produce the corresponding string element.  The order in which\n"
	    "@var{proc} is applied to the indices is not specified.")
#define FUNC_NAME s_scm_string_tabulate
{
  int clen, i;
  SCM res;
  SCM ch;
  char * p;

  SCM_VALIDATE_PROC (1, proc);
  SCM_VALIDATE_INUM_COPY (2, len, clen);
  SCM_ASSERT_RANGE (2, len, clen >= 0);

  res = scm_allocate_string (clen);
  p = SCM_STRING_CHARS (res);
  i = 0;
  while (i < clen)
    {
      ch = scm_call_1 (proc, SCM_MAKINUM (i));
      if (!SCM_CHARP (ch))
	SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
      *p++ = SCM_CHAR (ch);
      i++;
    }
  return res;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_to_listS, "string->list", 1, 2, 0,
	    (SCM str, SCM start, SCM end),
	    "Convert the string @var{str} into a list of characters.")
#define FUNC_NAME s_scm_string_to_listS
{
  char * cstr;
  int cstart, cend;
  SCM result = SCM_EOL;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  while (cstart < cend)
    {
      cend--;
      result = scm_cons (SCM_MAKE_CHAR (cstr[cend]), result);
    }
  return result;
}
#undef FUNC_NAME

SCM_DEFINE (scm_reverse_list_to_string, "reverse-list->string", 1, 0, 0,
            (SCM chrs),
	    "An efficient implementation of @code{(compose string->list\n"
	    "reverse)}:\n"
	    "\n"
	    "@smalllisp\n"
	    "(reverse-list->string '(#\a #\B #\c)) @result{} \"cBa\"\n"
	    "@end smalllisp")
#define FUNC_NAME s_scm_reverse_list_to_string
{
  SCM result;
  long i = scm_ilength (chrs);

  if (i < 0)
    SCM_WRONG_TYPE_ARG (1, chrs);
  result = scm_allocate_string (i);

  {
    unsigned char *data = SCM_STRING_UCHARS (result) + i;

    while (!SCM_NULLP (chrs))
      {
	SCM elt = SCM_CAR (chrs);

	SCM_VALIDATE_CHAR (SCM_ARGn, elt);
	data--;
	*data = SCM_CHAR (elt);
	chrs = SCM_CDR (chrs);
      }
  }
  return result;
}
#undef FUNC_NAME


SCM_SYMBOL (scm_sym_infix, "infix");
SCM_SYMBOL (scm_sym_strict_infix, "strict-infix");
SCM_SYMBOL (scm_sym_suffix, "suffix");
SCM_SYMBOL (scm_sym_prefix, "prefix");

SCM_DEFINE (scm_string_join, "string-join", 1, 2, 0,
            (SCM ls, SCM delimiter, SCM grammar),
	    "Append the string in the string list @var{ls}, using the string\n"
	    "@var{delim} as a delimiter between the elements of @var{ls}.\n"
	    "@var{grammar} is a symbol which specifies how the delimiter is\n"
	    "placed between the strings, and defaults to the symbol\n"
	    "@code{infix}.\n"
	    "\n"
	    "@table @code\n"
	    "@item infix\n"
	    "Insert the separator between list elements.  An empty string\n"
	    "will produce an empty list.\n"
	    "@item string-infix\n"
	    "Like @code{infix}, but will raise an error if given the empty\n"
	    "list.\n"
	    "@item suffix\n"
	    "Insert the separator after every list element.\n"
	    "@item prefix\n"
	    "Insert the separator before each list element.\n"
	    "@end table")
#define FUNC_NAME s_scm_string_join
{
#define GRAM_INFIX        0
#define GRAM_STRICT_INFIX 1
#define GRAM_SUFFIX       2
#define GRAM_PREFIX       3
  SCM tmp;
  SCM result;
  int gram = GRAM_INFIX;
  int del_len = 0, extra_len = 0;
  int len = 0;
  char * p;
  long strings = scm_ilength (ls);

  /* Validate the string list.  */
  if (strings < 0)
    SCM_WRONG_TYPE_ARG (1, ls);

  /* Validate the delimiter and record its length.  */
  if (SCM_UNBNDP (delimiter))
    {
      delimiter = scm_makfrom0str (" ");
      del_len = 1;
    }
  else
    {
      SCM_VALIDATE_STRING (2, delimiter);
      del_len = SCM_STRING_LENGTH (delimiter);
    }

  /* Validate the grammar symbol and remember the grammar.  */
  if (SCM_UNBNDP (grammar))
    gram = GRAM_INFIX;
  else if (SCM_EQ_P (grammar, scm_sym_infix))
    gram = GRAM_INFIX;
  else if (SCM_EQ_P (grammar, scm_sym_strict_infix))
    gram = GRAM_STRICT_INFIX;
  else if (SCM_EQ_P (grammar, scm_sym_suffix))
    gram = GRAM_SUFFIX;
  else if (SCM_EQ_P (grammar, scm_sym_prefix))
    gram = GRAM_PREFIX;
  else
    SCM_WRONG_TYPE_ARG (3, grammar);

  /* Check grammar constraints and calculate the space required for
     the delimiter(s).  */
  switch (gram)
    {
    case GRAM_INFIX:
      if (!SCM_NULLP (ls))
	extra_len = (strings > 0) ? ((strings - 1) * del_len) : 0;
      break;
    case GRAM_STRICT_INFIX:
      if (strings == 0)
	SCM_MISC_ERROR ("strict-infix grammar requires non-empty list",
			SCM_EOL);
      extra_len = (strings - 1) * del_len;
      break;
    default:
      extra_len = strings * del_len;
      break;
    }

  tmp = ls;
  while (SCM_CONSP (tmp))
    {
      SCM elt = SCM_CAR (tmp);
      SCM_VALIDATE_STRING (1, elt);
      len += SCM_STRING_LENGTH (elt);
      tmp = SCM_CDR (tmp);
    }

  result = scm_allocate_string (len + extra_len);
  p = SCM_STRING_CHARS (result);

  tmp = ls;
  switch (gram)
    {
    case GRAM_INFIX:
    case GRAM_STRICT_INFIX:
      while (!SCM_NULLP (tmp))
	{
	  SCM elt = SCM_CAR (tmp);
	  memmove (p, SCM_STRING_CHARS (elt),
		   SCM_STRING_LENGTH (elt) * sizeof (char));
	  p += SCM_STRING_LENGTH (elt);
	  if (!SCM_NULLP (SCM_CDR (tmp)) && del_len > 0)
	    {
	      memmove (p, SCM_STRING_CHARS (delimiter),
		       SCM_STRING_LENGTH (delimiter) * sizeof (char));
	      p += del_len;
	    }
	  tmp = SCM_CDR (tmp);
	}
      break;
    case GRAM_SUFFIX:
      while (!SCM_NULLP (tmp))
	{
	  SCM elt = SCM_CAR (tmp);
	  memmove (p, SCM_STRING_CHARS (elt),
		   SCM_STRING_LENGTH (elt) * sizeof (char));
	  p += SCM_STRING_LENGTH (elt);
	  if (del_len > 0)
	    {
	      memmove (p, SCM_STRING_CHARS (delimiter),
		       SCM_STRING_LENGTH (delimiter) * sizeof (char));
	      p += del_len;
	    }
	  tmp = SCM_CDR (tmp);
	}
      break;
    case GRAM_PREFIX:
      while (!SCM_NULLP (tmp))
	{
	  SCM elt = SCM_CAR (tmp);
	  if (del_len > 0)
	    {
	      memmove (p, SCM_STRING_CHARS (delimiter),
		       SCM_STRING_LENGTH (delimiter) * sizeof (char));
	      p += del_len;
	    }
	  memmove (p, SCM_STRING_CHARS (elt),
		   SCM_STRING_LENGTH (elt) * sizeof (char));
	  p += SCM_STRING_LENGTH (elt);
	  tmp = SCM_CDR (tmp);
	}
      break;
    }
  return result;
#undef GRAM_INFIX
#undef GRAM_STRICT_INFIX
#undef GRAM_SUFFIX
#undef GRAM_PREFIX
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_copyS, "string-copy", 1, 2, 0,
	    (SCM str, SCM start, SCM end),
	    "Return a freshly allocated copy of the string @var{str}.  If\n"
	    "given, @var{start} and @var{end} delimit the portion of\n"
	    "@var{str} which is copied.")
#define FUNC_NAME s_scm_string_copyS
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  return scm_mem2string (cstr + cstart, cend - cstart);

}
#undef FUNC_NAME


SCM_DEFINE (scm_substring_shared, "substring/shared", 2, 1, 0,
            (SCM str, SCM start, SCM end),
	    "Like @code{substring}, but the result may share memory with the\n"
	    "argument @var{str}.")
#define FUNC_NAME s_scm_substring_shared
{
  SCM_VALIDATE_STRING (1, str);
  SCM_VALIDATE_INUM (2, start);
  if (SCM_UNBNDP (end))
    end = SCM_MAKINUM (SCM_STRING_LENGTH (str));
  else
    SCM_VALIDATE_INUM (3, end);
  if (SCM_INUM (start) == 0 &&
      SCM_INUM (end) == SCM_STRING_LENGTH (str))
    return str;
  return scm_substring (str, start, end);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_copy_x, "string-copy!", 3, 2, 0,
            (SCM target, SCM tstart, SCM s, SCM start, SCM end),
	    "Copy the sequence of characters from index range [@var{start},\n"
	    "@var{end}) in string @var{s} to string @var{target}, beginning\n"
	    "at index @var{tstart}.  The characters are copied left-to-right\n"
	    "or right-to-left as needed -- the copy is guaranteed to work,\n"
	    "even if @var{target} and @var{s} are the same string.  It is an\n"
	    "error if the copy operation runs off the end of the target\n"
	    "string.")
#define FUNC_NAME s_scm_string_copy_x
{
  char * cstr, * ctarget;
  int cstart, cend, ctstart, dummy;
  int len;
  SCM sdummy = SCM_UNDEFINED;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, target, ctarget,
				    2, tstart, ctstart,
				    2, sdummy, dummy);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cstr,
				    4, start, cstart,
				    5, end, cend);
  len = cend - cstart;
  SCM_ASSERT_RANGE (3, s, len <= SCM_STRING_LENGTH (target) - ctstart);

  memmove (SCM_STRING_CHARS (target) + ctstart,
	   SCM_STRING_CHARS (s) + cstart,
	   len * sizeof (char));
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_take, "string-take", 2, 0, 0,
            (SCM s, SCM n),
	    "Return the @var{n} first characters of @var{s}.")
#define FUNC_NAME s_scm_string_take
{
  char * cstr;
  int cn;

  SCM_VALIDATE_STRING_COPY (1, s, cstr);
  SCM_VALIDATE_INUM_COPY (2, n, cn);
  SCM_ASSERT_RANGE (2, n, cn >= 0 && cn <= SCM_STRING_LENGTH (s));

  return scm_mem2string (cstr, cn);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_drop, "string-drop", 2, 0, 0,
            (SCM s, SCM n),
	    "Return all but the first @var{n} characters of @var{s}.")
#define FUNC_NAME s_scm_string_drop
{
  char * cstr;
  int cn;

  SCM_VALIDATE_STRING_COPY (1, s, cstr);
  SCM_VALIDATE_INUM_COPY (2, n, cn);
  SCM_ASSERT_RANGE (2, n, cn >= 0 && cn <= SCM_STRING_LENGTH (s));

  return scm_mem2string (cstr + cn, SCM_STRING_LENGTH (s) - cn);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_take_right, "string-take-right", 2, 0, 0,
            (SCM s, SCM n),
	    "Return the @var{n} last characters of @var{s}.")
#define FUNC_NAME s_scm_string_take_right
{
  char * cstr;
  int cn;

  SCM_VALIDATE_STRING_COPY (1, s, cstr);
  SCM_VALIDATE_INUM_COPY (2, n, cn);
  SCM_ASSERT_RANGE (2, n, cn >= 0 && cn <= SCM_STRING_LENGTH (s));

  return scm_mem2string (cstr + SCM_STRING_LENGTH (s) - cn, cn);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_drop_right, "string-drop-right", 2, 0, 0,
            (SCM s, SCM n),
	    "Return all but the last @var{n} characters of @var{s}.")
#define FUNC_NAME s_scm_string_drop_right
{
  char * cstr;
  int cn;

  SCM_VALIDATE_STRING_COPY (1, s, cstr);
  SCM_VALIDATE_INUM_COPY (2, n, cn);
  SCM_ASSERT_RANGE (2, n, cn >= 0 && cn <= SCM_STRING_LENGTH (s));

  return scm_mem2string (cstr, SCM_STRING_LENGTH (s) - cn);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_pad, "string-pad", 2, 3, 0,
	    (SCM s, SCM len, SCM chr, SCM start, SCM end),
	    "Take that characters from @var{start} to @var{end} from the\n"
	    "string @var{s} and return a new string, right-padded by the\n"
	    "character @var{chr} to length @var{len}.  If the resulting\n"
	    "string is longer than @var{len}, it is truncated on the right.")
#define FUNC_NAME s_scm_string_pad
{
  char cchr;
  char * cstr;
  int cstart, cend, clen;
  SCM result;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    4, start, cstart,
				    5, end, cend);
  SCM_VALIDATE_INUM_COPY (2, len, clen);
  if (SCM_UNBNDP (chr))
    cchr = ' ';
  else
    {
      SCM_VALIDATE_CHAR (3, chr);
      cchr = SCM_CHAR (chr);
    }
  result = scm_allocate_string (clen);
  if (clen < (cend - cstart))
    memmove (SCM_STRING_CHARS (result),
	     cstr + cend - clen,
	     clen * sizeof (char));
  else
    {
      memset (SCM_STRING_CHARS (result), cchr,
	      (clen - (cend - cstart)) * sizeof (char));
      memmove (SCM_STRING_CHARS (result) + (clen - (cend - cstart)),
	       cstr + cstart,
	       (cend - cstart) * sizeof (char));
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_pad_right, "string-pad-right", 2, 3, 0,
	    (SCM s, SCM len, SCM chr, SCM start, SCM end),
	    "Take that characters from @var{start} to @var{end} from the\n"
	    "string @var{s} and return a new string, left-padded by the\n"
	    "character @var{chr} to length @var{len}.  If the resulting\n"
	    "string is longer than @var{len}, it is truncated on the left.")
#define FUNC_NAME s_scm_string_pad_right
{
  char cchr;
  char * cstr;
  int cstart, cend, clen;
  SCM result;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    4, start, cstart,
				    5, end, cend);
  SCM_VALIDATE_INUM_COPY (2, len, clen);
  if (SCM_UNBNDP (chr))
    cchr = ' ';
  else
    {
      SCM_VALIDATE_CHAR (3, chr);
      cchr = SCM_CHAR (chr);
    }
  result = scm_allocate_string (clen);
  if (clen < (cend - cstart))
    memmove (SCM_STRING_CHARS (result), cstr + cstart, clen * sizeof (char));
  else
    {
      memset (SCM_STRING_CHARS (result) + (cend - cstart),
	      cchr, (clen - (cend - cstart)) * sizeof (char));
      memmove (SCM_STRING_CHARS (result), cstr + cstart,
	       (cend - cstart) * sizeof (char));
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_trim, "string-trim", 1, 3, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Trim @var{s} by skipping over all characters on the left\n"
	    "that satisfy the parameter @var{char_pred}:\n"
	    "\n"
	    "@itemize @bullet\n"
	    "@item\n"
	    "if it is the character @var{ch}, characters equal to\n"
	    "@var{ch} are trimmed,\n"
	    "\n"
	    "@item\n"
	    "if it is a procedure @var{pred} characters that\n"
	    "satisfy @var{pred} are trimmed,\n"
	    "\n"
	    "@item\n"
	    "if it is a character set, characters in that set are trimmed.\n"
	    "@end itemize\n"
	    "\n"
	    "If called without a @var{char_pred} argument, all whitespace is\n"
	    "trimmed.")
#define FUNC_NAME s_scm_string_trim
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_UNBNDP (char_pred))
    {
      while (cstart < cend)
	{
	  if (!isspace(cstr[cstart]))
	    break;
	  cstart++;
	}
    }
  else if (SCM_CHARP (char_pred))
    {
      char chr = SCM_CHAR (char_pred);
      while (cstart < cend)
	{
	  if (chr != cstr[cstart])
	    break;
	  cstart++;
	}
    }
  else if (SCM_CHARSETP (char_pred))
    {
      while (cstart < cend)
	{
	  if (!SCM_CHARSET_GET (char_pred, cstr[cstart]))
	    break;
	  cstart++;
	}
    }
  else
    {
      SCM_VALIDATE_PROC (2, char_pred);
      while (cstart < cend)
	{
	  SCM res;

	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
	  if (SCM_FALSEP (res))
	    break;
	  cstart++;
	}
    }
  return scm_mem2string (cstr + cstart, cend - cstart);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_trim_right, "string-trim-right", 1, 3, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Trim @var{s} by skipping over all characters on the rightt\n"
	    "that satisfy the parameter @var{char_pred}:\n"
	    "\n"
	    "@itemize @bullet\n"
	    "@item\n"
	    "if it is the character @var{ch}, characters equal to @var{ch}\n"
	    "are trimmed,\n"
	    "\n"
	    "@item\n"
	    "if it is a procedure @var{pred} characters that satisfy\n"
	    "@var{pred} are trimmed,\n"
	    "\n"
	    "@item\n"
	    "if it is a character sets, all characters in that set are\n"
	    "trimmed.\n"
	    "@end itemize\n"
	    "\n"
	    "If called without a @var{char_pred} argument, all whitespace is\n"
	    "trimmed.")
#define FUNC_NAME s_scm_string_trim_right
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_UNBNDP (char_pred))
    {
      while (cstart < cend)
	{
	  if (!isspace(cstr[cend - 1]))
	    break;
	  cend--;
	}
    }
  else if (SCM_CHARP (char_pred))
    {
      char chr = SCM_CHAR (char_pred);
      while (cstart < cend)
	{
	  if (chr != cstr[cend - 1])
	    break;
	  cend--;
	}
    }
  else if (SCM_CHARSETP (char_pred))
    {
      while (cstart < cend)
	{
	  if (!SCM_CHARSET_GET (char_pred, cstr[cend - 1]))
	    break;
	  cend--;
	}
    }
  else
    {
      SCM_VALIDATE_PROC (2, char_pred);
      while (cstart < cend)
	{
	  SCM res;

	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cend - 1]));
	  if (SCM_FALSEP (res))
	    break;
	  cend--;
	}
    }
  return scm_mem2string (cstr + cstart, cend - cstart);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_trim_both, "string-trim-both", 1, 3, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Trim @var{s} by skipping over all characters on both sides of\n"
	    "the string that satisfy the parameter @var{char_pred}:\n"
	    "\n"
	    "@itemize @bullet\n"
	    "@item\n"
	    "if it is the character @var{ch}, characters equal to @var{ch}\n"
	    "are trimmed,\n"
	    "\n"
	    "@item\n"
	    "if it is a procedure @var{pred} characters that satisfy\n"
	    "@var{pred} are trimmed,\n"
	    "\n"
	    "@item\n"
	    "if it is a character set, the characters in the set are\n"
	    "trimmed.\n"
	    "@end itemize\n"
	    "\n"
	    "If called without a @var{char_pred} argument, all whitespace is\n"
	    "trimmed.")
#define FUNC_NAME s_scm_string_trim_both
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_UNBNDP (char_pred))
    {
      while (cstart < cend)
	{
	  if (!isspace(cstr[cstart]))
	    break;
	  cstart++;
	}
      while (cstart < cend)
	{
	  if (!isspace(cstr[cend - 1]))
	    break;
	  cend--;
	}
    }
  else if (SCM_CHARP (char_pred))
    {
      char chr = SCM_CHAR (char_pred);
      while (cstart < cend)
	{
	  if (chr != cstr[cstart])
	    break;
	  cstart++;
	}
      while (cstart < cend)
	{
	  if (chr != cstr[cend - 1])
	    break;
	  cend--;
	}
    }
  else if (SCM_CHARSETP (char_pred))
    {
      while (cstart < cend)
	{
	  if (!SCM_CHARSET_GET (char_pred, cstr[cstart]))
	    break;
	  cstart++;
	}
      while (cstart < cend)
	{
	  if (!SCM_CHARSET_GET (char_pred, cstr[cend - 1]))
	    break;
	  cend--;
	}
    }
  else
    {
      SCM_VALIDATE_PROC (2, char_pred);
      while (cstart < cend)
	{
	  SCM res;

	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
	  if (SCM_FALSEP (res))
	    break;
	  cstart++;
	}
      while (cstart < cend)
	{
	  SCM res;

	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cend - 1]));
	  if (SCM_FALSEP (res))
	    break;
	  cend--;
	}
    }
  return scm_mem2string (cstr + cstart, cend - cstart);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_fill_xS, "string-fill!", 2, 2, 0,
	    (SCM str, SCM chr, SCM start, SCM end),
	    "Stores @var{chr} in every element of the given @var{str} and\n"
	    "returns an unspecified value.")
#define FUNC_NAME s_scm_string_fill_xS
{
  char * cstr;
  int cstart, cend;
  int c;
  long k;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    3, start, cstart,
				    4, end, cend);
  SCM_VALIDATE_CHAR_COPY (2, chr, c);
  for (k = cstart; k < cend; k++)
    cstr[k] = c;
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_compare, "string-compare", 5, 4, 0,
	    (SCM s1, SCM s2, SCM proc_lt, SCM proc_eq, SCM proc_gt, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the\n"
	    "mismatch index, depending upon whether @var{s1} is less than,\n"
	    "equal to, or greater than @var{s2}.  The mismatch index is the\n"
	    "largest index @var{i} such that for every 0 <= @var{j} <\n"
	    "@var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,\n"
	    "@var{i} is the first position that does not match.")
#define FUNC_NAME s_scm_string_compare
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    6, start1, cstart1,
				    7, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    8, start2, cstart2,
				    9, end2, cend2);
  SCM_VALIDATE_PROC (3, proc_lt);
  SCM_VALIDATE_PROC (4, proc_eq);
  SCM_VALIDATE_PROC (5, proc_gt);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (cstr1[cstart1] < cstr2[cstart2])
	return scm_call_1 (proc_lt, SCM_MAKINUM (cstart1));
      else if (cstr1[cstart1] > cstr2[cstart2])
	return scm_call_1 (proc_gt, SCM_MAKINUM (cstart1));
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return scm_call_1 (proc_gt, SCM_MAKINUM (cstart1));
  else if (cstart2 < cend2)
    return scm_call_1 (proc_lt, SCM_MAKINUM (cstart1));
  else
    return scm_call_1 (proc_eq, SCM_MAKINUM (cstart1));
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_compare_ci, "string-compare-ci", 5, 4, 0,
	    (SCM s1, SCM s2, SCM proc_lt, SCM proc_eq, SCM proc_gt, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the\n"
	    "mismatch index, depending upon whether @var{s1} is less than,\n"
	    "equal to, or greater than @var{s2}.  The mismatch index is the\n"
	    "largest index @var{i} such that for every 0 <= @var{j} <\n"
	    "@var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,\n"
	    "@var{i} is the first position that does not match.  The\n"
	    "character comparison is done case-insensitively.")
#define FUNC_NAME s_scm_string_compare_ci
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    6, start1, cstart1,
				    7, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    8, start2, cstart2,
				    9, end2, cend2);
  SCM_VALIDATE_PROC (3, proc_lt);
  SCM_VALIDATE_PROC (4, proc_eq);
  SCM_VALIDATE_PROC (5, proc_gt);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (scm_downcase (cstr1[cstart1]) < scm_downcase (cstr2[cstart2]))
	return scm_call_1 (proc_lt, SCM_MAKINUM (cstart1));
      else if (scm_downcase (cstr1[cstart1]) > scm_downcase (cstr2[cstart2]))
	return scm_call_1 (proc_gt, SCM_MAKINUM (cstart1));
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return scm_call_1 (proc_gt, SCM_MAKINUM (cstart1));
  else if (cstart2 < cend2)
    return scm_call_1 (proc_lt, SCM_MAKINUM (cstart1));
  else
    return scm_call_1 (proc_eq, SCM_MAKINUM (cstart1));
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_eq, "string=", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} and @var{s2} are not equal, a true\n"
	    "value otherwise.")
#define FUNC_NAME s_scm_string_eq
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (cstr1[cstart1] < cstr2[cstart2])
	return SCM_BOOL_F;
      else if (cstr1[cstart1] > cstr2[cstart2])
	return SCM_BOOL_F;
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_BOOL_F;
  else if (cstart2 < cend2)
    return SCM_BOOL_F;
  else
    return SCM_MAKINUM (cstart1);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_neq, "string<>", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} and @var{s2} are equal, a true\n"
	    "value otherwise.")
#define FUNC_NAME s_scm_string_neq
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (cstr1[cstart1] < cstr2[cstart2])
	return SCM_MAKINUM (cstart1);
      else if (cstr1[cstart1] > cstr2[cstart2])
	return SCM_MAKINUM (cstart1);
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_MAKINUM (cstart1);
  else if (cstart2 < cend2)
    return SCM_MAKINUM (cstart1);
  else
    return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_lt, "string<", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a\n"
	    "true value otherwise.")
#define FUNC_NAME s_scm_string_lt
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (cstr1[cstart1] < cstr2[cstart2])
	return SCM_MAKINUM (cstart1);
      else if (cstr1[cstart1] > cstr2[cstart2])
	return SCM_BOOL_F;
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_BOOL_F;
  else if (cstart2 < cend2)
    return SCM_MAKINUM (cstart1);
  else
    return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_gt, "string>", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} is less or equal to @var{s2}, a\n"
	    "true value otherwise.")
#define FUNC_NAME s_scm_string_gt
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (cstr1[cstart1] < cstr2[cstart2])
	return SCM_BOOL_F;
      else if (cstr1[cstart1] > cstr2[cstart2])
	return SCM_MAKINUM (cstart1);
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_MAKINUM (cstart1);
  else if (cstart2 < cend2)
    return SCM_BOOL_F;
  else
    return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_le, "string<=", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} is greater to @var{s2}, a true\n"
	    "value otherwise.")
#define FUNC_NAME s_scm_string_le
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (cstr1[cstart1] < cstr2[cstart2])
	return SCM_MAKINUM (cstart1);
      else if (cstr1[cstart1] > cstr2[cstart2])
	return SCM_BOOL_F;
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_BOOL_F;
  else if (cstart2 < cend2)
    return SCM_MAKINUM (cstart1);
  else
    return SCM_MAKINUM (cstart1);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_ge, "string>=", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} is less to @var{s2}, a true value\n"
	    "otherwise.")
#define FUNC_NAME s_scm_string_ge
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (cstr1[cstart1] < cstr2[cstart2])
	return SCM_BOOL_F;
      else if (cstr1[cstart1] > cstr2[cstart2])
	return SCM_MAKINUM (cstart1);
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_MAKINUM (cstart1);
  else if (cstart2 < cend2)
    return SCM_BOOL_F;
  else
    return SCM_MAKINUM (cstart1);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_ci_eq, "string-ci=", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} and @var{s2} are not equal, a true\n"
	    "value otherwise.  The character comparison is done\n"
	    "case-insensitively.")
#define FUNC_NAME s_scm_string_ci_eq
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (scm_downcase (cstr1[cstart1]) < scm_downcase (cstr2[cstart2]))
	return SCM_BOOL_F;
      else if (scm_downcase (cstr1[cstart1]) > scm_downcase (cstr2[cstart2]))
	return SCM_BOOL_F;
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_BOOL_F;
  else if (cstart2 < cend2)
    return SCM_BOOL_F;
  else
    return SCM_MAKINUM (cstart1);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_ci_neq, "string-ci<>", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} and @var{s2} are equal, a true\n"
	    "value otherwise.  The character comparison is done\n"
	    "case-insensitively.")
#define FUNC_NAME s_scm_string_ci_neq
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (scm_downcase (cstr1[cstart1]) < scm_downcase (cstr2[cstart2]))
	return SCM_MAKINUM (cstart1);
      else if (scm_downcase (cstr1[cstart1]) > scm_downcase (cstr2[cstart2]))
	return SCM_MAKINUM (cstart1);
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_MAKINUM (cstart1);
  else if (cstart2 < cend2)
    return SCM_MAKINUM (cstart1);
  else
    return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_ci_lt, "string-ci<", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a\n"
	    "true value otherwise.  The character comparison is done\n"
	    "case-insensitively.")
#define FUNC_NAME s_scm_string_ci_lt
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (scm_downcase (cstr1[cstart1]) < scm_downcase (cstr2[cstart2]))
	return SCM_MAKINUM (cstart1);
      else if (scm_downcase (cstr1[cstart1]) > scm_downcase (cstr2[cstart2]))
	return SCM_BOOL_F;
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_BOOL_F;
  else if (cstart2 < cend2)
    return SCM_MAKINUM (cstart1);
  else
    return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_ci_gt, "string-ci>", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} is less or equal to @var{s2}, a\n"
	    "true value otherwise.  The character comparison is done\n"
	    "case-insensitively.")
#define FUNC_NAME s_scm_string_ci_gt
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (scm_downcase (cstr1[cstart1]) < scm_downcase (cstr2[cstart2]))
	return SCM_BOOL_F;
      else if (scm_downcase (cstr1[cstart1]) > scm_downcase (cstr2[cstart2]))
	return SCM_MAKINUM (cstart1);
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_MAKINUM (cstart1);
  else if (cstart2 < cend2)
    return SCM_BOOL_F;
  else
    return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_ci_le, "string-ci<=", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} is greater to @var{s2}, a true\n"
	    "value otherwise.  The character comparison is done\n"
	    "case-insensitively.")
#define FUNC_NAME s_scm_string_ci_le
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (scm_downcase (cstr1[cstart1]) < scm_downcase (cstr2[cstart2]))
	return SCM_MAKINUM (cstart1);
      else if (scm_downcase (cstr1[cstart1]) > scm_downcase (cstr2[cstart2]))
	return SCM_BOOL_F;
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_BOOL_F;
  else if (cstart2 < cend2)
    return SCM_MAKINUM (cstart1);
  else
    return SCM_MAKINUM (cstart1);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_ci_ge, "string-ci>=", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return @code{#f} if @var{s1} is less to @var{s2}, a true value\n"
	    "otherwise.  The character comparison is done\n"
	    "case-insensitively.")
#define FUNC_NAME s_scm_string_ci_ge
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);

  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (scm_downcase (cstr1[cstart1]) < scm_downcase (cstr2[cstart2]))
	return SCM_BOOL_F;
      else if (scm_downcase (cstr1[cstart1]) > scm_downcase (cstr2[cstart2]))
	return SCM_MAKINUM (cstart1);
      cstart1++;
      cstart2++;
    }
  if (cstart1 < cend1)
    return SCM_MAKINUM (cstart1);
  else if (cstart2 < cend2)
    return SCM_BOOL_F;
  else
    return SCM_MAKINUM (cstart1);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_prefix_length, "string-prefix-length", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return the length of the longest common prefix of the two\n"
	    "strings.")
#define FUNC_NAME s_scm_string_prefix_length
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;
  int len = 0;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);
  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (cstr1[cstart1] != cstr2[cstart2])
	return SCM_MAKINUM (len);
      len++;
      cstart1++;
      cstart2++;
    }
  return SCM_MAKINUM (len);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_prefix_length_ci, "string-prefix-length-ci", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return the length of the longest common prefix of the two\n"
	    "strings, ignoring character case.")
#define FUNC_NAME s_scm_string_prefix_length_ci
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;
  int len = 0;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);
  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (scm_downcase (cstr1[cstart1]) != scm_downcase (cstr2[cstart2]))
	return SCM_MAKINUM (len);
      len++;
      cstart1++;
      cstart2++;
    }
  return SCM_MAKINUM (len);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_suffix_length, "string-suffix-length", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return the length of the longest common suffix of the two\n"
	    "strings.")
#define FUNC_NAME s_scm_string_suffix_length
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;
  int len = 0;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);
  while (cstart1 < cend1 && cstart2 < cend2)
    {
      cend1--;
      cend2--;
      if (cstr1[cend1] != cstr2[cend2])
	return SCM_MAKINUM (len);
      len++;
    }
  return SCM_MAKINUM (len);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_suffix_length_ci, "string-suffix-length-ci", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return the length of the longest common suffix of the two\n"
	    "strings, ignoring character case.")
#define FUNC_NAME s_scm_string_suffix_length_ci
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;
  int len = 0;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);
  while (cstart1 < cend1 && cstart2 < cend2)
    {
      cend1--;
      cend2--;
      if (scm_downcase (cstr1[cend1]) != scm_downcase (cstr2[cend2]))
	return SCM_MAKINUM (len);
      len++;
    }
  return SCM_MAKINUM (len);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_prefix_p, "string-prefix?", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Is @var{s1} a prefix of @var{s2}?")
#define FUNC_NAME s_scm_string_prefix_p
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;
  int len = 0, len1;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);
  len1 = cend1 - cstart1;
  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (cstr1[cstart1] != cstr2[cstart2])
	return SCM_BOOL (len == len1);
      len++;
      cstart1++;
      cstart2++;
    }
  return SCM_BOOL (len == len1);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_prefix_ci_p, "string-prefix-ci?", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Is @var{s1} a prefix of @var{s2}, ignoring character case?")
#define FUNC_NAME s_scm_string_prefix_ci_p
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;
  int len = 0, len1;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);
  len1 = cend1 - cstart1;
  while (cstart1 < cend1 && cstart2 < cend2)
    {
      if (scm_downcase (cstr1[cstart1]) != scm_downcase (cstr2[cstart2]))
	return SCM_BOOL (len == len1);
      len++;
      cstart1++;
      cstart2++;
    }
  return SCM_BOOL (len == len1);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_suffix_p, "string-suffix?", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Is @var{s1} a suffix of @var{s2}?")
#define FUNC_NAME s_scm_string_suffix_p
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;
  int len = 0, len1;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);
  len1 = cend1 - cstart1;
  while (cstart1 < cend1 && cstart2 < cend2)
    {
      cend1--;
      cend2--;
      if (cstr1[cend1] != cstr2[cend2])
	return SCM_BOOL (len == len1);
      len++;
    }
  return SCM_BOOL (len == len1);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_suffix_ci_p, "string-suffix-ci?", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Is @var{s1} a suffix of @var{s2}, ignoring character case?")
#define FUNC_NAME s_scm_string_suffix_ci_p
{
  char * cstr1, * cstr2;
  int cstart1, cend1, cstart2, cend2;
  int len = 0, len1;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);
  len1 = cend1 - cstart1;
  while (cstart1 < cend1 && cstart2 < cend2)
    {
      cend1--;
      cend2--;
      if (scm_downcase (cstr1[cend1]) != scm_downcase (cstr2[cend2]))
	return SCM_BOOL (len == len1);
      len++;
    }
  return SCM_BOOL (len == len1);
}
#undef FUNC_NAME


/* FIXME::martin: The `S' is to avoid a name clash with the procedure
   in the core, which does not accept a predicate. */
SCM_DEFINE (scm_string_indexS, "string-index", 2, 2, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Search through the string @var{s} from left to right, returning\n"
	    "the index of the first occurence of a character which\n"
	    "\n"
	    "@itemize @bullet\n"
	    "@item\n"
	    "equals @var{char_pred}, if it is character,\n"
	    "\n"
	    "@item\n"
	    "satisifies the predicate @var{char_pred}, if it is a procedure,\n"
	    "\n"
	    "@item\n"
	    "is in the set @var{char_pred}, if it is a character set.\n"
	    "@end itemize")
#define FUNC_NAME s_scm_string_indexS
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_CHARP (char_pred))
    {
      char cchr = SCM_CHAR (char_pred);
      while (cstart < cend)
	{
	  if (cchr == cstr[cstart])
	    return SCM_MAKINUM (cstart);
	  cstart++;
	}
    }
  else if (SCM_CHARSETP (char_pred))
    {
      while (cstart < cend)
	{
	  if (SCM_CHARSET_GET (char_pred, cstr[cstart]))
	    return SCM_MAKINUM (cstart);
	  cstart++;
	}
    }
  else
    {
      SCM_VALIDATE_PROC (2, char_pred);
      while (cstart < cend)
	{
	  SCM res;
	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
	  if (!SCM_FALSEP (res))
	    return SCM_MAKINUM (cstart);
	  cstart++;
	}
    }
  return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_index_right, "string-index-right", 2, 2, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Search through the string @var{s} from right to left, returning\n"
	    "the index of the last occurence of a character which\n"
	    "\n"
	    "@itemize @bullet\n"
	    "@item\n"
	    "equals @var{char_pred}, if it is character,\n"
	    "\n"
	    "@item\n"
	    "satisifies the predicate @var{char_pred}, if it is a procedure,\n"
	    "\n"
	    "@item\n"
	    "is in the set if @var{char_pred} is a character set.\n"
	    "@end itemize")
#define FUNC_NAME s_scm_string_index_right
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_CHARP (char_pred))
    {
      char cchr = SCM_CHAR (char_pred);
      while (cstart < cend)
	{
	  cend--;
	  if (cchr == cstr[cend])
	    return SCM_MAKINUM (cend);
	}
    }
  else if (SCM_CHARSETP (char_pred))
    {
      while (cstart < cend)
	{
	  cend--;
	  if (SCM_CHARSET_GET (char_pred, cstr[cend]))
	    return SCM_MAKINUM (cend);
	}
    }
  else
    {
      SCM_VALIDATE_PROC (2, char_pred);
      while (cstart < cend)
	{
	  SCM res;
	  cend--;
	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cend]));
	  if (!SCM_FALSEP (res))
	    return SCM_MAKINUM (cend);
	}
    }
  return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_skip, "string-skip", 2, 2, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Search through the string @var{s} from left to right, returning\n"
	    "the index of the first occurence of a character which\n"
	    "\n"
	    "@itemize @bullet\n"
	    "@item\n"
	    "does not equal @var{char_pred}, if it is character,\n"
	    "\n"
	    "@item\n"
	    "does not satisify the predicate @var{char_pred}, if it is a\n"
	    "procedure,\n"
	    "\n"
	    "@item\n"
	    "is not in the set if @var{char_pred} is a character set.\n"
	    "@end itemize")
#define FUNC_NAME s_scm_string_skip
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_CHARP (char_pred))
    {
      char cchr = SCM_CHAR (char_pred);
      while (cstart < cend)
	{
	  if (cchr != cstr[cstart])
	    return SCM_MAKINUM (cstart);
	  cstart++;
	}
    }
  else if (SCM_CHARSETP (char_pred))
    {
      while (cstart < cend)
	{
	  if (!SCM_CHARSET_GET (char_pred, cstr[cstart]))
	    return SCM_MAKINUM (cstart);
	  cstart++;
	}
    }
  else
    {
      SCM_VALIDATE_PROC (2, char_pred);
      while (cstart < cend)
	{
	  SCM res;
	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
	  if (SCM_FALSEP (res))
	    return SCM_MAKINUM (cstart);
	  cstart++;
	}
    }
  return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_skip_right, "string-skip-right", 2, 2, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Search through the string @var{s} from right to left, returning\n"
	    "the index of the last occurence of a character which\n"
	    "\n"
	    "@itemize @bullet\n"
	    "@item\n"
	    "does not equal @var{char_pred}, if it is character,\n"
	    "\n"
	    "@item\n"
	    "does not satisifie the predicate @var{char_pred}, if it is a\n"
	    "procedure,\n"
	    "\n"
	    "@item\n"
	    "is not in the set if @var{char_pred} is a character set.\n"
	    "@end itemize")
#define FUNC_NAME s_scm_string_skip_right
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_CHARP (char_pred))
    {
      char cchr = SCM_CHAR (char_pred);
      while (cstart < cend)
	{
	  cend--;
	  if (cchr != cstr[cend])
	    return SCM_MAKINUM (cend);
	}
    }
  else if (SCM_CHARSETP (char_pred))
    {
      while (cstart < cend)
	{
	  cend--;
	  if (!SCM_CHARSET_GET (char_pred, cstr[cend]))
	    return SCM_MAKINUM (cend);
	}
    }
  else
    {
      SCM_VALIDATE_PROC (2, char_pred);
      while (cstart < cend)
	{
	  SCM res;
	  cend--;
	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cend]));
	  if (SCM_FALSEP (res))
	    return SCM_MAKINUM (cend);
	}
    }
  return SCM_BOOL_F;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_count, "string-count", 2, 2, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Return the count of the number of characters in the string\n"
	    "@var{s} which\n"
	    "\n"
	    "@itemize @bullet\n"
	    "@item\n"
	    "equals @var{char_pred}, if it is character,\n"
	    "\n"
	    "@item\n"
	    "satisifies the predicate @var{char_pred}, if it is a procedure.\n"
	    "\n"
	    "@item\n"
	    "is in the set @var{char_pred}, if it is a character set.\n"
	    "@end itemize")
#define FUNC_NAME s_scm_string_count
{
  char * cstr;
  int cstart, cend;
  int count = 0;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_CHARP (char_pred))
    {
      char cchr = SCM_CHAR (char_pred);
      while (cstart < cend)
	{
	  if (cchr == cstr[cstart])
	    count++;
	  cstart++;
	}
    }
  else if (SCM_CHARSETP (char_pred))
    {
      while (cstart < cend)
	{
	  if (SCM_CHARSET_GET (char_pred, cstr[cstart]))
	    count++;
	  cstart++;
	}
    }
  else
    {
      SCM_VALIDATE_PROC (2, char_pred);
      while (cstart < cend)
	{
	  SCM res;
	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
	  if (!SCM_FALSEP (res))
	    count++;
	  cstart++;
	}
    }
  return SCM_MAKINUM (count);
}
#undef FUNC_NAME


/* FIXME::martin: This should definitely get implemented more
   efficiently -- maybe with Knuth-Morris-Pratt, like in the reference
   implementation.  */
SCM_DEFINE (scm_string_contains, "string-contains", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Does string @var{s1} contain string @var{s2}?  Return the index\n"
	    "in @var{s1} where @var{s2} occurs as a substring, or false.\n"
	    "The optional start/end indices restrict the operation to the\n"
	    "indicated substrings.")
#define FUNC_NAME s_scm_string_contains
{
  char * cs1, * cs2;
  int cstart1, cend1, cstart2, cend2;
  int len2, i, j;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cs1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cs2,
				    5, start2, cstart2,
				    6, end2, cend2);
  len2 = cend2 - cstart2;
  while (cstart1 <= cend1 - len2)
    {
      i = cstart1;
      j = cstart2;
      while (i < cend1 && j < cend2 && cs1[i] == cs2[j])
	{
	  i++;
	  j++;
	}
      if (j == cend2)
	return SCM_MAKINUM (cstart1);
      cstart1++;
    }
  return SCM_BOOL_F;
}
#undef FUNC_NAME


/* FIXME::martin: This should definitely get implemented more
   efficiently -- maybe with Knuth-Morris-Pratt, like in the reference
   implementation.  */
SCM_DEFINE (scm_string_contains_ci, "string-contains-ci", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Does string @var{s1} contain string @var{s2}?  Return the index\n"
	    "in @var{s1} where @var{s2} occurs as a substring, or false.\n"
	    "The optional start/end indices restrict the operation to the\n"
	    "indicated substrings.  Character comparison is done\n"
	    "case-insensitively.")
#define FUNC_NAME s_scm_string_contains_ci
{
  char * cs1, * cs2;
  int cstart1, cend1, cstart2, cend2;
  int len2, i, j;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cs1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cs2,
				    5, start2, cstart2,
				    6, end2, cend2);
  len2 = cend2 - cstart2;
  while (cstart1 <= cend1 - len2)
    {
      i = cstart1;
      j = cstart2;
      while (i < cend1 && j < cend2 &&
	     scm_downcase (cs1[i]) == scm_downcase (cs2[j]))
	{
	  i++;
	  j++;
	}
      if (j == cend2)
	return SCM_MAKINUM (cstart1);
      cstart1++;
    }
  return SCM_BOOL_F;
}
#undef FUNC_NAME


/* Helper function for the string uppercase conversion functions.
 * No argument checking is performed.  */
static SCM
string_upcase_x (SCM v, int start, int end)
{
  unsigned long k;

  for (k = start; k < end; ++k)
    SCM_STRING_UCHARS (v) [k] = scm_upcase (SCM_STRING_UCHARS (v) [k]);

  return v;
}


/* FIXME::martin: The `S' is to avoid a name clash with the procedure
   in the core, which does not accept start/end indices */
SCM_DEFINE (scm_string_upcase_xS, "string-upcase!", 1, 2, 0,
	    (SCM str, SCM start, SCM end),
	    "Destructively upcase every character in @code{str}.\n"
	    "\n"
	    "@lisp\n"
	    "(string-upcase! y)\n"
	    "@result{} \"ARRDEFG\"\n"
	    "y\n"
	    "@result{} \"ARRDEFG\"\n"
	    "@end lisp")
#define FUNC_NAME s_scm_string_upcase_xS
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  return string_upcase_x (str, cstart, cend);
}
#undef FUNC_NAME


/* FIXME::martin: The `S' is to avoid a name clash with the procedure
   in the core, which does not accept start/end indices */
SCM_DEFINE (scm_string_upcaseS, "string-upcase", 1, 2, 0,
	    (SCM str, SCM start, SCM end),
	    "Upcase every character in @code{str}.")
#define FUNC_NAME s_scm_string_upcaseS
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  return string_upcase_x (scm_string_copy (str), cstart, cend);
}
#undef FUNC_NAME


/* Helper function for the string lowercase conversion functions.
 * No argument checking is performed.  */
static SCM
string_downcase_x (SCM v, int start, int end)
{
  unsigned long k;

  for (k = start; k < end; ++k)
    SCM_STRING_UCHARS (v) [k] = scm_downcase (SCM_STRING_UCHARS (v) [k]);

  return v;
}


/* FIXME::martin: The `S' is to avoid a name clash with the procedure
   in the core, which does not accept start/end indices */
SCM_DEFINE (scm_string_downcase_xS, "string-downcase!", 1, 2, 0,
	    (SCM str, SCM start, SCM end),
	    "Destructively downcase every character in @var{str}.\n"
	    "\n"
	    "@lisp\n"
	    "y\n"
	    "@result{} \"ARRDEFG\"\n"
	    "(string-downcase! y)\n"
	    "@result{} \"arrdefg\"\n"
	    "y\n"
	    "@result{} \"arrdefg\"\n"
	    "@end lisp")
#define FUNC_NAME s_scm_string_downcase_xS
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  return string_downcase_x (str, cstart, cend);
}
#undef FUNC_NAME


/* FIXME::martin: The `S' is to avoid a name clash with the procedure
   in the core, which does not accept start/end indices */
SCM_DEFINE (scm_string_downcaseS, "string-downcase", 1, 2, 0,
	    (SCM str, SCM start, SCM end),
	    "Downcase every character in @var{str}.")
#define FUNC_NAME s_scm_string_downcaseS
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  return string_downcase_x (scm_string_copy (str), cstart, cend);
}
#undef FUNC_NAME


/* Helper function for the string capitalization functions.
 * No argument checking is performed.  */
static SCM
string_titlecase_x (SCM str, int start, int end)
{
  char * sz;
  int i, in_word = 0;

  sz = SCM_STRING_CHARS (str);
  for(i = start; i < end;  i++)
    {
      if (!SCM_FALSEP (scm_char_alphabetic_p (SCM_MAKE_CHAR (sz[i]))))
	{
	  if (!in_word)
	    {
	      sz[i] = scm_upcase(sz[i]);
	      in_word = 1;
	    }
	  else
	    {
	      sz[i] = scm_downcase(sz[i]);
	    }
	}
      else
	in_word = 0;
    }
  return str;
}


SCM_DEFINE (scm_string_titlecase_x, "string-titlecase!", 1, 2, 0,
	    (SCM str, SCM start, SCM end),
	    "Destructively titlecase every first character in a word in\n"
	    "@var{str}.")
#define FUNC_NAME s_scm_string_titlecase_x
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  return string_titlecase_x (str, cstart, cend);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_titlecase, "string-titlecase", 1, 2, 0,
	    (SCM str, SCM start, SCM end),
	    "Titlecase every first character in a word in @var{str}.")
#define FUNC_NAME s_scm_string_titlecase
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  return string_titlecase_x (scm_string_copy (str), cstart, cend);
}
#undef FUNC_NAME


/* Reverse the portion of @var{str} between str[cstart] (including)
   and str[cend] excluding.  */
static void
string_reverse_x (char * str, int cstart, int cend)
{
  char tmp;

  cend--;
  while (cstart < cend)
    {
      tmp = str[cstart];
      str[cstart] = str[cend];
      str[cend] = tmp;
      cstart++;
      cend--;
    }
}


SCM_DEFINE (scm_string_reverse, "string-reverse", 1, 2, 0,
            (SCM str, SCM start, SCM end),
	    "Reverse the string @var{str}.  The optional arguments\n"
	    "@var{start} and @var{end} delimit the region of @var{str} to\n"
	    "operate on.")
#define FUNC_NAME s_scm_string_reverse
{
  char * cstr;
  int cstart;
  int cend;
  SCM result;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  result = scm_string_copy (str);
  string_reverse_x (SCM_STRING_CHARS (result), cstart, cend);
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_reverse_x, "string-reverse!", 1, 2, 0,
            (SCM str, SCM start, SCM end),
	    "Reverse the string @var{str} in-place.  The optional arguments\n"
	    "@var{start} and @var{end} delimit the region of @var{str} to\n"
	    "operate on.  The return value is unspecified.")
#define FUNC_NAME s_scm_string_reverse_x
{
  char * cstr;
  int cstart;
  int cend;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
				    2, start, cstart,
				    3, end, cend);
  string_reverse_x (SCM_STRING_CHARS (str), cstart, cend);
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_append_shared, "string-append/shared", 0, 0, 1,
            (SCM ls),
	    "Like @code{string-append}, but the result may share memory\n"
	    "with the argument strings.")
#define FUNC_NAME s_scm_string_append_shared
{
  long i;

  SCM_VALIDATE_REST_ARGUMENT (ls);

  /* Optimize the one-argument case.  */
  i = scm_ilength (ls);
  if (i == 1)
    return SCM_CAR (ls);
  else
    return scm_string_append (ls);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_concatenate, "string-concatenate", 1, 0, 0,
            (SCM ls),
	    "Append the elements of @var{ls} (which must be strings)\n"
	    "together into a single string.  Guaranteed to return a freshly\n"
	    "allocated string.")
#define FUNC_NAME s_scm_string_concatenate
{
  long strings = scm_ilength (ls);
  SCM tmp, result;
  int len = 0;
  char * p;

  /* Validate the string list.  */
  if (strings < 0)
    SCM_WRONG_TYPE_ARG (1, ls);

  /* Calculate the size of the result string.  */
  tmp = ls;
  while (!SCM_NULLP (tmp))
    {
      SCM elt = SCM_CAR (tmp);
      SCM_VALIDATE_STRING (1, elt);
      len += SCM_STRING_LENGTH (elt);
      tmp = SCM_CDR (tmp);
    }
  result = scm_allocate_string (len);

  /* Copy the list elements into the result.  */
  p = SCM_STRING_CHARS (result);
  tmp = ls;
  while (!SCM_NULLP (tmp))
    {
      SCM elt = SCM_CAR (tmp);
      memmove (p, SCM_STRING_CHARS (elt),
	       SCM_STRING_LENGTH (elt) * sizeof (char));
      p += SCM_STRING_LENGTH (elt);
      tmp = SCM_CDR (tmp);
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_concatenate_reverse, "string-concatenate-reverse", 1, 2, 0,
            (SCM ls, SCM final_string, SCM end),
	    "Without optional arguments, this procedure is equivalent to\n"
	    "\n"
	    "@smalllisp\n"
	    "(string-concatenate (reverse ls))\n"
	    "@end smalllisp\n"
	    "\n"
	    "If the optional argument @var{final_string} is specified, it is\n"
	    "consed onto the beginning to @var{ls} before performing the\n"
	    "list-reverse and string-concatenate operations.  If @var{end}\n"
	    "is given, only the characters of @var{final_string} up to index\n"
	    "@var{end} are used.\n"
	    "\n"
	    "Guaranteed to return a freshly allocated string.")
#define FUNC_NAME s_scm_string_concatenate_reverse
{
  long strings;
  SCM tmp, result;
  int len = 0;
  char * p;
  int cend = 0;

  /* Check the optional arguments and calculate the additional length
     of the result string.  */
  if (!SCM_UNBNDP (final_string))
    {
      SCM_VALIDATE_STRING (2, final_string);
      if (!SCM_UNBNDP (end))
	{
	  SCM_VALIDATE_INUM_COPY (3, end, cend);
	  SCM_ASSERT_RANGE (3, end,
			    (cend >= 0) &&
			    (cend <= SCM_STRING_LENGTH (final_string)));
	}
      else
	{
	  cend = SCM_STRING_LENGTH (final_string);
	}
      len += cend;
    }
  strings = scm_ilength (ls);
  /* Validate the string list.  */
  if (strings < 0)
    SCM_WRONG_TYPE_ARG (1, ls);

  /* Calculate the length of the result string.  */
  tmp = ls;
  while (!SCM_NULLP (tmp))
    {
      SCM elt = SCM_CAR (tmp);
      SCM_VALIDATE_STRING (1, elt);
      len += SCM_STRING_LENGTH (elt);
      tmp = SCM_CDR (tmp);
    }

  result = scm_allocate_string (len);

  p = SCM_STRING_CHARS (result) + len;

  /* Construct the result string, possibly by using the optional final
     string.  */
  if (!SCM_UNBNDP (final_string))
    {
      p -= cend;
      memmove (p, SCM_STRING_CHARS (final_string), cend * sizeof (char));
    }
  tmp = ls;
  while (!SCM_NULLP (tmp))
    {
      SCM elt = SCM_CAR (tmp);
      p -= SCM_STRING_LENGTH (elt);
      memmove (p, SCM_STRING_CHARS (elt),
	       SCM_STRING_LENGTH (elt) * sizeof (char));
      tmp = SCM_CDR (tmp);
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_concatenate_shared, "string-concatenate/shared", 1, 0, 0,
            (SCM ls),
	    "Like @code{string-concatenate}, but the result may share memory\n"
	    "with the strings in the list @var{ls}.")
#define FUNC_NAME s_scm_string_concatenate_shared
{
  /* Optimize the one-string case.  */
  long i = scm_ilength (ls);
  if (i == 1)
    {
      SCM_VALIDATE_STRING (1, SCM_CAR (ls));
      return SCM_CAR (ls);
    }
  return scm_string_concatenate (ls);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_concatenate_reverse_shared, "string-concatenate-reverse/shared", 1, 2, 0,
            (SCM ls, SCM final_string, SCM end),
	    "Like @code{string-concatenate-reverse}, but the result may\n"
	    "share memory with the the strings in the @var{ls} arguments.")
#define FUNC_NAME s_scm_string_concatenate_reverse_shared
{
  /* Just call the non-sharing version.  */
  return scm_string_concatenate_reverse (ls, final_string, end);
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_map, "string-map", 2, 2, 0,
	    (SCM proc, SCM s, SCM start, SCM end),
	    "@var{proc} is a char->char procedure, it is mapped over\n"
	    "@var{s}.  The order in which the procedure is applied to the\n"
	    "string elements is not specified.")
#define FUNC_NAME s_scm_string_map
{
  char * cstr, *p;
  int cstart, cend;
  SCM result;

  SCM_VALIDATE_PROC (1, proc);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  result = scm_allocate_string (cend - cstart);
  p = SCM_STRING_CHARS (result);
  while (cstart < cend)
    {
      SCM ch = scm_call_1 (proc, SCM_MAKE_CHAR (cstr[cstart]));
      if (!SCM_CHARP (ch))
	SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
      cstart++;
      *p++ = SCM_CHAR (ch);
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_map_x, "string-map!", 2, 2, 0,
	    (SCM proc, SCM s, SCM start, SCM end),
	    "@var{proc} is a char->char procedure, it is mapped over\n"
	    "@var{s}.  The order in which the procedure is applied to the\n"
	    "string elements is not specified.  The string @var{s} is\n"
	    "modified in-place, the return value is not specified.")
#define FUNC_NAME s_scm_string_map_x
{
  char * cstr, *p;
  int cstart, cend;

  SCM_VALIDATE_PROC (1, proc);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  p = SCM_STRING_CHARS (s) + cstart;
  while (cstart < cend)
    {
      SCM ch = scm_call_1 (proc, SCM_MAKE_CHAR (cstr[cstart]));
      if (!SCM_CHARP (ch))
	SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
      cstart++;
      *p++ = SCM_CHAR (ch);
    }
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_fold, "string-fold", 3, 2, 0,
	    (SCM kons, SCM knil, SCM s, SCM start, SCM end),
	    "Fold @var{kons} over the characters of @var{s}, with @var{knil}\n"
	    "as the terminating element, from left to right.  @var{kons}\n"
	    "must expect two arguments: The actual character and the last\n"
	    "result of @var{kons}' application.")
#define FUNC_NAME s_scm_string_fold
{
  char * cstr;
  int cstart, cend;
  SCM result;

  SCM_VALIDATE_PROC (1, kons);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cstr,
				    4, start, cstart,
				    5, end, cend);
  result = knil;
  while (cstart < cend)
    {
      result = scm_call_2 (kons, SCM_MAKE_CHAR (cstr[cstart]), result);
      cstart++;
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_fold_right, "string-fold-right", 3, 2, 0,
	    (SCM kons, SCM knil, SCM s, SCM start, SCM end),
	    "Fold @var{kons} over the characters of @var{s}, with @var{knil}\n"
	    "as the terminating element, from right to left.  @var{kons}\n"
	    "must expect two arguments: The actual character and the last\n"
	    "result of @var{kons}' application.")
#define FUNC_NAME s_scm_string_fold_right
{
  char * cstr;
  int cstart, cend;
  SCM result;

  SCM_VALIDATE_PROC (1, kons);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cstr,
				    4, start, cstart,
				    5, end, cend);
  result = knil;
  while (cstart < cend)
    {
      result = scm_call_2 (kons, SCM_MAKE_CHAR (cstr[cend - 1]), result);
      cend--;
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_unfold, "string-unfold", 4, 2, 0,
	    (SCM p, SCM f, SCM g, SCM seed, SCM base, SCM make_final),
	    "@itemize @bullet\n"
	    "@item @var{g} is used to generate a series of @emph{seed}\n"
	    "values from the initial @var{seed}: @var{seed}, (@var{g}\n"
	    "@var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),\n"
	    "@dots{}\n"
	    "@item @var{p} tells us when to stop -- when it returns true\n"
	    "when applied to one of these seed values.\n"
	    "@item @var{f} maps each seed value to the corresponding\n"
	    "character in the result string.  These chars are assembled\n"
	    "into the string in a left-to-right order.\n"
	    "@item @var{base} is the optional initial/leftmost portion\n"
	    "of the constructed string; it default to the empty\n"
	    "string.\n"
	    "@item @var{make_final} is applied to the terminal seed\n"
	    "value (on which @var{p} returns true) to produce\n"
	    "the final/rightmost portion of the constructed string.\n"
	    "It defaults to @code{(lambda (x) "")}.\n"
	    "@end itemize")
#define FUNC_NAME s_scm_string_unfold
{
  SCM res, ans;

  SCM_VALIDATE_PROC (1, p);
  SCM_VALIDATE_PROC (2, f);
  SCM_VALIDATE_PROC (3, g);
  if (!SCM_UNBNDP (base))
    {
      SCM_VALIDATE_STRING (5, base);
      ans = base;
    }
  else
    ans = scm_allocate_string (0);
  if (!SCM_UNBNDP (make_final))
    SCM_VALIDATE_PROC (6, make_final);

  res = scm_call_1 (p, seed);
  while (SCM_FALSEP (res))
    {
      SCM str;
      SCM ch = scm_call_1 (f, seed);
      if (!SCM_CHARP (ch))
	SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f));
      str = scm_allocate_string (1);
      *SCM_STRING_CHARS (str) = SCM_CHAR (ch);

      ans = scm_string_append (scm_list_2 (ans, str));
      seed = scm_call_1 (g, seed);
      res = scm_call_1 (p, seed);
    }
  if (!SCM_UNBNDP (make_final))
    {
      res = scm_call_1 (make_final, seed);
      return scm_string_append (scm_list_2 (ans, res));
    }
  else
    return ans;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_unfold_right, "string-unfold-right", 4, 2, 0,
	    (SCM p, SCM f, SCM g, SCM seed, SCM base, SCM make_final),
	    "@itemize @bullet\n"
	    "@item @var{g} is used to generate a series of @emph{seed}\n"
	    "values from the initial @var{seed}: @var{seed}, (@var{g}\n"
	    "@var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),\n"
	    "@dots{}\n"
	    "@item @var{p} tells us when to stop -- when it returns true\n"
	    "when applied to one of these seed values.\n"
	    "@item @var{f} maps each seed value to the corresponding\n"
	    "character in the result string.  These chars are assembled\n"
	    "into the string in a right-to-left order.\n"
	    "@item @var{base} is the optional initial/rightmost portion\n"
	    "of the constructed string; it default to the empty\n"
	    "string.\n"
	    "@item @var{make_final} is applied to the terminal seed\n"
	    "value (on which @var{p} returns true) to produce\n"
	    "the final/leftmost portion of the constructed string.\n"
	    "It defaults to @code{(lambda (x) "")}.\n"
	    "@end itemize")
#define FUNC_NAME s_scm_string_unfold_right
{
  SCM res, ans;

  SCM_VALIDATE_PROC (1, p);
  SCM_VALIDATE_PROC (2, f);
  SCM_VALIDATE_PROC (3, g);
  if (!SCM_UNBNDP (base))
    {
      SCM_VALIDATE_STRING (5, base);
      ans = base;
    }
  else
    ans = scm_allocate_string (0);
  if (!SCM_UNBNDP (make_final))
    SCM_VALIDATE_PROC (6, make_final);

  res = scm_call_1 (p, seed);
  while (SCM_FALSEP (res))
    {
      SCM str;
      SCM ch = scm_call_1 (f, seed);
      if (!SCM_CHARP (ch))
	SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f));
      str = scm_allocate_string (1);
      *SCM_STRING_CHARS (str) = SCM_CHAR (ch);

      ans = scm_string_append (scm_list_2 (str, ans));
      seed = scm_call_1 (g, seed);
      res = scm_call_1 (p, seed);
    }
  if (!SCM_UNBNDP (make_final))
    {
      res = scm_call_1 (make_final, seed);
      return scm_string_append (scm_list_2 (res, ans));
    }
  else
    return ans;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_for_each, "string-for-each", 2, 2, 0,
	    (SCM proc, SCM s, SCM start, SCM end),
	    "@var{proc} is mapped over @var{s} in left-to-right order.  The\n"
	    "return value is not specified.")
#define FUNC_NAME s_scm_string_for_each
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_PROC (1, proc);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  while (cstart < cend)
    {
      scm_call_1 (proc, SCM_MAKE_CHAR (cstr[cstart]));
      cstart++;
    }
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

SCM_DEFINE (scm_string_for_each_index, "string-for-each-index", 2, 2, 0,
	    (SCM proc, SCM s, SCM start, SCM end),
	    "@var{proc} is mapped over @var{s} in left-to-right order.  The\n"
	    "return value is not specified.")
#define FUNC_NAME s_scm_string_for_each
{
  char * cstr;
  int cstart, cend;

  SCM_VALIDATE_PROC (1, proc);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  while (cstart < cend)
    {
      scm_call_1 (proc, SCM_MAKINUM (cstart));
      cstart++;
    }
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

SCM_DEFINE (scm_xsubstring, "xsubstring", 2, 3, 0,
	    (SCM s, SCM from, SCM to, SCM start, SCM end),
	    "This is the @emph{extended substring} procedure that implements\n"
	    "replicated copying of a substring of some string.\n"
	    "\n"
	    "@var{s} is a string, @var{start} and @var{end} are optional\n"
	    "arguments that demarcate a substring of @var{s}, defaulting to\n"
	    "0 and the length of @var{s}.  Replicate this substring up and\n"
	    "down index space, in both the positive and negative directions.\n"
	    "@code{xsubstring} returns the substring of this string\n"
	    "beginning at index @var{from}, and ending at @var{to}, which\n"
	    "defaults to @var{from} + (@var{end} - @var{start}).")
#define FUNC_NAME s_scm_xsubstring
{
  char * cs, * p;
  int cstart, cend, cfrom, cto;
  SCM result;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cs,
				    4, start, cstart,
				    5, end, cend);
  SCM_VALIDATE_INUM_COPY (2, from, cfrom);
  SCM_VALIDATE_INUM_DEF_COPY (3, to, cfrom + (cend - cstart), cto);
  if (cstart == cend && cfrom != cto)
    SCM_MISC_ERROR ("start and end indices must not be equal", SCM_EOL);

  result = scm_allocate_string (cto - cfrom);

  p = SCM_STRING_CHARS (result);
  while (cfrom < cto)
    {
      int t = ((cfrom < 0) ? -cfrom : cfrom) % (cend - cstart);
      if (cfrom < 0)
	*p = cs[(cend - cstart) - t];
      else
	*p = cs[t];
      cfrom++;
      p++;
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_xcopy_x, "string-xcopy!", 4, 3, 0,
	    (SCM target, SCM tstart, SCM s, SCM sfrom, SCM sto, SCM start, SCM end),
	    "Exactly the same as @code{xsubstring}, but the extracted text\n"
	    "is written into the string @var{target} starting at index\n"
	    "@var{tstart}.  The operation is not defined if @code{(eq?\n"
	    "@var{target} @var{s})} or these arguments share storage -- you\n"
	    "cannot copy a string on top of itself.")
#define FUNC_NAME s_scm_string_xcopy_x
{
  char * ctarget, * cs, * p;
  int ctstart, csfrom, csto, cstart, cend;
  SCM dummy = SCM_UNDEFINED;
  int cdummy;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, target, ctarget,
				    2, tstart, ctstart,
				    2, dummy, cdummy);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cs,
				    6, start, cstart,
				    7, end, cend);
  SCM_VALIDATE_INUM_COPY (4, sfrom, csfrom);
  SCM_VALIDATE_INUM_DEF_COPY (5, sto, csfrom + (cend - cstart), csto);
  if (cstart == cend && csfrom != csto)
    SCM_MISC_ERROR ("start and end indices must not be equal", SCM_EOL);
  SCM_ASSERT_RANGE (1, tstart,
		    ctstart + (csto - csfrom) <= SCM_STRING_LENGTH (target));

  p = ctarget + ctstart;
  while (csfrom < csto)
    {
      int t = ((csfrom < 0) ? -csfrom : csfrom) % (cend - cstart);
      if (csfrom < 0)
	*p = cs[(cend - cstart) - t];
      else
	*p = cs[t];
      csfrom++;
      p++;
    }
  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_replace, "string-replace", 2, 4, 0,
	    (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
	    "Return the string @var{s1}, but with the characters\n"
	    "@var{start1} @dots{} @var{end1} replaced by the characters\n"
	    "@var{start2} @dots{} @var{end2} from @var{s2}.")
#define FUNC_NAME s_scm_string_replace
{
  char * cstr1, * cstr2, * p;
  int cstart1, cend1, cstart2, cend2;
  SCM result;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
				    3, start1, cstart1,
				    4, end1, cend1);
  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
				    5, start2, cstart2,
				    6, end2, cend2);
  result = scm_allocate_string (cstart1 + (cend2 - cstart2) +
				SCM_STRING_LENGTH (s1) - cend1);
  p = SCM_STRING_CHARS (result);
  memmove (p, cstr1, cstart1 * sizeof (char));
  memmove (p + cstart1, cstr2 + cstart2, (cend2 - cstart2) * sizeof (char));
  memmove (p + cstart1 + (cend2 - cstart2),
	   cstr1 + cend1,
	   (SCM_STRING_LENGTH (s1) - cend1) * sizeof (char));
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_tokenize, "string-tokenize", 1, 3, 0,
	    (SCM s, SCM token_char, SCM start, SCM end),
	    "Split the string @var{s} into a list of substrings, where each\n"
	    "substring is a maximal non-empty contiguous sequence of\n"
	    "characters equal to the character @var{token_char}, or\n"
	    "whitespace, if @var{token_char} is not given.  If\n"
	    "@var{token_char} is a character set, it is used for finding the\n"
	    "token borders.")
#define FUNC_NAME s_scm_string_tokenize
{
  char * cstr;
  int cstart, cend;
  SCM result = SCM_EOL;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_UNBNDP (token_char))
    {
      int idx;

      while (cstart < cend)
	{
	  while (cstart < cend)
	    {
	      if (!isspace (cstr[cend - 1]))
		break;
	      cend--;
	    }
	  if (cstart >= cend)
	    break;
	  idx = cend;
	  while (cstart < cend)
	    {
	      if (isspace (cstr[cend - 1]))
		break;
	      cend--;
	    }
	  result = scm_cons (scm_mem2string (cstr + cend, idx - cend), result);
	}
    }
  else if (SCM_CHARSETP (token_char))
    {
      int idx;

      while (cstart < cend)
	{
	  while (cstart < cend)
	    {
	      if (!SCM_CHARSET_GET (token_char, cstr[cend - 1]))
		break;
	      cend--;
	    }
	  if (cstart >= cend)
	    break;
	  idx = cend;
	  while (cstart < cend)
	    {
	      if (SCM_CHARSET_GET (token_char, cstr[cend - 1]))
		break;
	      cend--;
	    }
	  result = scm_cons (scm_mem2string (cstr + cend, idx - cend), result);
	}
    }
  else
    {
      int idx;
      char chr;

      SCM_VALIDATE_CHAR (2, token_char);
      chr = SCM_CHAR (token_char);

      while (cstart < cend)
	{
	  while (cstart < cend)
	    {
	      if (cstr[cend - 1] != chr)
		break;
	      cend--;
	    }
	  if (cstart >= cend)
	    break;
	  idx = cend;
	  while (cstart < cend)
	    {
	      if (cstr[cend - 1] == chr)
		break;
	      cend--;
	    }
	  result = scm_cons (scm_mem2string (cstr + cend, idx - cend), result);
	}
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_filter, "string-filter", 2, 2, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Filter the string @var{s}, retaining only those characters that\n"
	    "satisfy the @var{char_pred} argument.  If the argument is a\n"
	    "procedure, it is applied to each character as a predicate, if\n"
	    "it is a character, it is tested for equality and if it is a\n"
	    "character set, it is tested for membership.")
#define FUNC_NAME s_scm_string_filter
{
  char * cstr;
  int cstart, cend;
  SCM result;
  int idx;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_CHARP (char_pred))
    {
      SCM ls = SCM_EOL;
      char chr;

      chr = SCM_CHAR (char_pred);
      idx = cstart;
      while (idx < cend)
	{
	  if (cstr[idx] == chr)
	    ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
	  idx++;
	}
      result = scm_reverse_list_to_string (ls);
    }
  else if (SCM_CHARSETP (char_pred))
    {
      SCM ls = SCM_EOL;

      idx = cstart;
      while (idx < cend)
	{
	  if (SCM_CHARSET_GET (char_pred, cstr[idx]))
	    ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
	  idx++;
	}
      result = scm_reverse_list_to_string (ls);
    }
  else
    {
      SCM ls = SCM_EOL;

      SCM_VALIDATE_PROC (2, char_pred);
      idx = cstart;
      while (idx < cend)
	{
	  SCM res;
	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[idx]));
	  if (!SCM_FALSEP (res))
	    ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
	  idx++;
	}
      result = scm_reverse_list_to_string (ls);
    }
  return result;
}
#undef FUNC_NAME


SCM_DEFINE (scm_string_delete, "string-delete", 2, 2, 0,
	    (SCM s, SCM char_pred, SCM start, SCM end),
	    "Filter the string @var{s}, retaining only those characters that\n"
	    "do not satisfy the @var{char_pred} argument.  If the argument\n"
	    "is a procedure, it is applied to each character as a predicate,\n"
	    "if it is a character, it is tested for equality and if it is a\n"
	    "character set, it is tested for membership.")
#define FUNC_NAME s_scm_string_delete
{
  char * cstr;
  int cstart, cend;
  SCM result;
  int idx;

  SCM_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
				    3, start, cstart,
				    4, end, cend);
  if (SCM_CHARP (char_pred))
    {
      SCM ls = SCM_EOL;
      char chr;

      chr = SCM_CHAR (char_pred);
      idx = cstart;
      while (idx < cend)
	{
	  if (cstr[idx] != chr)
	    ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
	  idx++;
	}
      result = scm_reverse_list_to_string (ls);
    }
  else if (SCM_CHARSETP (char_pred))
    {
      SCM ls = SCM_EOL;

      idx = cstart;
      while (idx < cend)
	{
	  if (!SCM_CHARSET_GET (char_pred, cstr[idx]))
	    ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
	  idx++;
	}
      result = scm_reverse_list_to_string (ls);
    }
  else
    {
      SCM ls = SCM_EOL;

      SCM_VALIDATE_PROC (2, char_pred);
      idx = cstart;
      while (idx < cend)
	{
	  SCM res;
	  res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[idx]));
	  if (SCM_FALSEP (res))
	    ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
	  idx++;
	}
      result = scm_reverse_list_to_string (ls);
    }
  return result;
}
#undef FUNC_NAME


/* Initialize the SRFI-13 module.  This function will be called by the
   loading Scheme module.  */
void
scm_init_srfi_13 (void)
{
  /* We initialize the SRFI-14 module here, because the string
     primitives need the charset smob type created by that module.  */
  scm_c_init_srfi_14 ();

  /* Install the string primitives.  */
#ifndef SCM_MAGIC_SNARFER
#include "srfi/srfi-13.x"
#endif
}

/* End of srfi-13.c.  */