1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
|
/****************************************************************************
**
** Implementation of the TQString class and related Unicode functions
**
** Created : 920722
**
** Copyright (C) 2015 Timothy Pearson. All rights reserved.
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the tools module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file. Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
// Don't define it while compiling this module, or USERS of TQt will
// not be able to link.
#ifdef QT_NO_CAST_ASCII
#undef QT_NO_CAST_ASCII
#endif
// WARNING
// When MAKE_TQSTRING_THREAD_SAFE is defined, overall TQt3 performance suffers badly!
// TQString is thread unsafe in many other areas; perhaps this option is not even useful?
// #define MAKE_TQSTRING_THREAD_SAFE 1
#include "ntqstring.h"
#include "ntqregexp.h"
#include "ntqdatastream.h"
#ifndef QT_NO_TEXTCODEC
#include "ntqtextcodec.h"
#endif
#include "ntqlocale.h"
#include "qlocale_p.h"
#include "qunicodetables_p.h"
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef Q_OS_TEMP
#include <locale.h>
#endif
#if defined(Q_WS_WIN)
#include "qt_windows.h"
#endif
#if defined(Q_OS_LINUX)
#include <sys/mman.h>
#endif
#if !defined( QT_NO_COMPONENT ) && !defined( QT_LITE_COMPONENT )
#include "ntqcleanuphandler.h"
#endif
#if defined(Q_OS_LINUX)
#define LINUX_MEMLOCK_LIMIT_BYTES 16384
#define LINUX_MEMLOCK_LIMIT_CHARACTERS LINUX_MEMLOCK_LIMIT_BYTES/sizeof(TQChar)
#endif
#ifndef LLONG_MAX
#define LLONG_MAX TQ_INT64_C(9223372036854775807)
#endif
#ifndef LLONG_MIN
#define LLONG_MIN (-LLONG_MAX - TQ_INT64_C(1))
#endif
#ifndef ULLONG_MAX
#define ULLONG_MAX TQ_UINT64_C(18446744073709551615)
#endif
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
#include "ntqmutex.h"
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
extern TQMutex *tqt_sharedStringMutex;
static int ucstrcmp( const TQString &as, const TQString &bs )
{
const TQChar *a = as.unicode();
const TQChar *b = bs.unicode();
if ( a == b )
return 0;
if ( a == 0 )
return 1;
if ( b == 0 )
return -1;
int l=TQMIN(as.length(),bs.length());
while ( l-- && *a == *b )
a++,b++;
if ( l==-1 )
return ( as.length()-bs.length() );
return a->unicode() - b->unicode();
}
static int ucstrncmp( const TQChar *a, const TQChar *b, int l )
{
while ( l-- && *a == *b )
a++,b++;
if ( l==-1 )
return 0;
return a->unicode() - b->unicode();
}
static int ucstrnicmp( const TQChar *a, const TQChar *b, int l )
{
while ( l-- && ::lower( *a ) == ::lower( *b ) )
a++,b++;
if ( l==-1 )
return 0;
return ::lower( *a ).unicode() - ::lower( *b ).unicode();
}
static uint computeNewMax( uint len )
{
if (len >= 0x80000000)
return len;
uint newMax = 4;
while ( newMax < len )
newMax *= 2;
// try to save some memory
if ( newMax >= 1024 * 1024 && len <= newMax - (newMax >> 2) )
newMax -= newMax >> 2;
return newMax;
}
static bool qIsUpper(char c)
{
return c >= 'A' && c <= 'Z';
}
static bool qIsDigit(char c)
{
return c >= '0' && c <= '9';
}
static char qToLower(char c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
else
return c;
}
/*!
\class TQCharRef ntqstring.h
\reentrant
\brief The TQCharRef class is a helper class for TQString.
\ingroup text
When you get an object of type TQCharRef, if you can assign to it,
the assignment will apply to the character in the string from
which you got the reference. That is its whole purpose in life.
The TQCharRef becomes invalid once modifications are made to the
string: if you want to keep the character, copy it into a TQChar.
Most of the TQChar member functions also exist in TQCharRef.
However, they are not explicitly documented here.
\sa TQString::operator[]() TQString::at() TQChar
*/
/*!
\class TQChar ntqstring.h
\reentrant
\brief The TQChar class provides a lightweight Unicode character.
\ingroup text
Unicode characters are (so far) 16-bit entities without any markup
or structure. This class represents such an entity. It is
lightweight, so it can be used everywhere. Most compilers treat it
like a "short int". (In a few years it may be necessary to make
TQChar 32-bit when more than 65536 Unicode code points have been
defined and come into use.)
TQChar provides a full complement of testing/classification
functions, converting to and from other formats, converting from
composed to decomposed Unicode, and trying to compare and
case-convert if you ask it to.
The classification functions include functions like those in
ctype.h, but operating on the full range of Unicode characters.
They all return TRUE if the character is a certain type of
character; otherwise they return FALSE. These classification
functions are isNull() (returns TRUE if the character is U+0000),
isPrint() (TRUE if the character is any sort of printable
character, including whitespace), isPunct() (any sort of
punctation), isMark() (Unicode Mark), isLetter (a letter),
isNumber() (any sort of numeric character), isLetterOrNumber(),
and isDigit() (decimal digits). All of these are wrappers around
category() which return the Unicode-defined category of each
character.
TQChar further provides direction(), which indicates the "natural"
writing direction of this character. The joining() function
indicates how the character joins with its neighbors (needed
mostly for Arabic) and finally mirrored(), which indicates whether
the character needs to be mirrored when it is printed in its
"unnatural" writing direction.
Composed Unicode characters (like å) can be converted to
decomposed Unicode ("a" followed by "ring above") by using
decomposition().
In Unicode, comparison is not necessarily possible and case
conversion is very difficult at best. Unicode, covering the
"entire" world, also includes most of the world's case and sorting
problems. TQt tries, but not very hard: operator==() and friends
will do comparison based purely on the numeric Unicode value (code
point) of the characters, and upper() and lower() will do case
changes when the character has a well-defined upper/lower-case
equivalent. There is no provision for locale-dependent case
folding rules or comparison; these functions are meant to be fast
so they can be used unambiguously in data structures. (See
TQString::localeAwareCompare() though.)
The conversion functions include unicode() (to a scalar), latin1()
(to scalar, but converts all non-Latin-1 characters to 0), row()
(gives the Unicode row), cell() (gives the Unicode cell),
digitValue() (gives the integer value of any of the numerous digit
characters), and a host of constructors.
More information can be found in the document \link unicode.html
About Unicode. \endlink
\sa TQString TQCharRef
*/
/*!
\enum TQChar::Category
This enum maps the Unicode character categories.
The following characters are normative in Unicode:
\value Mark_NonSpacing Unicode class name Mn
\value Mark_SpacingCombining Unicode class name Mc
\value Mark_Enclosing Unicode class name Me
\value Number_DecimalDigit Unicode class name Nd
\value Number_Letter Unicode class name Nl
\value Number_Other Unicode class name No
\value Separator_Space Unicode class name Zs
\value Separator_Line Unicode class name Zl
\value Separator_Paragraph Unicode class name Zp
\value Other_Control Unicode class name Cc
\value Other_Format Unicode class name Cf
\value Other_Surrogate Unicode class name Cs
\value Other_PrivateUse Unicode class name Co
\value Other_NotAssigned Unicode class name Cn
The following categories are informative in Unicode:
\value Letter_Uppercase Unicode class name Lu
\value Letter_Lowercase Unicode class name Ll
\value Letter_Titlecase Unicode class name Lt
\value Letter_Modifier Unicode class name Lm
\value Letter_Other Unicode class name Lo
\value Punctuation_Connector Unicode class name Pc
\value Punctuation_Dash Unicode class name Pd
\value Punctuation_Open Unicode class name Ps
\value Punctuation_Close Unicode class name Pe
\value Punctuation_InitialQuote Unicode class name Pi
\value Punctuation_FinalQuote Unicode class name Pf
\value Punctuation_Other Unicode class name Po
\value Symbol_Math Unicode class name Sm
\value Symbol_Currency Unicode class name Sc
\value Symbol_Modifier Unicode class name Sk
\value Symbol_Other Unicode class name So
There are two categories that are specific to TQt:
\value NoCategory used when TQt is dazed and confused and cannot
make sense of anything.
\value Punctuation_Dask old typo alias for Punctuation_Dash
*/
/*!
\enum TQChar::Direction
This enum type defines the Unicode direction attributes. See \link
http://www.unicode.org/ the Unicode Standard\endlink for a
description of the values.
In order to conform to C/C++ naming conventions "Dir" is prepended
to the codes used in the Unicode Standard.
*/
/*!
\enum TQChar::Decomposition
This enum type defines the Unicode decomposition attributes. See
\link http://www.unicode.org/ the Unicode Standard\endlink for a
description of the values.
*/
/*!
\enum TQChar::Joining
This enum type defines the Unicode joining attributes. See \link
http://www.unicode.org/ the Unicode Standard\endlink for a
description of the values.
*/
/*!
\enum TQChar::CombiningClass
This enum type defines names for some of the Unicode combining
classes. See \link http://www.unicode.org/ the Unicode
Standard\endlink for a description of the values.
*/
/*!
\fn void TQChar::setCell( uchar cell )
\internal
*/
/*!
\fn void TQChar::setRow( uchar row )
\internal
*/
/*!
\fn TQChar::TQChar()
Constructs a null TQChar (one that isNull()).
*/
/*!
\fn TQChar::TQChar( char c )
Constructs a TQChar corresponding to ASCII/Latin-1 character \a c.
*/
/*!
\fn TQChar::TQChar( uchar c )
Constructs a TQChar corresponding to ASCII/Latin-1 character \a c.
*/
/*!
\fn TQChar::TQChar( uchar c, uchar r )
Constructs a TQChar for Unicode cell \a c in row \a r.
*/
/*!
\fn TQChar::TQChar( const TQChar& c )
Constructs a copy of \a c. This is a deep copy, if such a
lightweight object can be said to have deep copies.
*/
/*!
\fn TQChar::TQChar( ushort rc )
Constructs a TQChar for the character with Unicode code point \a rc.
*/
/*!
\fn TQChar::TQChar( short rc )
Constructs a TQChar for the character with Unicode code point \a rc.
*/
/*!
\fn TQChar::TQChar( uint rc )
Constructs a TQChar for the character with Unicode code point \a rc.
*/
/*!
\fn TQChar::TQChar( int rc )
Constructs a TQChar for the character with Unicode code point \a rc.
*/
/*!
\fn bool TQChar::networkOrdered ()
\obsolete
Returns TRUE if this character is in network byte order (MSB
first); otherwise returns FALSE. This is platform dependent.
*/
/*!
\fn bool TQChar::isNull() const
Returns TRUE if the character is the Unicode character 0x0000
(ASCII NUL); otherwise returns FALSE.
*/
/*!
\fn uchar TQChar::cell () const
Returns the cell (least significant byte) of the Unicode
character.
*/
/*!
\fn uchar TQChar::row () const
Returns the row (most significant byte) of the Unicode character.
*/
/*!
Returns TRUE if the character is a printable character; otherwise
returns FALSE. This is any character not of category Cc or Cn.
Note that this gives no indication of whether the character is
available in a particular \link TQFont font\endlink.
*/
bool TQChar::isPrint() const
{
Category c = ::category( *this );
return !(c == Other_Control || c == Other_NotAssigned);
}
/*!
Returns TRUE if the character is a separator character
(Separator_* categories); otherwise returns FALSE.
*/
bool TQChar::isSpace() const
{
return ::isSpace( *this );
}
/*!
Returns TRUE if the character is a mark (Mark_* categories);
otherwise returns FALSE.
*/
bool TQChar::isMark() const
{
Category c = ::category( *this );
return c >= Mark_NonSpacing && c <= Mark_Enclosing;
}
/*!
Returns TRUE if the character is a punctuation mark (Punctuation_*
categories); otherwise returns FALSE.
*/
bool TQChar::isPunct() const
{
Category c = ::category( *this );
return (c >= Punctuation_Connector && c <= Punctuation_Other);
}
/*!
Returns TRUE if the character is a letter (Letter_* categories);
otherwise returns FALSE.
*/
bool TQChar::isLetter() const
{
Category c = ::category( *this );
return (c >= Letter_Uppercase && c <= Letter_Other);
}
/*!
Returns TRUE if the character is a number (of any sort - Number_*
categories); otherwise returns FALSE.
\sa isDigit()
*/
bool TQChar::isNumber() const
{
Category c = ::category( *this );
return c >= Number_DecimalDigit && c <= Number_Other;
}
/*!
Returns TRUE if the character is a letter or number (Letter_* or
Number_* categories); otherwise returns FALSE.
*/
bool TQChar::isLetterOrNumber() const
{
Category c = ::category( *this );
return (c >= Letter_Uppercase && c <= Letter_Other)
|| (c >= Number_DecimalDigit && c <= Number_Other);
}
/*!
Returns TRUE if the character is a decimal digit
(Number_DecimalDigit); otherwise returns FALSE.
*/
bool TQChar::isDigit() const
{
return (::category( *this ) == Number_DecimalDigit);
}
/*!
Returns TRUE if the character is a symbol (Symbol_* categories);
otherwise returns FALSE.
*/
bool TQChar::isSymbol() const
{
Category c = ::category( *this );
return c >= Symbol_Math && c <= Symbol_Other;
}
/*!
Returns the numeric value of the digit, or -1 if the character is
not a digit.
*/
int TQChar::digitValue() const
{
#ifndef QT_NO_UNICODETABLES
register int pos = TQUnicodeTables::decimal_info[row()];
if( !pos )
return -1;
return TQUnicodeTables::decimal_info[(pos<<8) + cell()];
#else
// ##### just latin1
if ( ucs < '0' || ucs > '9' )
return -1;
else
return ucs - '0';
#endif
}
/*!
Returns the character category.
\sa Category
*/
TQChar::Category TQChar::category() const
{
return ::category( *this );
}
/*!
Returns the character's direction.
\sa Direction
*/
TQChar::Direction TQChar::direction() const
{
return ::direction( *this );
}
/*!
\warning This function is not supported (it may change to use
Unicode character classes).
Returns information about the joining properties of the character
(needed for example, for Arabic).
*/
TQChar::Joining TQChar::joining() const
{
return ::joining( *this );
}
/*!
Returns TRUE if the character is a mirrored character (one that
should be reversed if the text direction is reversed); otherwise
returns FALSE.
*/
bool TQChar::mirrored() const
{
return ::mirrored( *this );
}
/*!
Returns the mirrored character if this character is a mirrored
character, otherwise returns the character itself.
*/
TQChar TQChar::mirroredChar() const
{
return ::mirroredChar( *this );
}
#ifndef QT_NO_UNICODETABLES
// ### REMOVE ME 4.0
static TQString shared_decomp;
#endif
/*!
\nonreentrant
Decomposes a character into its parts. Returns TQString::null if no
decomposition exists.
*/
const TQString &TQChar::decomposition() const
{
#ifndef QT_NO_UNICODETABLES
register int pos = TQUnicodeTables::decomposition_info[row()];
if(!pos) return TQString::null;
pos = TQUnicodeTables::decomposition_info[(pos<<8)+cell()];
if(!pos) return TQString::null;
pos+=2;
TQString s;
TQ_UINT16 c;
while ( (c = TQUnicodeTables::decomposition_map[pos++]) != 0 )
s += TQChar( c );
// ### In 4.0, return s, and not shared_decomp. shared_decomp
// prevents this function from being reentrant.
shared_decomp = s;
return shared_decomp;
#else
return TQString::null;
#endif
}
/*!
Returns the tag defining the composition of the character. Returns
TQChar::Single if no decomposition exists.
*/
TQChar::Decomposition TQChar::decompositionTag() const
{
#ifndef QT_NO_UNICODETABLES
register int pos = TQUnicodeTables::decomposition_info[row()];
if(!pos) return TQChar::Single;
pos = TQUnicodeTables::decomposition_info[(pos<<8)+cell()];
if(!pos) return TQChar::Single;
return (TQChar::Decomposition) TQUnicodeTables::decomposition_map[pos];
#else
return Single; // ########### FIX eg. just latin1
#endif
}
/*!
Returns the combining class for the character as defined in the
Unicode standard. This is mainly useful as a positioning hint for
marks attached to a base character.
The TQt text rendering engine uses this information to correctly
position non spacing marks around a base character.
*/
unsigned char TQChar::combiningClass() const
{
return ::combiningClass( *this );
}
/*!
Returns the lowercase equivalent if the character is uppercase;
otherwise returns the character itself.
*/
TQChar TQChar::lower() const
{
return ::lower( *this );
}
/*!
Returns the uppercase equivalent if the character is lowercase;
otherwise returns the character itself.
*/
TQChar TQChar::upper() const
{
return ::upper( *this );
}
/*!
\fn TQChar::operator char() const
Returns the Latin-1 character equivalent to the TQChar, or 0. This
is mainly useful for non-internationalized software.
\sa unicode()
*/
/*!
\fn ushort TQChar::unicode() const
Returns the numeric Unicode value equal to the TQChar. Normally,
you should use TQChar objects as they are equivalent, but for some
low-level tasks (e.g. indexing into an array of Unicode
information), this function is useful.
*/
/*!
\fn ushort & TQChar::unicode()
\overload
Returns a reference to the numeric Unicode value equal to the
TQChar.
*/
/*****************************************************************************
Documentation of TQChar related functions
*****************************************************************************/
/*!
\fn bool operator==( TQChar c1, TQChar c2 )
\relates TQChar
Returns TRUE if \a c1 and \a c2 are the same Unicode character;
otherwise returns FALSE.
*/
/*!
\fn bool operator==( char ch, TQChar c )
\overload
\relates TQChar
Returns TRUE if \a c is the ASCII/Latin-1 character \a ch;
otherwise returns FALSE.
*/
/*!
\fn bool operator==( TQChar c, char ch )
\overload
\relates TQChar
Returns TRUE if \a c is the ASCII/Latin-1 character \a ch;
otherwise returns FALSE.
*/
/*!
\fn int operator!=( TQChar c1, TQChar c2 )
\relates TQChar
Returns TRUE if \a c1 and \a c2 are not the same Unicode
character; otherwise returns FALSE.
*/
/*!
\fn int operator!=( char ch, TQChar c )
\overload
\relates TQChar
Returns TRUE if \a c is not the ASCII/Latin-1 character \a ch;
otherwise returns FALSE.
*/
/*!
\fn int operator!=( TQChar c, char ch )
\overload
\relates TQChar
Returns TRUE if \a c is not the ASCII/Latin-1 character \a ch;
otherwise returns FALSE.
*/
/*!
\fn int operator<=( TQChar c1, TQChar c2 )
\relates TQChar
Returns TRUE if the numeric Unicode value of \a c1 is less than
that of \a c2, or they are the same Unicode character; otherwise
returns FALSE.
*/
/*!
\fn int operator<=( TQChar c, char ch )
\overload
\relates TQChar
Returns TRUE if the numeric Unicode value of \a c is less than or
equal to that of the ASCII/Latin-1 character \a ch; otherwise
returns FALSE.
*/
/*!
\fn int operator<=( char ch, TQChar c )
\overload
\relates TQChar
Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
character \a ch is less than or equal to that of \a c; otherwise
returns FALSE.
*/
/*!
\fn int operator>=( TQChar c1, TQChar c2 )
\relates TQChar
Returns TRUE if the numeric Unicode value of \a c1 is greater than
that of \a c2, or they are the same Unicode character; otherwise
returns FALSE.
*/
/*!
\fn int operator>=( TQChar c, char ch )
\overload
\relates TQChar
Returns TRUE if the numeric Unicode value of \a c is greater than
or equal to that of the ASCII/Latin-1 character \a ch; otherwise
returns FALSE.
*/
/*!
\fn int operator>=( char ch, TQChar c )
\overload
\relates TQChar
Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
character \a ch is greater than or equal to that of \a c;
otherwise returns FALSE.
*/
/*!
\fn int operator<( TQChar c1, TQChar c2 )
\relates TQChar
Returns TRUE if the numeric Unicode value of \a c1 is less than
that of \a c2; otherwise returns FALSE.
*/
/*!
\fn int operator<( TQChar c, char ch )
\overload
\relates TQChar
Returns TRUE if the numeric Unicode value of \a c is less than that
of the ASCII/Latin-1 character \a ch; otherwise returns FALSE.
*/
/*!
\fn int operator<( char ch, TQChar c )
\overload
\relates TQChar
Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
character \a ch is less than that of \a c; otherwise returns
FALSE.
*/
/*!
\fn int operator>( TQChar c1, TQChar c2 )
\relates TQChar
Returns TRUE if the numeric Unicode value of \a c1 is greater than
that of \a c2; otherwise returns FALSE.
*/
/*!
\fn int operator>( TQChar c, char ch )
\overload
\relates TQChar
Returns TRUE if the numeric Unicode value of \a c is greater than
that of the ASCII/Latin-1 character \a ch; otherwise returns FALSE.
*/
/*!
\fn int operator>( char ch, TQChar c )
\overload
\relates TQChar
Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
character \a ch is greater than that of \a c; otherwise returns
FALSE.
*/
#ifndef QT_NO_UNICODETABLES
// small class used internally in TQString::Compose()
class TQLigature
{
public:
TQLigature( TQChar c );
TQ_UINT16 first() { cur = ligatures; return cur ? *cur : 0; }
TQ_UINT16 next() { return cur && *cur ? *(cur++) : 0; }
TQ_UINT16 current() { return cur ? *cur : 0; }
int match(TQString & str, unsigned int index);
TQChar head();
TQChar::Decomposition tag();
private:
TQ_UINT16 *ligatures;
TQ_UINT16 *cur;
};
TQLigature::TQLigature( TQChar c )
{
register int pos = TQUnicodeTables::ligature_info[c.row()];
if( !pos )
ligatures = 0;
else
{
pos = TQUnicodeTables::ligature_info[(pos<<8)+c.cell()];
ligatures = (TQ_UINT16 *)&(TQUnicodeTables::ligature_map[pos]);
}
cur = ligatures;
}
TQChar TQLigature::head()
{
if(current())
return TQChar(TQUnicodeTables::decomposition_map[current()+1]);
return TQChar::null;
}
TQChar::Decomposition TQLigature::tag()
{
if(current())
return (TQChar::Decomposition) TQUnicodeTables::decomposition_map[current()];
return TQChar::Canonical;
}
int TQLigature::match(TQString & str, unsigned int index)
{
unsigned int i=index;
if(!current()) return 0;
TQ_UINT16 lig = current() + 2;
TQ_UINT16 ch;
while ((i < str.length()) && (ch = TQUnicodeTables::decomposition_map[lig])) {
if (str[(int)i] != TQChar(ch))
return 0;
i++;
lig++;
}
if (!TQUnicodeTables::decomposition_map[lig])
{
return i-index;
}
return 0;
}
// this function is just used in TQString::compose()
static inline bool format(TQChar::Decomposition tag, TQString & str,
int index, int len)
{
unsigned int l = index + len;
unsigned int r = index;
bool left = FALSE, right = FALSE;
left = ((l < str.length()) &&
((str[(int)l].joining() == TQChar::Dual) ||
(str[(int)l].joining() == TQChar::Right)));
if (r > 0) {
r--;
//printf("joining(right) = %d\n", str[(int)r].joining());
right = (str[(int)r].joining() == TQChar::Dual);
}
switch (tag) {
case TQChar::Medial:
return (left & right);
case TQChar::Initial:
return (left && !right);
case TQChar::Final:
return (right);// && !left);
case TQChar::Isolated:
default:
return (!right && !left);
}
} // format()
#endif
TQStringData::TQStringData() : TQShared(),
unicode(0),
ascii(0),
len(0),
issimpletext(TRUE),
maxl(0),
islatin1(FALSE),
security_unpaged(FALSE) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
mutex = new TQMutex(FALSE);
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
ref();
}
TQStringData::TQStringData(TQChar *u, uint l, uint m) : TQShared(),
unicode(u),
ascii(0),
len(l),
issimpletext(FALSE),
maxl(m),
islatin1(FALSE),
security_unpaged(FALSE) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
mutex = new TQMutex(FALSE);
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
}
TQStringData::~TQStringData() {
if ( unicode ) {
delete[] ((char*)unicode);
}
#if defined(Q_OS_LINUX)
if ( ascii && security_unpaged ) {
munlock(ascii, LINUX_MEMLOCK_LIMIT_BYTES);
}
#endif
if ( ascii ) {
delete[] ascii;
}
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
if ( mutex ) {
delete mutex;
mutex = NULL;
}
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
}
void TQStringData::setDirty() {
if ( ascii ) {
delete [] ascii;
ascii = 0;
}
issimpletext = FALSE;
}
/*
TQString::compose() and visual() were developed by Gordon Tisher
<tisher@uniserve.ca>, with input from Lars Knoll <knoll@mpi-hd.mpg.de>,
who developed the unicode data tables.
*/
/*!
\warning This function is not supported in TQt 3.x. It is provided
for experimental and illustrative purposes only. It is mainly of
interest to those experimenting with Arabic and other
composition-rich texts.
Applies possible ligatures to a TQString. Useful when
composition-rich text requires rendering with glyph-poor fonts,
but it also makes compositions such as TQChar(0x0041) ('A') and
TQChar(0x0308) (Unicode accent diaresis), giving TQChar(0x00c4)
(German A Umlaut).
*/
void TQString::compose()
{
#ifndef QT_NO_UNICODETABLES
unsigned int index=0, len;
unsigned int cindex = 0;
TQChar code, head;
TQMemArray<TQChar> dia;
TQString composed = *this;
while (index < length()) {
code = at(index);
//printf("\n\nligature for 0x%x:\n", code.unicode());
TQLigature ligature(code);
ligature.first();
while ( ligature.current() ) {
if ((len = ligature.match(*this, index)) != 0) {
head = ligature.head();
unsigned short code = head.unicode();
// we exclude Arabic presentation forms A and a few
// other ligatures, which are undefined in most fonts
if(!(code > 0xfb50 && code < 0xfe80) &&
!(code > 0xfb00 && code < 0xfb2a)) {
// joining info is only needed for Arabic
if (format(ligature.tag(), *this, index, len)) {
//printf("using ligature 0x%x, len=%d\n",code,len);
// replace letter
composed.replace(cindex, len, TQChar(head));
index += len-1;
// we continue searching in case we have a final
// form because medial ones are preferred.
if ( len != 1 || ligature.tag() !=TQChar::Final )
break;
}
}
}
ligature.next();
}
cindex++;
index++;
}
*this = composed;
#endif
}
// These macros are used for efficient allocation of TQChar strings.
// IMPORTANT! If you change these, make sure you also change the
// "delete unicode" statement in ~TQStringData() in ntqstring.h correspondingly!
#define QT_ALLOC_QCHAR_VEC( N ) (TQChar*) new char[ sizeof(TQChar)*( N ) ]
#define QT_DELETE_QCHAR_VEC( P ) delete[] ((char*)( P ))
/*!
This utility function converts the 8-bit string \a ba to Unicode,
returning the result.
The caller is responsible for deleting the return value with
delete[].
*/
TQChar* TQString::latin1ToUnicode( const TQByteArray& ba, uint* len )
{
if ( ba.isNull() ) {
*len = 0;
return 0;
}
int l = 0;
while ( l < (int)ba.size() && ba[l] )
l++;
char* str = ba.data();
TQChar *uc = new TQChar[ l ]; // Can't use macro, since function is public
TQChar *result = uc;
if ( len )
*len = l;
while (l--)
*uc++ = *str++;
return result;
}
static TQChar* internalLatin1ToUnicode( const TQByteArray& ba, uint* len )
{
if ( ba.isNull() ) {
*len = 0;
return 0;
}
int l = 0;
while ( l < (int)ba.size() && ba[l] )
l++;
char* str = ba.data();
TQChar *uc = QT_ALLOC_QCHAR_VEC( l );
TQChar *result = uc;
if ( len )
*len = l;
while (l--)
*uc++ = *str++;
return result;
}
/*!
\overload
This utility function converts the '\0'-terminated 8-bit string \a
str to Unicode, returning the result and setting \a *len to the
length of the Unicode string.
The caller is responsible for deleting the return value with
delete[].
*/
TQChar* TQString::latin1ToUnicode( const char *str, uint* len, uint maxlen )
{
TQChar* result = 0;
uint l = 0;
if ( str ) {
if ( maxlen != (uint)-1 ) {
while ( l < maxlen && str[l] )
l++;
} else {
// Faster?
l = int(strlen( str ));
}
TQChar *uc = new TQChar[ l ]; // Can't use macro since function is public
result = uc;
uint i = l;
while ( i-- )
*uc++ = *str++;
}
if ( len )
*len = l;
return result;
}
static TQChar* internalLatin1ToUnicode( const char *str, uint* len, uint maxlen = (uint)-1 )
{
TQChar* result = 0;
uint l = 0;
if ( str ) {
if ( maxlen != (uint)-1 ) {
while ( l < maxlen && str[l] ) {
l++;
}
}
else {
// Faster?
l = int(strlen( str ));
}
TQChar *uc = QT_ALLOC_QCHAR_VEC( l );
result = uc;
uint i = l;
while ( i-- ) {
*uc++ = *str++;
}
}
if ( len ) {
*len = l;
}
return result;
}
/*!
ABI compatibility
*/
char* TQString::unicodeToLatin1(const TQChar *uc, uint l)
{
return unicodeToLatin1(uc, l, false);
}
/*!
This utility function converts \a l 16-bit characters from \a uc
to ASCII, returning a '\0'-terminated string.
The caller is responsible for deleting the resultant string with
delete[].
*/
char* TQString::unicodeToLatin1(const TQChar *uc, uint l, bool unpaged)
{
if (!uc) {
return 0;
}
char *a = new char[l+1];
char *result = a;
if (unpaged) {
#if defined(Q_OS_LINUX)
mlock(result, LINUX_MEMLOCK_LIMIT_BYTES);
#endif
}
while (l--) {
*a++ = (uc->unicode() > 0xff) ? '?' : (char)uc->unicode();
uc++;
}
*a = '\0';
return result;
}
/*****************************************************************************
TQString member functions
*****************************************************************************/
/*!
\class TQString ntqstring.h
\reentrant
\brief The TQString class provides an abstraction of Unicode text
and the classic C '\0'-terminated char array.
\ingroup tools
\ingroup shared
\ingroup text
\mainclass
TQString uses \link shclass.html implicit sharing\endlink, which
makes it very efficient and easy to use.
In all of the TQString methods that take \c {const char *}
parameters, the \c {const char *} is interpreted as a classic
C-style '\0'-terminated ASCII string. It is legal for the \c
{const char *} parameter to be 0. If the \c {const char *} is not
'\0'-terminated, the results are undefined. Functions that copy
classic C strings into a TQString will not copy the terminating
'\0' character. The TQChar array of the TQString (as returned by
unicode()) is generally not terminated by a '\0'. If you need to
pass a TQString to a function that requires a C '\0'-terminated
string use latin1().
\keyword TQString::null
A TQString that has not been assigned to anything is \e null, i.e.
both the length and data pointer is 0. A TQString that references
the empty string ("", a single '\0' char) is \e empty. Both null
and empty TQStrings are legal parameters to the methods. Assigning
\c{(const char *) 0} to TQString gives a null TQString. For
convenience, \c TQString::null is a null TQString. When sorting,
empty strings come first, followed by non-empty strings, followed
by null strings. We recommend using \c{if ( !str.isNull() )} to
check for a non-null string rather than \c{if ( !str )}; see \l
operator!() for an explanation.
Note that if you find that you are mixing usage of \l TQCString,
TQString, and \l TQByteArray, this causes lots of unnecessary
copying and might indicate that the true nature of the data you
are dealing with is uncertain. If the data is '\0'-terminated 8-bit
data, use \l TQCString; if it is unterminated (i.e. contains '\0's)
8-bit data, use \l TQByteArray; if it is text, use TQString.
Lists of strings are handled by the TQStringList class. You can
split a string into a list of strings using TQStringList::split(),
and join a list of strings into a single string with an optional
separator using TQStringList::join(). You can obtain a list of
strings from a string list that contain a particular substring or
that match a particular \link ntqregexp.html regex\endlink using
TQStringList::grep().
<b>Note for C programmers</b>
Due to C++'s type system and the fact that TQString is implicitly
shared, TQStrings can be treated like ints or other simple base
types. For example:
\code
TQString boolToString( bool b )
{
TQString result;
if ( b )
result = "True";
else
result = "False";
return result;
}
\endcode
The variable, result, is an auto variable allocated on the stack.
When return is called, because we're returning by value, The copy
constructor is called and a copy of the string is returned. (No
actual copying takes place thanks to the implicit sharing, see
below.)
Throughout TQt's source code you will encounter TQString usages like
this:
\code
TQString func( const TQString& input )
{
TQString output = input;
// process output
return output;
}
\endcode
The 'copying' of input to output is almost as fast as copying a
pointer because behind the scenes copying is achieved by
incrementing a reference count. TQString (like all TQt's implicitly
shared classes) operates on a copy-on-write basis, only copying if
an instance is actually changed.
If you wish to create a deep copy of a TQString without losing any
Unicode information then you should use TQDeepCopy.
\sa TQChar TQCString TQByteArray TQConstString
*/
/*! \enum TQt::ComparisonFlags
\internal
*/
/*!
\enum TQt::StringComparisonMode
This enum type is used to set the string comparison mode when
searching for an item. It is used by TQListBox, TQListView and
TQIconView, for example. We'll refer to the string being searched
as the 'target' string.
\value CaseSensitive The strings must match case sensitively.
\value ExactMatch The target and search strings must match exactly.
\value BeginsWith The target string begins with the search string.
\value EndsWith The target string ends with the search string.
\value Contains The target string contains the search string.
If you OR these flags together (excluding \c CaseSensitive), the
search criteria be applied in the following order: \c ExactMatch,
\c BeginsWith, \c EndsWith, \c Contains.
Matching is case-insensitive unless \c CaseSensitive is set. \c
CaseSensitive can be OR-ed with any combination of the other
flags.
*/
Q_EXPORT TQStringData *TQString::shared_null = 0;
QT_STATIC_CONST_IMPL TQString TQString::null;
QT_STATIC_CONST_IMPL TQChar TQChar::null;
QT_STATIC_CONST_IMPL TQChar TQChar::replacement((ushort)0xfffd);
QT_STATIC_CONST_IMPL TQChar TQChar::byteOrderMark((ushort)0xfeff);
QT_STATIC_CONST_IMPL TQChar TQChar::byteOrderSwapped((ushort)0xfffe);
QT_STATIC_CONST_IMPL TQChar TQChar::nbsp((ushort)0x00a0);
TQStringData* TQString::makeSharedNull()
{
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
if (tqt_sharedStringMutex) tqt_sharedStringMutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
if (TQString::shared_null) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
if (tqt_sharedStringMutex) tqt_sharedStringMutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
return TQString::shared_null;
}
TQString::shared_null = new TQStringData;
#if defined( Q_OS_MAC ) || defined(Q_OS_SOLARIS) || defined(Q_OS_HPUX) || defined(Q_OS_AIX)
TQString *that = const_cast<TQString *>(&TQString::null);
that->d = TQString::shared_null;
#endif
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
if (tqt_sharedStringMutex) tqt_sharedStringMutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
return TQString::shared_null;
}
/*!
\fn TQString::TQString()
Constructs a null string, i.e. both the length and data pointer
are 0.
\sa isNull()
*/
// FIXME
// Original TQt3 code stated that there is
// "No safe way to pre-init shared_null on ALL compilers/linkers"
// Is this still true?
TQString::TQString() :
d(0)
{
d = shared_null ? shared_null : makeSharedNull();
}
/*!
Constructs a string of length one, containing the character \a ch.
*/
TQString::TQString( TQChar ch )
{
d = new TQStringData( QT_ALLOC_QCHAR_VEC( 1 ), 1, 1 );
d->unicode[0] = ch;
}
/*!
Constructs an implicitly shared copy of \a s. This is very fast
since it only involves incrementing a reference count.
*/
TQString::TQString( const TQString &s ) :
d(s.d)
{
if ( d && (d != shared_null) ) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
d->ref();
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
}
}
/*!
\internal
Private function.
Constructs a string with preallocated space for \a size characters.
The string is empty.
\sa isNull()
*/
TQString::TQString( int size, bool /*dummy*/ )
{
if ( size ) {
int l = size;
TQChar* uc = QT_ALLOC_QCHAR_VEC( l );
d = new TQStringData( uc, 0, l );
} else {
d = shared_null ? shared_null : (shared_null=new TQStringData);
}
}
/*!
Constructs a string that is a deep copy of \a ba interpreted as a
classic C string.
*/
TQString::TQString( const TQByteArray& ba )
{
#ifndef QT_NO_TEXTCODEC
if ( TQTextCodec::codecForCStrings() ) {
d = 0;
*this = fromAscii( ba.data(), ba.size() );
return;
}
#endif
uint l;
TQChar *uc = internalLatin1ToUnicode(ba,&l);
d = new TQStringData(uc,l,l);
}
/*!
Constructs a string that is a deep copy of the first \a length
characters in the TQChar array.
If \a unicode and \a length are 0, then a null string is created.
If only \a unicode is 0, the string is empty but has \a length
characters of space preallocated: TQString expands automatically
anyway, but this may speed up some cases a little. We recommend
using the plain constructor and setLength() for this purpose since
it will result in more readable code.
\sa isNull() setLength()
*/
TQString::TQString( const TQChar* unicode, uint length )
{
if ( !unicode && !length ) {
d = shared_null ? shared_null : makeSharedNull();
}
else {
TQChar* uc = QT_ALLOC_QCHAR_VEC( length );
if ( unicode ) {
memcpy(uc, unicode, length*sizeof(TQChar));
}
d = new TQStringData(uc,unicode ? length : 0,length);
}
}
/*!
Constructs a string that is a deep copy of \a str, interpreted as
a classic C string. The encoding is assumed to be Latin-1, unless
you change it using TQTextCodec::setCodecForCStrings().
If \a str is 0, then a null string is created.
This is a cast constructor, but it is perfectly safe: converting a
Latin-1 \c{const char *} to TQString preserves all the information. You
can disable this constructor by defining \c QT_NO_CAST_ASCII when
you compile your applications. You can also make TQString objects
by using setLatin1(), fromLatin1(), fromLocal8Bit(), and
fromUtf8(). Or whatever encoding is appropriate for the 8-bit data
you have.
\sa isNull(), fromAscii()
*/
TQString::TQString( const char *str )
{
#ifndef QT_NO_TEXTCODEC
if ( TQTextCodec::codecForCStrings() ) {
d = 0;
*this = fromAscii( str );
return;
}
#endif
uint l;
TQChar *uc = internalLatin1ToUnicode(str,&l);
d = new TQStringData(uc,l,l);
}
#ifndef QT_NO_STL
/*!
Constructs a string that is a deep copy of \a str.
This is the same as fromAscii(\a str).
*/
TQString::TQString( const std::string &str )
{
#ifndef QT_NO_TEXTCODEC
if ( TQTextCodec::codecForCStrings() ) {
d = 0;
*this = fromAscii( str.c_str() );
return;
}
#endif
uint l;
TQChar *uc = internalLatin1ToUnicode(str.c_str(),&l);
d = new TQStringData(uc,l,l);
}
#endif
TQString::TQString( TQStringData* dd, bool /* dummy */ ) {
d = dd;
}
/*!
\fn TQString::~TQString()
Destroys the string and frees the string's data if this is the
last reference to the string.
*/
TQString::~TQString()
{
#if defined(QT_CHECK_RANGE)
if (!d) {
tqWarning( "TQString::~TQString: Double free or delete detected!" );
return;
}
#endif
if (d == shared_null) {
return;
}
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
if ( d->deref() ) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
d->deleteSelf();
d = NULL;
}
else {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
}
}
/*!
Deallocates any space reserved solely by this TQString.
If the string does not share its data with another TQString
instance, nothing happens; otherwise the function creates a new,
unique copy of this string. This function is called whenever the
string is modified.
*/
void TQString::real_detach()
{
setLength( length() );
}
void TQString::deref()
{
if ( d && (d != shared_null) ) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
if ( d->deref() ) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
if ( d != shared_null ) {
delete d;
}
d = 0;
}
else {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
}
}
}
void TQStringData::deleteSelf()
{
delete this;
}
/*!
\fn TQString& TQString::operator=( TQChar c )
Sets the string to contain just the single character \a c.
*/
/*!
\fn TQString& TQString::operator=( const std::string& s )
\overload
Makes a deep copy of \a s and returns a reference to the deep
copy.
*/
/*!
\fn TQString& TQString::operator=( char c )
\overload
Sets the string to contain just the single character \a c.
*/
/*!
\overload
Assigns a shallow copy of \a s to this string and returns a
reference to this string. This is very fast because the string
isn't actually copied.
*/
TQString &TQString::operator=( const TQString &s )
{
if ( s.d && (s.d != shared_null) ) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
s.d->mutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
s.d->ref();
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
s.d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
}
deref();
d = s.d;
return *this;
}
/*!
\overload
Assigns a deep copy of \a cstr, interpreted as a classic C
string, to this string. Returns a reference to this string.
*/
TQString &TQString::operator=( const TQCString& cstr )
{
return setAscii( cstr );
}
/*!
\overload
Assigns a deep copy of \a str, interpreted as a classic C string
to this string and returns a reference to this string.
If \a str is 0, then a null string is created.
\sa isNull()
*/
TQString &TQString::operator=( const char *str )
{
return setAscii(str);
}
/*!
\fn bool TQString::isNull() const
Returns TRUE if the string is null; otherwise returns FALSE. A
null string is always empty.
\code
TQString a; // a.unicode() == 0, a.length() == 0
a.isNull(); // TRUE, because a.unicode() == 0
a.isEmpty(); // TRUE, because a.length() == 0
\endcode
\sa isEmpty(), length()
*/
/*!
\fn bool TQString::isEmpty() const
Returns TRUE if the string is empty, i.e. if length() == 0;
otherwise returns FALSE. Null strings are also empty.
\code
TQString a( "" );
a.isEmpty(); // TRUE
a.isNull(); // FALSE
TQString b;
b.isEmpty(); // TRUE
b.isNull(); // TRUE
\endcode
\sa isNull(), length()
*/
/*!
\fn uint TQString::length() const
Returns the length of the string.
Null strings and empty strings have zero length.
\sa isNull(), isEmpty()
*/
/*!
If \a newLen is less than the length of the string, then the
string is truncated at position \a newLen. Otherwise nothing
happens.
\code
TQString s = "truncate me";
s.truncate( 5 ); // s == "trunc"
\endcode
\sa setLength()
*/
void TQString::truncate( uint newLen )
{
if ( newLen < d->len )
setLength( newLen );
}
/*!
Ensures that at least \a newLen characters are allocated to the
string, and sets the length of the string to \a newLen. Any new
space allocated contains arbitrary data.
\sa reserve(), truncate()
*/
void TQString::setLength( uint newLen )
{
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
if ( d->count != 1 || newLen > d->maxl ||
( newLen * 4 < d->maxl && d->maxl > 4 ) ) {
// detach, grow or shrink
uint newMax = computeNewMax( newLen );
TQChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
if ( nd ) {
uint len = TQMIN( d->len, newLen );
memcpy( nd, d->unicode, sizeof(TQChar) * len );
bool unpaged = d->security_unpaged;
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
deref();
d = new TQStringData( nd, newLen, newMax );
setSecurityUnPaged(unpaged);
}
else {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
}
}
else {
d->len = newLen;
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
d->setDirty();
}
}
/*!
\fn uint TQString::capacity() const
Returns the number of characters this string can hold
in the allocated memory.
\sa reserve(), squeeze()
*/
/*!
Ensures that at least \a minCapacity characters are allocated to
the string.
This function is useful for code that needs to build up a long
string and wants to avoid repeated reallocation. In this example,
we want to add to the string until some condition is true, and
we're fairly sure that size is big enough:
\code
TQString result;
int len = 0;
result.reserve(maxLen);
while (...) {
result[len++] = ... // fill part of the space
}
result.squeeze();
\endcode
If \e maxLen is an underestimate, the worst that will happen is
that the loop will slow down.
If it is not possible to allocate enough memory, the string
remains unchanged.
\sa capacity(), squeeze(), setLength()
*/
void TQString::reserve( uint minCapacity )
{
if ( d->maxl < minCapacity ) {
TQChar *nd = QT_ALLOC_QCHAR_VEC( minCapacity );
if ( nd ) {
uint len = d->len;
if ( len )
memcpy( nd, d->unicode, sizeof(TQChar) * len );
bool unpaged = d->security_unpaged;
deref();
d = new TQStringData( nd, len, minCapacity );
setSecurityUnPaged(unpaged);
}
}
}
/*!
Squeezes the string's capacity to the current content.
\sa capacity(), reserve()
*/
void TQString::squeeze()
{
if ( d->maxl > d->len ) {
TQChar *nd = QT_ALLOC_QCHAR_VEC( d->len );
if ( nd ) {
uint len = d->len;
if ( len )
memcpy( nd, d->unicode, sizeof(TQChar) * len );
bool unpaged = d->security_unpaged;
deref();
d = new TQStringData( nd, len, len );
setSecurityUnPaged(unpaged);
}
}
}
/*!
\internal
Like setLength, but doesn't shrink the allocated memory.
*/
void TQString::grow( uint newLen )
{
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
if ( d->count != 1 || newLen > d->maxl ) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
setLength( newLen );
}
else {
d->len = newLen;
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
d->setDirty();
}
}
struct ArgEscapeData
{
uint min_escape; // lowest escape sequence number
uint occurrences; // number of occurences of the lowest escape
// sequence number
uint locale_occurrences; // number of occurences of the lowest escape
// sequence number which contain 'L'
uint escape_len; // total length of escape sequences which will
// be replaced
};
static ArgEscapeData findArgEscapes(const TQString &s)
{
const TQChar *uc_begin = s.unicode();
const TQChar *uc_end = uc_begin + s.length();
ArgEscapeData d;
d.min_escape = 10;
d.occurrences = 0;
d.escape_len = 0;
d.locale_occurrences = 0;
const TQChar *c = uc_begin;
while (c != uc_end) {
while (c != uc_end && c->unicode() != '%')
++c;
if (c == uc_end || ++c == uc_end)
break;
bool locale_arg = FALSE;
if (c->unicode() == 'L') {
locale_arg = TRUE;
if (++c == uc_end)
break;
}
if (c->unicode() < '0' || c->unicode() > '9')
continue;
uint escape = c->unicode() - '0';
++c;
if (escape > d.min_escape)
continue;
if (escape < d.min_escape) {
d.min_escape = escape;
d.occurrences = 0;
d.escape_len = 0;
d.locale_occurrences = 0;
}
#if TQT_VERSION < 0x040000
// ### remove preprocessor in TQt 4.0
/* Since in TQt < 4.0 only the first instance is replaced,
escape_len should hold the length of only the first escape
sequence */
if (d.occurrences == 0)
#endif
{
++d.occurrences;
if (locale_arg) {
++d.locale_occurrences;
d.escape_len += 3;
}
else
d.escape_len += 2;
}
}
return d;
}
static TQString replaceArgEscapes(const TQString &s, const ArgEscapeData &d, int field_width,
const TQString &arg, const TQString &larg)
{
const TQChar *uc_begin = s.unicode();
const TQChar *uc_end = uc_begin + s.length();
uint abs_field_width = TQABS(field_width);
uint result_len = s.length()
- d.escape_len
+ (d.occurrences - d.locale_occurrences)
*TQMAX(abs_field_width, arg.length())
+ d.locale_occurrences
*TQMAX(abs_field_width, larg.length());
TQString result;
result.setLength(result_len);
TQChar *result_buff = (TQChar*) result.unicode();
TQChar *rc = result_buff;
const TQChar *c = uc_begin;
uint repl_cnt = 0;
while (c != uc_end) {
/* We don't have to check if we run off the end of the string with c,
because as long as d.occurrences > 0 we KNOW there are valid escape
sequences. */
const TQChar *text_start = c;
while (c->unicode() != '%')
++c;
const TQChar *escape_start = c++;
bool locale_arg = FALSE;
if (c->unicode() == 'L') {
locale_arg = TRUE;
++c;
}
if (c->unicode() != '0' + d.min_escape) {
memcpy(rc, text_start, (c - text_start)*sizeof(TQChar));
rc += c - text_start;
}
else {
++c;
memcpy(rc, text_start, (escape_start - text_start)*sizeof(TQChar));
rc += escape_start - text_start;
uint pad_chars;
if (locale_arg)
pad_chars = TQMAX(abs_field_width, larg.length()) - larg.length();
else
pad_chars = TQMAX(abs_field_width, arg.length()) - arg.length();
if (field_width > 0) { // left padded
for (uint i = 0; i < pad_chars; ++i)
(rc++)->unicode() = ' ';
}
if (locale_arg) {
memcpy(rc, larg.unicode(), larg.length()*sizeof(TQChar));
rc += larg.length();
}
else {
memcpy(rc, arg.unicode(), arg.length()*sizeof(TQChar));
rc += arg.length();
}
if (field_width < 0) { // right padded
for (uint i = 0; i < pad_chars; ++i)
(rc++)->unicode() = ' ';
}
if (++repl_cnt == d.occurrences) {
memcpy(rc, c, (uc_end - c)*sizeof(TQChar));
rc += uc_end - c;
Q_ASSERT(rc - result_buff == (int)result_len);
c = uc_end;
}
}
}
return result;
}
/*!
This function will return a string that replaces the lowest
numbered occurrence of \c %1, \c %2, ..., \c %9 with \a a.
The \a fieldWidth value specifies the minimum amount of space that
\a a is padded to. A positive value will produce right-aligned
text, whereas a negative value will produce left-aligned text.
The following example shows how we could create a 'status' string
when processing a list of files:
\code
TQString status = TQString( "Processing file %1 of %2: %3" )
.arg( i ) // current file's number
.arg( total ) // number of files to process
.arg( fileName ); // current file's name
\endcode
It is generally fine to use filenames and numbers as we have done
in the example above. But note that using arg() to construct
natural language sentences does not usually translate well into
other languages because sentence structure and word order often
differ between languages.
If there is no place marker (\c %1, \c %2, etc.), a warning
message (tqWarning()) is output and the result is undefined.
\warning If any placeholder occurs more than once, the result is undefined.
*/
TQString TQString::arg( const TQString& a, int fieldWidth ) const
{
ArgEscapeData d = findArgEscapes(*this);
if (d.occurrences == 0) {
tqWarning( "TQString::arg(): Argument missing: %s, %s", latin1(),
a.latin1() );
return *this;
}
return replaceArgEscapes(*this, d, fieldWidth, a, a);
}
/*!
\fn TQString TQString::arg( const TQString& a1, const TQString& a2 ) const
\overload
This is the same as str.arg(\a a1).arg(\a a2), except that
the strings are replaced in one pass. This can make a difference
if \a a1 contains e.g. \c{%1}:
\code
TQString str( "%1 %2" );
str.arg( "Hello", "world" ); // returns "Hello world"
str.arg( "Hello" ).arg( "world" ); // returns "Hello world"
str.arg( "(%1)", "Hello" ); // returns "(%1) Hello"
str.arg( "(%1)" ).arg( "Hello" ); // returns "(Hello) %2"
\endcode
*/
/*!
\fn TQString TQString::arg( const TQString& a1, const TQString& a2,
const TQString& a3 ) const
\overload
This is the same as calling str.arg(\a a1).arg(\a a2).arg(\a a3),
except that the strings are replaced in one pass.
*/
/*!
\fn TQString TQString::arg( const TQString& a1, const TQString& a2,
const TQString& a3, const TQString& a4 ) const
\overload
This is the same as calling
str.arg(\a a1).arg(\a a2).arg(\a a3).arg(\a a4),
except that the strings are replaced in one pass.
*/
/*!
\overload
The \a fieldWidth value specifies the minimum amount of space that
\a a is padded to. A positive value will produce a right-aligned
number, whereas a negative value will produce a left-aligned
number.
\a a is expressed in base \a base, which is 10 by default and must
be between 2 and 36.
The '%' can be followed by an 'L', in which case the sequence is
replaced with a localized representation of \a a. The conversion
uses the default locale. The default locale is determined from the
system's locale settings at application startup. It can be changed
using TQLocale::setDefault(). The 'L' flag is ignored if \a base is
not 10.
\code
TQString str;
str = TQString( "Decimal 63 is %1 in hexadecimal" )
.arg( 63, 0, 16 );
// str == "Decimal 63 is 3f in hexadecimal"
TQLocale::setDefault(TQLocale::English, TQLocale::UnitedStates);
str = TQString( "%1 %L2 %L3" )
.arg( 12345 )
.arg( 12345 )
.arg( 12345, 0, 16 );
// str == "12345 12,345 3039"
\endcode
*/
TQString TQString::arg( long a, int fieldWidth, int base ) const
{
return arg((TQ_LLONG)a, fieldWidth, base);
}
/*!
\overload
\a a is expressed in base \a base, which is 10 by default and must
be between 2 and 36. If \a base is 10, the '%L' syntax can be used
to produce localized strings.
*/
TQString TQString::arg( ulong a, int fieldWidth, int base ) const
{
return arg((TQ_ULLONG)a, fieldWidth, base);
}
/*!
\overload
\a a is expressed in base \a base, which is 10 by default and must
be between 2 and 36. If \a base is 10, the '%L' syntax can be used
to produce localized strings.
*/
TQString TQString::arg( TQ_LLONG a, int fieldWidth, int base ) const
{
ArgEscapeData d = findArgEscapes(*this);
if (d.occurrences == 0) {
tqWarning( "TQString::arg(): Argument missing: %s, %lld", latin1(),
a );
return *this;
}
TQString arg;
if (d.occurrences > d.locale_occurrences)
arg = number(a, base);
TQString locale_arg;
if (d.locale_occurrences > 0) {
TQLocale locale;
locale_arg = locale.d->longLongToString(a, -1, base, -1, TQLocalePrivate::ThousandsGroup);
}
return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg);
}
/*!
\overload
\a a is expressed in base \a base, which is 10 by default and must
be between 2 and 36. If \a base is 10, the '%L' syntax can be used
to produce localized strings.
*/
TQString TQString::arg( TQ_ULLONG a, int fieldWidth, int base ) const
{
ArgEscapeData d = findArgEscapes(*this);
if (d.occurrences == 0) {
tqWarning( "TQString::arg(): Argument missing: %s, %llu", latin1(),
a );
return *this;
}
TQString arg;
if (d.occurrences > d.locale_occurrences)
arg = number(a, base);
TQString locale_arg;
if (d.locale_occurrences > 0) {
TQLocale locale;
locale_arg = locale.d->unsLongLongToString(a, -1, base, -1, TQLocalePrivate::ThousandsGroup);
}
return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg);
}
/*!
\fn TQString TQString::arg( int a, int fieldWidth, int base ) const
\overload
\a a is expressed in base \a base, which is 10 by default and must
be between 2 and 36. If \a base is 10, the '%L' syntax can be used
to produce localized strings.
*/
/*!
\fn TQString TQString::arg( uint a, int fieldWidth, int base ) const
\overload
\a a is expressed in base \a base, which is 10 by default and must
be between 2 and 36. If \a base is 10, the '%L' syntax can be used
to produce localized strings.
*/
/*!
\fn TQString TQString::arg( short a, int fieldWidth, int base ) const
\overload
\a a is expressed in base \a base, which is 10 by default and must
be between 2 and 36. If \a base is 10, the '%L' syntax can be used
to produce localized strings.
*/
/*!
\fn TQString TQString::arg( ushort a, int fieldWidth, int base ) const
\overload
\a a is expressed in base \a base, which is 10 by default and must
be between 2 and 36. If \a base is 10, the '%L' syntax can be used
to produce localized strings.
*/
/*!
\overload
\a a is assumed to be in the Latin-1 character set.
*/
TQString TQString::arg( char a, int fieldWidth ) const
{
TQString c;
c += a;
return arg( c, fieldWidth );
}
/*!
\overload
*/
TQString TQString::arg( TQChar a, int fieldWidth ) const
{
TQString c;
c += a;
return arg( c, fieldWidth );
}
/*!
\overload
\target arg-formats
Argument \a a is formatted according to the \a fmt format specified,
which is 'g' by default and can be any of the following:
\table
\header \i Format \i Meaning
\row \i \c e \i format as [-]9.9e[+|-]999
\row \i \c E \i format as [-]9.9E[+|-]999
\row \i \c f \i format as [-]9.9
\row \i \c g \i use \c e or \c f format, whichever is the most concise
\row \i \c G \i use \c E or \c f format, whichever is the most concise
\endtable
With 'e', 'E', and 'f', \a prec is the number of digits after the
decimal point. With 'g' and 'G', \a prec is the maximum number of
significant digits (trailing zeroes are omitted).
\code
double d = 12.34;
TQString ds = TQString( "'E' format, precision 3, gives %1" )
.arg( d, 0, 'E', 3 );
// ds == "'E' format, precision 3, gives 1.234E+01"
\endcode
The '%L' syntax can be used to produce localized strings.
*/
TQString TQString::arg( double a, int fieldWidth, char fmt, int prec ) const
{
ArgEscapeData d = findArgEscapes(*this);
if (d.occurrences == 0) {
tqWarning( "TQString::arg(): Argument missing: %s, %g", latin1(),
a );
return *this;
}
TQString arg;
if (d.occurrences > d.locale_occurrences)
arg = number(a, fmt, prec);
TQString locale_arg;
if (d.locale_occurrences > 0) {
TQLocale locale;
TQLocalePrivate::DoubleForm form = TQLocalePrivate::DFDecimal;
uint flags = 0;
if (qIsUpper(fmt))
flags = TQLocalePrivate::CapitalEorX;
fmt = qToLower(fmt);
switch (fmt) {
case 'f':
form = TQLocalePrivate::DFDecimal;
break;
case 'e':
form = TQLocalePrivate::DFExponent;
break;
case 'g':
form = TQLocalePrivate::DFSignificantDigits;
break;
default:
#if defined(QT_CHECK_RANGE)
tqWarning( "TQString::setNum: Invalid format char '%c'", fmt );
#endif
break;
}
flags |= TQLocalePrivate::ThousandsGroup;
locale_arg = locale.d->doubleToString(a, prec, form, -1, flags);
}
return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg);
}
TQString TQString::multiArg( int numArgs, const TQString& a1, const TQString& a2,
const TQString& a3, const TQString& a4 ) const
{
TQString result;
union {
int digitUsed[10];
int argForDigit[10];
};
register const TQChar *uc = d->unicode;
const TQString *args[4];
const int len = (int) length();
const int end = len - 1;
int lastDigit = -1;
int i;
memset( digitUsed, 0, sizeof(digitUsed) );
args[0] = &a1;
args[1] = &a2;
args[2] = &a3;
args[3] = &a4;
for ( i = 0; i < end; i++ ) {
if ( uc[i] == '%' ) {
int digit = uc[i + 1].unicode() - '0';
if ( digit >= 0 && digit <= 9 )
digitUsed[digit]++;
}
}
for ( i = 0; i < numArgs; i++ ) {
do {
++lastDigit;
} while ( lastDigit < 10 && digitUsed[lastDigit] == 0 );
if ( lastDigit == 10 ) {
tqWarning( "TQString::arg(): Argument missing: %s, %s",
latin1(), args[i]->latin1() );
numArgs = i;
lastDigit = 9;
break;
}
argForDigit[lastDigit] = i;
}
i = 0;
while ( i < len ) {
if ( uc[i] == '%' && i != end ) {
int digit = uc[i + 1].unicode() - '0';
if ( digit >= 0 && digit <= lastDigit ) {
result += *args[argForDigit[digit]];
i += 2;
continue;
}
}
result += uc[i++];
}
return result;
}
/*!
Safely builds a formatted string from the format string \a cformat
and an arbitrary list of arguments. The format string supports all
the escape sequences of printf() in the standard C library.
The %s escape sequence expects a utf8() encoded string. The format
string \e cformat is expected to be in latin1. If you need a
Unicode format string, use arg() instead. For typesafe string
building, with full Unicode support, you can use TQTextOStream like
this:
\code
TQString str;
TQString s = ...;
int x = ...;
TQTextOStream( &str ) << s << " : " << x;
\endcode
For \link TQObject::tr() translations,\endlink especially if the
strings contains more than one escape sequence, you should
consider using the arg() function instead. This allows the order
of the replacements to be controlled by the translator, and has
Unicode support.
The %lc escape sequence expects a unicode character of type ushort
(as returned by TQChar::unicode()).
The %ls escape sequence expects a pointer to a zero-terminated
array of unicode characters of type ushort (as returned by
TQString::ucs2()).
\sa arg()
*/
#ifndef QT_NO_SPRINTF
TQString &TQString::sprintf(const char *cformat, ...)
{
va_list ap;
va_start(ap, cformat);
if ( !cformat || !*cformat ) {
// TQt 1.x compat
*this = fromLatin1( "" );
return *this;
}
TQString &s = vsprintf(cformat, ap);
va_end(ap);
return s;
}
TQString &TQString::vsprintf( const char* cformat, va_list ap )
{
TQLocale locale(TQLocale::C);
// Parse cformat
TQString result;
const char *c = cformat;
for (;;) {
// Copy non-escape chars to result
while (*c != '\0' && *c != '%')
result.append(*c++);
if (*c == '\0')
break;
// Found '%'
const char *escape_start = c;
++c;
if (*c == '\0') {
result.append('%'); // a % at the end of the string - treat as non-escape text
break;
}
if (*c == '%') {
result.append('%'); // %%
++c;
continue;
}
// Parse flag characters
unsigned flags = 0;
bool no_more_flags = FALSE;
do {
switch (*c) {
case '#': flags |= TQLocalePrivate::Alternate; break;
case '0': flags |= TQLocalePrivate::ZeroPadded; break;
case '-': flags |= TQLocalePrivate::LeftAdjusted; break;
case ' ': flags |= TQLocalePrivate::BlankBeforePositive; break;
case '+': flags |= TQLocalePrivate::AlwaysShowSign; break;
case '\'': flags |= TQLocalePrivate::ThousandsGroup; break;
default: no_more_flags = TRUE; break;
}
if (!no_more_flags)
++c;
} while (!no_more_flags);
if (*c == '\0') {
result.append(escape_start); // incomplete escape, treat as non-escape text
break;
}
// Parse field width
int width = -1; // -1 means unspecified
if (qIsDigit(*c)) {
TQString width_str;
while (*c != '\0' && qIsDigit(*c))
width_str.append(*c++);
// can't be negative - started with a digit
// contains at least one digit
width = width_str.toInt();
}
else if (*c == '*') {
width = va_arg(ap, int);
if (width < 0)
width = -1; // treat all negative numbers as unspecified
++c;
}
if (*c == '\0') {
result.append(escape_start); // incomplete escape, treat as non-escape text
break;
}
// Parse precision
int precision = -1; // -1 means unspecified
if (*c == '.') {
++c;
if (qIsDigit(*c)) {
TQString precision_str;
while (*c != '\0' && qIsDigit(*c))
precision_str.append(*c++);
// can't be negative - started with a digit
// contains at least one digit
precision = precision_str.toInt();
}
else if (*c == '*') {
precision = va_arg(ap, int);
if (precision < 0)
precision = -1; // treat all negative numbers as unspecified
++c;
}
}
if (*c == '\0') {
result.append(escape_start); // incomplete escape, treat as non-escape text
break;
}
// Parse the length modifier
enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t };
LengthMod length_mod = lm_none;
switch (*c) {
case 'h':
++c;
if (*c == 'h') {
length_mod = lm_hh;
++c;
}
else
length_mod = lm_h;
break;
case 'l':
++c;
if (*c == 'l') {
length_mod = lm_ll;
++c;
}
else
length_mod = lm_l;
break;
case 'L':
++c;
length_mod = lm_L;
break;
case 'j':
++c;
length_mod = lm_j;
break;
case 'z':
case 'Z':
++c;
length_mod = lm_z;
break;
case 't':
++c;
length_mod = lm_t;
break;
default: break;
}
if (*c == '\0') {
result.append(escape_start); // incomplete escape, treat as non-escape text
break;
}
// Parse the conversion specifier and do the conversion
TQString subst;
switch (*c) {
case 'd':
case 'i': {
TQ_LLONG i;
switch (length_mod) {
case lm_none: i = va_arg(ap, int); break;
case lm_hh: i = va_arg(ap, int); break;
case lm_h: i = va_arg(ap, int); break;
case lm_l: i = va_arg(ap, long int); break;
case lm_ll: i = va_arg(ap, TQ_LLONG); break;
case lm_j: i = va_arg(ap, long int); break;
case lm_z: i = va_arg(ap, size_t); break;
case lm_t: i = va_arg(ap, int); break;
default: i = 0; break;
}
subst = locale.d->longLongToString(i, precision, 10, width, flags);
++c;
break;
}
case 'o':
case 'u':
case 'x':
case 'X': {
TQ_ULLONG u;
switch (length_mod) {
case lm_none: u = va_arg(ap, unsigned int); break;
case lm_hh: u = va_arg(ap, unsigned int); break;
case lm_h: u = va_arg(ap, unsigned int); break;
case lm_l: u = va_arg(ap, unsigned long int); break;
case lm_ll: u = va_arg(ap, TQ_ULLONG); break;
default: u = 0; break;
}
if (qIsUpper(*c))
flags |= TQLocalePrivate::CapitalEorX;
int base = 10;
switch (qToLower(*c)) {
case 'o':
base = 8; break;
case 'u':
base = 10; break;
case 'x':
base = 16; break;
default: break;
}
subst = locale.d->unsLongLongToString(u, precision, base, width, flags);
++c;
break;
}
case 'E':
case 'e':
case 'F':
case 'f':
case 'G':
case 'g':
case 'A':
case 'a': {
double d;
if (length_mod == lm_L)
d = va_arg(ap, long double); // not supported - converted to a double
else
d = va_arg(ap, double);
if (qIsUpper(*c))
flags |= TQLocalePrivate::CapitalEorX;
TQLocalePrivate::DoubleForm form = TQLocalePrivate::DFDecimal;
switch (qToLower(*c)) {
case 'e': form = TQLocalePrivate::DFExponent; break;
case 'a': // not supported - decimal form used instead
case 'f': form = TQLocalePrivate::DFDecimal; break;
case 'g': form = TQLocalePrivate::DFSignificantDigits; break;
default: break;
}
subst = locale.d->doubleToString(d, precision, form, width, flags);
++c;
break;
}
case 'c': {
if (length_mod == lm_l)
subst = TQChar((ushort) va_arg(ap, int));
else
subst = (uchar) va_arg(ap, int);
++c;
break;
}
case 's': {
if (length_mod == lm_l) {
const ushort *buff = va_arg(ap, const ushort*);
const ushort *ch = buff;
while (*ch != 0)
++ch;
subst.setUnicodeCodes(buff, ch - buff);
} else
subst = TQString::fromUtf8(va_arg(ap, const char*));
if (precision != -1)
subst.truncate(precision);
++c;
break;
}
case 'p': {
TQ_ULLONG i;
#ifdef Q_OS_WIN64
i = (TQ_ULLONG) va_arg(ap, void*);
#else
i = (TQ_ULONG) va_arg(ap, void*);
#endif
#ifdef Q_OS_WIN32
flags |= TQLocalePrivate::CapitalEorX; // Windows does 1234ABCD
#else
flags |= TQLocalePrivate::Alternate; // Unix and Mac do 0x1234abcd
#endif
subst = locale.d->unsLongLongToString(i, precision, 16, width, flags);
++c;
break;
}
case 'n':
switch (length_mod) {
case lm_hh: {
signed char *n = va_arg(ap, signed char*);
*n = result.length();
break;
}
case lm_h: {
short int *n = va_arg(ap, short int*);
*n = result.length();
break;
}
case lm_l: {
long int *n = va_arg(ap, long int*);
*n = result.length();
break;
}
case lm_ll: {
TQ_LLONG *n = va_arg(ap, TQ_LLONG*);
volatile uint tmp = result.length(); // egcs-2.91.66 gets internal
*n = tmp; // compiler error without volatile
break;
}
default: {
int *n = va_arg(ap, int*);
*n = result.length();
break;
}
}
++c;
break;
default: // bad escape, treat as non-escape text
for (const char *cc = escape_start; cc != c; ++cc)
result.append(*cc);
continue;
}
if (flags & TQLocalePrivate::LeftAdjusted)
result.append(subst.leftJustify(width));
else
result.append(subst.rightJustify(width));
}
*this = result;
return *this;
}
#endif
/*!
Fills the string with \a len characters of value \a c, and returns
a reference to the string.
If \a len is negative (the default), the current string length is
used.
\code
TQString str;
str.fill( 'g', 5 ); // string == "ggggg"
\endcode
*/
TQString& TQString::fill( TQChar c, int len )
{
if ( len < 0 )
len = length();
if ( len == 0 ) {
*this = "";
} else {
bool unpaged = d->security_unpaged;
deref();
TQChar * nd = QT_ALLOC_QCHAR_VEC( len );
d = new TQStringData(nd,len,len);
setSecurityUnPaged(unpaged);
while (len--) *nd++ = c;
}
return *this;
}
/*!
\fn TQString TQString::copy() const
\obsolete
In TQt 2.0 and later, all calls to this function are needless. Just
remove them.
*/
/*!
\overload
Finds the first occurrence of the character \a c, starting at
position \a index. If \a index is -1, the search starts at the
last character; if -2, at the next to last character and so on.
(See findRev() for searching backwards.)
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
Returns the position of \a c or -1 if \a c could not be found.
*/
int TQString::find( TQChar c, int index, bool cs ) const
{
const uint l = length();
if ( index < 0 )
index += l;
if ( (uint)index >= l )
return -1;
register const TQChar *uc = unicode()+index;
const TQChar *end = unicode() + l;
if ( cs ) {
while ( uc < end && *uc != c )
uc++;
} else {
c = ::lower( c );
while ( uc < end && ::lower( *uc ) != c )
uc++;
}
if ( uint(uc - unicode()) >= l )
return -1;
return (int)(uc - unicode());
}
/* an implementation of the Boyer-Moore search algorithm
*/
/* initializes the skiptable to know haw far ahead we can skip on a wrong match
*/
static void bm_init_skiptable( const TQString &pattern, uint *skiptable, bool cs )
{
int i = 0;
register uint *st = skiptable;
int l = pattern.length();
while ( i++ < 0x100/8 ) {
*(st++) = l;
*(st++) = l;
*(st++) = l;
*(st++) = l;
*(st++) = l;
*(st++) = l;
*(st++) = l;
*(st++) = l;
}
const TQChar *uc = pattern.unicode();
if ( cs ) {
while ( l-- ) {
skiptable[ uc->cell() ] = l;
uc++;
}
} else {
while ( l-- ) {
skiptable[ ::lower( *uc ).cell() ] = l;
uc++;
}
}
}
static int bm_find( const TQString &str, int index, const TQString &pattern, uint *skiptable, bool cs )
{
const uint l = str.length();
if ( pattern.isEmpty() )
return index > (int)l ? -1 : index;
const TQChar *uc = str.unicode();
const TQChar *puc = pattern.unicode();
const uint pl = pattern.length();
const uint pl_minus_one = pl - 1;
register const TQChar *current = uc + index + pl_minus_one;
const TQChar *end = uc + l;
if ( cs ) {
while ( current < end ) {
uint skip = skiptable[ current->cell() ];
if ( !skip ) {
// possible match
while ( skip < pl ) {
if ( *(current - skip ) != puc[pl_minus_one-skip] )
break;
skip++;
}
if ( skip > pl_minus_one ) { // we have a match
return (current - uc) - skip + 1;
}
// in case we don't have a match we are a bit inefficient as we only skip by one
// when we have the non matching char in the string.
if ( skiptable[ (current-skip)->cell() ] == pl )
skip = pl - skip;
else
skip = 1;
}
current += skip;
}
} else {
while ( current < end ) {
uint skip = skiptable[ ::lower( *current ).cell() ];
if ( !skip ) {
// possible match
while ( skip < pl ) {
if ( ::lower( *(current - skip) ) != ::lower( puc[pl_minus_one-skip] ) )
break;
skip++;
}
if ( skip > pl_minus_one ) // we have a match
return (current - uc) - skip + 1;
// in case we don't have a match we are a bit inefficient as we only skip by one
// when we have the non matching char in the string.
if ( skiptable[ ::lower(*(current - skip)).cell() ] == pl )
skip = pl - skip;
else
skip = 1;
}
current += skip;
}
}
// not found
return -1;
}
#define REHASH( a ) \
if ( sl_minus_1 < sizeof(uint) * CHAR_BIT ) \
hashHaystack -= (a) << sl_minus_1; \
hashHaystack <<= 1
/*!
\overload
Finds the first occurrence of the string \a str, starting at
position \a index. If \a index is -1, the search starts at the
last character, if it is -2, at the next to last character and so
on. (See findRev() for searching backwards.)
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
Returns the position of \a str or -1 if \a str could not be found.
*/
int TQString::find( const TQString& str, int index, bool cs ) const
{
const uint l = length();
const uint sl = str.length();
if ( index < 0 )
index += l;
if ( sl + index > l )
return -1;
if ( !sl )
return index;
if (!l)
return -1;
#if defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION >= 0x1020
if ( sl == 1 )
return find( *str.unicode(), index, cs );
#endif
// we use the Boyer-Moore algorithm in cases where the overhead
// for the hash table should pay off, otherwise we use a simple
// hash function
if ( l > 500 && sl > 5 ) {
uint skiptable[0x100];
bm_init_skiptable( str, skiptable, cs );
return bm_find( *this, index, str, skiptable, cs );
}
/*
We use some hashing for efficiency's sake. Instead of
comparing strings, we compare the hash value of str with that of
a part of this TQString. Only if that matches, we call ucstrncmp
or ucstrnicmp.
*/
const TQChar* needle = str.unicode();
const TQChar* haystack = unicode() + index;
const TQChar* end = unicode() + (l-sl);
const uint sl_minus_1 = sl-1;
uint hashNeedle = 0, hashHaystack = 0, i;
if ( cs ) {
for ( i = 0; i < sl; ++i ) {
hashNeedle = ((hashNeedle<<1) + needle[i].unicode() );
hashHaystack = ((hashHaystack<<1) + haystack[i].unicode() );
}
hashHaystack -= (haystack+sl_minus_1)->unicode();
while ( haystack <= end ) {
hashHaystack += (haystack+sl_minus_1)->unicode();
if ( hashHaystack == hashNeedle
&& ucstrncmp( needle, haystack, sl ) == 0 )
return haystack-unicode();
REHASH( haystack->unicode() );
++haystack;
}
} else {
for ( i = 0; i < sl; ++i ) {
hashNeedle = ((hashNeedle<<1) +
::lower( needle[i].unicode() ).unicode() );
hashHaystack = ((hashHaystack<<1) +
::lower( haystack[i].unicode() ).unicode() );
}
hashHaystack -= ::lower(*(haystack+sl_minus_1)).unicode();
while ( haystack <= end ) {
hashHaystack += ::lower(*(haystack+sl_minus_1)).unicode();
if ( hashHaystack == hashNeedle
&& ucstrnicmp( needle, haystack, sl ) == 0 )
return haystack-unicode();
REHASH( ::lower(*haystack).unicode() );
++haystack;
}
}
return -1;
}
/*!
\fn int TQString::findRev( const char* str, int index ) const
Equivalent to findRev(TQString(\a str), \a index).
*/
/*!
\fn int TQString::find( const char* str, int index ) const
\overload
Equivalent to find(TQString(\a str), \a index).
*/
/*!
\overload
Finds the first occurrence of the character \a c, starting at
position \a index and searching backwards. If the index is -1, the
search starts at the last character, if it is -2, at the next to
last character and so on.
Returns the position of \a c or -1 if \a c could not be found.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
\code
TQString string( "bananas" );
int i = string.findRev( 'a' ); // i == 5
\endcode
*/
int TQString::findRev( TQChar c, int index, bool cs ) const
{
#if defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION < 0x1020
return findRev( TQString( c ), index, cs );
#else
const uint l = length();
if ( index < 0 )
index += l;
if ( (uint)index >= l )
return -1;
const TQChar *end = unicode();
register const TQChar *uc = end + index;
if ( cs ) {
while ( uc >= end && *uc != c )
uc--;
} else {
c = ::lower( c );
while ( uc >= end && ::lower( *uc ) != c )
uc--;
}
return uc - end;
#endif
}
/*!
\overload
Finds the first occurrence of the string \a str, starting at
position \a index and searching backwards. If the index is -1, the
search starts at the last character, if it is -2, at the next to
last character and so on.
Returns the position of \a str or -1 if \a str could not be found.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
\code
TQString string("bananas");
int i = string.findRev( "ana" ); // i == 3
\endcode
*/
int TQString::findRev( const TQString& str, int index, bool cs ) const
{
/*
See TQString::find() for explanations.
*/
const uint l = length();
if ( index < 0 )
index += l;
const uint sl = str.length();
int delta = l-sl;
if ( index < 0 || index > (int)l || delta < 0 )
return -1;
if ( index > delta )
index = delta;
#if defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION >= 0x1020
if ( sl == 1 )
return findRev( *str.unicode(), index, cs );
#endif
const TQChar* needle = str.unicode();
const TQChar* haystack = unicode() + index;
const TQChar* end = unicode();
const uint sl_minus_1 = sl-1;
const TQChar* n = needle+sl_minus_1;
const TQChar* h = haystack+sl_minus_1;
uint hashNeedle = 0, hashHaystack = 0, i;
if ( cs ) {
for ( i = 0; i < sl; ++i ) {
hashNeedle = ((hashNeedle<<1) + (n-i)->unicode() );
hashHaystack = ((hashHaystack<<1) + (h-i)->unicode() );
}
hashHaystack -= haystack->unicode();
while ( haystack >= end ) {
hashHaystack += haystack->unicode();
if ( hashHaystack == hashNeedle
&& ucstrncmp( needle, haystack, sl ) == 0 )
return haystack-unicode();
--haystack;
REHASH( (haystack+sl)->unicode() );
}
} else {
for ( i = 0; i < sl; ++i ) {
hashNeedle = ((hashNeedle<<1)
+ ::lower( (n-i)->unicode() ).unicode() );
hashHaystack = ((hashHaystack<<1)
+ ::lower( (h-i)->unicode() ).unicode() );
}
hashHaystack -= ::lower(*haystack).unicode();
while ( haystack >= end ) {
hashHaystack += ::lower(*haystack).unicode();
if ( hashHaystack == hashNeedle
&& ucstrnicmp( needle, haystack, sl ) == 0 )
return haystack-unicode();
--haystack;
REHASH( ::lower(*(haystack+sl)).unicode() );
}
}
return -1;
}
#undef REHASH
/*!
\enum TQString::SectionFlags
\value SectionDefault Empty fields are counted, leading and
trailing separators are not included, and the separator is
compared case sensitively.
\value SectionSkipEmpty Treat empty fields as if they don't exist,
i.e. they are not considered as far as \e start and \e end are
concerned.
\value SectionIncludeLeadingSep Include the leading separator (if
any) in the result string.
\value SectionIncludeTrailingSep Include the trailing separator
(if any) in the result string.
\value SectionCaseInsensitiveSeps Compare the separator
case-insensitively.
Any of the last four values can be OR-ed together to form a flag.
\sa section()
*/
/*!
\fn TQString TQString::section( TQChar sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const
This function returns a section of the string.
This string is treated as a sequence of fields separated by the
character, \a sep. The returned string consists of the fields from
position \a start to position \a end inclusive. If \a end is not
specified, all fields from position \a start to the end of the
string are included. Fields are numbered 0, 1, 2, etc., counting
from the left, and -1, -2, etc., counting from right to left.
The \a flags argument can be used to affect some aspects of the
function's behaviour, e.g. whether to be case sensitive, whether
to skip empty fields and how to deal with leading and trailing
separators; see \l{SectionFlags}.
\code
TQString csv( "forename,middlename,surname,phone" );
TQString s = csv.section( ',', 2, 2 ); // s == "surname"
TQString path( "/usr/local/bin/myapp" ); // First field is empty
TQString s = path.section( '/', 3, 4 ); // s == "bin/myapp"
TQString s = path.section( '/', 3, 3, SectionSkipEmpty ); // s == "myapp"
\endcode
If \a start or \a end is negative, we count fields from the right
of the string, the right-most field being -1, the one from
right-most field being -2, and so on.
\code
TQString csv( "forename,middlename,surname,phone" );
TQString s = csv.section( ',', -3, -2 ); // s == "middlename,surname"
TQString path( "/usr/local/bin/myapp" ); // First field is empty
TQString s = path.section( '/', -1 ); // s == "myapp"
\endcode
\sa TQStringList::split()
*/
/*!
\overload
This function returns a section of the string.
This string is treated as a sequence of fields separated by the
string, \a sep. The returned string consists of the fields from
position \a start to position \a end inclusive. If \a end is not
specified, all fields from position \a start to the end of the
string are included. Fields are numbered 0, 1, 2, etc., counting
from the left, and -1, -2, etc., counting from right to left.
The \a flags argument can be used to affect some aspects of the
function's behaviour, e.g. whether to be case sensitive, whether
to skip empty fields and how to deal with leading and trailing
separators; see \l{SectionFlags}.
\code
TQString data( "forename**middlename**surname**phone" );
TQString s = data.section( "**", 2, 2 ); // s == "surname"
\endcode
If \a start or \a end is negative, we count fields from the right
of the string, the right-most field being -1, the one from
right-most field being -2, and so on.
\code
TQString data( "forename**middlename**surname**phone" );
TQString s = data.section( "**", -3, -2 ); // s == "middlename**surname"
\endcode
\sa TQStringList::split()
*/
TQString TQString::section( const TQString &sep, int start, int end, int flags ) const
{
TQStringList sections = TQStringList::split(sep, *this, TRUE);
if(sections.isEmpty())
return TQString();
if(!(flags & SectionSkipEmpty)) {
if(start < 0)
start += int(sections.count());
if(end < 0)
end += int(sections.count());
} else {
int skip = 0;
for(TQStringList::Iterator it = sections.begin(); it != sections.end(); ++it) {
if((*it).isEmpty())
skip++;
}
if(start < 0)
start += int(sections.count()) - skip;
if(end < 0)
end += int(sections.count()) - skip;
}
int x = 0, run = 0;
TQString ret;
for(TQStringList::Iterator it = sections.begin(); x <= end && it != sections.end(); ++it) {
if(x >= start) {
if((*it).isEmpty()) {
run++;
} else {
if(!ret.isEmpty() || !(flags & SectionSkipEmpty)) {
int i_end = run;
if(!ret.isEmpty() && !(flags & SectionIncludeTrailingSep))
i_end++;
if((flags & SectionIncludeLeadingSep) && it != sections.begin() && x == start)
i_end++;
for(int i = 0; i < i_end; i++)
ret += sep;
} else if((flags & SectionIncludeLeadingSep) && it != sections.begin()) {
ret += sep;
}
run = 0;
ret += (*it);
if((flags & SectionIncludeTrailingSep) && it != sections.end())
ret += sep;
}
}
if(!(*it).isEmpty() || !(flags & SectionSkipEmpty))
x++;
}
return ret;
}
#ifndef QT_NO_REGEXP
class section_chunk {
public:
section_chunk(int l, TQString s) { length = l; string = s; }
int length;
TQString string;
};
/*!
\overload
This function returns a section of the string.
This string is treated as a sequence of fields separated by the
regular expression, \a reg. The returned string consists of the
fields from position \a start to position \a end inclusive. If \a
end is not specified, all fields from position \a start to the end
of the string are included. Fields are numbered 0, 1, 2, etc., counting
from the left, and -1, -2, etc., counting from right to left.
The \a flags argument can be used to affect some aspects of the
function's behaviour, e.g. whether to be case sensitive, whether
to skip empty fields and how to deal with leading and trailing
separators; see \l{SectionFlags}.
\code
TQString line( "forename\tmiddlename surname \t \t phone" );
TQRegExp sep( "\s+" );
TQString s = line.section( sep, 2, 2 ); // s == "surname"
\endcode
If \a start or \a end is negative, we count fields from the right
of the string, the right-most field being -1, the one from
right-most field being -2, and so on.
\code
TQString line( "forename\tmiddlename surname \t \t phone" );
TQRegExp sep( "\\s+" );
TQString s = line.section( sep, -3, -2 ); // s == "middlename surname"
\endcode
\warning Using this TQRegExp version is much more expensive than
the overloaded string and character versions.
\sa TQStringList::split() simplifyWhiteSpace()
*/
TQString TQString::section( const TQRegExp ®, int start, int end, int flags ) const
{
const TQChar *uc = unicode();
if(!uc)
return TQString();
TQRegExp sep(reg);
sep.setCaseSensitive(!(flags & SectionCaseInsensitiveSeps));
TQPtrList<section_chunk> l;
l.setAutoDelete(TRUE);
int n = length(), m = 0, last_m = 0, last = 0, last_len = 0;
while ( ( m = sep.search( *this, m ) ) != -1 ) {
l.append(new section_chunk(last_len, TQString(uc + last_m, m - last_m)));
last_m = m;
last_len = sep.matchedLength();
if((m += TQMAX(sep.matchedLength(), 1)) >= n) {
last = 1;
break;
}
}
if(!last)
l.append(new section_chunk(last_len, TQString(uc + last_m, n - last_m)));
if(start < 0)
start = l.count() + start;
if(end == -1)
end = l.count();
else if(end < 0)
end = l.count() + end;
int i = 0;
TQString ret;
for ( section_chunk *chk=l.first(); chk; chk=l.next(), i++ ) {
if((flags & SectionSkipEmpty) && chk->length == (int)chk->string.length()) {
if(i <= start)
start++;
end++;
}
if(i == start) {
ret = (flags & SectionIncludeLeadingSep) ? chk->string : chk->string.mid(chk->length);
} else if(i > start) {
ret += chk->string;
}
if(i == end) {
if((chk=l.next()) && flags & SectionIncludeTrailingSep)
ret += chk->string.left(chk->length);
break;
}
}
return ret;
}
#endif
/*!
\fn TQString TQString::section( char sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const
\overload
*/
/*!
\fn TQString TQString::section( const char *sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const
\overload
*/
/*!
Returns the number of times the character \a c occurs in the
string.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
\code
TQString string( "Trolltech and TQt" );
int n = string.contains( 't', FALSE );
// n == 3
\endcode
*/
int TQString::contains( TQChar c, bool cs ) const
{
int count = 0;
const TQChar *uc = unicode();
if ( !uc )
return 0;
int n = length();
if ( cs ) {
while ( n-- ) {
if ( *uc == c )
count++;
uc++;
}
} else {
c = ::lower( c );
while ( n-- ) {
if ( ::lower( *uc ) == c )
count++;
uc++;
}
}
return count;
}
/*!
\overload
Returns the number of times the string \a str occurs in the string.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
*/
int TQString::contains( const char* str, bool cs ) const
{
return contains( TQString(str), cs );
}
/*!
\fn int TQString::contains( char c, bool cs ) const
\overload
*/
/*!
\fn int TQString::find( char c, int index, bool cs ) const
\overload
Find character \a c starting from position \a index.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
*/
/*!
\fn int TQString::findRev( char c, int index, bool cs ) const
\overload
Find character \a c starting from position \a index and working
backwards.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
*/
/*!
\overload
Returns the number of times \a str occurs in the string.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
This function counts overlapping strings, so in the example below,
there are two instances of "ana" in "bananas".
\code
TQString str( "bananas" );
int i = str.contains( "ana" ); // i == 2
\endcode
\sa findRev()
*/
int TQString::contains( const TQString &str, bool cs ) const
{
if ( isNull() )
return 0;
int count = 0;
uint skiptable[0x100];
bm_init_skiptable( str, skiptable, cs );
int i = -1;
// use boyer-moore for the ultimate speed experience
while ( ( i = bm_find( *this, i + 1, str, skiptable, cs ) ) != -1 )
count++;
return count;
}
/*!
Returns a substring that contains the \a len leftmost characters
of the string.
The whole string is returned if \a len exceeds the length of the
string.
\code
TQString s = "Pineapple";
TQString t = s.left( 4 ); // t == "Pine"
\endcode
\sa right(), mid(), isEmpty()
*/
TQString TQString::left( uint len ) const
{
if ( isEmpty() ) {
return TQString();
} else if ( len == 0 ) { // ## just for 1.x compat:
return fromLatin1( "" );
} else if ( len >= length() ) {
return *this;
} else {
TQString s( len, TRUE );
memcpy( s.d->unicode, d->unicode, len * sizeof(TQChar) );
s.d->len = len;
return s;
}
}
/*!
Returns a string that contains the \a len rightmost characters of
the string.
If \a len is greater than the length of the string then the whole
string is returned.
\code
TQString string( "Pineapple" );
TQString t = string.right( 5 ); // t == "apple"
\endcode
\sa left(), mid(), isEmpty()
*/
TQString TQString::right( uint len ) const
{
if ( isEmpty() ) {
return TQString();
} else if ( len == 0 ) { // ## just for 1.x compat:
return fromLatin1( "" );
} else {
uint l = length();
if ( len >= l )
return *this;
TQString s( len, TRUE );
memcpy( s.d->unicode, d->unicode+(l-len), len*sizeof(TQChar) );
s.d->len = len;
return s;
}
}
/*!
Returns a string that contains the \a len characters of this
string, starting at position \a index.
Returns a null string if the string is empty or \a index is out of
range. Returns the whole string from \a index if \a index + \a len
exceeds the length of the string.
\code
TQString s( "Five pineapples" );
TQString t = s.mid( 5, 4 ); // t == "pine"
\endcode
\sa left(), right()
*/
TQString TQString::mid( uint index, uint len ) const
{
uint slen = length();
if ( isEmpty() || index >= slen ) {
return TQString();
} else if ( len == 0 ) { // ## just for 1.x compat:
return fromLatin1( "" );
} else {
if ( len > slen-index )
len = slen - index;
if ( index == 0 && len == slen )
return *this;
register const TQChar *p = unicode()+index;
TQString s( len, TRUE );
memcpy( s.d->unicode, p, len * sizeof(TQChar) );
s.d->len = len;
return s;
}
}
/*!
Returns a string of length \a width that contains this string
padded by the \a fill character.
If \a truncate is FALSE and the length of the string is more than
\a width, then the returned string is a copy of the string.
If \a truncate is TRUE and the length of the string is more than
\a width, then any characters in a copy of the string after length
\a width are removed, and the copy is returned.
\code
TQString s( "apple" );
TQString t = s.leftJustify( 8, '.' ); // t == "apple..."
\endcode
\sa rightJustify()
*/
TQString TQString::leftJustify( uint width, TQChar fill, bool truncate ) const
{
TQString result;
int len = length();
int padlen = width - len;
if ( padlen > 0 ) {
result.setLength(len+padlen);
if ( len )
memcpy( result.d->unicode, unicode(), sizeof(TQChar)*len );
TQChar* uc = result.d->unicode + len;
while (padlen--)
*uc++ = fill;
} else {
if ( truncate )
result = left( width );
else
result = *this;
}
return result;
}
/*!
Returns a string of length \a width that contains the \a fill
character followed by the string.
If \a truncate is FALSE and the length of the string is more than
\a width, then the returned string is a copy of the string.
If \a truncate is TRUE and the length of the string is more than
\a width, then the resulting string is truncated at position \a
width.
\code
TQString string( "apple" );
TQString t = string.rightJustify( 8, '.' ); // t == "...apple"
\endcode
\sa leftJustify()
*/
TQString TQString::rightJustify( uint width, TQChar fill, bool truncate ) const
{
TQString result;
int len = length();
int padlen = width - len;
if ( padlen > 0 ) {
result.setLength( len+padlen );
TQChar* uc = result.d->unicode;
while (padlen--)
*uc++ = fill;
if ( len )
memcpy( uc, unicode(), sizeof(TQChar)*len );
} else {
if ( truncate )
result = left( width );
else
result = *this;
}
return result;
}
/*!
Returns a lowercase copy of the string.
\code
TQString string( "TROlltECH" );
str = string.lower(); // str == "trolltech"
\endcode
\sa upper()
*/
TQString TQString::lower() const
{
int l = length();
register TQChar *p = d->unicode;
while ( l ) {
if ( *p != ::lower(*p) ) {
TQString s( *this );
s.real_detach();
p = s.d->unicode + ( p - d->unicode );
while ( l ) {
*p = ::lower( *p );
l--;
p++;
}
return s;
}
l--;
p++;
}
return *this;
}
/*!
Returns an uppercase copy of the string.
\code
TQString string( "TeXt" );
str = string.upper(); // t == "TEXT"
\endcode
\sa lower()
*/
TQString TQString::upper() const
{
int l = length();
register TQChar *p = d->unicode;
while ( l ) {
if ( *p != ::upper(*p) ) {
TQString s( *this );
s.real_detach();
p = s.d->unicode + ( p - d->unicode );
while ( l ) {
*p = ::upper( *p );
l--;
p++;
}
return s;
}
l--;
p++;
}
return *this;
}
/*!
Returns a string that has whitespace removed from the start and
the end.
Whitespace means any character for which TQChar::isSpace() returns
TRUE. This includes Unicode characters with decimal values 9
(TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR) and 32 (Space), and may
also include other Unicode characters.
\code
TQString string = " white space ";
TQString s = string.stripWhiteSpace(); // s == "white space"
\endcode
\sa simplifyWhiteSpace()
*/
TQString TQString::stripWhiteSpace() const
{
if ( isEmpty() ) // nothing to do
return *this;
register const TQChar *s = unicode();
if ( !s->isSpace() && !s[length()-1].isSpace() )
return *this;
int start = 0;
int end = length() - 1;
while ( start<=end && s[start].isSpace() ) // skip white space from start
start++;
if ( start <= end ) { // only white space
while ( end && s[end].isSpace() ) // skip white space from end
end--;
}
int l = end - start + 1;
if ( l <= 0 )
return TQString::fromLatin1("");
TQString result( l, TRUE );
memcpy( result.d->unicode, &s[start], sizeof(TQChar)*l );
result.d->len = l;
return result;
}
/*!
Returns a string that has whitespace removed from the start and
the end, and which has each sequence of internal whitespace
replaced with a single space.
Whitespace means any character for which TQChar::isSpace() returns
TRUE. This includes Unicode characters with decimal values 9
(TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR), and 32 (Space).
\code
TQString string = " lots\t of\nwhite space ";
TQString t = string.simplifyWhiteSpace();
// t == "lots of white space"
\endcode
\sa stripWhiteSpace()
*/
TQString TQString::simplifyWhiteSpace() const
{
if ( isEmpty() )
return *this;
TQString result;
result.setLength( length() );
const TQChar *from = unicode();
const TQChar *fromend = from+length();
int outc=0;
TQChar *to = result.d->unicode;
for (;;) {
while ( from!=fromend && from->isSpace() )
from++;
while ( from!=fromend && !from->isSpace() )
to[outc++] = *from++;
if ( from!=fromend )
to[outc++] = ' ';
else
break;
}
if ( outc > 0 && to[outc-1] == ' ' )
outc--;
result.truncate( outc );
return result;
}
/*!
Inserts \a s into the string at position \a index.
If \a index is beyond the end of the string, the string is
extended with spaces to length \a index and \a s is then appended
and returns a reference to the string.
\code
TQString string( "I like fish" );
str = string.insert( 2, "don't " );
// str == "I don't like fish"
\endcode
\sa remove(), replace()
*/
TQString &TQString::insert( uint index, const TQString &s )
{
// the sub function takes care of &s == this case.
return insert( index, s.unicode(), s.length() );
}
/*! \fn TQString &TQString::insert( uint index, const TQByteArray &s )
\overload
Inserts \a s into the string at position \a index and returns
a reference to the string.
*/
/*! \fn TQString &TQString::insert( uint index, const char *s )
\overload
Inserts \a s into the string at position \a index and returns
a reference to the string.
*/
#ifndef QT_NO_CAST_ASCII
TQString &TQString::insertHelper( uint index, const char *s, uint len )
{
if ( s ) {
#ifndef QT_NO_TEXTCODEC
if ( TQTextCodec::codecForCStrings() )
return insert( index, fromAscii( s, len ) );
#endif
if ( len == UINT_MAX )
len = int(strlen( s ));
if ( len == 0 )
return *this;
uint olen = length();
int nlen = olen + len;
if ( index >= olen ) { // insert after end of string
grow( len + index );
int n = index - olen;
TQChar* uc = d->unicode + olen;
while ( n-- )
*uc++ = ' ';
uc = d->unicode + index;
while ( len-- )
*uc++ = *s++;
} else { // normal insert
grow( nlen );
memmove( d->unicode + index + len, unicode() + index,
sizeof(TQChar) * (olen - index) );
TQChar* uc = d->unicode + index;
while ( len-- )
*uc++ = *s++;
}
}
return *this;
}
#endif
/*!
\overload
Inserts the first \a len characters in \a s into the string at
position \a index and returns a reference to the string.
*/
TQString &TQString::insert( uint index, const TQChar* s, uint len )
{
if ( len == 0 )
return *this;
uint olen = length();
int nlen = olen + len;
if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) {
// Part of me - take a copy.
TQChar *tmp = QT_ALLOC_QCHAR_VEC( len );
memcpy(tmp,s,len*sizeof(TQChar));
insert(index,tmp,len);
QT_DELETE_QCHAR_VEC( tmp );
return *this;
}
if ( index >= olen ) { // insert after end of string
grow( len + index );
int n = index - olen;
TQChar* uc = d->unicode+olen;
while (n--)
*uc++ = ' ';
memcpy( d->unicode+index, s, sizeof(TQChar)*len );
} else { // normal insert
grow( nlen );
memmove( d->unicode + index + len, unicode() + index,
sizeof(TQChar) * (olen - index) );
memcpy( d->unicode + index, s, sizeof(TQChar) * len );
}
return *this;
}
/*!
\overload
Insert \a c into the string at position \a index and returns a
reference to the string.
If \a index is beyond the end of the string, the string is
extended with spaces (ASCII 32) to length \a index and \a c is
then appended.
*/
TQString &TQString::insert( uint index, TQChar c ) // insert char
{
TQString s( c );
return insert( index, s );
}
/*!
\fn TQString& TQString::insert( uint index, char c )
\overload
Insert character \a c at position \a index.
*/
/*!
\fn TQString &TQString::prepend( const TQString &s )
Inserts \a s at the beginning of the string and returns a
reference to the string.
Equivalent to insert(0, \a s).
\code
TQString string = "42";
string.prepend( "The answer is " );
// string == "The answer is 42"
\endcode
\sa insert()
*/
/*!
\fn TQString& TQString::prepend( char ch )
\overload
Inserts \a ch at the beginning of the string and returns a
reference to the string.
Equivalent to insert(0, \a ch).
\sa insert()
*/
/*!
\fn TQString& TQString::prepend( TQChar ch )
\overload
Inserts \a ch at the beginning of the string and returns a
reference to the string.
Equivalent to insert(0, \a ch).
\sa insert()
*/
/*! \fn TQString& TQString::prepend( const TQByteArray &s )
\overload
Inserts \a s at the beginning of the string and returns a reference to the string.
Equivalent to insert(0, \a s).
\sa insert()
*/
/*! \fn TQString& TQString::prepend( const std::string &s )
\overload
Inserts \a s at the beginning of the string and returns a reference to the string.
Equivalent to insert(0, \a s).
\sa insert()
*/
/*!
\overload
Inserts \a s at the beginning of the string and returns a reference to the string.
Equivalent to insert(0, \a s).
\sa insert()
*/
TQString &TQString::prepend( const char *s )
{
return insert( 0, TQString(s) );
}
/*!
Removes \a len characters from the string starting at position \a
index, and returns a reference to the string.
If \a index is beyond the length of the string, nothing happens.
If \a index is within the string, but \a index + \a len is beyond
the end of the string, the string is truncated at position \a
index.
\code
TQString string( "Montreal" );
string.remove( 1, 4 ); // string == "Meal"
\endcode
\sa insert(), replace()
*/
TQString &TQString::remove( uint index, uint len )
{
uint olen = length();
if ( index >= olen ) {
// range problems
} else if ( index + len >= olen ) { // index ok
setLength( index );
} else if ( len != 0 ) {
real_detach();
memmove( d->unicode+index, d->unicode+index+len,
sizeof(TQChar)*(olen-index-len) );
setLength( olen-len );
}
return *this;
}
/*! \overload
Removes every occurrence of the character \a c in the string.
Returns a reference to the string.
This is the same as replace(\a c, "").
*/
TQString &TQString::remove( TQChar c )
{
int i = 0;
while ( i < (int) length() ) {
if ( constref(i) == c ) {
remove( i, 1 );
} else {
i++;
}
}
return *this;
}
/*! \overload
\fn TQString &TQString::remove( char c )
Removes every occurrence of the character \a c in the string.
Returns a reference to the string.
This is the same as replace(\a c, "").
*/
/*! \overload
Removes every occurrence of \a str in the string. Returns a
reference to the string.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
This is the same as replace(\a str, "", \a cs).
*/
TQString &TQString::remove( const TQString & str, bool cs )
{
if ( str.isEmpty() ) {
if ( isNull() )
real_detach();
} else {
int index = 0;
while ( (index = find(str, index, cs)) != -1 )
remove( index, str.length() );
}
return *this;
}
TQString &TQString::remove( const TQString & str )
{
return remove( str, TRUE );
}
/*! \overload
Replaces every occurrence of \a c1 with the char \a c2. Returns a
reference to the string.
*/
TQString &TQString::replace( TQChar c1, TQChar c2 )
{
if ( isEmpty() )
return *this;
real_detach();
uint i = 0;
while ( i < d->len ) {
if ( d->unicode[i] == c1 )
d->unicode[i] = c2;
i++;
}
return *this;
}
#ifndef QT_NO_REGEXP_CAPTURE
/*! \overload
Removes every occurrence of the regular expression \a rx in the
string. Returns a reference to the string.
This is the same as replace(\a rx, "").
*/
TQString &TQString::remove( const TQRegExp & rx )
{
return replace( rx, TQString::null );
}
#endif
/*!
\overload
Removes every occurrence of \a str in the string. Returns a
reference to the string.
*/
TQString &TQString::remove( const char *str )
{
return remove( TQString::fromAscii(str), TRUE );
}
/*!
Replaces \a len characters from the string with \a s, starting at
position \a index, and returns a reference to the string.
If \a index is beyond the length of the string, nothing is deleted
and \a s is appended at the end of the string. If \a index is
valid, but \a index + \a len is beyond the end of the string,
the string is truncated at position \a index, then \a s is
appended at the end.
\code
TQString string( "Say yes!" );
string = string.replace( 4, 3, "NO" );
// string == "Say NO!"
\endcode
\warning TQt 3.3.3 and earlier had different semantics for the
case \a index >= length(), which contradicted the documentation.
To avoid portability problems between TQt 3 versions and with TQt
4, we recommend that you never call the function with \a index >=
length().
\sa insert(), remove()
*/
TQString &TQString::replace( uint index, uint len, const TQString &s )
{
return replace( index, len, s.unicode(), s.length() );
}
/*! \overload
This is the same as replace(\a index, \a len, TQString(\a c)).
*/
TQString &TQString::replace( uint index, uint len, TQChar c )
{
return replace( index, len, &c, 1 );
}
/*! \overload
\fn TQString &TQString::replace( uint index, uint len, char c )
This is the same as replace(\a index, \a len, TQChar(\a c)).
*/
/*!
\overload
Replaces \a len characters with \a slen characters of TQChar data
from \a s, starting at position \a index, and returns a reference
to the string.
\sa insert(), remove()
*/
TQString &TQString::replace( uint index, uint len, const TQChar* s, uint slen )
{
if (index > length())
index = length();
real_detach();
if ( len == slen && index + len <= length() ) {
// Optimized common case: replace without size change
memcpy( d->unicode+index, s, len * sizeof(TQChar) );
} else if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) {
// Part of me - take a copy.
TQChar *tmp = QT_ALLOC_QCHAR_VEC( slen );
memcpy( tmp, s, slen * sizeof(TQChar) );
replace( index, len, tmp, slen );
QT_DELETE_QCHAR_VEC( tmp );
} else {
remove( index, len );
insert( index, s, slen );
}
return *this;
}
/*! \overload
Replaces every occurrence of the character \a c in the string
with \a after. Returns a reference to the string.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
Example:
\code
TQString s = "a,b,c";
s.replace( TQChar(','), " or " );
// s == "a or b or c"
\endcode
*/
TQString &TQString::replace( TQChar c, const TQString & after, bool cs )
{
return replace( TQString( c ), after, cs );
}
TQString &TQString::replace( TQChar c, const TQString & after )
{
return replace( TQString( c ), after, TRUE );
}
/*! \overload
\fn TQString &TQString::replace( char c, const TQString & after, bool cs )
Replaces every occurrence of the character \a c in the string
with \a after. Returns a reference to the string.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
*/
/*! \overload
Replaces every occurrence of the string \a before in the string
with the string \a after. Returns a reference to the string.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
Example:
\code
TQString s = "Greek is Greek";
s.replace( "Greek", "English" );
// s == "English is English"
\endcode
*/
TQString &TQString::replace( const TQString & before, const TQString & after,
bool cs )
{
if ( isEmpty() ) {
if ( !before.isEmpty() )
return *this;
} else {
if ( cs && before == after )
return *this;
}
real_detach();
int index = 0;
uint skiptable[256];
bm_init_skiptable( before, skiptable, cs );
const int bl = before.length();
const int al = after.length();
if ( bl == al ) {
if ( bl ) {
const TQChar *auc = after.unicode();
while ( (index = bm_find(*this, index, before, skiptable, cs) ) != -1 ) {
memcpy( d->unicode + index, auc, al * sizeof(TQChar) );
index += bl;
}
}
} else if ( al < bl ) {
const TQChar *auc = after.unicode();
uint to = 0;
uint movestart = 0;
uint num = 0;
while ( (index = bm_find(*this, index, before, skiptable, cs)) != -1 ) {
if ( num ) {
int msize = index - movestart;
if ( msize > 0 ) {
memmove( d->unicode + to, d->unicode + movestart, msize*sizeof(TQChar) );
to += msize;
}
} else {
to = index;
}
if ( al ) {
memcpy( d->unicode+to, auc, al*sizeof(TQChar) );
to += al;
}
index += bl;
movestart = index;
num++;
}
if ( num ) {
int msize = d->len - movestart;
if ( msize > 0 )
memmove( d->unicode + to, d->unicode + movestart, msize*sizeof(TQChar) );
setLength( d->len - num*(bl-al) );
}
} else {
// the most complex case. We don't want to loose performance by doing repeated
// copies and reallocs of the string.
while ( index != -1 ) {
uint indices[4096];
uint pos = 0;
while ( pos < 4095 ) {
index = bm_find( *this, index, before, skiptable, cs );
if ( index == -1 )
break;
indices[pos++] = index;
index += bl;
// avoid infinite loop
if ( !bl )
index++;
}
if ( !pos )
break;
// we have a table of replacement positions, use them for fast replacing
int adjust = pos*(al-bl);
// index has to be adjusted in case we get back into the loop above.
if ( index != -1 )
index += adjust;
uint newlen = d->len + adjust;
int moveend = d->len;
if ( newlen > d->len )
setLength( newlen );
while ( pos ) {
pos--;
int movestart = indices[pos] + bl;
int insertstart = indices[pos] + pos*(al-bl);
int moveto = insertstart + al;
memmove( d->unicode + moveto, d->unicode + movestart, (moveend - movestart)*sizeof(TQChar) );
memcpy( d->unicode + insertstart, after.unicode(), al*sizeof(TQChar) );
moveend = movestart-bl;
}
}
}
return *this;
}
TQString &TQString::replace( const TQString & before, const TQString & after )
{
return replace( before, after, TRUE );
}
#ifndef QT_NO_REGEXP_CAPTURE
/*! \overload
Replaces every occurrence of the regexp \a rx in the string with
\a after. Returns a reference to the string. For example:
\code
TQString s = "banana";
s.replace( TQRegExp("an"), "" );
// s == "ba"
\endcode
For regexps containing \link ntqregexp.html#capturing-text capturing
parentheses \endlink, occurrences of <b>\\1</b>, <b>\\2</b>, ...,
in \a after are replaced with \a{rx}.cap(1), cap(2), ...
\code
TQString t = "A <i>bon mot</i>.";
t.replace( TQRegExp("<i>([^<]*)</i>"), "\\emph{\\1}" );
// t == "A \\emph{bon mot}."
\endcode
\sa find(), findRev(), TQRegExp::cap()
*/
TQString &TQString::replace( const TQRegExp &rx, const TQString &after )
{
TQRegExp rx2 = rx;
if ( isEmpty() && rx2.search(*this) == -1 )
return *this;
real_detach();
int index = 0;
int numCaptures = rx2.numCaptures();
int al = after.length();
TQRegExp::CaretMode caretMode = TQRegExp::CaretAtZero;
if ( numCaptures > 0 ) {
if ( numCaptures > 9 )
numCaptures = 9;
const TQChar *uc = after.unicode();
int numBackRefs = 0;
for ( int i = 0; i < al - 1; i++ ) {
if ( uc[i] == '\\' ) {
int no = uc[i + 1].digitValue();
if ( no > 0 && no <= numCaptures )
numBackRefs++;
}
}
/*
This is the harder case where we have back-references.
We don't try to optimize it.
*/
if ( numBackRefs > 0 ) {
int *capturePositions = new int[numBackRefs];
int *captureNumbers = new int[numBackRefs];
int j = 0;
for ( int i = 0; i < al - 1; i++ ) {
if ( uc[i] == '\\' ) {
int no = uc[i + 1].digitValue();
if ( no > 0 && no <= numCaptures ) {
capturePositions[j] = i;
captureNumbers[j] = no;
j++;
}
}
}
while ( index <= (int)length() ) {
index = rx2.search( *this, index, caretMode );
if ( index == -1 )
break;
TQString after2 = after;
for ( j = numBackRefs - 1; j >= 0; j-- )
after2.replace( capturePositions[j], 2,
rx2.cap(captureNumbers[j]) );
replace( index, rx2.matchedLength(), after2 );
index += after2.length();
if ( rx2.matchedLength() == 0 ) {
// avoid infinite loop on 0-length matches (e.g., [a-z]*)
index++;
}
caretMode = TQRegExp::CaretWontMatch;
}
delete[] capturePositions;
delete[] captureNumbers;
return *this;
}
}
/*
This is the simple and optimized case where we don't have
back-references.
*/
while ( index != -1 ) {
struct {
int pos;
int length;
} replacements[2048];
uint pos = 0;
int adjust = 0;
while ( pos < 2047 ) {
index = rx2.search( *this, index, caretMode );
if ( index == -1 )
break;
int ml = rx2.matchedLength();
replacements[pos].pos = index;
replacements[pos++].length = ml;
index += ml;
adjust += al - ml;
// avoid infinite loop
if ( !ml )
index++;
}
if ( !pos )
break;
replacements[pos].pos = d->len;
uint newlen = d->len + adjust;
// to continue searching at the right position after we did
// the first round of replacements
if ( index != -1 )
index += adjust;
TQChar *newuc = QT_ALLOC_QCHAR_VEC( newlen + 1 );
TQChar *uc = newuc;
int copystart = 0;
uint i = 0;
while ( i < pos ) {
int copyend = replacements[i].pos;
int size = copyend - copystart;
memcpy( uc, d->unicode + copystart, size * sizeof(TQChar) );
uc += size;
memcpy( uc, after.unicode(), al * sizeof(TQChar) );
uc += al;
copystart = copyend + replacements[i].length;
i++;
}
memcpy( uc, d->unicode + copystart,
(d->len - copystart) * sizeof(TQChar) );
QT_DELETE_QCHAR_VEC( d->unicode );
d->unicode = newuc;
d->len = newlen;
d->maxl = newlen + 1;
d->setDirty();
caretMode = TQRegExp::CaretWontMatch;
}
return *this;
}
#endif
#ifndef QT_NO_REGEXP
/*!
Finds the first match of the regular expression \a rx, starting
from position \a index. If \a index is -1, the search starts at
the last character; if -2, at the next to last character and so
on. (See findRev() for searching backwards.)
Returns the position of the first match of \a rx or -1 if no match
was found.
\code
TQString string( "bananas" );
int i = string.find( TQRegExp("an"), 0 ); // i == 1
\endcode
\sa findRev() replace() contains()
*/
int TQString::find( const TQRegExp &rx, int index ) const
{
return rx.search( *this, index );
}
/*!
\overload
Finds the first match of the regexp \a rx, starting at position \a
index and searching backwards. If the index is -1, the search
starts at the last character, if it is -2, at the next to last
character and so on. (See findRev() for searching backwards.)
Returns the position of the match or -1 if no match was found.
\code
TQString string( "bananas" );
int i = string.findRev( TQRegExp("an") ); // i == 3
\endcode
\sa find()
*/
int TQString::findRev( const TQRegExp &rx, int index ) const
{
return rx.searchRev( *this, index );
}
/*!
\overload
Returns the number of times the regexp, \a rx, matches in the
string.
This function counts overlapping matches, so in the example below,
there are four instances of "ana" or "ama".
\code
TQString str = "banana and panama";
TQRegExp rxp = TQRegExp( "a[nm]a", TRUE, FALSE );
int i = str.contains( rxp ); // i == 4
\endcode
\sa find() findRev()
*/
int TQString::contains( const TQRegExp &rx ) const
{
int count = 0;
int index = -1;
int len = length();
while ( index < len - 1 ) { // count overlapping matches
index = rx.search( *this, index + 1 );
if ( index == -1 )
break;
count++;
}
return count;
}
#endif //QT_NO_REGEXP
/*!
Returns the string converted to a \c long using base \a
base, which is 10 by default and must be between 2 and 36 or 0. If
\a base is 0, the base is determined automatically using the
following rules:
<ul>
<li>If the string begins with "0x", it is assumed to
be hexadecimal;
<li>If it begins with "0", it is assumed to be octal;
<li>Otherwise it is assumed to be decimal.
</ul>
Returns 0 if the conversion fails.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
Leading and trailing whitespace is ignored by this function.
For information on how string-to-number functions in TQString handle
localized input, see toDouble().
\sa number()
*/
long TQString::toLong( bool *ok, int base ) const
{
TQ_LLONG v = toLongLong( ok, base );
if ( v < LONG_MIN || v > LONG_MAX ) {
if ( ok )
*ok = FALSE;
v = 0;
}
return long(v);
}
/*!
Returns the string converted to a \c {long long} using base \a
base, which is 10 by default and must be between 2 and 36 or 0. If
\a base is 0, the base is determined automatically using the
following rules:
<ul>
<li>If the string begins with "0x", it is assumed to
be hexadecimal;
<li>If it begins with "0", it is assumed to be octal;
<li>Otherwise it is assumed to be decimal.
</ul>
Returns 0 if the conversion fails.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
Leading and trailing whitespace is ignored by this function.
For information on how string-to-number functions in TQString handle
localized input, see toDouble().
\sa number()
*/
TQ_LLONG TQString::toLongLong( bool *ok, int base ) const
{
#if defined(QT_CHECK_RANGE)
if ( base != 0 && (base < 2 || base > 36) ) {
tqWarning( "TQString::toLongLong: Invalid base (%d)", base );
base = 10;
}
#endif
bool my_ok;
TQLocale def_locale;
TQ_LLONG result = def_locale.d->stringToLongLong(*this, base, &my_ok, TQLocalePrivate::FailOnGroupSeparators);
if (my_ok) {
if (ok != 0)
*ok = TRUE;
return result;
}
// If the default was not "C", try the "C" locale
if (def_locale.language() == TQLocale::C) {
if (ok != 0)
*ok = FALSE;
return 0;
}
TQLocale c_locale(TQLocale::C);
return c_locale.d->stringToLongLong(*this, base, ok, TQLocalePrivate::FailOnGroupSeparators);
}
/*!
Returns the string converted to an \c {unsigned long} using base \a
base, which is 10 by default and must be between 2 and 36 or 0. If
\a base is 0, the base is determined automatically using the
following rules:
<ul>
<li>If the string begins with "0x", it is assumed to
be hexadecimal;
<li>If it begins with "0", it is assumed to be octal;
<li>Otherwise it is assumed to be decimal.
</ul>
Returns 0 if the conversion fails.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
Leading and trailing whitespace is ignored by this function.
For information on how string-to-number functions in TQString handle
localized input, see toDouble().
\sa number()
*/
ulong TQString::toULong( bool *ok, int base ) const
{
TQ_ULLONG v = toULongLong( ok, base );
if ( v > ULONG_MAX ) {
if ( ok )
*ok = FALSE;
v = 0;
}
return ulong(v);
}
/*!
Returns the string converted to an \c {unsigned long long} using base \a
base, which is 10 by default and must be between 2 and 36 or 0. If
\a base is 0, the base is determined automatically using the
following rules:
<ul>
<li>If the string begins with "0x", it is assumed to
be hexadecimal;
<li>If it begins with "0", it is assumed to be octal;
<li>Otherwise it is assumed to be decimal.
</ul>
Returns 0 if the conversion fails.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
Leading and trailing whitespace is ignored by this function.
For information on how string-to-number functions in TQString handle
localized input, see toDouble().
\sa number()
*/
TQ_ULLONG TQString::toULongLong( bool *ok, int base ) const
{
#if defined(QT_CHECK_RANGE)
if ( base != 0 && (base < 2 || base > 36) ) {
tqWarning( "TQString::toULongLong: Invalid base %d", base );
base = 10;
}
#endif
bool my_ok;
TQLocale def_locale;
TQ_ULLONG result = def_locale.d->stringToUnsLongLong(*this, base, &my_ok, TQLocalePrivate::FailOnGroupSeparators);
if (my_ok) {
if (ok != 0)
*ok = TRUE;
return result;
}
// If the default was not "C", try the "C" locale
if (def_locale.language() == TQLocale::C) {
if (ok != 0)
*ok = FALSE;
return 0;
}
TQLocale c_locale(TQLocale::C);
return c_locale.d->stringToUnsLongLong(*this, base, ok, TQLocalePrivate::FailOnGroupSeparators);
}
/*!
Returns the string converted to a \c short using base \a
base, which is 10 by default and must be between 2 and 36 or 0. If
\a base is 0, the base is determined automatically using the
following rules:
<ul>
<li>If the string begins with "0x", it is assumed to
be hexadecimal;
<li>If it begins with "0", it is assumed to be octal;
<li>Otherwise it is assumed to be decimal.
</ul>
Returns 0 if the conversion fails.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
Leading and trailing whitespace is ignored by this function.
For information on how string-to-number functions in TQString handle
localized input, see toDouble().
\sa number()
*/
short TQString::toShort( bool *ok, int base ) const
{
TQ_LLONG v = toLongLong( ok, base );
if ( v < SHRT_MIN || v > SHRT_MAX ) {
if ( ok )
*ok = FALSE;
v = 0;
}
return (short)v;
}
/*!
Returns the string converted to an \c {unsigned short} using base \a
base, which is 10 by default and must be between 2 and 36 or 0. If
\a base is 0, the base is determined automatically using the
following rules:
<ul>
<li>If the string begins with "0x", it is assumed to
be hexadecimal;
<li>If it begins with "0", it is assumed to be octal;
<li>Otherwise it is assumed to be decimal.
</ul>
Returns 0 if the conversion fails.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
Leading and trailing whitespace is ignored by this function.
For information on how string-to-number functions in TQString handle
localized input, see toDouble().
\sa number()
*/
ushort TQString::toUShort( bool *ok, int base ) const
{
TQ_ULLONG v = toULongLong( ok, base );
if ( v > USHRT_MAX ) {
if ( ok )
*ok = FALSE;
v = 0;
}
return (ushort)v;
}
/*!
Returns the string converted to an \c int using base \a
base, which is 10 by default and must be between 2 and 36 or 0. If
\a base is 0, the base is determined automatically using the
following rules:
<ul>
<li>If the string begins with "0x", it is assumed to
be hexadecimal;
<li>If it begins with "0", it is assumed to be octal;
<li>Otherwise it is assumed to be decimal.
</ul>
Returns 0 if the conversion fails.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
\code
TQString str( "FF" );
bool ok;
int hex = str.toInt( &ok, 16 ); // hex == 255, ok == TRUE
int dec = str.toInt( &ok, 10 ); // dec == 0, ok == FALSE
\endcode
Leading and trailing whitespace is ignored by this function.
For information on how string-to-number functions in TQString handle
localized input, see toDouble().
\sa number()
*/
int TQString::toInt( bool *ok, int base ) const
{
TQ_LLONG v = toLongLong( ok, base );
if ( v < INT_MIN || v > INT_MAX ) {
if ( ok )
*ok = FALSE;
v = 0;
}
return (int)v;
}
/*!
Returns the string converted to an \c {unsigned int} using base \a
base, which is 10 by default and must be between 2 and 36 or 0. If
\a base is 0, the base is determined automatically using the
following rules:
<ul>
<li>If the string begins with "0x", it is assumed to
be hexadecimal;
<li>If it begins with "0", it is assumed to be octal;
<li>Otherwise it is assumed to be decimal.
</ul>
Returns 0 if the conversion fails.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
Leading and trailing whitespace is ignored by this function.
For information on how string-to-number functions in TQString handle
localized input, see toDouble().
\sa number()
*/
uint TQString::toUInt( bool *ok, int base ) const
{
TQ_ULLONG v = toULongLong( ok, base );
if ( v > UINT_MAX ) {
if ( ok )
*ok = FALSE;
v = 0;
}
return (uint)v;
}
/*!
Returns the string converted to a \c double value.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
\code
TQString string( "1234.56" );
double a = string.toDouble(); // a == 1234.56
\endcode
The string-to-number functions:
\list
\i toShort()
\i toUShort()
\i toInt()
\i toUInt()
\i toLong()
\i toULong()
\i toLongLong()
\i toULongLong()
\i toFloat()
\i toDouble()
\endlist
can handle numbers
represented in various locales. These representations may use different
characters for the decimal point, thousands group sepearator
and even individual digits. TQString's functions try to interpret
the string according to the current locale. The current locale is
determined from the system at application startup and can be changed
by calling TQLocale::setDefault(). If the string cannot be interpreted
according to the current locale, this function falls back
on the "C" locale.
\code
bool ok;
double d;
TQLocale::setDefault(TQLocale::C);
d = TQString( "1234,56" ).toDouble(&ok); // ok == false
d = TQString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
TQLocale::setDefault(TQLocale::German);
d = TQString( "1234,56" ).toDouble(&ok); // ok == true, d == 1234.56
d = TQString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
\endcode
Due to the ambiguity between the decimal point and thousands group
separator in various locales, these functions do not handle
thousands group separators. If you need to convert such numbers,
use the corresponding function in TQLocale.
\code
bool ok;
TQLocale::setDefault(TQLocale::C);
double d = TQString( "1,234,567.89" ).toDouble(&ok); // ok == false
\endcode
\warning If the string contains trailing whitespace this function
will fail, and set \a *ok to false if \a ok is not 0. Leading
whitespace is ignored.
\sa number() TQLocale::setDefault() TQLocale::toDouble() stripWhiteSpace()
*/
double TQString::toDouble( bool *ok ) const
{
// If there is trailing whitespace, set ok to false but return the correct
// result anyway to preserve behavour of pervious versions of TQt
if (length() > 0 && unicode()[length() - 1].isSpace()) {
TQString tmp = stripWhiteSpace();
if (ok != 0)
*ok = FALSE;
return tmp.toDouble();
}
// Try the default locale
bool my_ok;
TQLocale def_locale;
double result = def_locale.d->stringToDouble(*this, &my_ok, TQLocalePrivate::FailOnGroupSeparators);
if (my_ok) {
if (ok != 0)
*ok = TRUE;
return result;
}
// If the default was not "C", try the "C" locale
if (def_locale.language() == TQLocale::C) {
if (ok != 0)
*ok = FALSE;
return 0.0;
}
TQLocale c_locale(TQLocale::C);
return c_locale.d->stringToDouble(*this, ok, TQLocalePrivate::FailOnGroupSeparators);
}
/*!
Returns the string converted to a \c float value.
Returns 0.0 if the conversion fails.
If \a ok is not 0: if a conversion error occurs, \a *ok is set to
FALSE; otherwise \a *ok is set to TRUE.
For information on how string-to-number functions in TQString handle
localized input, see toDouble().
\warning If the string contains trailing whitespace this function
will fail, settings \a *ok to false if \a ok is not 0.
Leading whitespace is ignored.
\sa number()
*/
#define QT_MAX_FLOAT 3.4028234663852886e+38
float TQString::toFloat( bool *ok ) const
{
bool myOk;
double d = toDouble(&myOk);
if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
if (ok != 0)
*ok = FALSE;
return 0.0;
}
if (ok != 0)
*ok = TRUE;
return (float) d;
}
/*!
Sets the string to the printed value of \a n in base \a base and
returns a reference to the string. The returned string is in "C" locale.
The base is 10 by default and must be between 2 and 36.
\code
TQString string;
string = string.setNum( 1234 ); // string == "1234"
\endcode
*/
TQString &TQString::setNum( TQ_LLONG n, int base )
{
#if defined(QT_CHECK_RANGE)
if ( base < 2 || base > 36 ) {
tqWarning( "TQString::setNum: Invalid base %d", base );
base = 10;
}
#endif
TQLocale locale(TQLocale::C);
*this = locale.d->longLongToString(n, -1, base);
return *this;
}
/*!
\overload
Sets the string to the printed value of \a n in base \a base and
returns a reference to the string.
The base is 10 by default and must be between 2 and 36.
*/
TQString &TQString::setNum( TQ_ULLONG n, int base )
{
#if defined(QT_CHECK_RANGE)
if ( base < 2 || base > 36 ) {
tqWarning( "TQString::setNum: Invalid base %d", base );
base = 10;
}
#endif
TQLocale locale(TQLocale::C);
*this = locale.d->unsLongLongToString(n, -1, base);
return *this;
}
/*!
\fn TQString &TQString::setNum( long n, int base )
\overload
*/
// ### 4.0: inline
TQString &TQString::setNum( long n, int base )
{
return setNum( (TQ_LLONG)n, base );
}
/*!
\fn TQString &TQString::setNum( ulong n, int base )
\overload
*/
// ### 4.0: inline
TQString &TQString::setNum( ulong n, int base )
{
return setNum( (TQ_ULLONG)n, base );
}
/*!
\fn TQString &TQString::setNum( int n, int base )
\overload
Sets the string to the printed value of \a n in base \a base and
returns a reference to the string.
The base is 10 by default and must be between 2 and 36.
*/
/*!
\fn TQString &TQString::setNum( uint n, int base )
\overload
Sets the string to the printed value of \a n in base \a base and
returns a reference to the string.
The base is 10 by default and must be between 2 and 36.
*/
/*!
\fn TQString &TQString::setNum( short n, int base )
\overload
Sets the string to the printed value of \a n in base \a base and
returns a reference to the string.
The base is 10 by default and must be between 2 and 36.
*/
/*!
\fn TQString &TQString::setNum( ushort n, int base )
\overload
Sets the string to the printed value of \a n in base \a base and
returns a reference to the string.
The base is 10 by default and must be between 2 and 36.
*/
/*!
\overload
Sets the string to the printed value of \a n, formatted in format
\a f with precision \a prec, and returns a reference to the
string.
The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'. See \link
#arg-formats arg \endlink() for an explanation of the formats.
*/
TQString &TQString::setNum( double n, char f, int prec )
{
TQLocalePrivate::DoubleForm form = TQLocalePrivate::DFDecimal;
uint flags = 0;
if (qIsUpper(f))
flags = TQLocalePrivate::CapitalEorX;
f = qToLower(f);
switch (f) {
case 'f':
form = TQLocalePrivate::DFDecimal;
break;
case 'e':
form = TQLocalePrivate::DFExponent;
break;
case 'g':
form = TQLocalePrivate::DFSignificantDigits;
break;
default:
#if defined(QT_CHECK_RANGE)
tqWarning( "TQString::setNum: Invalid format char '%c'", f );
#endif
break;
}
TQLocale locale(TQLocale::C);
*this = locale.d->doubleToString(n, prec, form, -1, flags);
return *this;
}
/*!
\fn TQString &TQString::setNum( float n, char f, int prec )
\overload
Sets the string to the printed value of \a n, formatted in format
\a f with precision \a prec, and returns a reference to the
string.
The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'. See \link
#arg-formats arg \endlink() for an explanation of the formats.
*/
/*!
A convenience function that returns a string equivalent of the
number \a n to base \a base, which is 10 by default and must be
between 2 and 36. The returned string is in "C" locale.
\code
long a = 63;
TQString str = TQString::number( a, 16 ); // str == "3f"
TQString str = TQString::number( a, 16 ).upper(); // str == "3F"
\endcode
\sa setNum()
*/
TQString TQString::number( long n, int base )
{
TQString s;
s.setNum( n, base );
return s;
}
/*!
\overload
\sa setNum()
*/
TQString TQString::number( ulong n, int base )
{
TQString s;
s.setNum( n, base );
return s;
}
/*!
\overload
\sa setNum()
*/
TQString TQString::number( TQ_LLONG n, int base )
{
TQString s;
s.setNum( n, base );
return s;
}
/*!
\overload
\sa setNum()
*/
TQString TQString::number( TQ_ULLONG n, int base )
{
TQString s;
s.setNum( n, base );
return s;
}
/*!
\overload
\sa setNum()
*/
TQString TQString::number( int n, int base )
{
TQString s;
s.setNum( n, base );
return s;
}
/*!
\overload
A convenience factory function that returns a string
representation of the number \a n to the base \a base, which is 10
by default and must be between 2 and 36.
\sa setNum()
*/
TQString TQString::number( uint n, int base )
{
TQString s;
s.setNum( n, base );
return s;
}
/*!
\overload
Argument \a n is formatted according to the \a f format specified,
which is \c g by default, and can be any of the following:
\table
\header \i Format \i Meaning
\row \i \c e \i format as [-]9.9e[+|-]999
\row \i \c E \i format as [-]9.9E[+|-]999
\row \i \c f \i format as [-]9.9
\row \i \c g \i use \c e or \c f format, whichever is the most concise
\row \i \c G \i use \c E or \c f format, whichever is the most concise
\endtable
With 'e', 'E', and 'f', \a prec is the number of digits after the
decimal point. With 'g' and 'G', \a prec is the maximum number of
significant digits (trailing zeroes are omitted).
\code
double d = 12.34;
TQString ds = TQString( "'E' format, precision 3, gives %1" )
.arg( d, 0, 'E', 3 );
// ds == "1.234E+001"
\endcode
\sa setNum()
*/
TQString TQString::number( double n, char f, int prec )
{
TQString s;
s.setNum( n, f, prec );
return s;
}
/*! \obsolete
Sets the character at position \a index to \a c and expands the
string if necessary, filling with spaces.
This method is redundant in TQt 3.x, because operator[] will expand
the string as necessary.
*/
void TQString::setExpand( uint index, TQChar c )
{
int spaces = index - d->len;
at(index) = c;
while (spaces-->0)
d->unicode[--index]=' ';
}
/*!
\fn const char* TQString::data() const
\obsolete
Returns a pointer to a '\0'-terminated classic C string.
In TQt 1.x, this returned a char* allowing direct manipulation of the
string as a sequence of bytes. In TQt 2.x where TQString is a Unicode
string, char* conversion constructs a temporary string, and hence
direct character operations are meaningless.
*/
/*!
\fn bool TQString::operator!() const
Returns TRUE if this is a null string; otherwise returns FALSE.
\code
TQString name = getName();
if ( !name )
name = "Rodney";
\endcode
Note that if you say
\code
TQString name = getName();
if ( name )
doSomethingWith(name);
\endcode
It will call "operator const char*()", which is inefficent; you
may wish to define the macro \c QT_NO_ASCII_CAST when writing code
which you wish to remain Unicode-clean.
When you want the above semantics, use:
\code
TQString name = getName();
if ( !name.isNull() )
doSomethingWith(name);
\endcode
\sa isEmpty()
*/
/*!
\fn TQString& TQString::append( const TQString& str )
Appends \a str to the string and returns a reference to the
result.
\code
string = "Test";
string.append( "ing" ); // string == "Testing"
\endcode
Equivalent to operator+=().
*/
/*!
\fn TQString& TQString::append( char ch )
\overload
Appends character \a ch to the string and returns a reference to
the result.
Equivalent to operator+=().
*/
/*!
\fn TQString& TQString::append( TQChar ch )
\overload
Appends character \a ch to the string and returns a reference to
the result.
Equivalent to operator+=().
*/
/*! \fn TQString& TQString::append( const TQByteArray &str )
\overload
Appends \a str to the string and returns a reference to the result.
Equivalent to operator+=().
*/
/*! \fn TQString& TQString::append( const std::string &str )
\overload
Appends \a str to the string and returns a reference to the result.
Equivalent to operator+=().
*/
/*! \fn TQString& TQString::append( const char *str )
\overload
Appends \a str to the string and returns a reference to the result.
Equivalent to operator+=().
*/
/*!
Appends \a str to the string and returns a reference to the string.
*/
TQString& TQString::operator+=( const TQString &str )
{
uint len1 = length();
uint len2 = str.length();
if ( len2 ) {
if ( isEmpty() ) {
operator=( str );
} else {
grow( len1+len2 );
memcpy( d->unicode+len1, str.unicode(), sizeof(TQChar)*len2 );
}
} else if ( isNull() && !str.isNull() ) { // ## just for 1.x compat:
*this = fromLatin1( "" );
}
return *this;
}
#ifndef QT_NO_CAST_ASCII
TQString &TQString::operatorPlusEqHelper( const char *s, uint len2 )
{
if ( s ) {
#ifndef QT_NO_TEXTCODEC
if ( TQTextCodec::codecForCStrings() )
return operator+=( fromAscii( s, len2 ) );
#endif
uint len1 = length();
if ( len2 == UINT_MAX )
len2 = int(strlen( s ));
if ( len2 ) {
grow( len1 + len2 );
TQChar* uc = d->unicode + len1;
while ( len2-- )
*uc++ = *s++;
} else if ( isNull() ) { // ## just for 1.x compat:
*this = fromLatin1( "" );
}
}
return *this;
}
#endif
/*!
\overload
Appends \a str to the string and returns a reference to the string.
*/
#ifndef QT_NO_CAST_ASCII
TQString& TQString::operator+=( const char *str )
{
// ### TQt 4: make this function inline
return operatorPlusEqHelper( str );
}
#endif
/*! \overload
Appends \a c to the string and returns a reference to the string.
*/
TQString &TQString::operator+=( TQChar c )
{
grow( length()+1 );
d->unicode[length()-1] = c;
return *this;
}
/*!
\overload
Appends \a c to the string and returns a reference to the string.
*/
TQString &TQString::operator+=( char c )
{
#ifndef QT_NO_TEXTCODEC
if ( TQTextCodec::codecForCStrings() )
return operator+=( fromAscii( &c, 1 ) );
#endif
grow( length()+1 );
d->unicode[length()-1] = c;
return *this;
}
/*!
\fn TQString &TQString::operator+=( const TQByteArray &str )
\overload
Appends \a str to the string and returns a reference to the string.
*/
/*!
\fn TQString &TQString::operator+=( const std::string &str )
\overload
Appends \a str to the string and returns a reference to the string.
*/
/*!
\fn char TQChar::latin1() const
Returns the Latin-1 value of this character, or 0 if it
cannot be represented in Latin-1.
*/
/*!
Returns a Latin-1 representation of the string. The
returned value is undefined if the string contains non-Latin-1
characters. If you want to convert strings into formats other than
Unicode, see the TQTextCodec classes.
This function is mainly useful for boot-strapping legacy code to
use Unicode.
The result remains valid so long as one unmodified copy of the
source string exists.
\sa fromLatin1(), ascii(), utf8(), local8Bit()
*/
const char* TQString::latin1() const
{
if ( !d->ascii || !d->islatin1 ) {
if (d->security_unpaged) {
#if defined(Q_OS_LINUX)
if (d->ascii) {
munlock(d->ascii, LINUX_MEMLOCK_LIMIT_BYTES);
}
#endif
}
delete [] d->ascii;
d->ascii = unicodeToLatin1( d->unicode, d->len, d->security_unpaged );
d->islatin1 = TRUE;
}
return d->ascii;
}
/*!
Returns an 8-bit ASCII representation of the string.
If a codec has been set using TQTextCodec::codecForCStrings(),
it is used to convert Unicode to 8-bit char. Otherwise, this function
does the same as latin1().
\sa fromAscii(), latin1(), utf8(), local8Bit()
*/
const char* TQString::ascii() const
{
#ifndef QT_NO_TEXTCODEC
if ( TQTextCodec::codecForCStrings() ) {
if ( !d->ascii || d->islatin1 ) {
if (d->security_unpaged) {
#if defined(Q_OS_LINUX)
if (d->ascii) {
munlock(d->ascii, LINUX_MEMLOCK_LIMIT_BYTES);
}
#endif
}
delete [] d->ascii;
if (d->unicode) {
TQCString s = TQTextCodec::codecForCStrings()->fromUnicode( *this );
d->ascii = new char[s.length() + 1];
if (d->security_unpaged) {
#if defined(Q_OS_LINUX)
mlock(d->ascii, LINUX_MEMLOCK_LIMIT_BYTES);
#endif
}
memcpy(d->ascii, s.data(), s.length() + 1);
} else {
d->ascii = 0;
}
d->islatin1 = FALSE;
}
return d->ascii;
}
#endif // QT_NO_TEXTCODEC
return latin1();
}
void TQString::setSecurityUnPaged(bool lock) {
if (lock != d->security_unpaged) {
if (d->security_unpaged) {
#if defined(Q_OS_LINUX)
if (d->ascii) {
munlock(d->ascii, LINUX_MEMLOCK_LIMIT_BYTES);
}
#endif
d->security_unpaged = false;
}
else {
#if defined(Q_OS_LINUX)
if (d->ascii) {
mlock(d->ascii, LINUX_MEMLOCK_LIMIT_BYTES);
}
#endif
d->security_unpaged = true;
}
}
}
/*!
Returns the string encoded in UTF-8 format.
See TQTextCodec for more diverse coding/decoding of Unicode strings.
\sa fromUtf8(), ascii(), latin1(), local8Bit()
*/
TQCString TQString::utf8() const
{
int l = length();
int rlen = l*3+1;
TQCString rstr(rlen);
uchar* cursor = (uchar*)rstr.data();
const TQChar *ch = d->unicode;
for (int i=0; i < l; i++) {
uint u = ch->unicode();
if ( u < 0x80 ) {
*cursor++ = (uchar)u;
} else {
if ( u < 0x0800 ) {
*cursor++ = 0xc0 | ((uchar) (u >> 6));
} else {
if (u >= 0xd800 && u < 0xdc00 && i < l-1) {
unsigned short low = ch[1].unicode();
if (low >= 0xdc00 && low < 0xe000) {
++ch;
++i;
u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
}
}
if (u > 0xffff) {
// if people are working in utf8, but strings are encoded in eg. latin1, the resulting
// name might be invalid utf8. This and the corresponding code in fromUtf8 takes care
// we can handle this without loosing information. This can happen with latin filenames
// and a utf8 locale under Unix.
if (u > 0x10fe00 && u < 0x10ff00) {
*cursor++ = (u - 0x10fe00);
++ch;
continue;
} else {
*cursor++ = 0xf0 | ((uchar) (u >> 18));
*cursor++ = 0x80 | ( ((uchar) (u >> 12)) & 0x3f);
}
} else {
*cursor++ = 0xe0 | ((uchar) (u >> 12));
}
*cursor++ = 0x80 | ( ((uchar) (u >> 6)) & 0x3f);
}
*cursor++ = 0x80 | ((uchar) (u&0x3f));
}
++ch;
}
rstr.truncate( cursor - (uchar*)rstr.data() );
return rstr;
}
static TQChar *addOne(TQChar *qch, TQString &str)
{
long sidx = qch - str.unicode();
str.setLength(str.length()+1);
return (TQChar *)str.unicode() + sidx;
}
/*!
Returns the Unicode string decoded from the first \a len
bytes of \a utf8, ignoring the rest of \a utf8. If \a len is
-1 then the length of \a utf8 is used. If \a len is bigger than
the length of \a utf8 then it will use the length of \a utf8.
\code
TQString str = TQString::fromUtf8( "123456789", 5 );
// str == "12345"
\endcode
See TQTextCodec for more diverse coding/decoding of Unicode strings.
*/
TQString TQString::fromUtf8( const char* utf8, int len )
{
if ( !utf8 ) {
return TQString::null;
}
int slen = 0;
if (len >= 0) {
while (slen < len && utf8[slen]) {
slen++;
}
} else {
slen = int(strlen(utf8));
}
len = len < 0 ? slen : TQMIN(slen, len);
TQString result;
result.setLength( len ); // worst case
TQChar *qch = (TQChar *)result.unicode();
uint uc = 0;
uint min_uc = 0;
int need = 0;
int error = -1;
uchar ch;
for (int i=0; i<len; i++) {
ch = utf8[i];
if (need) {
if ( (ch&0xc0) == 0x80 ) {
uc = (uc << 6) | (ch & 0x3f);
need--;
if ( !need ) {
if (uc > 0xffff) {
// surrogate pair
uc -= 0x10000;
unsigned short high = uc/0x400 + 0xd800;
unsigned short low = uc%0x400 + 0xdc00;
*qch++ = TQChar(high);
*qch++ = TQChar(low);
} else if (uc < min_uc || (uc >= 0xd800 && uc <= 0xdfff) || (uc >= 0xfffe)) {
// overlong seqence, UTF16 surrogate or BOM
i = error;
qch = addOne(qch, result);
*qch++ = TQChar(0xdbff);
*qch++ = TQChar(0xde00+((uchar)utf8[i]));
} else {
*qch++ = uc;
}
}
} else {
// See TQString::utf8() for explanation.
//
// The surrogate below corresponds to a Unicode value of (0x10fe00+ch) which
// is in one of the private use areas of Unicode.
i = error;
qch = addOne(qch, result);
*qch++ = TQChar(0xdbff);
*qch++ = TQChar(0xde00+((uchar)utf8[i]));
need = 0;
}
} else {
if ( ch < 128 ) {
*qch++ = ch;
} else if ((ch & 0xe0) == 0xc0) {
uc = ch & 0x1f;
need = 1;
error = i;
min_uc = 0x80;
} else if ((ch & 0xf0) == 0xe0) {
uc = ch & 0x0f;
need = 2;
error = i;
min_uc = 0x800;
} else if ((ch&0xf8) == 0xf0) {
uc = ch & 0x07;
need = 3;
error = i;
min_uc = 0x10000;
} else {
// Error
qch = addOne(qch, result);
*qch++ = TQChar(0xdbff);
*qch++ = TQChar(0xde00+((uchar)utf8[i]));
}
}
}
if (need) {
// we have some invalid characters remaining we need to add to the string
for (int i = error; i < len; ++i) {
qch = addOne(qch, result);
*qch++ = TQChar(0xdbff);
*qch++ = TQChar(0xde00+((uchar)utf8[i]));
}
}
result.truncate( qch - result.unicode() );
return result;
}
/*!
Returns the Unicode string decoded from the first \a len
bytes of \a ascii, ignoring the rest of \a ascii. If \a len
is -1 then the length of \a ascii is used. If \a len is bigger
than the length of \a ascii then it will use the length of \a
ascii.
If a codec has been set using TQTextCodec::codecForCStrings(),
it is used to convert the string from 8-bit characters to Unicode.
Otherwise, this function does the same as fromLatin1().
This is the same as the TQString(const char*) constructor, but you
can make that constructor invisible if you compile with the define
\c QT_NO_CAST_ASCII, in which case you can explicitly create a
TQString from 8-bit ASCII text using this function.
\code
TQString str = TQString::fromAscii( "123456789", 5 );
// str == "12345"
\endcode
*/
TQString TQString::fromAscii( const char* ascii, int len )
{
#ifndef QT_NO_TEXTCODEC
if ( TQTextCodec::codecForCStrings() ) {
if ( !ascii )
return TQString::null;
if ( len < 0 )
len = (int)strlen( ascii );
if ( len == 0 || *ascii == '\0' )
return TQString::fromLatin1( "" );
return TQTextCodec::codecForCStrings()->toUnicode( ascii, len );
}
#endif
return fromLatin1( ascii, len );
}
/*!
Returns the Unicode string decoded from the first \a len
bytes of \a chars, ignoring the rest of \a chars. If \a len
is -1 then the length of \a chars is used. If \a len is bigger
than the length of \a chars then it will use the length of \a
chars.
\sa fromAscii()
*/
TQString TQString::fromLatin1( const char* chars, int len )
{
uint l;
TQChar *uc;
if ( len < 0 ) {
len = -1;
}
uc = internalLatin1ToUnicode( chars, &l, len );
TQString ret( new TQStringData(uc, l, l), TRUE );
return ret;
}
/*!
\fn const TQChar* TQString::unicode() const
Returns the Unicode representation of the string. The result
remains valid until the string is modified.
*/
/*!
Returns the string encoded in a locale-specific format. On X11,
this is the TQTextCodec::codecForLocale(). On Windows, it is a
system-defined encoding. On Mac OS X, this always uses UTF-8 as
the encoding.
See TQTextCodec for more diverse coding/decoding of Unicode
strings.
\sa fromLocal8Bit(), ascii(), latin1(), utf8()
*/
TQCString TQString::local8Bit() const
{
#ifdef QT_NO_TEXTCODEC
return latin1();
#else
#ifdef Q_WS_X11
TQTextCodec* codec = TQTextCodec::codecForLocale();
return codec
? codec->fromUnicode(*this)
: TQCString(latin1());
#endif
#if defined( Q_WS_MACX )
return utf8();
#endif
#if defined( Q_WS_MAC9 )
return TQCString(latin1()); //I'm evil..
#endif
#ifdef Q_WS_WIN
return isNull() ? TQCString("") : qt_winTQString2MB( *this );
#endif
#ifdef Q_WS_QWS
return utf8(); // ### if there is any 8 bit format supported?
#endif
#endif
}
/*!
Returns the Unicode string decoded from the first \a len
bytes of \a local8Bit, ignoring the rest of \a local8Bit. If
\a len is -1 then the length of \a local8Bit is used. If \a len is
bigger than the length of \a local8Bit then it will use the length
of \a local8Bit.
\code
TQString str = TQString::fromLocal8Bit( "123456789", 5 );
// str == "12345"
\endcode
\a local8Bit is assumed to be encoded in a locale-specific format.
See TQTextCodec for more diverse coding/decoding of Unicode strings.
*/
TQString TQString::fromLocal8Bit( const char* local8Bit, int len )
{
#ifdef QT_NO_TEXTCODEC
return fromLatin1( local8Bit, len );
#else
if ( !local8Bit )
return TQString::null;
#ifdef Q_WS_X11
TQTextCodec* codec = TQTextCodec::codecForLocale();
if ( len < 0 )
len = strlen( local8Bit );
return codec
? codec->toUnicode( local8Bit, len )
: fromLatin1( local8Bit, len );
#endif
#if defined( Q_WS_MAC )
return fromUtf8(local8Bit,len);
#endif
// Should this be OS_WIN32?
#ifdef Q_WS_WIN
if ( len >= 0 ) {
TQCString s(local8Bit,len+1);
return qt_winMB2TQString(s);
}
return qt_winMB2TQString( local8Bit );
#endif
#ifdef Q_WS_QWS
return fromUtf8(local8Bit,len);
#endif
#endif // QT_NO_TEXTCODEC
}
/*!
\fn TQString::operator const char *() const
Returns ascii(). Be sure to see the warnings documented in the
ascii() function. Note that for new code which you wish to be
strictly Unicode-clean, you can define the macro \c
QT_NO_ASCII_CAST when compiling your code to hide this function so
that automatic casts are not done. This has the added advantage
that you catch the programming error described in operator!().
*/
/*!
\fn TQString::operator std::string() const
Returns ascii() as a std::string.
\warning The function may cause an application to crash if a static C run-time is in use.
This can happen in Microsoft Visual C++ if TQt is configured as single-threaded. A safe
alternative is to call ascii() directly and construct a std::string manually.
*/
/*!
Returns the TQString as a zero terminated array of unsigned shorts
if the string is not null; otherwise returns zero.
The result remains valid so long as one unmodified
copy of the source string exists.
*/
const unsigned short *TQString::ucs2() const
{
if ( ! d->unicode )
return 0;
unsigned int len = d->len;
if ( d->maxl < len + 1 ) {
// detach, grow or shrink
uint newMax = computeNewMax( len + 1 );
TQChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
if ( nd ) {
if ( d->unicode )
memcpy( nd, d->unicode, sizeof(TQChar)*len );
((TQString *)this)->deref();
((TQString *)this)->d = new TQStringData( nd, len, newMax );
}
}
d->unicode[len] = 0;
return (unsigned short *) d->unicode;
}
/*!
Constructs a string that is a deep copy of \a str, interpreted as a
UCS2 encoded, zero terminated, Unicode string.
If \a str is 0, then a null string is created.
\sa isNull()
*/
TQString TQString::fromUcs2( const unsigned short *str )
{
if ( !str ) {
return TQString::null;
} else {
int length = 0;
while ( str[length] != 0 )
length++;
TQChar* uc = QT_ALLOC_QCHAR_VEC( length );
memcpy( uc, str, length*sizeof(TQChar) );
TQString ret( new TQStringData( uc, length, length ), TRUE );
return ret;
}
}
/*!
\fn TQChar TQString::at( uint ) const
Returns the character at index \a i, or 0 if \a i is beyond the
length of the string.
\code
const TQString string( "abcdefgh" );
TQChar ch = string.at( 4 );
// ch == 'e'
\endcode
If the TQString is not const (i.e. const TQString) or const& (i.e.
const TQString &), then the non-const overload of at() will be used
instead.
*/
/*!
\fn TQChar TQString::constref(uint i) const
Returns the TQChar at index \a i by value.
Equivalent to at(\a i).
\sa ref()
*/
/*!
\fn TQChar& TQString::ref(uint i)
Returns the TQChar at index \a i by reference, expanding the string
with TQChar::null if necessary. The resulting reference can be
assigned to, or otherwise used immediately, but becomes invalid
once furher modifications are made to the string.
\code
TQString string("ABCDEF");
TQChar ch = string.ref( 3 ); // ch == 'D'
\endcode
\sa constref()
*/
TQChar& TQString::ref(uint i) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
if ( (d->count != 1) || (i >= d->len) ) {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
subat( i );
}
else {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
}
d->setDirty();
return d->unicode[i];
}
/*!
\fn TQChar TQString::operator[]( int ) const
Returns the character at index \a i, or TQChar::null if \a i is
beyond the length of the string.
If the TQString is not const (i.e., const TQString) or const\&
(i.e., const TQString\&), then the non-const overload of operator[]
will be used instead.
*/
/*!
\fn TQCharRef TQString::operator[]( int )
\overload
The function returns a reference to the character at index \a i.
The resulting reference can then be assigned to, or used
immediately, but it will become invalid once further modifications
are made to the original string.
If \a i is beyond the length of the string then the string is
expanded with TQChar::nulls, so that the TQCharRef references a
valid (null) character in the string.
The TQCharRef internal class can be used much like a constant
TQChar, but if you assign to it, you change the original string
(which will detach itself because of TQString's copy-on-write
semantics). You will get compilation errors if you try to use the
result as anything but a TQChar.
*/
/*!
\fn TQCharRef TQString::at( uint i )
\overload
The function returns a reference to the character at index \a i.
The resulting reference can then be assigned to, or used
immediately, but it will become invalid once further modifications
are made to the original string.
If \a i is beyond the length of the string then the string is
expanded with TQChar::null.
*/
/*
Internal chunk of code to handle the
uncommon cases of at() above.
*/
void TQString::subat( uint i )
{
uint olen = d->len;
if ( i >= olen ) {
setLength( i+1 ); // i is index; i+1 is needed length
for ( uint j=olen; j<=i; j++ )
d->unicode[j] = TQChar::null;
} else {
// Just be sure to detach
real_detach();
}
}
/*!
Resizes the string to \a len characters and copies \a unicode into
the string. If \a unicode is 0, nothing is copied, but the
string is still resized to \a len. If \a len is zero, then the
string becomes a \link isNull() null\endlink string.
\sa setLatin1(), isNull()
*/
TQString& TQString::setUnicode( const TQChar *unicode, uint len )
{
if ( len == 0 ) { // set to null string
if ( d != shared_null ) { // beware of nullstring being set to nullstring
deref();
d = shared_null ? shared_null : makeSharedNull();
}
}
else {
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
if ( d->count != 1 || len > d->maxl || ( len * 4 < d->maxl && d->maxl > 4 ) ) {
// detach, grown or shrink
uint newMax = computeNewMax( len );
TQChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
if ( unicode ) {
memcpy( nd, unicode, sizeof(TQChar)*len );
}
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
deref();
d = new TQStringData( nd, len, newMax );
}
else {
d->len = len;
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
d->setDirty();
if ( unicode ) {
memcpy( d->unicode, unicode, sizeof(TQChar)*len );
}
}
}
return *this;
}
/*!
Resizes the string to \a len characters and copies \a
unicode_as_ushorts into the string (on some X11 client platforms
this will involve a byte-swapping pass).
If \a unicode_as_ushorts is 0, nothing is copied, but the string
is still resized to \a len. If \a len is zero, the string becomes
a \link isNull() null\endlink string.
\sa setLatin1(), isNull()
*/
TQString& TQString::setUnicodeCodes( const ushort* unicode_as_ushorts, uint len )
{
return setUnicode((const TQChar*)unicode_as_ushorts, len);
}
/*!
Sets this string to \a str, interpreted as a classic 8-bit ASCII C
string. If \a len is -1 (the default), then it is set to
strlen(str).
If \a str is 0 a null string is created. If \a str is "", an empty
string is created.
\sa isNull(), isEmpty()
*/
TQString &TQString::setAscii( const char *str, int len )
{
#ifndef QT_NO_TEXTCODEC
if ( TQTextCodec::codecForCStrings() ) {
*this = TQString::fromAscii( str, len );
return *this;
}
#endif // QT_NO_TEXTCODEC
return setLatin1( str, len );
}
/*!
Sets this string to \a str, interpreted as a classic Latin-1 C
string. If \a len is -1 (the default), then it is set to
strlen(str).
If \a str is 0 a null string is created. If \a str is "", an empty
string is created.
\sa isNull(), isEmpty()
*/
TQString &TQString::setLatin1( const char *str, int len )
{
if ( str == 0 )
return setUnicode(0,0);
if ( len < 0 )
len = int(strlen( str ));
if ( len == 0 ) { // won't make a null string
*this = TQString::fromLatin1( "" );
} else {
setUnicode( 0, len ); // resize but not copy
TQChar *p = d->unicode;
while ( len-- )
*p++ = *str++;
}
return *this;
}
/*! \internal
*/
void TQString::checkSimpleText() const
{
TQChar *p = d->unicode;
TQChar *end = p + d->len;
while ( p < end ) {
ushort uc = p->unicode();
// sort out regions of complex text formatting
if ( uc > 0x058f && ( uc < 0x1100 || uc > 0xfb0f ) ) {
d->issimpletext = FALSE;
return;
}
p++;
}
d->issimpletext = TRUE;
}
/*! \fn bool TQString::simpleText() const
\internal
*/
/*! \internal
*/
bool TQString::isRightToLeft() const
{
int len = length();
TQChar *p = d->unicode;
while ( len-- ) {
switch( ::direction( *p ) )
{
case TQChar::DirL:
case TQChar::DirLRO:
case TQChar::DirLRE:
return FALSE;
case TQChar::DirR:
case TQChar::DirAL:
case TQChar::DirRLO:
case TQChar::DirRLE:
return TRUE;
default:
break;
}
++p;
}
return FALSE;
}
/*!
\fn int TQString::compare( const TQString & s1, const TQString & s2 )
Lexically compares \a s1 with \a s2 and returns an integer less
than, equal to, or greater than zero if \a s1 is less than, equal
to, or greater than \a s2.
The comparison is based exclusively on the numeric Unicode values
of the characters and is very fast, but is not what a human would
expect. Consider sorting user-interface strings with
TQString::localeAwareCompare().
\code
int a = TQString::compare( "def", "abc" ); // a > 0
int b = TQString::compare( "abc", "def" ); // b < 0
int c = TQString::compare( "abc", "abc" ); // c == 0
\endcode
*/
/*!
\overload
Lexically compares this string with \a s and returns an integer
less than, equal to, or greater than zero if it is less than, equal
to, or greater than \a s.
*/
int TQString::compare( const TQString& s ) const
{
return ucstrcmp( *this, s );
}
/*!
\fn int TQString::localeAwareCompare( const TQString & s1, const TQString & s2 )
Compares \a s1 with \a s2 and returns an integer less than, equal
to, or greater than zero if \a s1 is less than, equal to, or
greater than \a s2.
The comparison is performed in a locale- and also
platform-dependent manner. Use this function to present sorted
lists of strings to the user.
\sa TQString::compare() TQTextCodec::locale()
*/
/*!
\overload
Compares this string with \a s.
*/
#if !defined(CSTR_LESS_THAN)
#define CSTR_LESS_THAN 1
#define CSTR_EQUAL 2
#define CSTR_GREATER_THAN 3
#endif
int TQString::localeAwareCompare( const TQString& s ) const
{
// do the right thing for null and empty
if ( isEmpty() || s.isEmpty() )
return compare( s );
#if defined(Q_WS_WIN)
int res;
QT_WA( {
const TCHAR* s1 = (TCHAR*)ucs2();
const TCHAR* s2 = (TCHAR*)s.ucs2();
res = CompareStringW( LOCALE_USER_DEFAULT, 0, s1, length(), s2, s.length() );
} , {
TQCString s1 = local8Bit();
TQCString s2 = s.local8Bit();
res = CompareStringA( LOCALE_USER_DEFAULT, 0, s1.data(), s1.length(), s2.data(), s2.length() );
} );
switch ( res ) {
case CSTR_LESS_THAN:
return -1;
case CSTR_GREATER_THAN:
return 1;
default:
return 0;
}
#elif defined(Q_WS_MACX)
int delta = 0;
#if !defined(QT_NO_TEXTCODEC)
TQTextCodec *codec = TQTextCodec::codecForLocale();
if (codec)
delta = strcoll(codec->fromUnicode(*this), codec->fromUnicode(s));
if (delta == 0)
#endif
delta = ucstrcmp(*this, s);
return delta;
#elif defined(Q_WS_X11)
// declared in <string.h>
int delta = strcoll( local8Bit(), s.local8Bit() );
if ( delta == 0 )
delta = ucstrcmp( *this, s );
return delta;
#else
return ucstrcmp( *this, s );
#endif
}
bool operator==( const TQString &s1, const TQString &s2 )
{
if ( s1.unicode() == s2.unicode() )
return TRUE;
return (s1.length() == s2.length()) && s1.isNull() == s2.isNull() &&
(memcmp((char*)s1.unicode(),(char*)s2.unicode(),
s1.length()*sizeof(TQChar)) == 0 );
}
bool operator!=( const TQString &s1, const TQString &s2 )
{ return !(s1==s2); }
bool operator<( const TQString &s1, const TQString &s2 )
{ return ucstrcmp(s1,s2) < 0; }
bool operator<=( const TQString &s1, const TQString &s2 )
{ return ucstrcmp(s1,s2) <= 0; }
bool operator>( const TQString &s1, const TQString &s2 )
{ return ucstrcmp(s1,s2) > 0; }
bool operator>=( const TQString &s1, const TQString &s2 )
{ return ucstrcmp(s1,s2) >= 0; }
bool operator==( const TQString &s1, const char *s2 )
{
if ( !s2 )
return s1.isNull();
int len = s1.length();
const TQChar *uc = s1.unicode();
while ( len ) {
if ( !(*s2) || uc->unicode() != (uchar) *s2 )
return FALSE;
++uc;
++s2;
--len;
}
return !*s2;
}
bool operator==( const char *s1, const TQString &s2 )
{ return (s2 == s1); }
bool operator!=( const TQString &s1, const char *s2 )
{ return !(s1==s2); }
bool operator!=( const char *s1, const TQString &s2 )
{ return !(s1==s2); }
bool operator<( const TQString &s1, const char *s2 )
{ return ucstrcmp(s1,s2) < 0; }
bool operator<( const char *s1, const TQString &s2 )
{ return ucstrcmp(s1,s2) < 0; }
bool operator<=( const TQString &s1, const char *s2 )
{ return ucstrcmp(s1,s2) <= 0; }
bool operator<=( const char *s1, const TQString &s2 )
{ return ucstrcmp(s1,s2) <= 0; }
bool operator>( const TQString &s1, const char *s2 )
{ return ucstrcmp(s1,s2) > 0; }
bool operator>( const char *s1, const TQString &s2 )
{ return ucstrcmp(s1,s2) > 0; }
bool operator>=( const TQString &s1, const char *s2 )
{ return ucstrcmp(s1,s2) >= 0; }
bool operator>=( const char *s1, const TQString &s2 )
{ return ucstrcmp(s1,s2) >= 0; }
/*****************************************************************************
Documentation for TQString related functions
*****************************************************************************/
/*!
\fn bool operator==( const TQString &s1, const TQString &s2 )
\relates TQString
Returns TRUE if \a s1 is equal to \a s2; otherwise returns FALSE.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1, \a s2) == 0.
\sa isNull(), isEmpty()
*/
/*!
\fn bool operator==( const TQString &s1, const char *s2 )
\overload
\relates TQString
Returns TRUE if \a s1 is equal to \a s2; otherwise returns FALSE.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1, \a s2) == 0.
\sa isNull(), isEmpty()
*/
/*!
\fn bool operator==( const char *s1, const TQString &s2 )
\overload
\relates TQString
Returns TRUE if \a s1 is equal to \a s2; otherwise returns FALSE.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1, \a s2) == 0.
\sa isNull(), isEmpty()
*/
/*!
\fn bool operator!=( const TQString &s1, const TQString &s2 )
\relates TQString
Returns TRUE if \a s1 is not equal to \a s2; otherwise returns FALSE.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1, \a s2) != 0.
\sa isNull(), isEmpty()
*/
/*!
\fn bool operator!=( const TQString &s1, const char *s2 )
\overload
\relates TQString
Returns TRUE if \a s1 is not equal to \a s2; otherwise returns FALSE.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1, \a s2) != 0.
\sa isNull(), isEmpty()
*/
/*!
\fn bool operator!=( const char *s1, const TQString &s2 )
\overload
\relates TQString
Returns TRUE if \a s1 is not equal to \a s2; otherwise returns FALSE.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1, \a s2) != 0.
\sa isNull(), isEmpty()
*/
/*!
\fn bool operator<( const TQString &s1, const char *s2 )
\relates TQString
Returns TRUE if \a s1 is lexically less than \a s2; otherwise returns FALSE.
The comparison is case sensitive.
Equivalent to compare(\a s1, \a s2) \< 0.
*/
/*!
\fn bool operator<( const char *s1, const TQString &s2 )
\overload
\relates TQString
Returns TRUE if \a s1 is lexically less than \a s2; otherwise returns FALSE.
The comparison is case sensitive.
Equivalent to compare(\a s1, \a s2) \< 0.
*/
/*!
\fn bool operator<=( const TQString &s1, const char *s2 )
\relates TQString
Returns TRUE if \a s1 is lexically less than or equal to \a s2;
otherwise returns FALSE.
The comparison is case sensitive.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1,\a s2) \<= 0.
\sa isNull(), isEmpty()
*/
/*!
\fn bool operator<=( const char *s1, const TQString &s2 )
\overload
\relates TQString
Returns TRUE if \a s1 is lexically less than or equal to \a s2;
otherwise returns FALSE.
The comparison is case sensitive.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1, \a s2) \<= 0.
\sa isNull(), isEmpty()
*/
/*!
\fn bool operator>( const TQString &s1, const char *s2 )
\relates TQString
Returns TRUE if \a s1 is lexically greater than \a s2; otherwise
returns FALSE.
The comparison is case sensitive.
Equivalent to compare(\a s1, \a s2) \> 0.
*/
/*!
\fn bool operator>( const char *s1, const TQString &s2 )
\overload
\relates TQString
Returns TRUE if \a s1 is lexically greater than \a s2; otherwise
returns FALSE.
The comparison is case sensitive.
Equivalent to compare(\a s1, \a s2) \> 0.
*/
/*!
\fn bool operator>=( const TQString &s1, const char *s2 )
\relates TQString
Returns TRUE if \a s1 is lexically greater than or equal to \a s2;
otherwise returns FALSE.
The comparison is case sensitive.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1, \a s2) \>= 0.
\sa isNull(), isEmpty()
*/
/*!
\fn bool operator>=( const char *s1, const TQString &s2 )
\overload
\relates TQString
Returns TRUE if \a s1 is lexically greater than or equal to \a s2;
otherwise returns FALSE.
The comparison is case sensitive.
Note that a null string is not equal to a not-null empty string.
Equivalent to compare(\a s1, \a s2) \>= 0.
\sa isNull(), isEmpty()
*/
/*!
\fn const TQString operator+( const TQString &s1, const TQString &s2 )
\relates TQString
Returns a string which is the result of concatenating the string
\a s1 and the string \a s2.
Equivalent to \a {s1}.append(\a s2).
*/
/*!
\fn const TQString operator+( const TQString &s1, const char *s2 )
\overload
\relates TQString
Returns a string which is the result of concatenating the string
\a s1 and character \a s2.
Equivalent to \a {s1}.append(\a s2).
*/
/*!
\fn const TQString operator+( const char *s1, const TQString &s2 )
\overload
\relates TQString
Returns a string which is the result of concatenating the
character \a s1 and string \a s2.
*/
/*!
\fn const TQString operator+( const TQString &s, char c )
\overload
\relates TQString
Returns a string which is the result of concatenating the string
\a s and character \a c.
Equivalent to \a {s}.append(\a c).
*/
/*!
\fn const TQString operator+( char c, const TQString &s )
\overload
\relates TQString
Returns a string which is the result of concatenating the
character \a c and string \a s.
Equivalent to \a {s}.prepend(\a c).
*/
/*****************************************************************************
TQString stream functions
*****************************************************************************/
#ifndef QT_NO_DATASTREAM
/*!
\relates TQString
Writes the string \a str to the stream \a s.
See also \link datastreamformat.html Format of the TQDataStream operators \endlink
*/
TQDataStream &operator<<( TQDataStream &s, const TQString &str )
{
if ( s.version() == 1 ) {
TQCString l( str.latin1() );
s << l;
}
else {
int byteOrder = s.byteOrder();
const TQChar* ub = str.unicode();
if ( ub || s.version() < 3 ) {
static const uint auto_size = 1024;
char t[auto_size];
char *b;
if ( str.length()*sizeof(TQChar) > auto_size ) {
b = new char[str.length()*sizeof(TQChar)];
} else {
b = t;
}
int l = str.length();
char *c=b;
while ( l-- ) {
if ( byteOrder == TQDataStream::BigEndian ) {
*c++ = (char)ub->row();
*c++ = (char)ub->cell();
} else {
*c++ = (char)ub->cell();
*c++ = (char)ub->row();
}
ub++;
}
s.writeBytes( b, sizeof(TQChar)*str.length() );
if ( str.length()*sizeof(TQChar) > auto_size )
delete [] b;
} else {
// write null marker
s << (TQ_UINT32)0xffffffff;
}
}
return s;
}
/*!
\relates TQString
Reads a string from the stream \a s into string \a str.
See also \link datastreamformat.html Format of the TQDataStream operators \endlink
*/
TQDataStream &operator>>( TQDataStream &s, TQString &str )
{
#ifdef QT_QSTRING_UCS_4
#if defined(Q_CC_GNU)
#warning "operator>> not working properly"
#endif
#endif
if ( s.version() == 1 ) {
TQCString l;
s >> l;
str = TQString( l );
}
else {
TQ_UINT32 bytes = 0;
s >> bytes; // read size of string
if ( bytes == 0xffffffff ) { // null string
str = TQString::null;
} else if ( bytes > 0 ) { // not empty
int byteOrder = s.byteOrder();
str.setLength( bytes/2 );
TQChar* ch = str.d->unicode;
static const uint auto_size = 1024;
char t[auto_size];
char *b;
if ( bytes > auto_size ) {
b = new char[bytes];
} else {
b = t;
}
s.readRawBytes( b, bytes );
int bt = bytes/2;
char *oldb = b;
while ( bt-- ) {
if ( byteOrder == TQDataStream::BigEndian )
*ch++ = (ushort) (((ushort)b[0])<<8) | (uchar)b[1];
else
*ch++ = (ushort) (((ushort)b[1])<<8) | (uchar)b[0];
b += 2;
}
if ( bytes > auto_size )
delete [] oldb;
} else {
str = "";
}
}
return s;
}
#endif // QT_NO_DATASTREAM
/*****************************************************************************
TQConstString member functions
*****************************************************************************/
/*!
\class TQConstString ntqstring.h
\reentrant
\ingroup text
\brief The TQConstString class provides string objects using constant Unicode data.
In order to minimize copying, highly optimized applications can
use TQConstString to provide a TQString-compatible object from
existing Unicode data. It is then the programmer's responsibility
to ensure that the Unicode data exists for the entire lifetime of
the TQConstString object.
A TQConstString is created with the TQConstString constructor. The
string held by the object can be obtained by calling string().
*/
/*!
Constructs a TQConstString that uses the first \a length Unicode
characters in the array \a unicode. Any attempt to modify copies
of the string will cause it to create a copy of the data, thus it
remains forever unmodified.
The data in \a unicode is not copied. The caller must be able to
guarantee that \a unicode will not be deleted or modified.
*/
TQConstString::TQConstString( const TQChar* unicode, uint length ) :
TQString( new TQStringData( (TQChar*)unicode, length, length ), TRUE )
{
}
/*!
Destroys the TQConstString, creating a copy of the data if other
strings are still using it.
*/
TQConstString::~TQConstString()
{
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->lock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
if ( d->count > 1 ) {
TQChar* cp = QT_ALLOC_QCHAR_VEC( d->len );
memcpy( cp, d->unicode, d->len*sizeof(TQChar) );
d->unicode = cp;
}
else {
d->unicode = 0;
}
// The original d->unicode is now unlinked.
#if defined(QT_THREAD_SUPPORT) && defined(MAKE_TQSTRING_THREAD_SAFE)
d->mutex->unlock();
#endif // QT_THREAD_SUPPORT && MAKE_TQSTRING_THREAD_SAFE
}
/*!
\fn const TQString& TQConstString::string() const
Returns a constant string referencing the data passed during
construction.
*/
/*!
Returns TRUE if the string starts with \a s; otherwise returns
FALSE.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
\code
TQString str( "Bananas" );
str.startsWith( "Ban" ); // returns TRUE
str.startsWith( "Car" ); // returns FALSE
\endcode
\sa endsWith()
*/
bool TQString::startsWith( const TQString& s, bool cs ) const
{
if ( isNull() )
return s.isNull();
if ( s.length() > length() )
return FALSE;
if ( cs ) {
return memcmp((char*)d->unicode, (char*)s.d->unicode, s.length()*sizeof(TQChar)) == 0;
} else {
for ( int i = 0; i < (int) s.length(); i++ ) {
if ( ::lower(d->unicode[i]) != ::lower(s.d->unicode[i]) )
return FALSE;
}
}
return TRUE;
}
bool TQString::startsWith( const TQString& s ) const
{
return startsWith( s, TRUE );
}
/*!
Returns TRUE if the string ends with \a s; otherwise returns
FALSE.
If \a cs is TRUE (the default), the search is case sensitive;
otherwise the search is case insensitive.
\code
TQString str( "Bananas" );
str.endsWith( "anas" ); // returns TRUE
str.endsWith( "pple" ); // returns FALSE
\endcode
\sa startsWith()
*/
bool TQString::endsWith( const TQString& s, bool cs ) const
{
if ( isNull() )
return s.isNull();
int pos = length() - s.length();
if ( pos < 0 )
return FALSE;
if ( cs ) {
return memcmp((char*)&d->unicode[pos], (char*)s.d->unicode, s.length()*sizeof(TQChar)) == 0;
} else {
for ( int i = 0; i < (int) s.length(); i++ ) {
if ( ::lower(d->unicode[pos + i]) != ::lower(s.d->unicode[i]) )
return FALSE;
}
}
return TRUE;
}
bool TQString::endsWith( const TQString& s ) const
{
return endsWith( s, TRUE );
}
/*! \fn void TQString::detach()
If the string does not share its data with another TQString instance,
nothing happens; otherwise the function creates a new, unique copy of
this string. This function is called whenever the string is modified. The
implicit sharing mechanism is implemented this way.
*/
#if defined(Q_OS_WIN32)
#include <windows.h>
/*!
\obsolete
Returns a static Windows TCHAR* from a TQString, adding NUL if \a
addnul is TRUE.
The lifetime of the return value is until the next call to this function,
or until the last copy of str is deleted, whatever comes first.
Use ucs2() instead.
*/
const void* qt_winTchar(const TQString& str, bool)
{
// So that the return value lives long enough.
static TQString str_cache;
str_cache = str;
#ifdef UNICODE
return str_cache.ucs2();
#else
return str_cache.latin1();
#endif
}
/*!
Makes a new '\0'-terminated Windows TCHAR* from a TQString.
*/
void* qt_winTchar_new(const TQString& str)
{
if ( str.isNull() )
return 0;
int l = str.length()+1;
TCHAR *tc = new TCHAR[ l ];
#ifdef UNICODE
memcpy( tc, str.ucs2(), sizeof(TCHAR)*l );
#else
memcpy( tc, str.latin1(), sizeof(TCHAR)*l );
#endif
return tc;
}
/*!
Makes a TQString from a Windows TCHAR*.
*/
TQString qt_winTQString(void* tc)
{
#ifdef UNICODE
return TQString::fromUcs2( (ushort*)tc );
#else
return TQString::fromLatin1( (TCHAR *)tc );
#endif
}
TQCString qt_winTQString2MB( const TQString& s, int uclen )
{
if ( uclen < 0 )
uclen = s.length();
if ( s.isNull() )
return TQCString();
if ( uclen == 0 )
return TQCString("");
BOOL used_def;
TQCString mb(4096);
int len;
while ( !(len=WideCharToMultiByte(CP_ACP, 0, (const WCHAR*)s.unicode(), uclen,
mb.data(), mb.size()-1, 0, &used_def)) )
{
int r = GetLastError();
if ( r == ERROR_INSUFFICIENT_BUFFER ) {
mb.resize(1+WideCharToMultiByte( CP_ACP, 0,
(const WCHAR*)s.unicode(), uclen,
0, 0, 0, &used_def));
// and try again...
} else {
#ifndef QT_NO_DEBUG
// Fail.
tqWarning("WideCharToMultiByte cannot convert multibyte text (error %d): %s (UTF8)",
r, s.utf8().data());
#endif
break;
}
}
mb[len]='\0';
return mb;
}
// WATCH OUT: mblen must include the NUL (or just use -1)
TQString qt_winMB2TQString( const char* mb, int mblen )
{
if ( !mb || !mblen )
return TQString::null;
const int wclen_auto = 4096;
WCHAR wc_auto[wclen_auto];
int wclen = wclen_auto;
WCHAR *wc = wc_auto;
int len;
while ( !(len=MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED,
mb, mblen, wc, wclen )) )
{
int r = GetLastError();
if ( r == ERROR_INSUFFICIENT_BUFFER ) {
if ( wc != wc_auto ) {
tqWarning("Size changed in MultiByteToWideChar");
break;
} else {
wclen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED,
mb, mblen, 0, 0 );
wc = new WCHAR[wclen];
// and try again...
}
} else {
// Fail.
tqWarning("MultiByteToWideChar cannot convert multibyte text");
break;
}
}
if ( len <= 0 )
return TQString::null;
TQString s( (TQChar*)wc, len - 1 ); // len - 1: we don't want terminator
if ( wc != wc_auto )
delete [] wc;
return s;
}
#endif // Q_OS_WIN32
|