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
|
/*
FALCON - The Falcon Programming Language.
FILE: string.cpp
Implementation of Core Strings.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: mer nov 24 2004
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/** \file
Implementation of Core Strings.
\todo Add support for international strings.
*/
#include <falcon/memory.h>
#include <falcon/string.h>
#include <falcon/stream.h>
#include <falcon/common.h>
#include <falcon/vm.h>
#include <string.h>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
namespace Falcon {
namespace csh {
Static handler_static;
Buffer handler_buffer;
Static16 handler_static16;
Buffer16 handler_buffer16;
Static32 handler_static32;
Buffer32 handler_buffer32;
template<typename t1, typename t2>
inline void copySized( byte* dest_, byte* src_, uint32 size )
{
if ( size != 0 )
{
t1* dest = (t1*) dest_;
t2* src = (t2*) src_;
do {
size--;
dest[size] = (t1) src[size];
} while( size > 0 );
}
}
template<typename T>
inline void copySame( byte* dest_, byte* src_, uint32 size )
{
if ( size != 0 )
{
uint32 len = size * sizeof(T);
T* dest = (T*) dest_;
T* src = (T*) src_;
// overlapping? -- use memmove, else use memcpy; but do the check only if worth
if( len < 10 ||
(src + len > dest) ||
(dest + len > src )
)
memmove( dest, src, len );
else
memcpy( dest, src, len );
}
}
// service function; adapts a smaller buffer into a larger one
// srclen is in "elements" (bytes * charLen).
// returns also the dynamic buffer manipulator useful to handle the target buffer
static Base* adaptBuffer( byte *srcBuffer, uint32 srcPos, uint32 srcCharLen,
byte *destBuffer, uint32 destPos, uint32 destCharLen,
uint32 srcLen )
{
srcBuffer += srcPos * srcCharLen;
destBuffer += destPos * destCharLen;
switch( destCharLen )
{
case 1:
switch( srcCharLen ) {
case 1: copySame<byte>( destBuffer, srcBuffer, srcLen ); break;
case 2: copySized<byte, uint16>( destBuffer, srcBuffer, srcLen ); break;
case 4: copySized<byte, uint32>( destBuffer, srcBuffer, srcLen ); break;
}
return &handler_buffer;
case 2:
switch( srcCharLen ) {
case 1: copySized<uint16, byte>( destBuffer, srcBuffer, srcLen ); break;
case 2: copySame<uint16>( destBuffer, srcBuffer, srcLen ); break;
case 4: copySized<uint16, uint32>( destBuffer, srcBuffer, srcLen ); break;
}
return &handler_buffer16;
case 4:
switch( srcCharLen ) {
case 1: copySized<uint32,byte>( destBuffer, srcBuffer, srcLen ); break;
case 2: copySized<uint32,uint16>( destBuffer, srcBuffer, srcLen ); break;
case 4: copySame<uint32>( destBuffer, srcBuffer, srcLen ); break;
}
return &handler_buffer32;
}
return 0;
}
uint32 Byte::length( const String *str ) const
{
return str->size() / charSize();
}
uint32 Byte::getCharAt( const String *str, uint32 pos ) const
{
return (uint32) str->getRawStorage()[pos];
}
uint32 Static16::length( const String *str ) const
{
return str->size() >> 1;
}
uint32 Static32::length( const String *str ) const
{
return str->size() >> 2;
}
uint32 Buffer16::length( const String *str ) const
{
return str->size() >> 1;
}
uint32 Buffer32::length( const String *str ) const
{
return str->size() >> 2;
}
uint32 Static16::getCharAt( const String *str, uint32 pos ) const
{
return (uint32) reinterpret_cast< uint16 *>(str->getRawStorage())[ pos ];
}
uint32 Buffer16::getCharAt( const String *str, uint32 pos ) const
{
return (uint32) reinterpret_cast< uint16 *>(str->getRawStorage())[ pos ];
}
uint32 Static32::getCharAt( const String *str, uint32 pos ) const
{
return reinterpret_cast< uint32 *>(str->getRawStorage())[ pos ];
}
uint32 Buffer32::getCharAt( const String *str, uint32 pos ) const
{
return reinterpret_cast< uint32 *>(str->getRawStorage())[ pos ];
}
void Byte::subString( const String *str, int32 start, int32 end, String *tgt ) const
{
uint32 nlen = str->length();
if( start < 0 )
start = int(nlen) + start;
if( end < 0 )
end = int(nlen) + end + 1;
if ( start < 0 || start >= (int)nlen || end < 0 || end == start) {
tgt->size( 0 );
return;
}
byte *storage, *source;
int16 cs = charSize();
source = str->getRawStorage();
if ( end < start ) {
uint32 len = start - end + 1;
if ( tgt->allocated() < len * cs ) {
storage = (byte *) memAlloc( len * cs );
}
else
storage = tgt->getRawStorage();
switch( cs )
{
case 1:
{
for( uint32 i = 0; i < len ; i ++ )
storage[i] = source[start-i];
tgt->size( len );
}
break;
case 2:
{
uint16 *storage16 = (uint16 *) storage;
uint16 *source16 = (uint16 *) source;
for( uint32 i = 0; i < len ; i ++ )
storage16[i] = source16[start-i];
tgt->size( len * 2 );
}
break;
case 4:
{
uint32 *storage32 = (uint32 *) storage;
uint32 *source32 = (uint32 *) source;
for( uint32 i = 0; i < len ; i ++ )
storage32[i] = source32[start-i];
tgt->size( len * 4 );
}
break;
}
}
else {
if ( end > (int)nlen )
end = nlen;
uint32 len = (end - start)*cs;
if ( tgt->allocated() < len ) {
storage = (byte *) memAlloc( len );
}
else
storage = tgt->getRawStorage();
memcpy( storage, str->getRawStorage() + (start * cs) , len );
tgt->size( len );
}
// was the storage not enough?
if ( storage != tgt->getRawStorage() )
{
if ( tgt->allocated() != 0 )
tgt->manipulator()->destroy( tgt );
tgt->allocated( tgt->size() );
tgt->setRawStorage( storage );
}
switch( cs )
{
case 1: tgt->manipulator( &handler_buffer ); break;
case 2: tgt->manipulator( &handler_buffer16 ); break;
case 4: tgt->manipulator( &handler_buffer32 ); break;
}
}
bool Byte::change( String *str, uint32 start, uint32 end, const String *source ) const
{
uint32 strLen = str->length();
if ( start >= strLen )
return false;
if ( end > strLen )
end = strLen;
if ( end < start ) {
uint32 temp = end;
end = start+1;
start = temp;
}
int32 len = end - start;
insert( str, start, len, source );
return true;
}
String *Byte::clone( const String *str ) const
{
return new String( *str );
}
uint32 Byte::find( const String *str, const String *element, uint32 start, uint32 end ) const
{
if ( str->size() == 0 || element->size() == 0 )
return npos;
if ( end > str->length() ) // npos is defined to be greater than any size
end = str->length();
if ( end < start ) {
uint32 temp = end;
end = start;
start = temp;
}
uint32 pos = start;
uint32 keyStart = element->getCharAt( 0 );
uint32 elemLen = element->length();
while( pos + elemLen <= end ) {
if ( str->getCharAt( pos ) == keyStart )
{
uint32 len = 1;
while( pos + len < end && len < elemLen && element->getCharAt(len) == str->getCharAt( pos + len ) )
len++;
if ( len == elemLen )
return pos;
}
pos++;
}
// not found.
return npos;
}
uint32 Byte::rfind( const String *str, const String *element, uint32 start, uint32 end ) const
{
if ( str->size() == 0 || element->size() == 0 )
return npos;
if ( end > str->length() ) // npos is defined to be greater than any size
end = str->length();
if ( end < start ) {
uint32 temp = end;
end = start;
start = temp;
}
uint32 keyStart = element->getCharAt( 0 );
uint32 elemLen = element->length();
if ( elemLen > (end - start) )
{
// can't possibly be found
return npos;
}
uint32 pos = end - elemLen;
while( pos >= start ) {
if ( str->getCharAt( pos ) == keyStart ) {
uint32 len = 1;
while( pos + len < end && len < elemLen && element->getCharAt(len) == str->getCharAt( pos + len ) )
len++;
if ( len == elemLen )
return pos;
}
if ( pos == 0 ) break;
pos--;
}
// not found.
return npos;
}
void Byte::remove( String *str, uint32 pos, uint32 len ) const
{
uint32 sl = str->length();
if ( len == 0 || pos > sl )
return;
uint32 cs = charSize();
if ( pos + len > sl )
len = sl - pos;
uint32 newLen = (sl - len) *cs;
byte *mem = (byte *) memAlloc( newLen );
if ( pos > 0 )
memcpy( mem, str->getRawStorage(), pos * cs );
if ( pos + len < sl )
memcpy( mem + pos *cs, str->getRawStorage() +( pos + len) *cs , (sl - pos - len) * cs );
// for non-static strings...
if ( str->allocated() != 0 )
{
memFree( str->getRawStorage() );
}
str->setRawStorage( mem, newLen );
// subclasses will set correct manipulator if needed.
}
void Byte::bufferize( String *str ) const
{
// already buffered?
if ( ! str->isStatic() )
return;
uint32 size = str->m_size;
if ( size != 0 ) {
uint32 oldSize = str->allocated();
byte *mem = (byte *) memAlloc( size );
memcpy( mem, str->getRawStorage(), size );
if( oldSize != 0 )
{
memFree( str->getRawStorage() );
}
str->setRawStorage( mem, size );
str->m_class = str->m_class->bufferedManipulator();
}
}
void Byte::bufferize( String *str, const String *strOrig ) const
{
// copy the other string contents.
if ( str->m_allocated != 0 )
memFree( str->m_storage );
uint32 size = strOrig->m_size;
if ( size == 0 ) {
str->m_class = &handler_static;
str->setRawStorage( 0, 0 );
}
else {
byte *mem = (byte *) memAlloc( size );
memcpy( mem, strOrig->getRawStorage(), size );
str->setRawStorage( mem, size );
str->m_class = strOrig->m_class->bufferedManipulator();
}
}
void Byte::reserve( String *str, uint32 size, bool relative, bool block ) const
{
if ( relative )
size += str->m_allocated;
register int32 chs = charSize();
if ( block )
{
if ( size % FALCON_STRING_ALLOCATION_BLOCK != 0 )
{
size /= (FALCON_STRING_ALLOCATION_BLOCK * chs);
size ++;
size *= (FALCON_STRING_ALLOCATION_BLOCK * chs);
}
}
uint32 nextAlloc = size * chs;
// the required size may be already allocated
if ( nextAlloc > str->allocated() )
{
byte *mem = (byte *) memAlloc( nextAlloc );
uint32 size = str->m_size;
if ( str->m_size > 0 )
memcpy( mem, str->m_storage, str->m_size );
// we can now destroy the old string.
if ( str->allocated() != 0 )
memFree( str->m_storage );
str->m_storage = mem;
str->m_size = size;
str->m_allocated = nextAlloc;
}
// let the subclasses set the correct manipulator
}
//============================================================0
void Static::shrink( String *str ) const
{
// do nothing
}
void Static::reserve( String *str, uint32 size, bool relative, bool block ) const
{
Byte::reserve( str, size, relative, block );
str->m_class = &handler_buffer;
}
const Base *Static::bufferedManipulator() const
{
return &handler_buffer;
}
void Static16::reserve( String *str, uint32 size, bool relative, bool block ) const
{
Byte::reserve( str, size, relative, block );
str->m_class = &handler_buffer16;
}
const Base *Static16::bufferedManipulator() const
{
return &handler_buffer16;
}
void Static32::reserve( String *str, uint32 size, bool relative, bool block ) const
{
Byte::reserve( str, size, relative, block );
str->m_class = &handler_buffer32;
}
const Base *Static32::bufferedManipulator() const
{
return &handler_buffer32;
}
void Static::setCharAt( String *str, uint32 pos, uint32 chr ) const
{
byte *buffer;
int32 size = str->size();
if( chr <= 0xFF )
{
buffer = (byte *) memAlloc( size );
memcpy( buffer, str->getRawStorage(), size );
buffer[ pos ] = (byte) chr;
str->manipulator( &handler_buffer );
}
else if ( chr <= 0xFFFF )
{
uint16 *buf16 = (uint16 *) memAlloc( size * 2 );
buffer = str->getRawStorage();
for ( int i = 0; i < size; i ++ )
buf16[ i ] = (uint16) buffer[ i ];
buf16[ pos ] = (uint16) chr;
buffer = (byte *) buf16;
size *= 2;
str->manipulator( &handler_buffer16 );
}
else
{
uint32 *buf32 = (uint32 *) memAlloc( size * 4 );
buffer = str->getRawStorage();
for ( int i = 0; i < size; i ++ )
buf32[ i ] = (uint32) buffer[ i ];
buf32[ pos ] = chr;
buffer = (byte *) buf32;
size *= 4;
str->manipulator( &handler_buffer32 );
}
uint32 oldSize = str->allocated();
if( oldSize != 0 )
memFree( str->getRawStorage() );
str->setRawStorage( buffer, size );
}
void Static16::setCharAt( String *str, uint32 pos, uint32 chr ) const
{
byte *buffer;
int32 size = str->size();
if ( chr <= 0xFFFF )
{
uint16 *buf16 = (uint16 *) memAlloc( size );
memcpy( buf16, str->getRawStorage(), size );
buf16[ pos ] = (uint16) chr;
buffer = (byte *) buf16;
str->manipulator( &handler_buffer16 );
}
else
{
uint32 *buf32 = (uint32 *) memAlloc( size * 2 );
uint16 *buf16 = (uint16 *) str->getRawStorage();
for ( int i = 0; i < size; i ++ )
buf32[ i ] = (uint32) buf16[ i ];
buf32[ pos ] = chr;
buffer = (byte *) buf32;
size *= 2;
str->manipulator( &handler_buffer32 );
}
uint32 oldSize = str->allocated();
str->setRawStorage( buffer, size );
if( oldSize != 0 )
memFree( str->getRawStorage() );
}
void Static32::setCharAt( String *str, uint32 pos, uint32 chr ) const
{
byte *buffer;
int32 size = str->size();
uint32 *buf32 = (uint32 *) memAlloc( size );
memcpy( buf32, str->getRawStorage(), size );
buf32[ pos ] = chr;
buffer = (byte *) buf32;
str->manipulator( &handler_buffer32 );
uint32 oldSize = str->allocated();
str->setRawStorage( buffer, size );
if( oldSize != 0 )
memFree( str->getRawStorage() );
}
void Buffer::setCharAt( String *str, uint32 pos, uint32 chr ) const
{
byte *buffer;
int32 size = str->size();
if( chr <= 0xFF )
{
str->getRawStorage()[ pos ] = (byte) chr;
}
else if ( chr <= 0xFFFF )
{
uint16 *buf16 = (uint16 *) memAlloc( size * 2 );
buffer = str->getRawStorage();
for ( int i = 0; i < size; i ++ )
buf16[ i ] = (uint16) buffer[ i ];
buf16[ pos ] = (uint16) chr;
size *= 2;
str->manipulator( &handler_buffer16 );
if( str->allocated() > 0 )
memFree( buffer );
str->setRawStorage( (byte *) buf16, size );
}
else
{
uint32 *buf32 = (uint32 *) memAlloc( size * 4 );
buffer = str->getRawStorage();
for ( int i = 0; i < size; i ++ )
buf32[ i ] = (uint32) buffer[ i ];
buf32[ pos ] = chr;
size *= 4;
str->manipulator( &handler_buffer32 );
if( str->allocated() > 0 )
memFree( buffer );
str->setRawStorage( (byte *) buf32, size );
}
}
void Buffer16::setCharAt( String *str, uint32 pos, uint32 chr ) const
{
if ( chr <= 0xFFFF )
{
uint16 *buf16 = (uint16 *) str->getRawStorage();
buf16[ pos ] = (uint16) chr;
}
else
{
int32 size = str->size();
uint32 *buf32 = (uint32 *) memAlloc( size * 2 );
uint16 *buf16 = (uint16 *) str->getRawStorage();
for ( int i = 0; i < size; i ++ )
buf32[ i ] = (uint32) buf16[ i ];
buf32[ pos ] = chr;
size *= 2;
str->manipulator( &handler_buffer32 );
if( str->allocated() > 0 )
memFree( buf16 );
str->setRawStorage( (byte *) buf32, size );
}
}
void Buffer32::setCharAt( String *str, uint32 pos, uint32 chr ) const
{
uint32 *buf32 = (uint32 *) str->getRawStorage();
buf32[ pos ] = chr;
}
void Static::insert( String *str, uint32 pos, uint32 len, const String *source ) const
{
uint32 sourceLen = source->length();
uint32 strLen = str->length();
if ( pos + len > str->size() )
len = str->size() - pos;
uint32 strCharSize = str->manipulator()->charSize();
uint32 destCharSize = source->manipulator()->charSize() > str->manipulator()->charSize() ?
source->manipulator()->charSize() : str->manipulator()->charSize() ; // can be 1 or larger
uint32 finalSize = destCharSize * (strLen - len + sourceLen );
uint32 finalAlloc = ((finalSize / FALCON_STRING_ALLOCATION_BLOCK) + 1) *
FALCON_STRING_ALLOCATION_BLOCK;
// we know we have to relocate, so just do the relocation step
byte *mem = (byte*) memAlloc( finalAlloc );
if ( pos > 0 )
adaptBuffer( str->getRawStorage(), 0, strCharSize, mem, 0, destCharSize, pos );
str->manipulator(
adaptBuffer( source->getRawStorage(), 0, source->manipulator()->charSize(),
mem, pos, destCharSize, sourceLen ) );
if ( pos + len < strLen )
adaptBuffer( str->getRawStorage(), pos + len, strCharSize,
mem, pos + sourceLen, destCharSize,
strLen - pos - len );
str->size( finalSize );
// Static strings CAN have non-static memory: expecially if they are de-serialized strings in modules.
uint32 oldSize = str->allocated();
str->allocated( finalAlloc );
if ( oldSize > 0 )
{
memFree( str->getRawStorage() );
}
str->setRawStorage( mem );
}
void Buffer::insert( String *str, uint32 pos, uint32 len, const String *source ) const
{
uint32 sourceLen = source->length();
uint32 strLen = str->length();
if ( pos + len > str->size() )
len = str->size() - pos;
uint32 strCharSize = str->manipulator()->charSize();
uint32 posBytes = pos *strCharSize;
uint32 lenBytes = len *strCharSize;
uint32 destCharSize = source->manipulator()->charSize() > strCharSize ?
source->manipulator()->charSize() : strCharSize; // can be 1 or larger
uint32 finalSize = destCharSize * (strLen - len + sourceLen );
// should we re-allocate?
if( finalSize > str->allocated() || destCharSize > strCharSize )
{
uint32 finalAlloc = ((finalSize / FALCON_STRING_ALLOCATION_BLOCK) + 1) *
FALCON_STRING_ALLOCATION_BLOCK;
// we know we have to relocate, so just do the relocation step
byte *mem = (byte*) memAlloc( finalAlloc );
if ( pos > 0 )
adaptBuffer( str->getRawStorage(), 0, strCharSize,
mem, 0, destCharSize, pos );
str->manipulator(
adaptBuffer( source->getRawStorage(), 0, source->manipulator()->charSize(),
mem, pos, destCharSize, sourceLen ) );
if ( pos + len < strLen )
adaptBuffer( str->getRawStorage(), pos + len, strCharSize,
mem, pos+sourceLen, destCharSize,
strLen - pos - len );
if ( str->allocated() != 0 )
memFree( str->getRawStorage() );
str->allocated( finalAlloc );
str->setRawStorage( mem );
}
else
{
// should we move the tail?
if ( pos + len < strLen )
{
// can we maintain our char size?
if( destCharSize == strCharSize )
{
uint32 sourceLenBytes = sourceLen * destCharSize;
// then just move the postfix away
memmove( str->getRawStorage() + posBytes + sourceLenBytes,
str->getRawStorage() + posBytes + lenBytes,
str->size() - posBytes - lenBytes );
}
else {
// adapt it to the new size.
adaptBuffer( str->getRawStorage(), pos + len, strCharSize,
str->getRawStorage(), pos + sourceLen, destCharSize,
strLen - pos - len );
}
}
// adapt the incoming part
str->manipulator(
adaptBuffer( source->getRawStorage(), 0, source->manipulator()->charSize(),
str->getRawStorage(), pos, destCharSize,
sourceLen ) );
// eventually adapt the head -- adaptBuffer can work on itself
if ( pos > 0 && destCharSize != strCharSize )
str->manipulator(
adaptBuffer( str->getRawStorage(), 0, strCharSize,
str->getRawStorage(), 0, destCharSize,
pos ) );
}
str->size( finalSize );
}
void Static::remove( String *str, uint32 pos, uint32 len ) const
{
Byte::remove( str, pos, len );
// changing string type.
str->manipulator( &handler_buffer );
}
void Static16::remove( String *str, uint32 pos, uint32 len ) const
{
Byte::remove( str, pos, len );
// changing string type.
str->manipulator( &handler_buffer16 );
}
void Static32::remove( String *str, uint32 pos, uint32 len ) const
{
Byte::remove( str, pos, len );
// changing string type.
str->manipulator( &handler_buffer32 );
}
void Static::destroy( String *str ) const
{
if ( str->allocated() > 0 ) {
memFree( str->getRawStorage() );
str->allocated( 0 );
str->size(0);
}
}
void Buffer::shrink( String *str ) const
{
if( str->size() > str->allocated() )
{
if ( str->size() == 0 )
{
destroy( str );
}
else {
byte *mem = (byte *) memRealloc( str->getRawStorage(), str->size() );
if ( mem != str->getRawStorage() )
{
memcpy( mem, str->getRawStorage(), str->size() );
str->setRawStorage( mem );
}
str->allocated( str->size() );
}
}
}
void Buffer::reserve( String *str, uint32 size, bool relative, bool block ) const
{
Byte::reserve( str, size, relative, block );
// manipulator is ok
}
void Buffer::destroy( String *str ) const
{
if ( str->allocated() > 0 ) {
memFree( str->getRawStorage() );
str->allocated( 0 );
str->size(0);
}
}
} // namespace csh
//=================================================================
// The string class
//=================================================================
String::String( uint32 size ):
m_class( &csh::handler_buffer ),
m_bExported( false ),
m_bCore( false )
{
m_storage = (byte *) memAlloc( size );
m_allocated = size;
m_size = 0;
}
String::String( const char *data ):
m_class( &csh::handler_static ),
m_allocated( 0 ),
m_storage( (byte*) const_cast< char *>(data) ),
m_bExported( false ),
m_bCore( false )
{
m_size = strlen( data );
}
String::String( const char *data, int32 len ):
m_class( &csh::handler_buffer ),
m_bExported( false ),
m_bCore( false )
{
m_size = len >= 0 ? len : strlen( data );
m_allocated = (( m_size / FALCON_STRING_ALLOCATION_BLOCK ) + 1 ) * FALCON_STRING_ALLOCATION_BLOCK;
m_storage = (byte *) memAlloc( m_allocated );
memcpy( m_storage, data, m_size );
}
String::String( const wchar_t *data ):
m_allocated( 0 ),
m_storage( (byte*) const_cast< wchar_t *>(data) ),
m_bExported( false ),
m_bCore( false )
{
if ( sizeof( wchar_t ) == 2 )
m_class = &csh::handler_static16;
else
m_class = &csh::handler_static32;
uint32 s = 0;
while( data[s] != 0 )
++s;
m_size = s * sizeof( wchar_t );
}
String::String( const wchar_t *data, int32 len ):
m_allocated( 0 ),
m_storage( (byte *) const_cast< wchar_t *>( data ) ),
m_bExported( false ),
m_bCore( false )
{
if ( sizeof( wchar_t ) == 2 )
m_class = &csh::handler_buffer16;
else
m_class = &csh::handler_buffer32;
if ( len >= 0 )
{
m_size = len * sizeof( wchar_t );
}
else
{
uint32 s = 0;
while( data[s] != 0 )
++s;
m_size = s * sizeof( wchar_t );
}
m_allocated = (( m_size / FALCON_STRING_ALLOCATION_BLOCK ) + 1 ) * FALCON_STRING_ALLOCATION_BLOCK;
m_storage = (byte *) memAlloc( m_allocated );
memcpy( m_storage, data, m_size );
}
String::String( const String &other, uint32 begin, uint32 end ):
m_allocated( 0 ),
m_size( 0 ),
m_storage( 0 ),
m_bExported( false ),
m_bCore( false )
{
// by default, copy manipulator
m_class = other.m_class;
if ( other.m_allocated == 0 )
{
if ( other.m_size == 0 ) {
m_size = 0;
m_allocated = 0;
setRawStorage( 0 );
}
else {
if( begin < end )
{
uint32 cs = m_class->charSize();
setRawStorage( other.getRawStorage() + begin * cs );
m_size = (end - begin ) * cs;
if ( m_size > (other.m_size - (begin *cs )) )
m_size = other.m_size - (begin *cs );
}
else {
// reverse substring, we have to bufferize.
other.m_class->subString( &other, begin, end, this );
}
}
}
else
other.m_class->subString( &other, begin, end, this );
}
void String::copy( const String &other )
{
if ( m_allocated != 0 )
m_class->destroy( this );
m_class = other.m_class;
m_size = other.m_size;
m_allocated = other.m_allocated;
if ( m_allocated > 0 ) {
m_storage = (byte *) memAlloc( m_allocated );
if ( m_size > 0 )
memcpy( m_storage, other.m_storage, m_size );
}
else
m_storage = other.m_storage;
}
String &String::adopt( char *buffer, uint32 size, uint32 allocated )
{
if ( m_allocated != 0 )
m_class->destroy( this );
m_class = &csh::handler_buffer;
m_size = size;
m_allocated = allocated;
m_storage = (byte *) buffer;
return *this;
}
String &String::adopt( wchar_t *buffer, uint32 size, uint32 allocated )
{
if ( m_allocated != 0 )
m_class->destroy( this );
if ( sizeof( wchar_t ) == 2 )
m_class = &csh::handler_buffer16;
else
m_class = &csh::handler_buffer32;
m_size = size * sizeof( wchar_t );
m_allocated = allocated;
m_storage = (byte *) buffer;
return *this;
}
int String::compare( const char *other ) const
{
uint32 pos = 0;
uint32 len = length();
while( pos < len )
{
uint32 c1 = getCharAt( pos );
if ( c1 < (uint32) *other )
return -1;
// return greater also if other is ended; we are NOT ended...
else if ( c1 > (uint32) *other || (uint32) *other == 0 )
return 1;
other ++;
pos++;
}
if ( *other != 0 )
// other is greater
return -1;
// the strings are the same
return 0;
}
int String::compareIgnoreCase( const char *other ) const
{
uint32 pos = 0;
uint32 len = length();
while( pos < len )
{
uint32 c1 = getCharAt( pos );
uint32 cmpval = (uint32) *other;
if ( c1 >=0x41 && c1 <= 0x5A )
c1 += 0x20;
if ( cmpval >=0x41 && cmpval <= 0x5A )
cmpval += 0x20;
if ( c1 < cmpval )
return -1;
// return greater also if other is ended; we are NOT ended...
else if ( c1 > cmpval || cmpval == 0 )
return 1;
other ++;
pos++;
}
if ( *other != 0 )
// other is greater
return -1;
// the strings are the same
return 0;
}
int String::compare( const wchar_t *other ) const
{
uint32 pos = 0;
uint32 len = length();
while( pos < len )
{
uint32 c1 = getCharAt( pos );
if ( c1 < (uint32) *other )
return -1;
// return greater also if other is ended; we are NOT ended...
else if ( c1 > (uint32) *other || (uint32) *other == 0 )
return 1;
other ++;
pos++;
}
if ( *other != 0 )
// other is greater
return -1;
// the strings are the same
return 0;
}
int String::compareIgnoreCase( const wchar_t *other ) const
{
uint32 pos = 0;
uint32 len = length();
while( pos < len )
{
uint32 c1 = getCharAt( pos );
uint32 cmpval = (uint32) *other;
if ( c1 >=0x41 && c1 <= 0x5A )
c1 += 0x20;
if ( cmpval >=0x41 && cmpval <= 0x5A )
cmpval += 0x20;
if ( c1 < cmpval )
return -1;
// return greater also if other is ended; we are NOT ended...
else if ( c1 > cmpval || cmpval == 0 )
return 1;
other ++;
pos++;
}
if ( *other != 0 )
// other is greater
return -1;
// the strings are the same
return 0;
}
int String::compare( const String &other ) const
{
uint32 len1 = length();
uint32 len2 = other.length();
uint32 len = len1 > len2 ? len2 : len1;
uint32 pos = 0;
while( pos < len )
{
uint32 c1 = getCharAt( pos );
uint32 c2 = other.getCharAt( pos );
if ( c1 < c2 )
return -1;
// return greater also if other is ended; we are NOT ended...
else if ( c1 > c2 )
return 1;
pos++;
}
// the strings are the same?
if ( len1 < len2 )
return -1;
// return greater also if other is ended; we are NOT ended...
else if ( len1 > len2 )
return 1;
// yes
return 0;
}
int String::compareIgnoreCase( const String &other ) const
{
uint32 len1 = length();
uint32 len2 = other.length();
uint32 len = len1 > len2 ? len2 : len1;
uint32 pos = 0;
while( pos < len )
{
uint32 c1 = getCharAt( pos );
uint32 c2 = other.getCharAt( pos );
if ( c1 >=0x41 && c1 <= 0x5A )
c1 += 0x20;
if ( c2 >=0x41 && c2 <= 0x5A )
c2 += 0x20;
if ( c1 < c2 )
return -1;
// return greater also if other is ended; we are NOT ended...
else if ( c1 > c2 )
return 1;
pos++;
}
// the strings are the same?
if ( len1 < len2 )
return -1;
// return greater also if other is ended; we are NOT ended...
else if ( len1 > len2 )
return 1;
// yes
return 0;
}
uint32 String::toCString( char *target, uint32 bufsize ) const
{
uint32 len = length();
// we already know that the buffer is too small?
if ( bufsize <= len )
return npos;
uint32 pos = 0;
uint32 done = 0;
while( done < len && pos < bufsize )
{
uint32 chr = getCharAt( done );
if ( chr < 0x80 )
{
target[ pos ++ ] = chr;
}
else if ( chr < 0x800 )
{
if ( pos + 2 > bufsize )
return npos;
target[ pos ++ ] = 0xC0 | ((chr >> 6 ) & 0x1f);
target[ pos ++ ] = 0x80 | (0x3f & chr);
}
else if ( chr < 0x10000 )
{
if ( pos + 3 > bufsize )
return npos;
target[ pos ++ ] = 0xE0 | ((chr >> 12) & 0x0f );
target[ pos ++ ] = 0x80 | ((chr >> 6) & 0x3f );
target[ pos ++ ] = 0x80 | (0x3f & chr);
}
else
{
if ( pos + 4 > bufsize )
return npos;
target[ pos ++ ] = 0xF0 | ((chr >> 18) & 0x7 );
target[ pos ++ ] = 0x80 | ((chr >> 12) & 0x3f );
target[ pos ++ ] = 0x80 | ((chr >> 6) & 0x3f );
target[ pos ++ ] = 0x80 | (0x3f & chr );
}
++done;
}
if ( pos >= bufsize )
return npos;
target[pos] = '\0';
return pos;
}
uint32 String::toWideString( wchar_t *target, uint32 bufsize ) const
{
uint32 len = length();
if ( bufsize <= len * sizeof( wchar_t ) )
return npos;
if ( sizeof( wchar_t ) == 2 ) {
for( uint32 i = 0; i < len; i ++ )
{
uint32 chr = getCharAt( i );
if ( chr > 0xFFFF )
target[ i ] = L'?';
else
target[ i ] = chr;
}
}
else {
for( uint32 i = 0; i < len; i ++ )
{
target[ i ] = getCharAt( i );
}
}
target[len] = 0;
return (int32) len;
}
void String::append( const String &str )
{
/* if ( str.size() < FALCON_STRING_ALLOCATION_BLOCK )
m_class->reserve( this, FALCON_STRING_ALLOCATION_BLOCK, true, true ); */
m_class->insert( this, length(), 0, &str );
}
void String::append( uint32 chr )
{
if ( size() >= allocated() )
{
m_class->reserve( this, size() + FALCON_STRING_ALLOCATION_BLOCK, false, true ); // allocates a whole block next
}
// reserve forces to buffer, so we have only to extend
size( size() + m_class->charSize() );
// and insert the last char:
setCharAt( length() - 1, chr );
// if the chr can't fit our size, the whole string will be refitted, including the last
// empty char that we added above.
}
void String::prepend( uint32 chr )
{
if ( size() >= allocated() )
{
m_class->reserve( this, size() + FALCON_STRING_ALLOCATION_BLOCK, false, true ); // allocates a whole block next
}
// reserve forces to buffer, so we have only to extend
memmove( m_storage + m_class->charSize(), m_storage, size() );
size( size() + m_class->charSize() );
// and insert the first char
setCharAt( 0, chr );
}
void String::escape( String &strout ) const
{
internal_escape( strout, false );
}
void String::escapeFull( String &strout ) const
{
internal_escape( strout, true );
}
void String::internal_escape( String &strout, bool full ) const
{
int len = length();
int pos = 0;
strout.m_class->reserve( &strout, len ); // prepare for at least len chars
strout.size( 0 ); // clear target string
while( pos < len )
{
uint32 chat = getCharAt( pos );
switch( chat )
{
case '"':strout += "\\\""; break;
case '\'':strout += "\\\'"; break;
case '\r': strout += "\\r"; break;
case '\n': strout += "\\n"; break;
case '\t': strout += "\\t"; break;
case '\b': strout += "\\b"; break;
case '\\': strout += "\\\\"; break;
default:
if ( chat < 32 || (chat >= 128 && full) ) {
strout += 'x';
strout.writeNumberHex(chat, true );
}
else{
strout += chat;
}
}
pos++;
}
}
void String::unescape()
{
uint32 len = length();
uint32 pos = 0;
while( pos < len )
{
uint32 chat = getCharAt( pos );
if ( chat == (uint32) '\\' )
{
// an escape must take place
uint32 endSub = pos + 1;
if( endSub == len - 1 )
return;
uint32 chnext = getCharAt( endSub );
uint32 chsub=0;
switch( chnext )
{
case '"': chsub = (uint32) '"'; break;
case '\r': chsub = (uint32) '\r'; break;
case '\n': chsub = (uint32) '\n'; break;
case '\t': chsub = (uint32) '\t'; break;
case '\b': chsub = (uint32) '\b'; break;
case '\\': chsub = (uint32) '\\'; break;
case '0':
// parse octal number
endSub ++;
chsub = 0;
// max lenght of octals = 11 chars, + 2 for stubs
while( endSub < len && endSub - pos < 13 )
{
chnext = getCharAt( endSub );
if ( chnext < 0x30 || chnext > 0x37 )
break;
chsub <<= 3; //*8
chsub |= (0x7) & (chnext - 0x30);
endSub ++;
}
break;
case 'x':
// parse exadecimal number
endSub ++;
chsub = 0;
// max lenght of octals = 11 chars, + 2 for stubs
while( endSub < len && endSub - pos < 13 )
{
chnext = getCharAt( endSub );
if ( chnext >= 0x30 && chnext <= 0x39 ) // 0 - 9
{
chsub <<= 4; //*16
chsub |= chnext - 0x30;
}
else if( chnext >= 0x41 && chnext <= 0x46 ) // A - F
{
chsub <<= 4; //*16
chsub |= chnext - 0x41 + 10;
}
else if( chnext >= 0x61 && chnext <= 0x66 ) // a - f
{
chsub <<= 4; //*16
chsub |= chnext - 0x61 + 10;
}
endSub ++;
}
break;
}
// substitute the char
setCharAt( pos, chsub );
// remove the rest
remove( pos + 1, endSub - pos );
}
pos++;
}
}
void String::serialize( Stream *out ) const
{
uint32 size = m_bExported ? m_size | 0x80000000 : m_size;
size = endianInt32( size );
out->write( (byte *) &size, sizeof(size) );
if ( m_size != 0 && out->good() )
{
byte chars = m_class->charSize();
out->write( &chars, 1 );
#ifdef FALCON_LITTLE_ENDIAN
out->write( m_storage, m_size );
#else
// in big endian environ, we have to reverse the code.
if( chars == 1 )
{
out->write( m_storage, m_size );
}
else if ( chars == 2 )
{
for( int i = 0; i < m_size/2; i ++ )
{
uint16 chr = (uint32) endianInt16((uint16) getCharAt( i ) );
out->write( (byte *) &chr, 2 );
if (! out->good() )
return;
}
}
else if ( chars == 4 )
{
for( int i = 0; i < m_size/4; i ++ )
{
uint32 chr = (uint32) endianInt32( getCharAt( i ) );
out->write( (byte *) &chr, 4 );
if (! out->good() )
return;
}
}
#endif
}
}
bool String::deserialize( Stream *in, bool bStatic )
{
uint32 size;
in->read( (byte *) &size, sizeof( size ) );
size = endianInt32(size);
m_bExported = (size & 0x80000000) == 0x80000000;
size = size & 0x7FFFFFFF;
// if the size of the deserialized string is 0, we have an empty string.
if ( size == 0 )
{
m_size = 0;
// if we had something allocated, we got to free it.
if ( m_allocated > 0 )
{
memFree( m_storage );
m_storage = 0;
m_allocated = 0;
}
// anyhow, set the handler to static and return.
manipulator(&csh::handler_static);
return true;
}
if ( in->good() )
{
byte chars;
in->read( &chars, 1 );
// determine the needed manipulator
if ( bStatic )
{
if( m_size < size )
{
return false;
}
m_size = size;
switch( chars )
{
case 1: manipulator( &csh::handler_static ); break;
case 2: manipulator( &csh::handler_static16 ); break;
case 4: manipulator( &csh::handler_static32 ); break;
default: return false;
}
}
else {
switch( chars )
{
case 1: manipulator( &csh::handler_buffer ); break;
case 2: manipulator( &csh::handler_buffer16 ); break;
case 4: manipulator( &csh::handler_buffer32 ); break;
default: return false;
}
m_allocated = m_size = size;
if ( m_storage != 0 )
memFree( m_storage );
m_storage = (byte *) memAlloc( m_allocated );
if( m_storage == 0 )
return false;
}
#ifdef FALCON_LITTLE_ENDIAN
in->read( m_storage, m_size );
#else
// in big endian environ, we have to reverse the code.
in->read( m_storage, m_size );
if ( ! in->good() )
return;
if ( chars == 2 )
{
uint16* storage16 = (uint16*) m_storage;
for( int i = 0; i < m_size/2; i ++ )
{
storage16[i] = (uint16) endianInt16( storage16[i] );
}
}
else if ( chars == 4 )
{
uint32* storage32 = (uint32*) m_storage;
for( int i = 0; i < m_size/4; i ++ )
{
storage32[i] = (uint32) endianInt32( storage32[i] );
}
}
#endif
}
return true;
}
void String::c_ize()
{
if ( allocated() <= size() || getCharAt( length() ) != 0 )
{
append( 0 );
size( size() - m_class->charSize() );
}
}
bool String::setCharSize( uint32 nsize, uint32 subst )
{
// same size?
if ( nsize == m_class->charSize() )
return true;
// change only the manipulator?
if( size() == 0 ) {
m_class->destroy( this ); // dispose anyhow
allocated(0);
switch( nsize ) {
case 1: m_class = &csh::handler_buffer; break;
case 2: m_class = &csh::handler_buffer16; break;
case 4: m_class = &csh::handler_buffer32; break;
default: return false;
}
return true;
}
if ( nsize != 1 && nsize != 2 && nsize != 4 )
return false;
// full change.
// use allocated to decide re-allocation under new char size.
byte *mem = getRawStorage();
uint32 oldcs = m_class->charSize();
uint32 nalloc = (allocated()/oldcs) * nsize;
uint32 oldsize = size();
byte *nmem = (byte*) memAlloc( nalloc );
csh::Base* manipulator = csh::adaptBuffer( mem, 0, oldcs, nmem, 0, nsize, length() );
m_class->destroy( this );
allocated( nalloc );
size( (oldsize/oldcs)*nsize );
m_class = manipulator;
setRawStorage( nmem );
return true;
}
bool String::parseInt( int64 &target, uint32 pos ) const
{
uint32 len = length();
if ( pos >= len )
return false;
target = 0;
bool neg = false;
uint32 chnext = getCharAt( pos );
if ( chnext == (uint32) '-' )
{
neg = true;
pos ++;
if ( pos == len )
return false;
chnext = getCharAt( pos );
}
// detect overflow
int64 tgtCopy = target;
while( chnext >= 0x30 && chnext <= 0x39 )
{
if( target < tgtCopy )
return false;
tgtCopy = target;
target *= 10;
target += chnext - 0x30;
pos ++;
if ( pos == len )
break;
chnext = getCharAt( pos );
}
if ( chnext < 0x30 || chnext > 0x39 )
return false;
if (neg)
target = -target;
return true;
}
bool String::parseDouble( double &target, uint32 pos ) const
{
char buffer[64];
uint32 maxlen = 63;
uint32 len = length();
if ( pos >= len )
return false;
// if we are single byte string, just copy
if ( m_class->charSize() == 1 )
{
if ( maxlen > len - pos )
maxlen = len - pos;
memcpy( buffer, m_storage, maxlen );
buffer[ maxlen ] = '\0';
}
else {
// else convert to C string
uint32 bufpos = 0;
while ( bufpos < maxlen && pos < len )
{
uint32 chr = getCharAt( pos );
if( chr != (uint32) '-' && chr != (uint32) 'e' && chr != (uint32) 'E' &&
chr < 0x30 && chr > 0x39 )
return false;
buffer[ bufpos ] = (char) chr;
bufpos ++;
pos ++;
}
}
// then apply sscanf
if ( sscanf( buffer, "%lf", &target ) == 1 )
return true;
return false;
}
bool String::parseBin( uint64 &target, uint32 pos ) const
{
uint32 len = length();
if ( pos >= len )
return false;
// parse octal number
target = 0;
uint32 endSub = pos;
// max length of binary = 64 chars, + 2 for stubs
while( endSub < len && (endSub - pos < 64) )
{
uint32 chnext = getCharAt( endSub );
if ( chnext < 0x30 || chnext > 0x31 )
break;
target <<= 1; //*2
target |= (0x1) & (chnext - 0x30);
endSub ++;
}
if( endSub != pos )
return true;
return false;
}
bool String::parseOctal( uint64 &target, uint32 pos ) const
{
uint32 len = length();
if ( pos >= len )
return false;
// parse octal number
target = 0;
uint32 endSub = pos;
// max length of octals = 11 chars, + 2 for stubs
while( endSub < len && (endSub - pos < 26) )
{
uint32 chnext = getCharAt( endSub );
if ( chnext < 0x30 || chnext > 0x37 )
break;
target <<= 3; //*8
target |= (0x7) & (chnext - 0x30);
endSub ++;
}
if( endSub != pos )
return true;
return false;
}
bool String::parseHex( uint64 &target, uint32 pos ) const
{
uint32 len = length();
if ( pos >= len )
return false;
// parse octal number
target = 0;
uint32 endSub = pos;
while( endSub < len && (endSub - pos < 16) )
{
uint32 chnext = getCharAt( endSub );
if ( chnext >= 0x30 && chnext <= 0x39 ) // 0 - 9
{
target <<= 4; //*16
target |= chnext - 0x30;
}
else if( chnext >= 0x41 && chnext <= 0x46 ) // A - F
{
target <<= 4; //*16
target |= chnext - 0x41 + 10;
}
else if( chnext >= 0x61 && chnext <= 0x66 ) // a - f
{
target <<= 4; //*16
target |= chnext - 0x61 + 10;
}
endSub ++;
}
if( endSub != pos )
return true;
return false;
}
void String::writeNumber( int64 number )
{
// prepare the buffer
bool neg;
char buffer[21];
uint32 pos = 19;
buffer[20] = '\0';
if ( number == 0 )
{
buffer[pos] = '0';
}
else {
if ( number < 0 )
{
neg = true;
number = - number;
if ( number < 0 )
{
// is NAN
append( "NaN" );
return;
}
}
else
neg = false;
while( number != 0 ) {
buffer[pos--] = (char) ((number % 10) + 0x30);
number /= 10;
}
if ( neg )
buffer[ pos ] = '-';
else
pos++;
}
append( buffer + pos );
}
void String::writeNumberHex( uint64 number, bool uppercase, int ciphers )
{
// prepare the buffer
char buffer[18];
uint32 pos = 16;
buffer[17] = '\0';
byte base = uppercase ? 0x41 : 0x61;
if( ciphers > 16 )
{
ciphers = 16;
}
if ( number == 0 )
{
buffer[pos] = '0';
}
else {
while( number != 0 ) {
byte b = (byte)(number & 0xf);
if ( b <= 9 )
buffer[pos--] = (char) (b + 0x30);
else {
buffer[pos--] = (char) ( b - 10 + base );
}
number >>= 4;
ciphers--;
}
}
while( ciphers > 0 )
{
buffer[pos--] = '0';
ciphers --;
}
append( buffer + pos + 1 );
}
void String::writeNumberOctal( uint64 number )
{
// prepare the buffer
char buffer[32];
uint32 pos = 30;
buffer[31] = '\0';
if ( number == 0 )
{
buffer[pos] = '0';
}
else {
while( number != 0 ) {
buffer[pos--] = (char) ((number & 0x7) + 0x30);
number >>= 3;
}
pos++;
}
append( buffer + pos );
}
void String::writeNumber( int64 number, const String &format )
{
char buffer[64];
char bufFormat[32];
if ( format.toCString( bufFormat, 32 ) == npos )
return;
sprintf( buffer, bufFormat, number );
append( buffer );
}
void String::writeNumber( double number, const String &format )
{
char buffer[64];
char bufFormat[32];
if ( format.toCString( bufFormat, 32 ) == npos )
return;
sprintf( buffer, bufFormat, number );
append( buffer );
}
String &String::bufferize( const String &other )
{
m_class->bufferize( this, &other );
return *this;
}
String &String::bufferize()
{
m_class->bufferize( this );
return *this;
}
void String::trim( int mode )
{
uint32 front = 0;
uint32 len = length();
// modes: 0 = all, 1 = front, 2 = back
// first, trim from behind.
if ( mode == 0 || mode == 2 ) {
while( len > 0 )
{
uint32 chr = getCharAt( len - 1 );
if( chr != ' ' && chr != '\n' && chr != '\r' && chr != '\t' )
{
break;
}
len --;
}
if ( len == 0 )
{
// string is actually empty.
m_size = 0;
return;
}
}
// front trim
if ( mode == 0 || mode == 1 ) {
while( front < len )
{
uint32 chr = getCharAt( front );
if( chr != ' ' && chr != '\n' && chr != '\r' && chr != '\t' )
{
break;
}
++front;
}
}
// front can't be == to len.
if ( front > 0 )
{
// source and dest should be different, but it will work for this configuration.
m_class->subString( this, front, len, this );
}
else {
m_size = len * m_class->charSize();
}
}
void String::lower()
{
uint32 len = length();
for( uint32 i = 0; i < len; i++ )
{
uint32 chr = getCharAt( i );
if ( chr >= 'A' && chr <= 'Z' ) {
setCharAt( i, chr | 0x20 );
}
}
}
void String::upper()
{
uint32 len = length();
for( uint32 i = 0; i < len; i++ )
{
uint32 chr = getCharAt( i );
if ( chr >= 'a' && chr <= 'z' ) {
setCharAt( i, chr & ~0x20 );
}
}
}
bool String::fromUTF8( const char *utf8 )
{
return fromUTF8( utf8, -1 );
}
bool String::fromUTF8( const char *utf8, int len )
{
// destroy old contents
if ( m_allocated )
{
m_class->destroy( this );
m_allocated = 0;
}
m_size = 0;
// empty string?
if ( len == 0 || *utf8 == 0 )
{
m_class = &csh::handler_static;
return true;
}
// start scanning
while ( len != 0 && *utf8 != 0 )
{
uint32 chr = 0;
byte in = (byte) *utf8;
// 4 bytes? -- pattern 1111 0xxx
int count;
if ( (in & 0xF8) == 0xF0 )
{
chr = (in & 0x7 ) << 18;
count = 18;
}
// pattern 1110 xxxx
else if ( (in & 0xF0) == 0xE0 )
{
chr = (in & 0xF) << 12;
count = 12;
}
// pattern 110x xxxx
else if ( (in & 0xE0) == 0xC0 )
{
chr = (in & 0x1F) << 6;
count = 6;
}
else if( in < 0x80 )
{
chr = (uint32) in;
count = 0;
}
// invalid pattern
else {
return false;
}
// read the other characters with pattern 0x10xx xxxx
while( count > 0 )
{
count -= 6;
utf8++;
byte in = (byte) *utf8;
if ( in == 0 ) {
// short utf8 sequence
return false;
}
else if( (in & 0xC0) != 0x80 )
{
// unrecognized pattern, protocol error
return false;
}
chr |= (in & 0x3f) << count;
}
this->append( chr );
utf8++;
--len;
}
return true;
}
bool String::startsWith( const String &str, bool icase ) const
{
uint32 len = str.length();
if ( len > length() ) return false;
if ( icase )
{
for ( uint32 i = 0; i < len; i ++ )
{
uint32 chr1, chr2;
if ( (chr1 = str.getCharAt(i)) != (chr2 = getCharAt(i)) )
{
if ( chr1 >= 'A' && chr1 <= 'z' && (chr1 | 0x20) != (chr2|0x20) )
return false;
}
}
}
else
{
for ( uint32 i = 0; i < len; i ++ )
if ( str.getCharAt(i) != getCharAt(i) )
return false;
}
return true;
}
bool String::endsWith( const String &str, bool icase ) const
{
uint32 len = str.length();
uint32 mlen = length();
uint32 start = mlen-len;
if ( len > mlen ) return false;
if ( icase )
{
for ( uint32 i = 0; i < len; ++i )
{
uint32 chr1, chr2;
if ( (chr1 = str.getCharAt(i)) != (chr2 = getCharAt(i+start)) )
{
if ( ((chr1 >= 'A' && chr1 <= 'Z') || (chr1 >= 'a' && chr1 <= 'z') )
&& (chr1 | 0x20) == (chr2|0x20) )
{
continue;
}
return false;
}
}
}
else
{
for ( uint32 i = 0; i < len; ++i )
if ( str.getCharAt(i) != getCharAt(i+start) )
return false;
}
return true;
}
bool String::wildcardMatch( const String& wildcard, bool bIcase ) const
{
const String* wcard = &wildcard;
const String* cfr = this;
uint32 wpos = 0, wlen = wcard->length();
uint32 cpos = 0, clen = cfr->length();
uint32 wstarpos = 0xFFFFFFFF;
while ( cpos < clen )
{
if( wpos == wlen )
{
// we have failed the match; but if we had a star, we
// may roll back to the starpos and try to match the
// rest of the string
if ( wstarpos != 0xFFFFFFFF )
{
wpos = wstarpos;
}
else {
// no way, we're doomed.
break;
}
}
uint32 wchr = wcard->getCharAt( wpos );
uint32 cchr = cfr->getCharAt( cpos );
switch( wchr )
{
case '?': // match any character
wpos++;
cpos++;
break;
case '*':
{
// mark for restart in case of bad match.
wstarpos = wpos;
// match till the next character
wpos++;
// eat all * in a row
while( wpos < wlen )
{
wchr = wcard->getCharAt( wpos );
if ( wchr != '*' )
break;
wpos++;
}
if ( wpos == wlen )
{
// we have consumed all the chars
cpos = clen;
break;
}
//eat up to next character
while( cpos < clen )
{
cchr = cfr->getCharAt( cpos );
if ( cchr == wchr )
break;
cpos ++;
}
// we have eaten up the same char? -- then advance also wpos to prepare next loop
if ( cchr == wchr )
{
wpos++;
cpos++;
}
// else, everything must stay as it is, so cpos == clen but wpos != wlen causing fail.
}
break;
default:
if ( cchr == wchr ||
( bIcase && cchr < 128 && wchr < 128 && (cchr | 32) == (wchr | 32) )
)
{
cpos++;
wpos++;
}
else
{
// can we retry?
if ( wstarpos != 0xFFFFFFFF )
wpos = wstarpos;
else {
// check failed -- we're doomed
return false;
}
}
}
}
// at the end of the loop, the match is ok only if both the cpos and wpos are at the end
return wpos == wlen && cpos == clen;
}
void String::escapeQuotes()
{
uint32 len = length();
uint32 i = 0;
while( i < len )
{
register uint32 chr = getCharAt(i);
switch( chr )
{
case '\'': case '\"':
insert( i, 0, "\\" );
i+=2;
++len;
break;
default:
++i;
}
}
}
void String::unescapeQuotes()
{
uint32 len = length();
uint32 i = 0;
bool state = false;
while( i < len )
{
register uint32 chr = getCharAt(i);
switch( chr )
{
case '\'': case '\"':
if( state )
{
remove( i-1, 1 );
--len;
}
break;
case '\\':
state = true;
++i;
break;
default:
state = false;
++i;
}
}
}
//============================================================
void string_deletor( void *data )
{
delete (String *) data;
}
}
/* end of cstring.cpp */
|