1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
|
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2001 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <rasmus@php.net> |
| Stig Sæther Bakken <ssb@fast.no> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
#include <stdio.h>
#include "php.h"
#include "reg.h"
#include "php_string.h"
#include "php_variables.h"
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
#ifdef HAVE_LANGINFO_H
# include <langinfo.h>
#endif
#include "scanf.h"
#include "zend_API.h"
#include "zend_execute.h"
#include "php_globals.h"
#include "basic_functions.h"
#include "php_smart_str.h"
#ifdef ZTS
#include "TSRM.h"
#endif
#define STR_PAD_LEFT 0
#define STR_PAD_RIGHT 1
#define STR_PAD_BOTH 2
#define PHP_PATHINFO_DIRNAME 0
#define PHP_PATHINFO_BASENAME 1
#define PHP_PATHINFO_EXTENSION 2
/* {{{ register_string_constants
*/
void register_string_constants(INIT_FUNC_ARGS)
{
REGISTER_LONG_CONSTANT("STR_PAD_LEFT", STR_PAD_LEFT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("STR_PAD_RIGHT", STR_PAD_RIGHT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("STR_PAD_BOTH", STR_PAD_BOTH, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PATHINFO_DIRNAME", PHP_PATHINFO_DIRNAME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PATHINFO_BASENAME", PHP_PATHINFO_BASENAME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PATHINFO_EXTENSION", PHP_PATHINFO_EXTENSION, CONST_CS | CONST_PERSISTENT);
#ifdef HAVE_LOCALECONV
/* If last members of struct lconv equal CHAR_MAX, no grouping is done */
/* This is bad, but since we are going to be hardcoding in the POSIX stuff anyway... */
# ifndef HAVE_LIMITS_H
# define CHAR_MAX 127
# endif
REGISTER_LONG_CONSTANT("CHAR_MAX", CHAR_MAX, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef HAVE_LOCALE_H
REGISTER_LONG_CONSTANT("LC_CTYPE", LC_CTYPE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("LC_NUMERIC", LC_NUMERIC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("LC_TIME", LC_TIME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("LC_COLLATE", LC_COLLATE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("LC_MONETARY", LC_MONETARY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("LC_ALL", LC_ALL, CONST_CS | CONST_PERSISTENT);
# ifdef LC_MESSAGES
REGISTER_LONG_CONSTANT("LC_MESSAGES", LC_MESSAGES, CONST_CS | CONST_PERSISTENT);
# endif
#endif
}
/* }}} */
int php_tag_find(char *tag, int len, char *set);
/* this is read-only, so it's ok */
static char hexconvtab[] = "0123456789abcdef";
/* localeconv mutex */
#ifdef ZTS
static MUTEX_T locale_mutex = NULL;
#endif
/* {{{ php_bin2hex
*/
static char *php_bin2hex(const unsigned char *old, const size_t oldlen, size_t *newlen)
{
unsigned char *result = NULL;
size_t i, j;
result = (char *) emalloc(oldlen * 2 * sizeof(char) + 1);
if(!result) {
return result;
}
for(i = j = 0; i < oldlen; i++) {
result[j++] = hexconvtab[old[i] >> 4];
result[j++] = hexconvtab[old[i] & 15];
}
result[j] = '\0';
if(newlen) *newlen = oldlen * 2 * sizeof(char);
return result;
}
/* }}} */
#ifdef HAVE_LOCALECONV
/* {{{ localeconv_r
* glibc's localeconv is not reentrant, so lets make it so ... sorta */
struct lconv *localeconv_r(struct lconv *out)
{
struct lconv *res;
# ifdef ZTS
tsrm_mutex_lock( locale_mutex );
# endif
/* localeconv doesn't return an error condition */
res = localeconv();
*out = *res;
# ifdef ZTS
tsrm_mutex_unlock( locale_mutex );
# endif
return out;
}
/* }}} */
# ifdef ZTS
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(localeconv)
{
locale_mutex = tsrm_mutex_alloc();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(localeconv)
{
tsrm_mutex_free( locale_mutex );
locale_mutex = NULL;
return SUCCESS;
}
/* }}} */
# endif
#endif
/* {{{ proto string bin2hex(string data)
Converts the binary representation of data to hex */
PHP_FUNCTION(bin2hex)
{
zval **data;
char *result;
size_t newlen;
if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &data) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(data);
result = php_bin2hex((*data)->value.str.val, (*data)->value.str.len, &newlen);
if(!result) {
RETURN_FALSE;
}
RETURN_STRINGL(result, newlen, 0);
}
/* }}} */
/* {{{ proto int strspn(string str, string mask)
Find length of initial segment consisting entirely of characters found in mask */
PHP_FUNCTION(strspn)
{
zval **s1,**s2;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &s1, &s2) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(s1);
convert_to_string_ex(s2);
RETURN_LONG(php_strspn((*s1)->value.str.val, (*s2)->value.str.val,
(*s1)->value.str.val + (*s1)->value.str.len,
(*s2)->value.str.val + (*s2)->value.str.len));
}
/* }}} */
/* {{{ proto int strcspn(string str, string mask)
Find length of initial segment consisting entirely of characters not found in mask */
PHP_FUNCTION(strcspn)
{
zval **s1,**s2;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &s1, &s2) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(s1);
convert_to_string_ex(s2);
RETURN_LONG(php_strcspn((*s1)->value.str.val, (*s2)->value.str.val,
(*s1)->value.str.val + (*s1)->value.str.len,
(*s2)->value.str.val + (*s2)->value.str.len));
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION(nl_langinfo) */
#if HAVE_NL_LANGINFO
PHP_MINIT_FUNCTION(nl_langinfo)
{
#define REGISTER_NL_LANGINFO_CONSTANT(x) REGISTER_LONG_CONSTANT(#x, x, CONST_CS | CONST_PERSISTENT)
#ifdef ABDAY_1
REGISTER_NL_LANGINFO_CONSTANT(ABDAY_1);
REGISTER_NL_LANGINFO_CONSTANT(ABDAY_2);
REGISTER_NL_LANGINFO_CONSTANT(ABDAY_3);
REGISTER_NL_LANGINFO_CONSTANT(ABDAY_4);
REGISTER_NL_LANGINFO_CONSTANT(ABDAY_5);
REGISTER_NL_LANGINFO_CONSTANT(ABDAY_6);
REGISTER_NL_LANGINFO_CONSTANT(ABDAY_7);
#endif
#ifdef DAY_1
REGISTER_NL_LANGINFO_CONSTANT(DAY_1);
REGISTER_NL_LANGINFO_CONSTANT(DAY_2);
REGISTER_NL_LANGINFO_CONSTANT(DAY_3);
REGISTER_NL_LANGINFO_CONSTANT(DAY_4);
REGISTER_NL_LANGINFO_CONSTANT(DAY_5);
REGISTER_NL_LANGINFO_CONSTANT(DAY_6);
REGISTER_NL_LANGINFO_CONSTANT(DAY_7);
#endif
#ifdef ABMON_1
REGISTER_NL_LANGINFO_CONSTANT(ABMON_1);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_2);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_3);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_4);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_5);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_6);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_7);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_8);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_9);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_10);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_11);
REGISTER_NL_LANGINFO_CONSTANT(ABMON_12);
#endif
#ifdef MON_1
REGISTER_NL_LANGINFO_CONSTANT(MON_1);
REGISTER_NL_LANGINFO_CONSTANT(MON_2);
REGISTER_NL_LANGINFO_CONSTANT(MON_3);
REGISTER_NL_LANGINFO_CONSTANT(MON_4);
REGISTER_NL_LANGINFO_CONSTANT(MON_5);
REGISTER_NL_LANGINFO_CONSTANT(MON_6);
REGISTER_NL_LANGINFO_CONSTANT(MON_7);
REGISTER_NL_LANGINFO_CONSTANT(MON_8);
REGISTER_NL_LANGINFO_CONSTANT(MON_9);
REGISTER_NL_LANGINFO_CONSTANT(MON_10);
REGISTER_NL_LANGINFO_CONSTANT(MON_11);
REGISTER_NL_LANGINFO_CONSTANT(MON_12);
#endif
#ifdef AM_STR
REGISTER_NL_LANGINFO_CONSTANT(AM_STR);
#endif
#ifdef PM_STR
REGISTER_NL_LANGINFO_CONSTANT(PM_STR);
#endif
#ifdef D_T_FMT
REGISTER_NL_LANGINFO_CONSTANT(D_T_FMT);
#endif
#ifdef D_FMT
REGISTER_NL_LANGINFO_CONSTANT(D_FMT);
#endif
#ifdef T_FMT
REGISTER_NL_LANGINFO_CONSTANT(T_FMT);
#endif
#ifdef T_FMT_AMPM
REGISTER_NL_LANGINFO_CONSTANT(T_FMT_AMPM);
#endif
#ifdef ERA
REGISTER_NL_LANGINFO_CONSTANT(ERA);
#endif
#ifdef ERA_YEAR
REGISTER_NL_LANGINFO_CONSTANT(ERA_YEAR);
#endif
#ifdef ERA_D_T_FMT
REGISTER_NL_LANGINFO_CONSTANT(ERA_D_T_FMT);
#endif
#ifdef ERA_D_FMT
REGISTER_NL_LANGINFO_CONSTANT(ERA_D_FMT);
#endif
#ifdef ERA_T_FMT
REGISTER_NL_LANGINFO_CONSTANT(ERA_T_FMT);
#endif
#ifdef ALT_DIGITS
REGISTER_NL_LANGINFO_CONSTANT(ALT_DIGITS);
#endif
#ifdef INT_CURR_SYMBOL
REGISTER_NL_LANGINFO_CONSTANT(INT_CURR_SYMBOL);
#endif
#ifdef CURRENCY_SYMBOL
REGISTER_NL_LANGINFO_CONSTANT(CURRENCY_SYMBOL);
#endif
#ifdef CRNCYSTR
REGISTER_NL_LANGINFO_CONSTANT(CRNCYSTR);
#endif
#ifdef MON_DECIMAL_POINT
REGISTER_NL_LANGINFO_CONSTANT(MON_DECIMAL_POINT);
#endif
#ifdef MON_THOUSANDS_SEP
REGISTER_NL_LANGINFO_CONSTANT(MON_THOUSANDS_SEP);
#endif
#ifdef MON_GROUPING
REGISTER_NL_LANGINFO_CONSTANT(MON_GROUPING);
#endif
#ifdef POSITIVE_SIGN
REGISTER_NL_LANGINFO_CONSTANT(POSITIVE_SIGN);
#endif
#ifdef NEGATIVE_SIGN
REGISTER_NL_LANGINFO_CONSTANT(NEGATIVE_SIGN);
#endif
#ifdef INT_FRAC_DIGITS
REGISTER_NL_LANGINFO_CONSTANT(INT_FRAC_DIGITS);
#endif
#ifdef FRAC_DIGITS
REGISTER_NL_LANGINFO_CONSTANT(FRAC_DIGITS);
#endif
#ifdef P_CS_PRECEDES
REGISTER_NL_LANGINFO_CONSTANT(P_CS_PRECEDES);
#endif
#ifdef P_SEP_BY_SPACE
REGISTER_NL_LANGINFO_CONSTANT(P_SEP_BY_SPACE);
#endif
#ifdef N_CS_PRECEDES
REGISTER_NL_LANGINFO_CONSTANT(N_CS_PRECEDES);
#endif
#ifdef N_SEP_BY_SPACE
REGISTER_NL_LANGINFO_CONSTANT(N_SEP_BY_SPACE);
#endif
#ifdef P_SIGN_POSN
REGISTER_NL_LANGINFO_CONSTANT(P_SIGN_POSN);
#endif
#ifdef N_SIGN_POSN
REGISTER_NL_LANGINFO_CONSTANT(N_SIGN_POSN);
#endif
#ifdef DECIMAL_POINT
REGISTER_NL_LANGINFO_CONSTANT(DECIMAL_POINT);
#endif
#ifdef RADIXCHAR
REGISTER_NL_LANGINFO_CONSTANT(RADIXCHAR);
#endif
#ifdef THOUSANDS_SEP
REGISTER_NL_LANGINFO_CONSTANT(THOUSANDS_SEP);
#endif
#ifdef THOUSEP
REGISTER_NL_LANGINFO_CONSTANT(THOUSEP);
#endif
#ifdef GROUPING
REGISTER_NL_LANGINFO_CONSTANT(GROUPING);
#endif
#ifdef YESEXPR
REGISTER_NL_LANGINFO_CONSTANT(YESEXPR);
#endif
#ifdef NOEXPR
REGISTER_NL_LANGINFO_CONSTANT(NOEXPR);
#endif
#ifdef YESSTR
REGISTER_NL_LANGINFO_CONSTANT(YESSTR);
#endif
#ifdef NOSTR
REGISTER_NL_LANGINFO_CONSTANT(NOSTR);
#endif
#ifdef CODESET
REGISTER_NL_LANGINFO_CONSTANT(CODESET);
#endif
#undef REGISTER_NL_LANGINFO_CONSTANT
return SUCCESS;
}
/* }}} */
PHP_FUNCTION(nl_langinfo)
{
zval ** item;
char * value;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &item) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(item);
value = nl_langinfo(Z_LVAL_PP(item));
if (value == NULL) {
RETURN_FALSE;
}
else {
RETURN_STRING(value, 1);
}
}
#endif
#ifdef HAVE_STRCOLL
/* {{{ proto int strcoll(string str1, string str2)
Compare two strings using the current locale */
PHP_FUNCTION(strcoll)
{
zval **s1, **s2;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &s1, &s2) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(s1);
convert_to_string_ex(s2);
RETURN_LONG(strcoll((const char *)(*s1)->value.str.val, (const char *)(*s2)->value.str.val));
}
/* }}} */
#endif
/* {{{ php_charmask
* Fills a 256-byte bytemask with input. You can specify a range like 'a..z',
* it needs to be incrementing.
* Returns: FAILURE/SUCCESS wether the input was correct (i.e. no range errors)
*/
int php_charmask(unsigned char *input, int len, char *mask TSRMLS_DC)
{
unsigned char *end;
unsigned char c;
int result = SUCCESS;
memset(mask, 0, 256);
for (end=input+len; input<end; input++) {
c=*input;
if (input+3<end && input[1] == '.' && input[2] == '.'
&& input[3] >= c) {
memset(mask+c, 1, input[3] - c + 1);
input+=3;
} else if (input+1<end && input[0] == '.' && input[1] == '.') {
/* Error, try to be as helpful as possible:
(a range ending/starting with '.' won't be captured here) */
if (end-len>=input) { /* there was no 'left' char */
php_error(E_WARNING,"Invalid '..'-range passed to %s(), no character to the left of '..'",get_active_function_name(TSRMLS_C));
result = FAILURE;
continue;
}
if (input+2>=end) { /* there is no 'right' char */
php_error(E_WARNING,"Invalid '..'-range passed to %s(), no character to the right of '..'",get_active_function_name(TSRMLS_C));
result = FAILURE;
continue;
}
if (input[-1] > input[2]) { /* wrong order */
php_error(E_WARNING,"Invalid '..'-range passed to %s(), '..'-range needs to be incrementing",get_active_function_name(TSRMLS_C));
result = FAILURE;
continue;
}
/* FIXME: better error (a..b..c is the only left possibility?) */
php_error(E_WARNING,"Invalid '..'-range passed to %s()",get_active_function_name(TSRMLS_C));
result = FAILURE;
continue;
} else {
mask[c]=1;
}
}
return result;
}
/* }}} */
/* {{{ php_trim
Compatibility function, ports old-API to new one. (DEPRECATED)
*/
void php_trim(zval *str, zval *return_value, int mode TSRMLS_DC)
{
php_trim2(str, NULL, return_value, mode TSRMLS_CC);
}
/* }}} */
/* {{{ php_trim2
*/
PHPAPI void php_trim2(zval *str, zval *what, zval *return_value, int mode TSRMLS_DC)
/* mode 1 : trim left
mode 2 : trim right
mode 3 : trim left and right
what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')
*/
{
register int i;
int len = str->value.str.len;
int trimmed = 0;
char *c = str->value.str.val;
char mask[256];
if (what) {
php_charmask(what->value.str.val, what->value.str.len, mask TSRMLS_CC);
} else {
php_charmask(" \n\r\t\v\0", 6, mask TSRMLS_CC);
}
if (mode & 1) {
for (i = 0; i < len; i++) {
if (mask[(unsigned char)c[i]]) {
trimmed++;
} else {
break;
}
}
len -= trimmed;
c += trimmed;
}
if (mode & 2) {
for (i = len - 1; i >= 0; i--) {
if (mask[(unsigned char)c[i]]) {
len--;
} else {
break;
}
}
}
RETVAL_STRINGL(c, len, 1);
}
/* }}} */
/* {{{ proto string rtrim(string str)
An alias for chop */
/* }}} */
/* {{{ proto string chop(string str)
Remove trailing whitespace */
PHP_FUNCTION(chop)
{
zval **str, **what;
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2)
WRONG_PARAM_COUNT;
zend_get_parameters_ex(ZEND_NUM_ARGS(), &str, &what);
convert_to_string_ex(str);
if (ZEND_NUM_ARGS() == 2)
convert_to_string_ex(str);
/* convert_to_string_ex never fails (last line: op->type = IS_STRING),
so, not checking for that. */
php_trim2(*str, ZEND_NUM_ARGS()==2?*what:NULL, return_value, 2 TSRMLS_CC);
}
/* }}} */
/* {{{ proto string trim(string str)
Strip whitespace from the beginning and end of a string */
PHP_FUNCTION(trim)
{
zval **str, **what;
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2)
WRONG_PARAM_COUNT;
zend_get_parameters_ex(ZEND_NUM_ARGS(), &str, &what);
convert_to_string_ex(str);
if (ZEND_NUM_ARGS() == 2)
convert_to_string_ex(str);
php_trim2(*str, ZEND_NUM_ARGS()==2?*what:NULL, return_value, 3 TSRMLS_CC);
}
/* }}} */
/* {{{ proto string ltrim(string str)
Strip whitespace from the beginning of a string */
PHP_FUNCTION(ltrim)
{
zval **str, **what;
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2)
WRONG_PARAM_COUNT;
zend_get_parameters_ex(ZEND_NUM_ARGS(), &str, &what);
convert_to_string_ex(str);
if (ZEND_NUM_ARGS() == 2)
convert_to_string_ex(str);
php_trim2(*str, ZEND_NUM_ARGS()==2?*what:NULL, return_value, 1 TSRMLS_CC);
}
/* }}} */
/* {{{ proto string wordwrap(string str [, int width [, string break [, int cut]]])
Wrap buffer to selected number of characters using string break char */
PHP_FUNCTION(wordwrap)
{
pval **ptext, **plinelength, **pbreakchar, **cut;
long i=0, l=0, pgr=0, linelength=0, last=0, breakcharlen, docut=0;
char *text, *breakchar, *newtext;
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 4 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &ptext, &plinelength, &pbreakchar, &cut) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(ptext);
if (Z_STRVAL_PP(ptext) == 0)
RETVAL_FALSE;
text = (*ptext)->value.str.val;
if (ZEND_NUM_ARGS() > 1) {
convert_to_long_ex(plinelength);
linelength = (*plinelength)->value.lval;
}
else {
linelength = 75;
}
if (ZEND_NUM_ARGS() > 2) {
convert_to_string_ex(pbreakchar);
breakchar = (*pbreakchar)->value.str.val;
breakcharlen = (*pbreakchar)->value.str.len;
}
else {
breakchar = "\n";
breakcharlen = 1;
}
if (ZEND_NUM_ARGS() > 3) {
convert_to_long_ex(cut);
docut = (*cut)->value.lval;
}
/* Special case for a single-character break as it needs no
additional storage space */
if (breakcharlen == 1 && docut == 0) {
newtext = estrdup (text);
while (newtext[i] != '\0') {
/* prescan line to see if it is greater than linelength */
l = 0;
while (newtext[i+l] != breakchar[0]) {
if (newtext[i+l] == '\0') {
l--;
break;
}
l++;
}
if (l >= linelength) {
pgr = l;
l = linelength;
/* needs breaking; work backwards to find previous word */
while (l >= 0) {
if (newtext[i+l] == ' ') {
newtext[i+l] = breakchar[0];
break;
}
l--;
}
if (l == -1) {
/* couldn't break is backwards, try looking forwards */
l = linelength;
while (l <= pgr) {
if(newtext[i+l] == ' ') {
newtext[i+l] = breakchar[0];
break;
}
l++;
}
}
}
i += l+1;
}
RETVAL_STRINGL(newtext, strlen(newtext), 1);
efree(newtext);
}
else {
/* Multiple character line break */
newtext = emalloc((*ptext)->value.str.len * (breakcharlen+1)+1);
newtext[0] = '\0';
i = 0;
while (text[i] != '\0') {
/* prescan line to see if it is greater than linelength */
l = 0;
while (text[i+l] != '\0') {
if (text[i+l] == breakchar[0]) {
if (breakcharlen == 1 || strncmp(text+i+l, breakchar, breakcharlen)==0)
break;
}
l++;
}
if (l >= linelength) {
pgr = l;
l = linelength;
/* needs breaking; work backwards to find previous word */
while (l >= 0) {
if (text[i+l] == ' ') {
strncat(newtext, text+last, i+l-last);
strcat(newtext, breakchar);
last = i + l + 1;
break;
}
l--;
}
if (l == -1) {
/* couldn't break it backwards, try looking forwards */
l = linelength - 1;
while (l <= pgr) {
if (docut == 0)
{
if (text[i+l] == ' ') {
strncat(newtext, text+last, i+l-last);
strcat(newtext, breakchar);
last = i + l + 1;
break;
}
}
if (docut == 1)
{
if (text[i+l] == ' ' || l > i-last) {
strncat(newtext, text+last, i+l-last+1);
strcat(newtext, breakchar);
last = i + l + 1;
break;
}
}
l++;
}
}
i += l+1;
}
else {
i += (l ? l : 1);
}
}
if (i+l > last) {
strcat(newtext, text+last);
}
RETVAL_STRINGL(newtext, strlen(newtext), 1);
efree(newtext);
}
}
/* }}} */
/* {{{ php_explode
*/
PHPAPI void php_explode(zval *delim, zval *str, zval *return_value, int limit)
{
char *p1, *p2, *endp;
endp = str->value.str.val + str->value.str.len;
p1 = str->value.str.val;
p2 = php_memnstr(str->value.str.val, delim->value.str.val, delim->value.str.len, endp);
if (p2 == NULL) {
add_next_index_stringl(return_value, p1, str->value.str.len, 1);
} else {
do {
add_next_index_stringl(return_value, p1, p2-p1, 1);
p1 = p2 + delim->value.str.len;
} while ((p2 = php_memnstr(p1, delim->value.str.val, delim->value.str.len, endp)) != NULL &&
(limit == -1 || --limit > 1));
if (p1 <= endp)
add_next_index_stringl(return_value, p1, endp-p1, 1);
}
}
/* }}} */
/* {{{ proto array explode(string separator, string str [, int limit])
Split a string on string separator and return array of components */
PHP_FUNCTION(explode)
{
zval **str, **delim, **zlimit = NULL;
int limit;
switch (ZEND_NUM_ARGS()) {
case 2:
if (zend_get_parameters_ex(2, &delim, &str) == FAILURE)
WRONG_PARAM_COUNT;
limit=-1;
break;
case 3:
if (zend_get_parameters_ex(3, &delim, &str, &zlimit) == FAILURE)
WRONG_PARAM_COUNT;
convert_to_long_ex(zlimit);
limit = (*zlimit)->value.lval;
break;
default:
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
convert_to_string_ex(delim);
if (! (*delim)->value.str.len) {
php_error(E_WARNING,"Empty delimiter");
RETURN_FALSE;
}
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
if(limit==0 || limit==1) {
add_index_stringl(return_value, 0, (*str)->value.str.val, (*str)->value.str.len, 1);
} else {
php_explode(*delim, *str, return_value, limit);
}
}
/* }}} */
/* {{{ proto string join(array src, string glue)
An alias for implode */
/* }}} */
/* {{{ php_implode
*/
PHPAPI void php_implode(zval *delim, zval *arr, zval *return_value)
{
zval **tmp;
int len = 0, count = 0, target = 0;
HashPosition pos;
/* convert everything to strings, and calculate length */
zend_hash_internal_pointer_reset_ex(arr->value.ht, &pos);
while (zend_hash_get_current_data_ex(arr->value.ht, (void **) &tmp, &pos) == SUCCESS) {
convert_to_string_ex(tmp);
len += (*tmp)->value.str.len;
if (count>0) {
len += delim->value.str.len;
}
count++;
zend_hash_move_forward_ex(arr->value.ht, &pos);
}
/* do it */
return_value->value.str.val = (char *) emalloc(len + 1);
return_value->value.str.val[0] = '\0';
return_value->value.str.val[len] = '\0';
zend_hash_internal_pointer_reset_ex(arr->value.ht, &pos);
while (zend_hash_get_current_data_ex(arr->value.ht, (void **) &tmp, &pos) == SUCCESS) {
count--;
memcpy(return_value->value.str.val + target, (*tmp)->value.str.val,
(*tmp)->value.str.len);
target += (*tmp)->value.str.len;
if (count > 0) {
memcpy(return_value->value.str.val + target, delim->value.str.val,
delim->value.str.len);
target += delim->value.str.len;
}
zend_hash_move_forward_ex(arr->value.ht, &pos);
}
return_value->type = IS_STRING;
return_value->value.str.len = len;
}
/* }}} */
/* {{{ proto string implode(array src, string glue)
Join array elements placing glue string between items and return one string */
PHP_FUNCTION(implode)
{
zval **arg1, **arg2, *delim, *arr;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) {
WRONG_PARAM_COUNT;
}
if ((*arg1)->type == IS_ARRAY) {
SEPARATE_ZVAL(arg1);
arr = *arg1;
convert_to_string_ex(arg2);
delim = *arg2;
} else if ((*arg2)->type == IS_ARRAY) {
SEPARATE_ZVAL(arg2)
arr = *arg2;
convert_to_string_ex(arg1);
delim = *arg1;
} else {
php_error(E_WARNING, "Bad arguments to %s()",
get_active_function_name(TSRMLS_C));
return;
}
php_implode(delim, arr, return_value);
}
/* }}} */
#define STRTOK_TABLE(p) BG(strtok_table)[(unsigned char) *p]
/* {{{ proto string strtok([string str,] string token)
Tokenize a string */
PHP_FUNCTION(strtok)
{
zval **args[2];
zval **tok, **str;
char *token;
char *token_end;
char *p;
char *pe;
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 ||
zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE)
WRONG_PARAM_COUNT;
switch (ZEND_NUM_ARGS()) {
case 1:
tok = args[0];
break;
case 2:
str = args[0];
tok = args[1];
convert_to_string_ex(str);
zval_add_ref(str);
if (BG(strtok_zval))
zval_ptr_dtor(&BG(strtok_zval));
BG(strtok_zval) = *str;
BG(strtok_last) = BG(strtok_string) = Z_STRVAL_PP(str);
BG(strtok_len) = Z_STRLEN_PP(str);
break;
}
p = BG(strtok_last); /* Where we start to search */
pe = BG(strtok_string) + BG(strtok_len);
if (!p || p >= pe)
RETURN_FALSE;
convert_to_string_ex(tok);
token = Z_STRVAL_PP(tok);
token_end = token + Z_STRLEN_PP(tok);
while (token < token_end)
STRTOK_TABLE(token++) = 1;
/* Skip leading delimiters */
while (STRTOK_TABLE(p))
if (++p >= pe) {
/* no other chars left */
BG(strtok_last) = NULL;
RETVAL_FALSE;
goto restore;
}
/* We know at this place that *p is no delimiter, so skip it */
while (++p < pe)
if (STRTOK_TABLE(p))
goto return_token;
if (p - BG(strtok_last)) {
return_token:
RETVAL_STRINGL(BG(strtok_last), p - BG(strtok_last), 1);
BG(strtok_last) = p + 1;
} else {
RETVAL_FALSE;
BG(strtok_last) = NULL;
}
/* Restore table -- usually faster then memset'ing the table
on every invocation */
restore:
token = Z_STRVAL_PP(tok);
while (token < token_end)
STRTOK_TABLE(token++) = 0;
}
/* }}} */
/* {{{ php_strtoupper
*/
PHPAPI char *php_strtoupper(char *s, size_t len)
{
char *c;
int ch;
size_t i;
c = s;
for (i=0; i<len; i++) {
ch = toupper((unsigned char)*c);
*c++ = ch;
}
return (s);
}
/* }}} */
/* {{{ proto string strtoupper(string str)
Make a string uppercase */
PHP_FUNCTION(strtoupper)
{
zval **arg;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg)) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(arg);
*return_value = **arg;
zval_copy_ctor(return_value);
php_strtoupper(return_value->value.str.val, return_value->value.str.len);
}
/* }}} */
/* {{{ php_strtolower
*/
PHPAPI char *php_strtolower(char *s, size_t len)
{
register int ch;
char *c;
size_t i;
c = s;
for (i=0; i<len; i++) {
ch = tolower((unsigned char)*c);
*c++ = ch;
}
return (s);
}
/* }}} */
/* {{{ proto string strtolower(string str)
Make a string lowercase */
PHP_FUNCTION(strtolower)
{
zval **str;
char *ret;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str)) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
*return_value = **str;
zval_copy_ctor(return_value);
ret = php_strtolower(return_value->value.str.val, return_value->value.str.len);
}
/* }}} */
/* {{{ php_basename
*/
PHPAPI char *php_basename(char *s, size_t len, char *suffix, size_t sufflen)
{
char *ret=NULL, *c, *p=NULL, buf='\0';
c = s + len - 1;
/* do suffix removal as the unix command does */
if(suffix && (len > sufflen)) {
if(!strncmp(suffix,c-sufflen+1,sufflen)) {
c -= sufflen;
*(c + 1) = '\0';
}
}
/* strip trailing slashes */
while (*c == '/'
#ifdef PHP_WIN32
|| *c == '\\'
#endif
)
c--;
if(c < s+len-1) {
buf = *(c + 1); /* Save overwritten char */
*(c + 1) = '\0'; /* overwrite char */
p = c + 1; /* Save pointer to overwritten char */
}
if ((c = strrchr(s, '/'))
#ifdef PHP_WIN32
|| (c = strrchr(s, '\\'))
#endif
) {
ret = estrdup(c + 1);
} else {
ret = estrdup(s);
}
if(buf) *p = buf;
return (ret);
}
/* }}} */
/* {{{ proto string basename(string path [, string suffix])
Return the filename component of the path */
PHP_FUNCTION(basename)
{
zval **str, **suffix;
char *ret;
switch(ZEND_NUM_ARGS()) {
case 2:
if(zend_get_parameters_ex(2, &str, &suffix)) WRONG_PARAM_COUNT;
convert_to_string_ex(str);
convert_to_string_ex(suffix);
break;
case 1:
if(zend_get_parameters_ex(1, &str)) WRONG_PARAM_COUNT;
convert_to_string_ex(str);
suffix=NULL;
break;
default: WRONG_PARAM_COUNT;
}
ret = php_basename(Z_STRVAL_PP(str)
, Z_STRLEN_PP(str)
, (suffix)?Z_STRVAL_PP(suffix):NULL
, (suffix)?Z_STRLEN_PP(suffix):0
);
RETVAL_STRING(ret, 0)
}
/* }}} */
/* {{{ php_dirname
*
* This function doesn't work with absolute paths in Win32 such as C:\foo
* (and it didn't before either). This needs to be fixed
*/
PHPAPI void php_dirname(char *path, int len)
{
register char *end = path + len - 1;
if (len <= 0) {
/* Illegal use of this function */
return;
}
/* Strip trailing slashes */
while (end >= path && IS_SLASH(*end)) {
end--;
}
if (end < path) {
/* The path only contained slashes */
path[0] = DEFAULT_SLASH;
path[1] = '\0';
return;
}
/* Strip filename */
while (end >= path && !IS_SLASH(*end)) {
end--;
}
if (end < path) {
/* No slash found, therefore return '.' */
path[0] = '.';
path[1] = '\0';
return;
}
/* Strip slashes which came before the file name */
while (end >= path && IS_SLASH(*end)) {
end--;
}
if (end < path) {
path[0] = DEFAULT_SLASH;
path[1] = '\0';
return;
}
*(end+1) = '\0';
}
/* }}} */
/* {{{ proto string dirname(string path)
Return the directory name component of the path */
PHP_FUNCTION(dirname)
{
zval **str;
char *ret;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
ret = estrndup(Z_STRVAL_PP(str), Z_STRLEN_PP(str));
php_dirname(ret, Z_STRLEN_PP(str));
RETVAL_STRING(ret, 0);
}
/* }}} */
/* {{{ proto array pathinfo(string path)
Return information about a certain string */
PHP_FUNCTION(pathinfo)
{
zval **path, **uopt, *tmp;
char *ret;
int argc = ZEND_NUM_ARGS(), opt, len;
if (argc < 1 || argc > 2 ||
zend_get_parameters_ex(argc, &path, &uopt) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(path);
len = Z_STRLEN_PP(path);
if (argc > 1) {
convert_to_long_ex(uopt);
opt = Z_LVAL_PP(uopt);
if (opt < PHP_PATHINFO_DIRNAME || opt > PHP_PATHINFO_EXTENSION) {
php_error(E_WARNING, "Invalid option in call to %s()",
get_active_function_name(TSRMLS_C));
return;
}
}
MAKE_STD_ZVAL(tmp);
array_init(tmp);
if (argc < 2 || opt == PHP_PATHINFO_DIRNAME) {
ret = estrndup(Z_STRVAL_PP(path), len);
php_dirname(ret, len);
if (*ret)
add_assoc_string(tmp, "dirname", ret, 1);
efree(ret);
}
if (argc < 2 || opt == PHP_PATHINFO_BASENAME) {
ret = php_basename(Z_STRVAL_PP(path), len, NULL, 0);
add_assoc_string(tmp, "basename", ret, 0);
}
if (argc < 2 || opt == PHP_PATHINFO_EXTENSION) {
char *p;
int idx;
p = strrchr(Z_STRVAL_PP(path), '.');
if (p) {
idx = p - Z_STRVAL_PP(path);
add_assoc_stringl(tmp, "extension", Z_STRVAL_PP(path) + idx + 1, len - idx - 1, 1);
}
}
if (argc == 2) {
zval **element;
zend_hash_get_current_data(Z_ARRVAL_P(tmp), (void **)&element);
*return_value = **element;
} else {
*return_value = *tmp;
}
zval_copy_ctor(return_value);
zval_dtor(tmp);
efree(tmp);
}
/* }}} */
/* {{{ php_stristr
* case insensitve strstr */
PHPAPI char *php_stristr(unsigned char *s, unsigned char *t,
size_t s_len, size_t t_len)
{
php_strtolower(s, s_len);
php_strtolower(t, t_len);
return php_memnstr(s, t, t_len, s + s_len);
}
/* }}} */
/* {{{ php_strspn
*/
PHPAPI size_t php_strspn(char *s1, char *s2, char *s1_end, char *s2_end)
{
register const char *p = s1, *spanp;
register char c = *p;
cont:
for (spanp = s2; p != s1_end && spanp != s2_end;)
if (*spanp++ == c) {
c = *(++p);
goto cont;
}
return (p - s1);
}
/* }}} */
/* {{{ php_strcspn
*/
PHPAPI size_t php_strcspn(char *s1, char *s2, char *s1_end, char *s2_end)
{
register const char *p, *spanp;
register char c = *s1;
for (p = s1;;) {
spanp = s2;
do {
if (*spanp == c || p == s1_end)
return (p - s1);
} while (spanp++ < s2_end);
c = *(++p);
}
/* NOTREACHED */
}
/* }}} */
/* {{{ proto string stristr(string haystack, string needle)
Find first occurrence of a string within another, case insensitive */
PHP_FUNCTION(stristr)
{
zval **haystack, **needle;
char *found = NULL;
int found_offset;
char *haystack_orig;
char needle_char[2];
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) ==
FAILURE) {
WRONG_PARAM_COUNT;
}
SEPARATE_ZVAL(haystack);
SEPARATE_ZVAL(needle);
convert_to_string_ex(haystack);
haystack_orig = estrndup((*haystack)->value.str.val,
(*haystack)->value.str.len);
if ((*needle)->type == IS_STRING) {
if ((*needle)->value.str.len==0) {
php_error(E_WARNING,"Empty delimiter");
efree(haystack_orig);
RETURN_FALSE;
}
found = php_stristr((*haystack)->value.str.val, (*needle)->value.str.val,
(*haystack)->value.str.len, (*needle)->value.str.len);
} else {
convert_to_long_ex(needle);
needle_char[0] = tolower((char) (*needle)->value.lval);
needle_char[1] = '\0';
found = php_stristr((*haystack)->value.str.val, needle_char,
(*haystack)->value.str.len, 1);
}
if (found) {
found_offset = found - (*haystack)->value.str.val;
RETVAL_STRINGL(haystack_orig + found_offset,
(*haystack)->value.str.len - found_offset, 1);
} else {
RETVAL_FALSE;
}
efree(haystack_orig);
}
/* }}} */
/* {{{ proto string strstr(string haystack, string needle)
Find first occurrence of a string within another */
PHP_FUNCTION(strstr)
{
zval **haystack, **needle;
char *haystack_end;
char *found = NULL;
char needle_char[2];
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) ==
FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(haystack);
haystack_end = (*haystack)->value.str.val + (*haystack)->value.str.len;
if ((*needle)->type == IS_STRING) {
if ((*needle)->value.str.len==0) {
php_error(E_WARNING,"Empty delimiter");
RETURN_FALSE;
}
found = php_memnstr((*haystack)->value.str.val, (*needle)->value.str.val,
(*needle)->value.str.len, haystack_end);
} else {
convert_to_long_ex(needle);
needle_char[0] = (char) (*needle)->value.lval;
needle_char[1] = '\0';
found = php_memnstr((*haystack)->value.str.val, needle_char, 1, haystack_end);
}
if (found) {
RETVAL_STRING(found, 1);
} else {
RETVAL_FALSE;
}
}
/* }}} */
/* {{{ proto string strchr(string haystack, string needle)
An alias for strstr */
/* }}} */
/* {{{ proto int strpos(string haystack, string needle [, int offset])
Find position of first occurrence of a string within another */
PHP_FUNCTION(strpos)
{
zval **haystack, **needle, **OFFSET;
int offset = 0;
char *found = NULL;
char *endp;
char *startp;
switch(ZEND_NUM_ARGS()) {
case 2:
if (zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) {
WRONG_PARAM_COUNT;
}
break;
case 3:
if (zend_get_parameters_ex(3, &haystack, &needle, &OFFSET) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(OFFSET);
offset = (*OFFSET)->value.lval;
if (offset < 0) {
php_error(E_WARNING,"offset not contained in string");
RETURN_FALSE;
}
break;
default:
WRONG_PARAM_COUNT;
}
convert_to_string_ex(haystack);
if (offset > (*haystack)->value.str.len) {
php_error(E_WARNING,"offset not contained in string");
RETURN_FALSE;
}
startp = (*haystack)->value.str.val;
startp+= offset;
endp = (*haystack)->value.str.val;
endp+= (*haystack)->value.str.len;
if ((*needle)->type == IS_STRING) {
if ((*needle)->value.str.len==0) {
php_error(E_WARNING,"Empty delimiter");
RETURN_FALSE;
}
found = php_memnstr(startp, (*needle)->value.str.val, (*needle)->value.str.len, endp);
} else {
char buf;
convert_to_long_ex(needle);
buf = (char) (*needle)->value.lval;
found = php_memnstr(startp, &buf, 1, endp);
}
if (found) {
RETVAL_LONG(found - (*haystack)->value.str.val);
} else {
RETVAL_FALSE;
}
}
/* }}} */
/* {{{ proto int strrpos(string haystack, string needle)
Find position of last occurrence of a character in a string within another */
PHP_FUNCTION(strrpos)
{
zval **haystack, **needle;
char *found = NULL;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(haystack);
if ((*needle)->type == IS_STRING) {
found = strrchr((*haystack)->value.str.val, *(*needle)->value.str.val);
} else {
convert_to_long_ex(needle);
found = strrchr((*haystack)->value.str.val, (char) (*needle)->value.lval);
}
if (found) {
RETVAL_LONG((*haystack)->value.str.len - strlen(found));
} else {
RETVAL_FALSE;
}
}
/* }}} */
/* {{{ proto string strrchr(string haystack, string needle)
Find the last occurrence of a character in a string within another */
PHP_FUNCTION(strrchr)
{
zval **haystack, **needle;
char *found = NULL;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) ==
FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(haystack);
if ((*needle)->type == IS_STRING) {
found = strrchr((*haystack)->value.str.val, *(*needle)->value.str.val);
} else {
convert_to_long_ex(needle);
found = strrchr((*haystack)->value.str.val, (*needle)->value.lval);
}
if (found) {
RETVAL_STRING(found,1);
} else {
RETVAL_FALSE;
}
}
/* }}} */
/* {{{ php_chunk_split
*/
static char *php_chunk_split(char *src, int srclen, char *end, int endlen,
int chunklen, int *destlen)
{
char *dest;
char *p, *q;
int chunks; /* complete chunks! */
int restlen;
chunks = srclen / chunklen;
restlen = srclen - chunks * chunklen; /* srclen % chunklen */
dest = emalloc((srclen + (chunks + 1) * endlen + 1) * sizeof(char));
for(p = src, q = dest; p < (src + srclen - chunklen + 1); ) {
memcpy(q, p, chunklen);
q += chunklen;
memcpy(q, end, endlen);
q += endlen;
p += chunklen;
}
if(restlen) {
memcpy(q, p, restlen);
q += restlen;
memcpy(q, end, endlen);
q += endlen;
}
*q = '\0';
if (destlen) {
*destlen = q - dest;
}
return(dest);
}
/* }}} */
/* {{{ proto string chunk_split(string str [, int chunklen [, string ending]])
Return split line */
PHP_FUNCTION(chunk_split)
{
zval **p_str, **p_chunklen, **p_ending;
int argc;
char *result;
char *end = "\r\n";
int endlen = 2;
int chunklen = 76;
int result_len;
argc = ZEND_NUM_ARGS();
if (argc < 1 || argc > 3 ||
zend_get_parameters_ex(argc, &p_str, &p_chunklen, &p_ending) == FAILURE) {
WRONG_PARAM_COUNT;
}
switch(argc) {
case 3:
convert_to_string_ex(p_ending);
end = (*p_ending)->value.str.val;
endlen = (*p_ending)->value.str.len;
case 2:
convert_to_long_ex(p_chunklen);
chunklen = (*p_chunklen)->value.lval;
case 1:
convert_to_string_ex(p_str);
}
if(chunklen <= 0) {
php_error(E_WARNING, "Chunk length should be greater than zero");
RETURN_FALSE;
}
if((*p_str)->value.str.len == 0) {
RETURN_EMPTY_STRING();
}
result = php_chunk_split((*p_str)->value.str.val, (*p_str)->value.str.len,
end, endlen, chunklen, &result_len);
if(result) {
RETVAL_STRINGL(result, result_len, 0);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string substr(string str, int start [, int length])
Return part of a string */
PHP_FUNCTION(substr)
{
zval **str, **from, **len;
int argc, l;
int f;
argc = ZEND_NUM_ARGS();
if ((argc == 2 && zend_get_parameters_ex(2, &str, &from) == FAILURE) ||
(argc == 3 && zend_get_parameters_ex(3, &str, &from, &len) == FAILURE) ||
argc < 2 || argc > 3) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
convert_to_long_ex(from);
f = (*from)->value.lval;
if (argc == 2) {
l = (*str)->value.str.len;
} else {
convert_to_long_ex(len);
l = (*len)->value.lval;
}
/* if "from" position is negative, count start position from the end
* of the string
*/
if (f < 0) {
f = (*str)->value.str.len + f;
if (f < 0) {
f = 0;
}
}
/* if "length" position is negative, set it to the length
* needed to stop that many chars from the end of the string
*/
if (l < 0) {
l = ((*str)->value.str.len - f) + l;
if (l < 0) {
l = 0;
}
}
if (f >= (int)(*str)->value.str.len) {
RETURN_FALSE;
}
if((f+l) > (int)(*str)->value.str.len) {
l = (int)(*str)->value.str.len - f;
}
RETVAL_STRINGL((*str)->value.str.val + f, l, 1);
}
/* }}} */
/* {{{ proto string substr_replace(string str, string repl, int start [, int length])
Replace part of a string with another string */
PHP_FUNCTION(substr_replace)
{
zval** str;
zval** from;
zval** len;
zval** repl;
char* result;
int result_len;
int argc;
int l;
int f;
argc = ZEND_NUM_ARGS();
if ((argc == 3 && zend_get_parameters_ex(3, &str, &repl, &from) == FAILURE) ||
(argc == 4 && zend_get_parameters_ex(4, &str, &repl, &from, &len) == FAILURE) ||
argc < 3 || argc > 4) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
convert_to_string_ex(repl);
convert_to_long_ex(from);
f = (*from)->value.lval;
if (argc == 3) {
l = (*str)->value.str.len;
} else {
convert_to_long_ex(len);
l = (*len)->value.lval;
}
/* if "from" position is negative, count start position from the end
* of the string
*/
if (f < 0) {
f = (*str)->value.str.len + f;
if (f < 0) {
f = 0;
}
} else if (f > (int)(*str)->value.str.len)
f = (int)(*str)->value.str.len;
/* if "length" position is negative, set it to the length
* needed to stop that many chars from the end of the string
*/
if (l < 0) {
l = ((*str)->value.str.len - f) + l;
if (l < 0) {
l = 0;
}
}
if((f+l) > (int)(*str)->value.str.len) {
l = (int)(*str)->value.str.len - f;
}
result_len = (*str)->value.str.len - l + (*repl)->value.str.len;
result = (char *)ecalloc(result_len + 1, sizeof(char *));
memcpy(result, (*str)->value.str.val, f);
memcpy(&result[f], (*repl)->value.str.val, (*repl)->value.str.len);
memcpy(&result[f + (*repl)->value.str.len], (*str)->value.str.val + f + l,
(*str)->value.str.len - f - l);
RETVAL_STRINGL(result, result_len, 0);
}
/* }}} */
/* {{{ proto string quotemeta(string str)
Quote meta characters */
PHP_FUNCTION(quotemeta)
{
zval **arg;
char *str, *old;
char *old_end;
char *p, *q;
char c;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(arg);
old = (*arg)->value.str.val;
old_end = (*arg)->value.str.val + (*arg)->value.str.len;
if (old == old_end) {
RETURN_FALSE;
}
str = emalloc(2 * (*arg)->value.str.len + 1);
for(p = old, q = str; p != old_end; p++) {
c = *p;
switch(c) {
case '.':
case '\\':
case '+':
case '*':
case '?':
case '[':
case '^':
case ']':
case '$':
case '(':
case ')':
*q++ = '\\';
/* break is missing _intentionally_ */
default:
*q++ = c;
}
}
*q = 0;
RETVAL_STRINGL(erealloc(str, q - str + 1), q - str, 0);
}
/* }}} */
/* {{{ proto int ord(string character)
Return ASCII value of character */
PHP_FUNCTION(ord)
{
zval **str;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
RETVAL_LONG((unsigned char)(*str)->value.str.val[0]);
}
/* }}} */
/* {{{ proto string chr(int ascii)
Convert ASCII code to a character */
PHP_FUNCTION(chr)
{
zval **num;
char temp[2];
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(num);
temp[0] = (char) (*num)->value.lval;
temp[1] = '\0';
RETVAL_STRINGL(temp, 1,1);
}
/* }}} */
/* {{{ proto string ucfirst(string str)
Make a string's first character uppercase */
PHP_FUNCTION(ucfirst)
{
zval **arg;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(arg);
if (!*(*arg)->value.str.val) {
RETURN_FALSE;
}
*return_value=**arg;
zval_copy_ctor(return_value);
*return_value->value.str.val = toupper((unsigned char)*return_value->value.str.val);
}
/* }}} */
/* {{{ proto string ucwords(string str)
Uppercase the first character of every word in a string */
PHP_FUNCTION(ucwords)
{
zval **str;
register char *r, *r_end;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
if (!Z_STRLEN_PP(str)) {
RETURN_FALSE;
}
*return_value=**str;
zval_copy_ctor(return_value);
r=return_value->value.str.val;
*r=toupper((unsigned char)*r);
for(r_end = r + return_value->value.str.len - 1 ; r < r_end ; ) {
if(isspace((int)*r++)) {
*r=toupper((unsigned char)*r);
}
}
}
/* }}} */
/* {{{ php_strtr
*/
PHPAPI char *php_strtr(char *str, int len, char *str_from,
char *str_to, int trlen)
{
int i;
unsigned char xlat[256];
if ((trlen < 1) || (len < 1)) {
return str;
}
for (i = 0; i < 256; xlat[i] = i, i++);
for (i = 0; i < trlen; i++) {
xlat[(unsigned char) str_from[i]] = str_to[i];
}
for (i = 0; i < len; i++) {
str[i] = xlat[(unsigned char) str[i]];
}
return str;
}
/* }}} */
/* {{{ php_strtr_array
*/
static void php_strtr_array(zval *return_value,char *str,int slen,HashTable *hash)
{
zval *entry;
char *string_key;
zval **trans;
zval ctmp;
ulong num_key;
int minlen = 128*1024;
int maxlen = 0, pos, len, found;
char *key;
HashPosition hpos;
smart_str result = {0};
zend_hash_internal_pointer_reset_ex(hash, &hpos);
while (zend_hash_get_current_data_ex(hash, (void **)&entry, &hpos) == SUCCESS) {
switch (zend_hash_get_current_key_ex(hash, &string_key, NULL, &num_key, 0, &hpos)) {
case HASH_KEY_IS_STRING:
len = strlen(string_key);
if (len > maxlen) maxlen = len;
if (len < minlen) minlen = len;
break;
case HASH_KEY_IS_LONG:
ctmp.type = IS_LONG;
ctmp.value.lval = num_key;
convert_to_string(&ctmp);
len = ctmp.value.str.len;
zval_dtor(&ctmp);
if (len > maxlen) maxlen = len;
if (len < minlen) minlen = len;
break;
}
zend_hash_move_forward_ex(hash, &hpos);
}
key = emalloc(maxlen+1);
pos = 0;
while (pos < slen) {
if ((pos + maxlen) > slen) {
maxlen = slen - pos;
}
found = 0;
memcpy(key,str+pos,maxlen);
for (len = maxlen; len >= minlen; len--) {
key[ len ]=0;
if (zend_hash_find(hash,key,len+1,(void**)&trans) == SUCCESS) {
char *tval;
int tlen;
zval tmp;
if ((*trans)->type != IS_STRING) {
tmp = **trans;
zval_copy_ctor(&tmp);
convert_to_string(&tmp);
tval = tmp.value.str.val;
tlen = tmp.value.str.len;
} else {
tval = (*trans)->value.str.val;
tlen = (*trans)->value.str.len;
}
smart_str_appendl(&result, tval, tlen);
pos += len;
found = 1;
if ((*trans)->type != IS_STRING) {
zval_dtor(&tmp);
}
break;
}
}
if (! found) {
smart_str_appendc(&result, str[pos++]);
}
}
efree(key);
smart_str_0(&result);
RETVAL_STRINGL(result.c,result.len,0);
}
/* }}} */
/* {{{ proto string strtr(string str, string from, string to)
Translate characters in str using given translation tables */
PHP_FUNCTION(strtr)
{ /* strtr(STRING,FROM,TO) */
zval **str, **from, **to;
int ac = ZEND_NUM_ARGS();
if (ac < 2 || ac > 3 || zend_get_parameters_ex(ac, &str, &from, &to) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (ac == 2 && (*from)->type != IS_ARRAY) {
php_error(E_WARNING,"arg2 must be passed an array");
RETURN_FALSE;
}
convert_to_string_ex(str);
/* shortcut for empty string */
if(Z_STRLEN_PP(str) == 0) {
RETURN_EMPTY_STRING();
}
if (ac == 2) {
php_strtr_array(return_value,(*str)->value.str.val,(*str)->value.str.len,HASH_OF(*from));
} else {
convert_to_string_ex(from);
convert_to_string_ex(to);
*return_value=**str;
zval_copy_ctor(return_value);
php_strtr(return_value->value.str.val,
return_value->value.str.len,
(*from)->value.str.val,
(*to)->value.str.val,
MIN((*from)->value.str.len,(*to)->value.str.len));
}
}
/* }}} */
/* {{{ proto string strrev(string str)
Reverse a string */
PHP_FUNCTION(strrev)
{
zval **str;
int i,len;
char c;
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &str)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
*return_value = **str;
zval_copy_ctor(return_value);
len = return_value->value.str.len;
for (i=0; i<len-1-i; i++) {
c=return_value->value.str.val[i];
return_value->value.str.val[i] = return_value->value.str.val[len-1-i];
return_value->value.str.val[len-1-i]=c;
}
}
/* }}} */
/* {{{ php_similar_str
*/
static void php_similar_str(const char *txt1, int len1, const char *txt2,
int len2, int *pos1, int *pos2, int *max)
{
char *p, *q;
char *end1 = (char *) txt1 + len1;
char *end2 = (char *) txt2 + len2;
int l;
*max = 0;
for (p = (char *) txt1; p < end1; p++) {
for (q = (char *) txt2; q < end2; q++) {
for (l = 0; (p + l < end1) && (q + l < end2) && (p[l] == q[l]);
l++);
if (l > *max) {
*max = l;
*pos1 = p - txt1;
*pos2 = q - txt2;
}
}
}
}
/* }}} */
/* {{{ php_similar_char
*/
static int php_similar_char(const char *txt1, int len1,
const char *txt2, int len2)
{
int sum;
int pos1, pos2, max;
php_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max);
if ((sum = max)) {
if (pos1 && pos2)
sum += php_similar_char(txt1, pos1, txt2, pos2);
if ((pos1 + max < len1) && (pos2 + max < len2))
sum += php_similar_char(txt1 + pos1 + max, len1 - pos1 - max,
txt2 + pos2 + max, len2 - pos2 - max);
}
return sum;
}
/* }}} */
/* {{{ proto int similar_text(string str1, string str2 [, double percent])
Calculates the similarity between two strings */
PHP_FUNCTION(similar_text)
{
zval **t1, **t2, **percent;
int ac = ZEND_NUM_ARGS();
int sim;
if (ac < 2 || ac > 3 ||
zend_get_parameters_ex(ac, &t1, &t2, &percent) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(t1);
convert_to_string_ex(t2);
if (ac > 2) {
convert_to_double_ex(percent);
}
if (((*t1)->value.str.len + (*t2)->value.str.len) == 0) {
if(ac > 2) {
(*percent)->value.dval = 0;
}
RETURN_LONG(0);
}
sim = php_similar_char((*t1)->value.str.val, (*t1)->value.str.len,
(*t2)->value.str.val, (*t2)->value.str.len);
if (ac > 2) {
(*percent)->value.dval = sim * 200.0 / ((*t1)->value.str.len + (*t2)->value.str.len);
}
RETURN_LONG(sim);
}
/* }}} */
/* {{{ php_stripslashes
*
* be careful, this edits the string in-place */
PHPAPI void php_stripslashes(char *str, int *len TSRMLS_DC)
{
char *s, *t;
int l;
char escape_char='\\';
if (PG(magic_quotes_sybase)) {
escape_char='\'';
}
if (len != NULL) {
l = *len;
} else {
l = strlen(str);
}
s = str;
t = str;
while (l > 0) {
if (*t == escape_char) {
t++; /* skip the slash */
if (len != NULL)
(*len)--;
l--;
if (l > 0) {
if(*t=='0') {
*s++='\0';
t++;
} else {
*s++ = *t++; /* preserve the next character */
}
l--;
}
} else {
if (s != t)
*s++ = *t++;
else {
s++;
t++;
}
l--;
}
}
if (s != t) {
*s = '\0';
}
}
/* }}} */
/* {{{ proto string addcslashes(string str, string charlist)
Escape all chars mentioned in charlist with backslash. It creates octal representations if asked to backslash characters with 8th bit set or with ASCII<32 (except '\n', '\r', '\t' etc...) */
PHP_FUNCTION(addcslashes)
{
zval **str, **what;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &str, &what) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
convert_to_string_ex(what);
if (Z_STRLEN_PP(str) == 0) {
RETURN_EMPTY_STRING();
}
if (Z_STRLEN_PP(what) == 0) {
RETURN_STRINGL(Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1);
}
Z_STRVAL_P(return_value) = php_addcslashes(Z_STRVAL_PP(str),
Z_STRLEN_PP(str), &Z_STRLEN_P(return_value), 0, Z_STRVAL_PP(what),
Z_STRLEN_PP(what) TSRMLS_CC);
return_value->type = IS_STRING;
}
/* }}} */
/* {{{ proto string addslashes(string str)
Escape single quote, double quotes and backslash characters in a string with backslashes */
PHP_FUNCTION(addslashes)
{
zval **str;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
if (Z_STRLEN_PP(str) == 0) {
RETURN_EMPTY_STRING();
}
Z_STRVAL_P(return_value) = php_addslashes(Z_STRVAL_PP(str),
Z_STRLEN_PP(str), &Z_STRLEN_P(return_value), 0 TSRMLS_CC);
Z_TYPE_P(return_value) = IS_STRING;
}
/* }}} */
/* {{{ proto string stripcslashes(string str)
Strip backslashes from a string. Uses C-style conventions */
PHP_FUNCTION(stripcslashes)
{
zval **str;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
*return_value = **str;
zval_copy_ctor(return_value);
php_stripcslashes(return_value->value.str.val,&return_value->value.str.len);
}
/* }}} */
/* {{{ proto string stripslashes(string str)
Strip backslashes from a string */
PHP_FUNCTION(stripslashes)
{
zval **str;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
*return_value = **str;
zval_copy_ctor(return_value);
php_stripslashes(Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value) TSRMLS_CC);
}
/* }}} */
#ifndef HAVE_STRERROR
/* {{{ php_strerror
*/
char *php_strerror(int errnum)
{
extern int sys_nerr;
extern char *sys_errlist[];
TSRMLS_FETCH();
if ((unsigned int)errnum < sys_nerr) return(sys_errlist[errnum]);
(void)sprintf(BG(str_ebuf), "Unknown error: %d", errnum);
return(BG(str_ebuf));
}
/* }}} */
#endif
/* {{{ php_stripcslashes
*/
PHPAPI void php_stripcslashes(char *str, int *len)
{
char *source,*target,*end;
int nlen = *len, i;
char numtmp[4];
for (source=str,end=str+nlen,target=str; source<end; source++) {
if (*source == '\\' && source+1<end) {
source++;
switch (*source) {
case 'n': *target++='\n'; nlen--; break;
case 'r': *target++='\r'; nlen--; break;
case 'a': *target++='\a'; nlen--; break;
case 't': *target++='\t'; nlen--; break;
case 'v': *target++='\v'; nlen--; break;
case 'b': *target++='\b'; nlen--; break;
case 'f': *target++='\f'; nlen--; break;
case '\\': *target++='\\'; nlen--; break;
case 'x': if (source+1<end && isxdigit((int)(*(source+1)))) {
numtmp[0] = *++source;
if (source+1<end && isxdigit((int)(*(source+1)))) {
numtmp[1] = *++source;
numtmp[2] = '\0';
nlen-=3;
} else {
numtmp[1] = '\0';
nlen-=2;
}
*target++=(char)strtol(numtmp, NULL, 16);
break;
}
/* break is left intentionally */
default: i=0;
while (source<end && *source>='0' && *source<='7' && i<3) {
numtmp[i++] = *source++;
}
if (i) {
numtmp[i]='\0';
*target++=(char)strtol(numtmp, NULL, 8);
nlen-=i;
source--;
} else {
*target++=*source;
nlen--;
}
}
} else {
*target++=*source;
}
}
if(nlen != 0) {
*target='\0';
}
*len = nlen;
}
/* }}} */
/* {{{ php_addcslashes
*/
PHPAPI char *php_addcslashes(char *str, int length, int *new_length, int should_free, char *what, int wlength TSRMLS_DC)
{
char flags[256];
char *new_str = emalloc((length?length:(length=strlen(str)))*4+1);
char *source,*target;
char *end;
char c;
int newlen;
if (!wlength) {
wlength = strlen(what);
}
if (!length) {
length = strlen(str);
}
php_charmask(what, wlength, flags TSRMLS_CC);
for (source=str,end=source+length,target=new_str; (c=*source) || source<end; source++) {
if (flags[(unsigned char)c]) {
if ((unsigned char)c<32 || (unsigned char)c>126) {
*target++ = '\\';
switch (c) {
case '\n': *target++ = 'n'; break;
case '\t': *target++ = 't'; break;
case '\r': *target++ = 'r'; break;
case '\a': *target++ = 'a'; break;
case '\v': *target++ = 'v'; break;
case '\b': *target++ = 'b'; break;
case '\f': *target++ = 'f'; break;
default: target += sprintf(target, "%03o", (unsigned char)c);
}
continue;
}
*target++ = '\\';
}
*target++ = c;
}
*target = 0;
newlen = target-new_str;
if (target-new_str<length*4) {
new_str = erealloc(new_str, newlen+1);
}
if (new_length) {
*new_length = newlen;
}
if (should_free) {
STR_FREE(str);
}
return new_str;
}
/* }}} */
/* {{{ php_addslashes
*/
PHPAPI char *php_addslashes(char *str, int length, int *new_length, int should_free TSRMLS_DC)
{
/* maximum string length, worst case situation */
char *new_str;
char *source,*target;
char *end;
char c;
if (!str) {
*new_length = 0;
return str;
}
new_str = (char *) emalloc((length?length:(length=strlen(str)))*2+1);
for (source=str,end=source+length,target=new_str; source<end; source++) {
c = *source;
switch(c) {
case '\0':
*target++ = '\\';
*target++ = '0';
break;
case '\'':
if (PG(magic_quotes_sybase)) {
*target++ = '\'';
*target++ = '\'';
break;
}
/* break is missing *intentionally* */
case '\"':
case '\\':
if (!PG(magic_quotes_sybase)) {
*target++ = '\\';
}
/* break is missing *intentionally* */
default:
*target++ = c;
break;
}
}
*target = 0;
if (new_length) {
*new_length = target - new_str;
}
if (should_free) {
STR_FREE(str);
}
return new_str;
}
/* }}} */
#define _HEB_BLOCK_TYPE_ENG 1
#define _HEB_BLOCK_TYPE_HEB 2
#define isheb(c) (((((unsigned char) c)>=224) && (((unsigned char) c)<=250)) ? 1 : 0)
#define _isblank(c) (((((unsigned char) c)==' ' || ((unsigned char) c)=='\t')) ? 1 : 0)
#define _isnewline(c) (((((unsigned char) c)=='\n' || ((unsigned char) c)=='\r')) ? 1 : 0)
/* {{{ php_char_to_str
*/
PHPAPI void php_char_to_str(char *str,uint len,char from,char *to,int to_len,zval *result)
{
int char_count=0;
char *source,*target,*tmp,*source_end=str+len, *tmp_end=NULL;
for (source=str; source<source_end; source++) {
if (*source==from) {
char_count++;
}
}
result->type = IS_STRING;
if (char_count==0) {
result->value.str.val = estrndup(str,len);
result->value.str.len = len;
return;
}
result->value.str.len = len+char_count*(to_len-1);
result->value.str.val = target = (char *) emalloc(result->value.str.len+1);
for (source=str; source<source_end; source++) {
if (*source==from) {
for (tmp=to,tmp_end=tmp+to_len; tmp<tmp_end; tmp++) {
*target = *tmp;
target++;
}
} else {
*target = *source;
target++;
}
}
*target = 0;
}
/* }}} */
/* {{{ boyer_str_to_str */
static char *boyer_str_to_str(char *haystack, int length,
char *needle, int needle_len, char *str,
int str_len, int *new_length)
{
char *p, *pe, *cursor, *end, *r;
int off;
char jump_table[256];
smart_str result = {0};
/*
* We implement only the first half of the Boyer-Moore algorithm,
* because the second half is too expensive to compute during run-time.
* TODO: Split matching into compile-/match-stage.
*/
/* Prepare the jump_table which contains the skip offsets */
memset(jump_table, needle_len, 256);
off = needle_len - 1;
/* Calculate the default start where each comparison starts */
pe = needle + off;
/* Assign skip offsets based on the pattern */
for (p = needle; p <= pe; p++)
jump_table[(unsigned char) *p] = off--;
/* Start to look at the first possible position for the pattern */
cursor = haystack + needle_len - 1;
/* The cursor must not cross this limit */
end = haystack + length;
/* Start to copy at haystack */
r = haystack;
nextiter:
while (cursor < end) {
p = pe; /* Compare from right to left */
while (*p == *cursor) {
if (--p < needle) { /* Found the pattern */
/* Append whatever was not matched */
smart_str_appendl(&result, r, cursor - r);
/* Append replacement string */
smart_str_appendl(&result, str, str_len);
/* Update copy pointer */
r = cursor + needle_len;
/* needle_len was substracted from cursor for
* this comparison, add it back. Also add
* needle_len - 1 which is the default search
* offset.
*/
cursor += (needle_len << 1) - 1;
/* Next iteration */
goto nextiter;
}
cursor--;
}
cursor += jump_table[(unsigned char) *cursor];
}
if (r < end) /* Copy the remaining data */
smart_str_appendl(&result, r, end - r);
smart_str_0(&result); /* NUL-ify result */
if (new_length)
*new_length = result.len;
return result.c;
}
/* }}} */
/* {{{ php_str_to_str
*/
PHPAPI char *php_str_to_str(char *haystack, int length,
char *needle, int needle_len, char *str, int str_len, int *_new_length)
{
char *p;
char *r;
char *end = haystack + length;
smart_str result = {0};
for (p = haystack;
(r = php_memnstr(p, needle, needle_len, end));
p = r + needle_len) {
smart_str_appendl(&result, p, r - p);
smart_str_appendl(&result, str, str_len);
}
if (p < end)
smart_str_appendl(&result, p, end - p);
smart_str_0(&result);
if (_new_length) *_new_length = result.len;
return result.c;
}
/* }}} */
/* {{{ php_str_replace_in_subject
*/
static void php_str_replace_in_subject(zval *search, zval *replace, zval **subject, zval *result, int boyer)
{
zval **search_entry,
**replace_entry = NULL,
temp_result;
char *replace_value = NULL;
int replace_len = 0;
char *(*str_to_str)(char *, int, char *, int, char *, int, int *);
str_to_str = boyer ? boyer_str_to_str : php_str_to_str;
/* Make sure we're dealing with strings. */
convert_to_string_ex(subject);
Z_TYPE_P(result) = IS_STRING;
if (Z_STRLEN_PP(subject) == 0) {
ZVAL_STRINGL(result, empty_string, 0, 1);
return;
}
/* If search is an array */
if (Z_TYPE_P(search) == IS_ARRAY) {
/* Duplicate subject string for repeated replacement */
*result = **subject;
zval_copy_ctor(result);
zend_hash_internal_pointer_reset(Z_ARRVAL_P(search));
if (Z_TYPE_P(replace) == IS_ARRAY) {
zend_hash_internal_pointer_reset(Z_ARRVAL_P(replace));
} else {
/* Set replacement value to the passed one */
replace_value = Z_STRVAL_P(replace);
replace_len = Z_STRLEN_P(replace);
}
/* For each entry in the search array, get the entry */
while (zend_hash_get_current_data(Z_ARRVAL_P(search), (void **)&search_entry) == SUCCESS) {
/* Make sure we're dealing with strings. */
convert_to_string_ex(search_entry);
if(Z_STRLEN_PP(search_entry) == 0) {
zend_hash_move_forward(Z_ARRVAL_P(search));
continue;
}
/* If replace is an array. */
if (Z_TYPE_P(replace) == IS_ARRAY) {
/* Get current entry */
if (zend_hash_get_current_data(Z_ARRVAL_P(replace), (void **)&replace_entry) == SUCCESS) {
/* Make sure we're dealing with strings. */
convert_to_string_ex(replace_entry);
/* Set replacement value to the one we got from array */
replace_value = Z_STRVAL_PP(replace_entry);
replace_len = Z_STRLEN_PP(replace_entry);
zend_hash_move_forward(Z_ARRVAL_P(replace));
} else {
/* We've run out of replacement strings, so use an empty one. */
replace_value = empty_string;
replace_len = 0;
}
}
if(Z_STRLEN_PP(search_entry) == 1) {
php_char_to_str(Z_STRVAL_P(result),
Z_STRLEN_P(result),
Z_STRVAL_PP(search_entry)[0],
replace_value,
replace_len,
&temp_result);
} else if (Z_STRLEN_PP(search_entry) > 1) {
Z_STRVAL(temp_result) = str_to_str(Z_STRVAL_P(result), Z_STRLEN_P(result),
Z_STRVAL_PP(search_entry), Z_STRLEN_PP(search_entry),
replace_value, replace_len, &Z_STRLEN(temp_result));
}
efree(Z_STRVAL_P(result));
Z_STRVAL_P(result) = Z_STRVAL(temp_result);
Z_STRLEN_P(result) = Z_STRLEN(temp_result);
zend_hash_move_forward(Z_ARRVAL_P(search));
}
} else {
if (Z_STRLEN_P(search) == 1) {
php_char_to_str(Z_STRVAL_PP(subject),
Z_STRLEN_PP(subject),
Z_STRVAL_P(search)[0],
Z_STRVAL_P(replace),
Z_STRLEN_P(replace),
result);
} else if (Z_STRLEN_P(search) > 1) {
Z_STRVAL_P(result) = str_to_str(Z_STRVAL_PP(subject), Z_STRLEN_PP(subject),
Z_STRVAL_P(search), Z_STRLEN_P(search),
Z_STRVAL_P(replace), Z_STRLEN_P(replace), &Z_STRLEN_P(result));
} else {
*result = **subject;
zval_copy_ctor(result);
}
}
}
/* }}} */
/* {{{ proto mixed str_replace(mixed search, mixed replace, mixed subject[, bool boyer])
Replace all occurrences of search in haystack with replace */
PHP_FUNCTION(str_replace)
{
zval **subject, **search, **replace, **subject_entry, **pboyer;
zval *result;
char *string_key;
ulong string_key_len, num_key;
int boyer = 0;
if(ZEND_NUM_ARGS() < 3 ||
ZEND_NUM_ARGS() > 4 ||
zend_get_parameters_ex(ZEND_NUM_ARGS(), &search,
&replace, &subject, &pboyer) == FAILURE) {
WRONG_PARAM_COUNT;
}
switch (ZEND_NUM_ARGS()) {
case 4:
convert_to_boolean_ex(pboyer);
if (Z_BVAL_PP(pboyer))
boyer = 1;
break;
}
SEPARATE_ZVAL(search);
SEPARATE_ZVAL(replace);
SEPARATE_ZVAL(subject);
/* Make sure we're dealing with strings and do the replacement. */
if (Z_TYPE_PP(search) != IS_ARRAY) {
convert_to_string_ex(search);
convert_to_string_ex(replace);
} else if (Z_TYPE_PP(replace) != IS_ARRAY)
convert_to_string_ex(replace);
/* if subject is an array */
if (Z_TYPE_PP(subject) == IS_ARRAY) {
array_init(return_value);
zend_hash_internal_pointer_reset(Z_ARRVAL_PP(subject));
/* For each subject entry, convert it to string, then perform replacement
and add the result to the return_value array. */
while (zend_hash_get_current_data(Z_ARRVAL_PP(subject), (void **)&subject_entry) == SUCCESS) {
MAKE_STD_ZVAL(result);
php_str_replace_in_subject(*search, *replace, subject_entry, result, boyer);
/* Add to return array */
switch(zend_hash_get_current_key_ex(Z_ARRVAL_PP(subject), &string_key,
&string_key_len, &num_key, 0, NULL)) {
case HASH_KEY_IS_STRING:
add_assoc_zval_ex(return_value, string_key, string_key_len, result);
break;
case HASH_KEY_IS_LONG:
add_index_zval(return_value, num_key, result);
break;
}
zend_hash_move_forward(Z_ARRVAL_PP(subject));
}
} else { /* if subject is not an array */
php_str_replace_in_subject(*search, *replace, subject, return_value, boyer);
}
}
/* }}} */
/* {{{ php_hebrev
*
* Converts Logical Hebrew text (Hebrew Windows style) to Visual text
* Cheers/complaints/flames - Zeev Suraski <zeev@php.net>
*/
static void php_hebrev(INTERNAL_FUNCTION_PARAMETERS,int convert_newlines)
{
zval **str,**max_chars_per_line;
char *heb_str,*tmp,*target,*opposite_target,*broken_str;
int block_start, block_end, block_type, block_length, i;
int block_ended;
long max_chars=0;
int begin,end,char_count,orig_begin;
switch(ZEND_NUM_ARGS()) {
case 1:
if (zend_get_parameters_ex(1, &str)==FAILURE) {
RETURN_FALSE;
}
break;
case 2:
if (zend_get_parameters_ex(2, &str, &max_chars_per_line)==FAILURE) {
RETURN_FALSE;
}
convert_to_long_ex(max_chars_per_line);
max_chars = (*max_chars_per_line)->value.lval;
break;
default:
WRONG_PARAM_COUNT;
break;
}
convert_to_string_ex(str);
if ((*str)->value.str.len==0) {
RETURN_FALSE;
}
tmp = (*str)->value.str.val;
block_start=block_end=0;
block_ended=0;
heb_str = (char *) emalloc((*str)->value.str.len+1);
target = heb_str+(*str)->value.str.len;
opposite_target = heb_str;
*target = 0;
target--;
block_length=0;
if (isheb(*tmp)) {
block_type = _HEB_BLOCK_TYPE_HEB;
} else {
block_type = _HEB_BLOCK_TYPE_ENG;
}
do {
if (block_type==_HEB_BLOCK_TYPE_HEB) {
while((isheb((int)*(tmp+1)) || _isblank((int)*(tmp+1)) || ispunct((int)*(tmp+1)) || (int)*(tmp+1)=='\n' ) && block_end<(*str)->value.str.len-1) {
tmp++;
block_end++;
block_length++;
}
for (i=block_start; i<=block_end; i++) {
*target = (*str)->value.str.val[i];
switch (*target) {
case '(':
*target = ')';
break;
case ')':
*target = '(';
break;
default:
break;
}
target--;
}
block_type = _HEB_BLOCK_TYPE_ENG;
} else {
while(!isheb(*(tmp+1)) && (int)*(tmp+1)!='\n' && block_end<(*str)->value.str.len-1) {
tmp++;
block_end++;
block_length++;
}
while ((_isblank((int)*tmp) || ispunct((int)*tmp)) && *tmp!='/' && *tmp!='-' && block_end>block_start) {
tmp--;
block_end--;
}
for (i=block_end; i>=block_start; i--) {
*target = (*str)->value.str.val[i];
target--;
}
block_type = _HEB_BLOCK_TYPE_HEB;
}
block_start=block_end+1;
} while(block_end<(*str)->value.str.len-1);
broken_str = (char *) emalloc((*str)->value.str.len+1);
begin=end=(*str)->value.str.len-1;
target = broken_str;
while (1) {
char_count=0;
while ((!max_chars || char_count<max_chars) && begin>0) {
char_count++;
begin--;
if (begin<=0 || _isnewline(heb_str[begin])) {
while(begin>0 && _isnewline(heb_str[begin-1])) {
begin--;
char_count++;
}
break;
}
}
if (char_count==max_chars) { /* try to avoid breaking words */
int new_char_count=char_count, new_begin=begin;
while (new_char_count>0) {
if (_isblank(heb_str[new_begin]) || _isnewline(heb_str[new_begin])) {
break;
}
new_begin++;
new_char_count--;
}
if (new_char_count>0) {
char_count=new_char_count;
begin=new_begin;
}
}
orig_begin=begin;
if (_isblank(heb_str[begin])) {
heb_str[begin]='\n';
}
while (begin<=end && _isnewline(heb_str[begin])) { /* skip leading newlines */
begin++;
}
for (i=begin; i<=end; i++) { /* copy content */
*target = heb_str[i];
target++;
}
for (i=orig_begin; i<=end && _isnewline(heb_str[i]); i++) {
*target = heb_str[i];
target++;
}
begin=orig_begin;
if (begin<=0) {
*target = 0;
break;
}
begin--;
end=begin;
}
efree(heb_str);
if (convert_newlines) {
php_char_to_str(broken_str,(*str)->value.str.len,'\n',"<br>\n",5,return_value);
efree(broken_str);
} else {
return_value->value.str.val = broken_str;
return_value->value.str.len = (*str)->value.str.len;
return_value->type = IS_STRING;
}
}
/* }}} */
/* {{{ proto string hebrev(string str [, int max_chars_per_line])
Convert logical Hebrew text to visual text */
PHP_FUNCTION(hebrev)
{
php_hebrev(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
}
/* }}} */
/* {{{ proto string hebrevc(string str [, int max_chars_per_line])
Convert logical Hebrew text to visual text with newline conversion */
PHP_FUNCTION(hebrevc)
{
php_hebrev(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
}
/* }}} */
/* {{{ proto string nl2br(string str)
Converts newlines to HTML line breaks */
PHP_FUNCTION(nl2br)
{
zval **str;
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &str)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
php_char_to_str((*str)->value.str.val,(*str)->value.str.len,'\n',"<br />\n",7,return_value);
}
/* }}} */
/* {{{ proto string strip_tags(string str [, string allowable_tags])
Strips HTML and PHP tags from a string */
PHP_FUNCTION(strip_tags)
{
char *buf;
zval **str, **allow=NULL;
char *allowed_tags=NULL;
int allowed_tags_len=0;
switch(ZEND_NUM_ARGS()) {
case 1:
if(zend_get_parameters_ex(1, &str)==FAILURE) {
RETURN_FALSE;
}
break;
case 2:
if(zend_get_parameters_ex(2, &str, &allow)==FAILURE) {
RETURN_FALSE;
}
convert_to_string_ex(allow);
allowed_tags = (*allow)->value.str.val;
allowed_tags_len = (*allow)->value.str.len;
break;
default:
WRONG_PARAM_COUNT;
break;
}
convert_to_string_ex(str);
buf = estrndup((*str)->value.str.val,(*str)->value.str.len);
php_strip_tags(buf, (*str)->value.str.len, 0, allowed_tags, allowed_tags_len);
RETURN_STRING(buf, 0);
}
/* }}} */
/* {{{ proto string setlocale(mixed category, string locale)
Set locale information */
PHP_FUNCTION(setlocale)
{
zval **pcategory, **plocale;
zval *category, *locale;
int cat;
char *loc, *retval;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &pcategory, &plocale)==FAILURE)
WRONG_PARAM_COUNT;
#ifdef HAVE_SETLOCALE
convert_to_string_ex(plocale);
locale = *plocale;
if (Z_TYPE_PP(pcategory) == IS_LONG) {
convert_to_long_ex(pcategory);
cat = Z_LVAL_PP(pcategory);
} else { /* FIXME: The following behaviour should be removed. */
php_error(E_NOTICE,"Passing locale category name as string is deprecated. Use the LC_* -constants instead.");
convert_to_string_ex(pcategory);
category = *pcategory;
if (!strcasecmp ("LC_ALL", category->value.str.val))
cat = LC_ALL;
else if (!strcasecmp ("LC_COLLATE", category->value.str.val))
cat = LC_COLLATE;
else if (!strcasecmp ("LC_CTYPE", category->value.str.val))
cat = LC_CTYPE;
#ifdef LC_MESSAGES
else if (!strcasecmp ("LC_MESSAGES", category->value.str.val))
cat = LC_MESSAGES;
#endif
else if (!strcasecmp ("LC_MONETARY", category->value.str.val))
cat = LC_MONETARY;
else if (!strcasecmp ("LC_NUMERIC", category->value.str.val))
cat = LC_NUMERIC;
else if (!strcasecmp ("LC_TIME", category->value.str.val))
cat = LC_TIME;
else {
php_error(E_WARNING,"Invalid locale category name %s, must be one of LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC or LC_TIME", category->value.str.val);
RETURN_FALSE;
}
}
if (!strcmp ("0", locale->value.str.val)) {
loc = NULL;
} else {
loc = locale->value.str.val;
}
retval = setlocale (cat, loc);
if (retval) {
/* Remember if locale was changed */
if (loc) {
STR_FREE(BG(locale_string));
BG(locale_string) = estrdup(retval);
}
RETVAL_STRING(retval,1);
return;
}
#endif
RETURN_FALSE;
}
/* }}} */
/* {{{ proto void parse_str(string encoded_string, [array result])
Parses GET/POST/COOKIE data and sets global variables */
PHP_FUNCTION(parse_str)
{
zval **arg;
zval **arrayArg;
zval *sarg;
char *res = NULL;
int argCount;
int old_rg;
argCount = ARG_COUNT(ht);
if(argCount < 1 || argCount > 2 || zend_get_parameters_ex(argCount, &arg, &arrayArg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(arg);
sarg = *arg;
if (sarg->value.str.val && *sarg->value.str.val) {
res = estrndup(sarg->value.str.val, sarg->value.str.len);
}
old_rg = PG(register_globals);
if(argCount == 1) {
PG(register_globals) = 1;
php_treat_data(PARSE_STRING, res, NULL TSRMLS_CC);
} else {
PG(register_globals) = 0;
/* Clear out the array that was passed in. */
zval_dtor(*arrayArg);
array_init(*arrayArg);
php_treat_data(PARSE_STRING, res, *arrayArg TSRMLS_CC);
}
PG(register_globals) = old_rg;
}
/* }}} */
#define PHP_TAG_BUF_SIZE 1023
/* {{{ php_tag_find
*
* Check if tag is in a set of tags
*
* states:
*
* 0 start tag
* 1 first non-whitespace char seen
*/
int php_tag_find(char *tag, int len, char *set) {
char c, *n, *t;
int state=0, done=0;
char *norm = emalloc(len+1);
n = norm;
t = tag;
c = tolower(*t);
/*
normalize the tag removing leading and trailing whitespace
and turn any <a whatever...> into just <a> and any </tag>
into <tag>
*/
if (!len) {
return 0;
}
while(!done) {
switch(c) {
case '<':
*(n++) = c;
break;
case '>':
done =1;
break;
default:
if(!isspace((int)c)) {
if(state==0) {
state=1;
if(c!='/') *(n++) = c;
} else {
*(n++) = c;
}
} else {
if(state==1) done=1;
}
break;
}
c = tolower(*(++t));
}
*(n++) = '>';
*n = '\0';
if(strstr(set,norm)) {
done=1;
} else {
done=0;
}
efree(norm);
return done;
}
/* }}} */
/* {{{ php_strip_tags
A simple little state-machine to strip out html and php tags
State 0 is the output state, State 1 means we are inside a
normal html tag and state 2 means we are inside a php tag.
The state variable is passed in to allow a function like fgetss
to maintain state across calls to the function.
lc holds the last significant character read and br is a bracket
counter.
When an allow string is passed in we keep track of the string
in state 1 and when the tag is closed check it against the
allow string to see if we should allow it.
*/
PHPAPI void php_strip_tags(char *rbuf, int len, int state, char *allow, int allow_len)
{
char *tbuf, *buf, *p, *tp, *rp, c, lc;
int br, i=0;
buf = estrndup(rbuf,len);
c = *buf;
lc = '\0';
p = buf;
rp = rbuf;
br = 0;
if(allow) {
php_strtolower(allow, allow_len);
tbuf = emalloc(PHP_TAG_BUF_SIZE+1);
tp = tbuf;
} else {
tbuf = tp = NULL;
}
while(i<len) {
switch (c) {
case '<':
if (state == 0) {
lc = '<';
state = 1;
if(allow) {
*(tp++) = '<';
}
}
break;
case '(':
if (state == 2) {
if (lc != '\"') {
lc = '(';
br++;
}
} else if (allow && state == 1) {
*(tp++) = c;
} else if (state == 0) {
*(rp++) = c;
}
break;
case ')':
if (state == 2) {
if (lc != '\"') {
lc = ')';
br--;
}
} else if (allow && state == 1) {
*(tp++) = c;
} else if (state == 0) {
*(rp++) = c;
}
break;
case '>':
if (state == 1) {
lc = '>';
state = 0;
if(allow) {
*(tp++) = '>';
*tp='\0';
if(php_tag_find(tbuf, tp-tbuf, allow)) {
memcpy(rp,tbuf,tp-tbuf);
rp += tp-tbuf;
}
tp = tbuf;
}
} else if (state == 2) {
if (!br && lc != '\"' && *(p-1)=='?') {
state = 0;
tp = tbuf;
}
} else {
*(rp++) = c;
}
break;
case '\"':
if (state == 2) {
if (lc == '\"') {
lc = '\0';
} else if (lc != '\\') {
lc = '\"';
}
} else if (state == 0) {
*(rp++) = c;
} else if (allow && state == 1) {
*(tp++) = c;
}
break;
case '?':
if (state==1 && *(p-1)=='<') {
br=0;
state=2;
break;
}
/* fall-through */
default:
if (state == 0) {
*(rp++) = c;
} else if(allow && state == 1) {
*(tp++) = c;
if( (tp-tbuf)>=PHP_TAG_BUF_SIZE ) { /* no buffer overflows */
tp = tbuf;
}
}
break;
}
c = *(++p);
i++;
}
*rp = '\0';
efree(buf);
if(allow) efree(tbuf);
}
/* }}} */
/* {{{ proto string str_repeat(string input, int mult)
Returns the input string repeat mult times */
PHP_FUNCTION(str_repeat)
{
zval **input_str; /* Input string */
zval **mult; /* Multiplier */
char *result; /* Resulting string */
int result_len; /* Length of the resulting string */
int i;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &input_str, &mult) == FAILURE) {
WRONG_PARAM_COUNT;
}
/* Make sure we're dealing with proper types */
convert_to_string_ex(input_str);
convert_to_long_ex(mult);
if ((*mult)->value.lval < 0) {
php_error(E_WARNING, "Second argument to %s() has to be greater than or equal to 0",
get_active_function_name(TSRMLS_C));
return;
}
/* Don't waste our time if it's empty */
if ((*input_str)->value.str.len == 0)
RETURN_STRINGL(empty_string, 0, 1);
/* ... or if the multiplier is zero */
if ((*mult)->value.lval == 0)
RETURN_STRINGL(empty_string, 0, 1);
/* Initialize the result string */
result_len = (*input_str)->value.str.len * (*mult)->value.lval;
result = (char *)emalloc(result_len + 1);
/* Copy the input string into the result as many times as necessary */
for (i=0; i<(*mult)->value.lval; i++) {
memcpy(result + (*input_str)->value.str.len * i,
(*input_str)->value.str.val,
(*input_str)->value.str.len);
}
result[result_len] = '\0';
RETURN_STRINGL(result, result_len, 0);
}
/* }}} */
/* {{{ proto mixed count_chars(string input [, int mode])
Returns info about what characters are used in input */
PHP_FUNCTION(count_chars)
{
zval **input, **mode;
int chars[256];
int ac=ZEND_NUM_ARGS();
int mymode=0;
unsigned char *buf;
int len, inx;
char retstr[256];
int retlen=0;
if (ac < 1 || ac > 2 || zend_get_parameters_ex(ac, &input, &mode) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(input);
if (ac == 2) {
convert_to_long_ex(mode);
mymode = (*mode)->value.lval;
if (mymode < 0 || mymode > 4) {
php_error(E_WARNING, "unknown mode");
RETURN_FALSE;
}
}
len = (*input)->value.str.len;
buf = (unsigned char *) (*input)->value.str.val;
memset((void*) chars,0,sizeof(chars));
while (len > 0) {
chars[*buf]++;
buf++;
len--;
}
if (mymode < 3) {
array_init(return_value);
}
for (inx=0; inx < 256; inx++) {
switch (mymode) {
case 0:
add_index_long(return_value,inx,chars[inx]);
break;
case 1:
if (chars[inx] != 0) {
add_index_long(return_value,inx,chars[inx]);
}
break;
case 2:
if (chars[inx] == 0) {
add_index_long(return_value,inx,chars[inx]);
}
break;
case 3:
if (chars[inx] != 0) {
retstr[retlen++] = inx;
}
break;
case 4:
if (chars[inx] == 0) {
retstr[retlen++] = inx;
}
break;
}
}
if (mymode >= 3 && mymode <= 4) {
RETURN_STRINGL(retstr,retlen,1);
}
}
/* }}} */
/* {{{ php_strnatcmp
*/
static void php_strnatcmp(INTERNAL_FUNCTION_PARAMETERS, int fold_case)
{
zval **s1, **s2;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &s1, &s2) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(s1);
convert_to_string_ex(s2);
RETURN_LONG(strnatcmp_ex((*s1)->value.str.val, (*s1)->value.str.len,
(*s2)->value.str.val, (*s2)->value.str.len,
fold_case));
}
/* }}} */
/* {{{ proto int strnatcmp(string s1, string s2)
Returns the result of string comparison using 'natural' algorithm */
PHP_FUNCTION(strnatcmp)
{
php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto array localeconv( void )
Returns numeric formatting information based on the current locale */
PHP_FUNCTION(localeconv)
{
zval *grouping, *mon_grouping;
int len, i;
MAKE_STD_ZVAL(grouping);
MAKE_STD_ZVAL(mon_grouping);
/* We don't need no stinkin' parameters... */
if (ZEND_NUM_ARGS() > 0) {
WRONG_PARAM_COUNT;
}
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
if (array_init(grouping) == FAILURE || array_init(mon_grouping) == FAILURE) {
RETURN_FALSE;
}
#ifdef HAVE_LOCALECONV
{
struct lconv currlocdata;
localeconv_r( &currlocdata );
/* Grab the grouping data out of the array */
len = strlen(currlocdata.grouping);
for (i=0;i<len;i++) {
add_index_long(grouping, i, currlocdata.grouping[i]);
}
/* Grab the monetary grouping data out of the array */
len = strlen(currlocdata.mon_grouping);
for (i=0;i<len;i++) {
add_index_long(mon_grouping, i, currlocdata.mon_grouping[i]);
}
add_assoc_string(return_value, "decimal_point", currlocdata.decimal_point, 1);
add_assoc_string(return_value, "thousands_sep", currlocdata.thousands_sep, 1);
add_assoc_string(return_value, "int_curr_symbol", currlocdata.int_curr_symbol, 1);
add_assoc_string(return_value, "currency_symbol", currlocdata.currency_symbol, 1);
add_assoc_string(return_value, "mon_decimal_point", currlocdata.mon_decimal_point, 1);
add_assoc_string(return_value, "mon_thousands_sep", currlocdata.mon_thousands_sep, 1);
add_assoc_string(return_value, "positive_sign", currlocdata.positive_sign, 1);
add_assoc_string(return_value, "negative_sign", currlocdata.negative_sign, 1);
add_assoc_long( return_value, "int_frac_digits", currlocdata.int_frac_digits );
add_assoc_long( return_value, "frac_digits", currlocdata.frac_digits );
add_assoc_long( return_value, "p_cs_precedes", currlocdata.p_cs_precedes );
add_assoc_long( return_value, "p_sep_by_space", currlocdata.p_sep_by_space );
add_assoc_long( return_value, "n_cs_precedes", currlocdata.n_cs_precedes );
add_assoc_long( return_value, "n_sep_by_space", currlocdata.n_sep_by_space );
add_assoc_long( return_value, "p_sign_posn", currlocdata.p_sign_posn );
add_assoc_long( return_value, "n_sign_posn", currlocdata.n_sign_posn );
}
#else
/* Ok, it doesn't look like we have locale info floating around, so I guess it
wouldn't hurt to just go ahead and return the POSIX locale information? */
add_index_long(grouping, 0, -1);
add_index_long(mon_grouping, 0, -1);
add_assoc_string(return_value, "decimal_point", "\x2E", 1);
add_assoc_string(return_value, "thousands_sep", "", 1);
add_assoc_string(return_value, "int_curr_symbol", "", 1);
add_assoc_string(return_value, "currency_symbol", "", 1);
add_assoc_string(return_value, "mon_decimal_point", "\x2E", 1);
add_assoc_string(return_value, "mon_thousands_sep", "", 1);
add_assoc_string(return_value, "positive_sign", "", 1);
add_assoc_string(return_value, "negative_sign", "", 1);
add_assoc_long( return_value, "int_frac_digits", CHAR_MAX );
add_assoc_long( return_value, "frac_digits", CHAR_MAX );
add_assoc_long( return_value, "p_cs_precedes", CHAR_MAX );
add_assoc_long( return_value, "p_sep_by_space", CHAR_MAX );
add_assoc_long( return_value, "n_cs_precedes", CHAR_MAX );
add_assoc_long( return_value, "n_sep_by_space", CHAR_MAX );
add_assoc_long( return_value, "p_sign_posn", CHAR_MAX );
add_assoc_long( return_value, "n_sign_posn", CHAR_MAX );
#endif
zend_hash_update(return_value->value.ht, "grouping", 9, &grouping, sizeof(zval *), NULL);
zend_hash_update(return_value->value.ht, "mon_grouping", 13, &mon_grouping, sizeof(zval *), NULL);
}
/* }}} */
/* {{{ proto int strnatcasecmp(string s1, string s2)
Returns the result of case-insensitive string comparison using 'natural' algorithm */
PHP_FUNCTION(strnatcasecmp)
{
php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto int substr_count(string haystack, string needle)
Returns the number of times a substring occurs in the string */
PHP_FUNCTION(substr_count)
{
zval **haystack, **needle;
int i, length, count = 0;
char *p, *endp, cmp;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(haystack);
convert_to_string_ex(needle);
if ((*needle)->value.str.len == 0) {
php_error(E_WARNING, "Empty substring");
RETURN_FALSE;
} else if ((*needle)->value.str.len == 1) {
/* Special optimized case to avoid calls to php_memnstr(). */
for (i = 0, p = (*haystack)->value.str.val,
length = (*haystack)->value.str.len, cmp = (*needle)->value.str.val[0];
i < length; i++) {
if (p[i] == cmp) {
count++;
}
}
} else {
p = (*haystack)->value.str.val;
endp = p + (*haystack)->value.str.len;
while (p <= endp) {
if( (p = php_memnstr(p, (*needle)->value.str.val, (*needle)->value.str.len, endp)) != NULL ) {
p += (*needle)->value.str.len;
count++;
} else {
break;
}
}
}
RETURN_LONG(count);
}
/* }}} */
/* {{{ proto string str_pad(string input, int pad_length [, string pad_string [, int pad_type]])
Returns input string padded on the left or right to specified length with pad_string */
PHP_FUNCTION(str_pad)
{
/* Input arguments */
zval **input, /* Input string */
**pad_length, /* Length to pad to */
**pad_string, /* Padding string */
**pad_type; /* Padding type (left/right/both) */
/* Helper variables */
int num_pad_chars; /* Number of padding characters (total - input size) */
char *result = NULL; /* Resulting string */
int result_len = 0; /* Length of the resulting string */
char *pad_str_val = " "; /* Pointer to padding string */
int pad_str_len = 1; /* Length of the padding string */
int pad_type_val = STR_PAD_RIGHT; /* The padding type value */
int i, left_pad=0, right_pad=0;
if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 4 ||
zend_get_parameters_ex(ZEND_NUM_ARGS(), &input, &pad_length, &pad_string, &pad_type) == FAILURE) {
WRONG_PARAM_COUNT;
}
/* Perform initial conversion to expected data types. */
convert_to_string_ex(input);
convert_to_long_ex(pad_length);
num_pad_chars = Z_LVAL_PP(pad_length) - Z_STRLEN_PP(input);
/* If resulting string turns out to be shorter than input string,
we simply copy the input and return. */
if (num_pad_chars < 0) {
*return_value = **input;
zval_copy_ctor(return_value);
return;
}
/* Setup the padding string values if specified. */
if (ZEND_NUM_ARGS() > 2) {
convert_to_string_ex(pad_string);
if (Z_STRLEN_PP(pad_string) == 0) {
php_error(E_WARNING, "Padding string cannot be empty in %s()",
get_active_function_name(TSRMLS_C));
return;
}
pad_str_val = Z_STRVAL_PP(pad_string);
pad_str_len = Z_STRLEN_PP(pad_string);
if (ZEND_NUM_ARGS() > 3) {
convert_to_long_ex(pad_type);
pad_type_val = Z_LVAL_PP(pad_type);
if (pad_type_val < STR_PAD_LEFT || pad_type_val > STR_PAD_BOTH) {
php_error(E_WARNING, "Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH in %s()", get_active_function_name(TSRMLS_C));
return;
}
}
}
result = (char *)emalloc(Z_STRLEN_PP(input) + num_pad_chars + 1);
/* We need to figure out the left/right padding lengths. */
switch (pad_type_val) {
case STR_PAD_RIGHT:
left_pad = 0;
right_pad = num_pad_chars;
break;
case STR_PAD_LEFT:
left_pad = num_pad_chars;
right_pad = 0;
break;
case STR_PAD_BOTH:
left_pad = num_pad_chars / 2;
right_pad = num_pad_chars - left_pad;
break;
}
/* First we pad on the left. */
for (i = 0; i < left_pad; i++)
result[result_len++] = pad_str_val[i % pad_str_len];
/* Then we copy the input string. */
memcpy(result + result_len, Z_STRVAL_PP(input), Z_STRLEN_PP(input));
result_len += Z_STRLEN_PP(input);
/* Finally, we pad on the right. */
for (i = 0; i < right_pad; i++)
result[result_len++] = pad_str_val[i % pad_str_len];
result[result_len] = '\0';
RETURN_STRINGL(result, result_len, 0);
}
/* }}} */
/* {{{ proto mixed sscanf(string str, string format [, string ...])
Implements an ANSI C compatible sscanf */
PHP_FUNCTION(sscanf)
{
zval **format;
zval **literal;
int result;
zval ***args;
int argCount;
argCount = ZEND_NUM_ARGS();
if (argCount < 2) {
WRONG_PARAM_COUNT;
}
args = (zval ***)emalloc(argCount * sizeof(zval **));
if (!args || (zend_get_parameters_array_ex(argCount,args) == FAILURE)) {
efree( args );
WRONG_PARAM_COUNT;
}
literal = args[0];
format = args[1];
convert_to_string_ex( format );
convert_to_string_ex( literal );
result = php_sscanf_internal( (*literal)->value.str.val,
(*format)->value.str.val,
argCount, args,
2, &return_value TSRMLS_CC);
efree(args);
if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
WRONG_PARAM_COUNT;
}
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 tw=78 fdm=marker
* vim<600: sw=4 ts=4 tw=78
*/
|