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
|
/*
FALCON - The Falcon Programming Language.
FILE: string.cpp
Short description
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: ven nov 5 2004
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/*#
@beginmodule core
*/
/** \file
Short description
*/
#include <falcon/module.h>
#include <falcon/vm.h>
#include <falcon/string.h>
#include <falcon/carray.h>
#include <falcon/memory.h>
#include <falcon/membuf.h>
#include <string.h>
/*#
@funset core_string_functions String functions
@brief Functions manipulating strings
@beginset core_string_functions
*/
namespace Falcon {
namespace core {
static void process_strFrontBackParams( VMachine *vm, String* &str, bool &bNumeric, bool &bRemove, int32 &len )
{
Item *i_count;
if ( vm->self().isMethodic() )
{
str = vm->self().asString();
i_count = vm->param(0);
bRemove = vm->param(1) != 0 && vm->param(1)->isTrue();
bNumeric = vm->param(2) != 0 && vm->param(2)->isTrue();
}
else
{
Item *i_str = vm->param(0);
if( i_str == 0 || ! i_str->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).extra( "S,[N,B,B]" ) );
}
str = i_str->asString();
i_count = vm->param(1);
bRemove = vm->param(2) != 0 && vm->param(2)->isTrue();
bNumeric = vm->param(3) != 0 && vm->param(3)->isTrue();
}
if ( i_count == 0 || i_count->isNil() )
{
len = 1;
}
else if ( ! i_count->isOrdinal() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).extra( "S,[N,B,B]" ) );
}
else {
len = (int32) i_count->forceInteger();
}
}
/*#
@method charSize String
@brief Returns or changes the size in bytes in this string.
@optparam bpc New value for bytes per character (1, 2 or 4).
@return This string if @b bpc is given, or the current character size value if not given.
*/
FALCON_FUNC String_charSize( VMachine *vm )
{
Item *i_bpc = vm->param(0);
String* str = vm->self().asString();
if ( i_bpc == 0 )
{
// no parameters -- just read us.
vm->retval( (int64) str->manipulator()->charSize() );
return;
}
else if( ! i_bpc->isOrdinal() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.extra( "[N]" ) );
}
uint32 bpc = (uint32) i_bpc->forceInteger();
if ( ! str->setCharSize( bpc ) )
{
throw new ParamError( ErrorParam( e_param_range, __LINE__ ) );
}
vm->retval( str );
}
/*#
@method front String
@brief Returns the first character in a string.
@optparam count Number of characters to be returned (defaults to 1).
@optparam numeric If true, returns a character value instead of a string.
@optparam remove If true, also remove the character.
@return The first element or nil if the string is empty.
This method returns a string containing one character from the beginning of the string,
or eventually more characters in case a number > 1 is specified in @b count.
If @b remove is true, then the character is removed and the string is shrunk.
@see strFront
If @b numeric is true,
the UNICODE value of the string character will be returned, otherwise the caller
will receive a string containing the desired character. In this case, @b count
parameter will be ignored and only one UNICODE value will be returned.
*/
/*#
@function strFront
@brief Returns the first character(s) in a string.
@param str The string on which to operate.
@optparam count Number of characters to be taken (defaults to 1).
@optparam remove If true, also remove the character.
@optparam numeric If true, returns a character value instead of a string.
@return The first element or nil if the string is empty.
This function returns a string containing one character from the beginning of @b str,
or eventually more characters in case a number > 1 is specified in @b count.
If @b remove is true, then the character is removed and the string is shrunk.
@see String.front
If @b numeric is true,
the UNICODE value of the string character will be returned, otherwise the caller
will receive a string containing the desired character. In this case, @b count
parameter will be ignored and only one UNICODE value will be returned.
*/
FALCON_FUNC mth_strFront( VMachine *vm )
{
String *str;
bool bNumeric;
bool bRemove;
int32 len;
process_strFrontBackParams( vm, str, bNumeric, bRemove, len );
if ( bNumeric )
{
if ( str->size() == 0 )
{
vm->retnil();
}
else
{
vm->retval( (int64) str->getCharAt( 0 ) );
if( bRemove )
str->remove( 0, 1 );
}
}
else
{
if ( len <= 0 ) {
vm->retval( new CoreString( "" ) );
}
else if ( len > (int32) str->length() ) {
vm->retval( new CoreString( *str ) );
}
else {
vm->retval( new CoreString( *str, 0, len ) );
}
if( bRemove )
str->remove(0, len );
}
}
/*#
@method back String
@brief Returns the last character in a string.
@optparam count Number of characters to be taken (defaults to 1).
@optparam numeric If true, returns a character value instead of a string.
@optparam remove If true, remove also the character.
@return The last element or nil if the string is empty.
This function returns a string containing one character from the end of the string,
or eventually more characters in case a number > 1 is specified in @b count.
If @b remove is true, then the character is removed and the string is shrunk.
@see strFront
If @b numeric is true,
the UNICODE value of the string character will be returned, otherwise the caller
will receive a string containing the desired character. In this case, @b count
parameter will be ignored and only one UNICODE value will be returned.
*/
/*#
@function strBack
@brief Returns the last character(s) in a string.
@param str The string on which to operate.
@optparam count Number of characters to be taken (defaults to 1).
@optparam remove If true, also remove the character.
@optparam numeric If true, returns a character value instead of a string.
@return The last element or nil if the string is empty.
This function returns a string containing one character from the end of @b str,
or eventually more characters in case a number > 1 is specified in @b count.
If @b remove is true, then the characters are removed and the string is shrunk.
@see String.back
If @b numeric is true,
the UNICODE value of the string character will be returned, otherwise the caller
will receive a string containing the desired character. In this case, @b count
parameter will be ignored and only one UNICODE value will be returned.
*/
FALCON_FUNC mth_strBack( VMachine *vm )
{
String *str;
bool bNumeric;
bool bRemove;
int32 len;
process_strFrontBackParams( vm, str, bNumeric, bRemove, len );
int32 strLen = str->length();
if ( bNumeric )
{
if ( str->size() == 0 )
{
vm->retnil();
}
else
{
vm->retval( (int64) str->getCharAt( strLen-1 ) );
if( bRemove )
str->remove( strLen-1, 1 );
}
}
else
{
if ( len <= 0 ) {
vm->retval( new CoreString( "" ) );
}
else if ( len >= strLen ) {
vm->retval( new CoreString( *str ) );
}
else {
vm->retval( new CoreString( *str, strLen-len ) );
}
if( bRemove )
str->remove( strLen-len, len );
}
}
/**
@method first String
@brief Returns an iterator to the head of this string.
@return An iterator.
*/
/*FALCON_FUNC String_first( VMachine *vm )
{
Item *itclass = vm->findWKI( "Iterator" );
fassert( itclass != 0 );
CoreObject *iterator = itclass->asClass()->createInstance();
iterator->setProperty( "_pos", Item( 0 ) );
iterator->setProperty( "_origin", vm->self() );
vm->retval( iterator );
}*/
/**
@method last String
@brief Returns an iterator to the tail of this string.
@return An iterator.
*/
/*FALCON_FUNC String_last( VMachine *vm )
{
Item *itclass = vm->findWKI( "Iterator" );
fassert( itclass != 0 );
CoreObject *iterator = itclass->asClass()->createInstance();
String *orig = vm->self().asString();
iterator->setProperty( "_pos", Item( orig->size() == 0 ? 0 : (int64) orig->length() - 1 ) );
iterator->setProperty( "_origin", vm->self() );
vm->retval( iterator );
}*/
/*#
@function strSplitTrimmed
@brief Subdivides a string in an array of substrings given a token substring.
@param string The string that must be split.
@param token The token by which the string should be split.
@optparam count Optional maximum split count.
@return An array of strings containing the split string.
This function returns an array of strings extracted from the given parameter.
The array is filled with strings extracted from the first parameter, by dividing
it based on the occurrences of the token substring. A count parameter may be
provided to limit the splitting, so to take into consideration only the first
relevant tokens. Adjacent matching tokens will be ignored.
If no matches are possible, the returned array contains
at worst a single element containing a copy of the whole string passed as a
parameter.
Contrarily to @a strSplit, this function will "eat up" adjacent tokens. While
@a strSplit is more adequate to parse field-oriented strings (as i.e.
colon separated fields in configuration files) this function is best employed
in word extraction.
@note this function is equivalent to the FBOM method String.splittr
@note See @a Tokenizer for a more adequate function to scan extensively
wide strings.
*/
/*#
@method splittr String
@brief Subdivides a string in an array of substrings given a token substring.
@param token The token by which the string should be split.
@optparam count Optional maximum split count.
@return An array of strings containing the split string.
@see strSplitTrimmed
*/
FALCON_FUNC mth_strSplitTrimmed ( ::Falcon::VMachine *vm )
{
Item *target;
Item *splitstr;
Item *count;
// Parameter checking;
if( vm->self().isMethodic() )
{
target = &vm->self();
splitstr = vm->param(0);
count = vm->param(1);
}
else
{
target = vm->param(0);
splitstr = vm->param(1);
count = vm->param(2);
}
uint32 limit;
if ( target == 0 || ! target->isString()
|| (splitstr != 0 && ! (splitstr->isString() || splitstr->isNil()))
|| ( count != 0 && ! count->isOrdinal() ) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "S, [N]" : "S, S, [N]" ) );
}
limit = count == 0 ? 0xffffffff: (int32) count->forceInteger();
// Parameter extraction.
String *tg_str = target->asString();
uint32 tg_len = target->asString()->length();
// split in chars?
if( splitstr == 0 || splitstr->isNil() || splitstr->asString()->size() == 0)
{
// split the string in an array.
if( limit > tg_len )
limit = tg_len;
CoreArray* ca = new CoreArray( limit );
for( uint32 i = 0; i < limit - 1; ++i )
{
CoreString* elem = new CoreString(1);
elem->append( tg_str->getCharAt(i) );
ca->append( elem );
}
// add remains if there are any
if(limit <= tg_len)
ca->append(tg_str->subString(limit - 1));
vm->retval( ca );
return;
}
String *sp_str = splitstr->asString();
uint32 sp_len = splitstr->asString()->length();
// return item.
CoreArray *retarr = new CoreArray;
// if the split string is empty, return empty string
if ( sp_len == 0 )
{
retarr->append( new CoreString() );
vm->retval( retarr );
return;
}
// if the token is wider than the string, just return the string
if ( tg_len < sp_len )
{
retarr->append( new CoreString( *tg_str ) );
vm->retval( retarr );
return;
}
uint32 pos = 0;
uint32 last_pos = 0;
bool lastIsEmpty = false;
// scan the string
while( limit > 1 && pos <= tg_len - sp_len )
{
uint32 sp_pos = 0;
// skip matching pattern-
while ( tg_str->getCharAt( pos ) == sp_str->getCharAt( sp_pos ) &&
sp_pos < sp_len && pos <= tg_len - sp_len ) {
sp_pos++;
pos++;
}
// a match?
if ( sp_pos == sp_len ) {
// put the item in the array.
uint32 splitend = pos - sp_len;
retarr->append( new CoreString( String( *tg_str, last_pos, splitend ) ) );
lastIsEmpty = (last_pos >= splitend);
last_pos = pos;
limit--;
// skip matching pattern
while( sp_pos == sp_len && pos <= tg_len - sp_len ) {
sp_pos = 0;
last_pos = pos;
while ( tg_str->getCharAt( pos ) == sp_str->getCharAt( sp_pos )
&& sp_pos < sp_len && pos <= tg_len - sp_len ) {
sp_pos++;
pos++;
}
}
pos = last_pos;
}
else
pos++;
}
// Residual element?
// -- but only if we didn't already put a "" in the array
if ( limit >= 1 && ! lastIsEmpty ) {
retarr->append( new CoreString( String( *tg_str, last_pos, (uint32) tg_len ) ) );
}
vm->retval( retarr );
}
/*#
@function strSplit
@brief Subdivides a string in an array of substrings given a token substring.
@param string The string that must be split.
@optparam token The token by which the string should be split.
@optparam count Optional maximum split count.
@return An array of strings containing the split string.
This function returns an array of strings extracted from the given parameter.
The array is filled with strings extracted from the first parameter, by dividing
it based on the occurrences of the token substring. A count parameter may be
provided to limit the splitting, so to take into consideration only the first
relevant tokens. Adjacent matching tokens will cause the returned array to
contains empty strings. If no matches are possible, the returned array contains
at worst a single element containing a copy of the whole string passed as a
parameter.
For example, the following may be useful to parse a INI file where keys are
separated from values by "=" signs:
@code
key, value = strSplit( string, "=", 2 )
@endcode
This code would return an array of 2 items at maximum; if no "=" signs are found
in string, the above code would throw an error because of unpacking size, a
thing that may be desirable in a parsing code. If there are more than one "=" in
the string, only the first starting from left is considered, while the others
are returned in the second item, unparsed.
If the @b token is empty or not given, the string is returned as a sequence of
1-character strings in an array.
@note This function is equivalent to the fbom method @a String.split. The above
example can be rewritten as:
@code
key, value = string.split( "=", 2 )
@endcode
*/
/*#
@method split String
@brief Subdivides a string in an array of substrings given a token substring.
@optparam token The token by which the string should be split.
@optparam count Optional maximum split count.
@return An array of strings containing the split string.
@see strSplit
*/
FALCON_FUNC mth_strSplit ( ::Falcon::VMachine *vm )
{
Item *target;
Item *splitstr;
Item *count;
// Parameter checking;
if( vm->self().isMethodic() )
{
target = &vm->self();
splitstr = vm->param(0);
count = vm->param(1);
}
else
{
target = vm->param(0);
splitstr = vm->param(1);
count = vm->param(2);
}
if ( (target == 0 || ! target->isString())
|| (splitstr != 0 && ! (splitstr->isString() || splitstr->isNil()))
|| ( count != 0 && ! count->isOrdinal() ) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "S, [N]" : "S, S, [N]" ) );
}
// Parameter extraction.
String *tg_str = target->asString();
uint32 tg_len = target->asString()->length();
uint32 limit = count == 0 ? 0xffffffff: (int32) count->forceInteger();
// split in chars?
if( splitstr == 0 || splitstr->isNil() || splitstr->asString()->size() == 0)
{
// split the string in an array.
if( limit > tg_len )
limit = tg_len;
CoreArray* ca = new CoreArray( limit );
for( uint32 i = 0; i < limit - 1; ++i )
{
CoreString* elem = new CoreString(1);
elem->append( tg_str->getCharAt(i) );
ca->append( elem );
}
// add remains if there are any
if(limit <= tg_len)
ca->append(tg_str->subString(limit - 1));
vm->retval( ca );
return;
}
String *sp_str = splitstr->asString();
uint32 sp_len = sp_str->length();
// return item.
CoreArray *retarr = new CoreArray;
// if the split string is empty, return empty string
if ( sp_len == 0 )
{
retarr->append( new CoreString() );
vm->retval( retarr );
return;
}
// if the token is wider than the string, just return the string
if ( tg_len < sp_len )
{
retarr->append( new CoreString( *tg_str ) );
vm->retval( retarr );
return;
}
uint32 pos = 0;
uint32 last_pos = 0;
// scan the string
while( limit > 1 && pos <= tg_len - sp_len )
{
uint32 sp_pos = 0;
// skip matching pattern-
while ( tg_str->getCharAt( pos ) == sp_str->getCharAt( sp_pos ) &&
sp_pos < sp_len && pos <= tg_len - sp_len ) {
sp_pos++;
pos++;
}
// a match?
if ( sp_pos == sp_len ) {
// put the item in the array.
uint32 splitend = pos - sp_len;
retarr->append( new CoreString( *tg_str, last_pos, splitend ) );
last_pos = pos;
limit--;
}
else
pos++;
}
// Residual element?
if ( limit >= 1 || last_pos < tg_len ) {
uint32 splitend = tg_len;
retarr->append( new CoreString( *tg_str, last_pos, splitend ) );
}
vm->retval( retarr );
}
/*#
@function strMerge
@brief Merges an array of strings into a string.
@param array An array of strings to be merged.
@optparam mergeStr A string placed between each merge.
@optparam count Maximum count of merges.
@return The merged string.
The function will return a new string containing the concatenation
of the strings inside the array parameter. If the array is empty,
an empty string is returned. If a mergeStr parameter is given, it
is added to each pair of string merged; mergeStr is never added at
the end of the new string. A count parameter may be specified to
limit the number of elements merged in the array.
The function may be used in this way:
@code
a = strMerge( [ "a", "string", "of", "words" ], " " )
printl( a ) // prints "a string of words"
@endcode
If an element of the array is not a string, an error is raised.
*/
/*#
@method merge String
@brief Merges an array of strings into one.
@param array An array of strings to be merged.
@return This string.
This method works as strMerge, using this string as
separator between the strings in the array.
The function may be used in this way:
@code
a = " ".merge( [ "a", "string", "of", "words" ] )
printl( a ) // prints "a string of words"
@endcode
If an element of the array is not a string, an error is raised.
*/
FALCON_FUNC mth_strMerge ( ::Falcon::VMachine *vm )
{
// Parameter checking;
Item *source = vm->param(0);
Item *mergestr = vm->param(1);
Item *count = vm->param(2);
uint64 limit;
if ( source == 0 || ! source->isArray()
|| ( mergestr != 0 && ! mergestr->isString() )
|| ( count != 0 && ! count->isOrdinal() ) )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "A" : "A,[S],[N]" ) );
}
// Parameter estraction.
limit = count == 0 ? 0xffffffff: count->forceInteger();
String *mr_str;
if( mergestr != 0 )
{
mr_str = mergestr->asString();
}
else
mr_str = vm->self().isMethodic() ? vm->self().asString() : 0;
Item *elements = source->asArray()->items().elements();
uint32 len = source->asArray()->length();
if ( limit < len )
len = (uint32) limit;
CoreString *ts = new CoreString;
// filling the target.
for( uint32 i = 1; i <= len ; i ++ )
{
Item* item = elements+(i-1);
if ( item->isString() )
*ts += *item->asString();
else
{
String temp;
vm->itemToString( temp, item );
*ts += temp;
}
if ( mr_str != 0 && i < len )
ts->append( *mr_str );
ts->reserve( len/i * ts->size() );
}
vm->retval( ts );
}
/*#
@method join String
@brief Joins the parameters into a new string.
@param ...
@return A new string created joining the parameters, left to right.
If this string is not empty, it is copied between each joined string.
For example, the next code separates each value with ", "
@code
> ", ".join( "have", "a", "nice", "day" )
@endcode
If the parameters are not string, a standard @a toString conversion is tried.
*/
FALCON_FUNC String_join ( ::Falcon::VMachine *vm )
{
// Parameter extraction.
CoreString *ts = new CoreString;
String *self = vm->self().asString();
uint32 pc = vm->paramCount();
if ( pc > 0 )
{
Item *head = vm->param(0);
if ( head->isString() )
*ts = *head->asString();
else
vm->itemToString( *ts, head );
ts->bufferize();
for( uint32 i = 1; i < pc; i++ )
{
if( self->size() != 0 )
*ts += *self;
Item *item = vm->param(i);
if ( item->isString() )
*ts += *item->asString();
else
{
String temp;
vm->itemToString( temp, item );
*ts += temp;
}
ts->reserve( pc/i * ts->size() );
}
}
vm->regA().setString( ts );
}
/*#
@function strFind
@brief Finds a substring.
@param string String where the search will take place.
@param needle Substring to search for.
@optparam start Optional position from which to start the search in string.
@optparam end Optional position at which to end the search in string.
@return The position where the substring is found, or -1.
Returns the index in string were needle begins, or -1 if not present. Giving a
start parameter will cause the search to start from the given position up to the
end of the string; if a match can be made at start position, then the the
returned value will be the same as start, so when repeating searches in a string
for all the possible matches, repeat until the result is -1 by adding one to the
returned value and using it as start position for the next search.
If an end position is given, it is used as upper limit for the search, so that
the search is in the interval [start, end-1].
@note This function is equivalent to the fbom method String.find
*/
/*#
@method find String
@brief Finds a substring.
@param needle Substring to search for.
@optparam start Optional position from which to start the search in string.
@optparam end Optional position at which to end the search in string.
@return The position where the substring is found, or -1.
@see strFind
*/
FALCON_FUNC mth_strFind ( ::Falcon::VMachine *vm )
{
// Parameter checking;
Item *target = vm->param(0);
Item *needle = vm->param(1);
Item *start_item = vm->param(2);
Item *end_item = vm->param(3);
if ( vm->self().isMethodic() )
{
target = &vm->self();
needle = vm->param(0);
start_item = vm->param(1);
end_item = vm->param(2);
}
else
{
target = vm->param(0);
needle = vm->param(1);
start_item = vm->param(2);
end_item = vm->param(3);
}
if ( target == 0 || ! target->isString()
|| needle == 0 || ! needle->isString()
|| (start_item != 0 && ! start_item->isOrdinal())
|| (end_item != 0 && ! end_item->isOrdinal())
)
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "S,[N],[N]" : "S,S,[N],[N]" ) );
}
int64 start = start_item == 0 ? 0 : start_item->forceInteger();
int64 end = end_item == 0 ? csh::npos : end_item->forceInteger();
String *sTarget = target->asString();
// negative? -- fix
if ( start < 0 ) end = sTarget->length() + start +1;
// out of range?
if ( start < 0 || start >= sTarget->length() )
{
vm->retval( -1 );
return;
}
if ( end < 0 ) end = sTarget->length() + end+1;
// again < than 0? -- it's out of range.
if ( end < 0 )
{
vm->retval( -1 );
return;
}
uint32 pos = target->asString()->find( *needle->asString(), (uint32) start, (uint32) end );
if ( pos != csh::npos )
vm->retval( (int64)pos );
else
vm->retval( -1 );
}
/*#
@function strBackFind
@brief Finds a substring backwards.
@param string String where the search will take place.
@param needle Substring to search for.
@optparam start Optional position from which to start the search in string.
@optparam end Optional position at which to end the search in string.
@return The position where the substring is found, or -1.
Works exactly as @a strFind, except for the fact that the last match
in the string (or in the specified interval) is returned.
*/
/*#
@method rfind String
@brief Finds a substring backwards.
@param needle Substring to search for.
@optparam start Optional position from which to start the search in string.
@optparam end Optional position at which to end the search in string.
@return The position where the substring is found, or -1.
@see strBackFind
*/
FALCON_FUNC mth_strBackFind ( ::Falcon::VMachine *vm )
{
// Parameter checking;
Item *target = vm->param(0);
Item *needle = vm->param(1);
Item *start_item = vm->param(2);
Item *end_item = vm->param(3);
if ( vm->self().isMethodic() )
{
target = &vm->self();
needle = vm->param(0);
start_item = vm->param(1);
end_item = vm->param(2);
}
else
{
target = vm->param(0);
needle = vm->param(1);
start_item = vm->param(2);
end_item = vm->param(3);
}
if ( target == 0 || ! target->isString()
|| needle == 0 || ! needle->isString()
|| (start_item != 0 && ! start_item->isOrdinal())
|| (end_item != 0 && ! end_item->isOrdinal())
)
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "S,[N],[N]" : "S,S,[N],[N]" ) );
}
int64 start = start_item == 0 ? 0 : (int64) start_item->forceInteger();
int64 end = end_item == 0 ? csh::npos : (int64) end_item->forceInteger();
String *sTarget = target->asString();
// negative? -- fix
if ( start < 0 ) end = sTarget->length() + start +1;
// out of range?
if ( start < 0 || start >= sTarget->length() )
{
vm->retval( -1 );
return;
}
if ( end < 0 ) end = sTarget->length() + end+1;
// again < than 0? -- it's out of range.
if ( end < 0 )
{
vm->retval( -1 );
return;
}
uint32 pos = target->asString()->rfind( *needle->asString(), (uint32) start, (uint32) end );
if ( pos != csh::npos )
vm->retval( (int)pos );
else
vm->retval( -1 );
}
/*#
@method rtrim String
@brief Trims trailing whitespaces in a string.
@optparam trimSet A set of characters that must be removed.
@return The trimmed version of the string.
@raise AccessError if the given item is not a string.
A new string, which is a copy of the original one with all characters in @b trimSet
at the end of the string removed, is returned. If @b trimSet is not supplied, it
defaults to space, tabulation characters, new lines and carriage returns. The
original string is unmodified.
*/
/*#
@method ftrim String
@brief Trims front whitespaces in a string.
@optparam trimSet A set of characters that must be removed.
@return The trimmed version of the string.
@raise AccessError if the given item is not a string.
A new string, which is a copy of the original one with all characters in @b trimSet
at the beginning of the string removed, is returned. If @b trimSet is not supplied, it
defaults to space, tabulation characters, new lines and carriage returns. The
original string is unmodified.
@see strFrontTrim
*/
/*#
@method trim String
@brief Trims whitespaces from both ends of a string.
@optparam trimSet A set of characters that must be removed.
@return The trimmed version of the string.
@raise AccessError if the given item is not a string.
A new string, which is a copy of the original one with all characters in @b trimSet
at both ends of the string removed, is returned. If @b trimSet is not supplied, it
defaults to space, tabulation characters, new lines and carriage returns. The
original string is unmodified.
@see strTrim
*/
/*#
@function strTrim
@brief Removes the white spaces at the beginning and at the end of a string.
@param string The string to be trimmed.
@optparam trimSet A set of characters that must be removed.
@return The trimmed substring.
A new string, which is a copy of the original one with all characters in @b trimSet
at the end of the string removed, is returned. If @b trimSet is not supplied, it
defaults to space, tabulation characters, new lines and carriage returns. The
original string is unmodified.
*/
FALCON_FUNC mth_strTrim ( ::Falcon::VMachine *vm )
{
String *self;
Item *trimChars;
if ( vm->self().isMethodic() )
{
self = vm->self().asString();
trimChars = vm->param(0);
}
else
{
Item *i_str = vm->param( 0 );
if ( i_str == 0 || ! i_str->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
}
self = i_str->asString();
trimChars = vm->param(1);
}
CoreString *cs = new CoreString( *self );
cs->garbage().mark( vm->generation() );
if ( trimChars == 0 || trimChars->isNil() ) {
cs->trim();
vm->retval( cs );
}
else if ( ! trimChars->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
}
else
{
String *trim = trimChars->asString();
int32 tLen = trim->length();
int32 len = cs->length();
int32 start = 0;
int32 end = len;
uint32 chr;
int found = 0;
while( start < len )
{
found = 0;
chr = cs->getCharAt( start );
for ( int32 tIdx=0; tIdx < tLen; tIdx++ )
if ( chr == trim->getCharAt( tIdx ) )
found = 1;
if ( found == 0 )
break;
start++;
}
while( end > start )
{
found = 0;
chr = cs->getCharAt( end - 1 );
for ( int32 tIdx=0; tIdx < tLen; tIdx++ )
if ( chr == trim->getCharAt( tIdx ) )
found = 1;
if ( found == 0 )
break;
end--;
}
// an empty string if set is empty
vm->retval( cs->subString( start, end ) );
}
}
/*#
@function strFrontTrim
@brief Removes white spaces from the front of the string.
@param string The string to be trimmed.
@optparam trimSet A set of characters that must be removed.
@return The trimmed substring.
A new string, which is a copy of the original one with all characters in @b trimSet
at the beginning of the string removed, is returned. If @b trimSet is not supplied, it
defaults to space, tabulation characters, new lines and carriage returns. The
original string is unmodified.
*/
FALCON_FUNC mth_strFrontTrim ( ::Falcon::VMachine *vm )
{
String *self;
Item *trimChars;
if ( vm->self().isMethodic() )
{
self = vm->self().asString();
trimChars = vm->param(0);
}
else
{
Item *i_str = vm->param( 0 );
if ( i_str == 0 || ! i_str->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
}
self = i_str->asString();
trimChars = vm->param(1);
}
if (trimChars == 0 ) {
CoreString *cs = new CoreString( *self );
cs->frontTrim();
vm->retval( cs );
}
else if ( ! trimChars->isString() ) {
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
}
else {
int pos = 0;
int32 len = self->length();
String *trim = trimChars->asString();
int32 tLen = trim->length();
while( pos < len )
{
uint32 chr = self->getCharAt( pos );
int found = 0;
for ( int32 tIdx = 0; tIdx < tLen; tIdx++ )
if ( chr == trim->getCharAt( tIdx ) )
found = 1;
if ( found == 0 )
break;
pos++;
}
// has something to be trimmed?
if ( pos < len )
vm->retval( new CoreString( self->subString( pos, len ) ) );
else
vm->retval( new CoreString );
}
}
/*#
@function strBackTrim
@brief Removes white spaces at both the beginning and the end of the string.
@param string The string to be trimmed.
@optparam trimSet A set of characters that must be removed.
@return The trimmed substring.
A new string, which is a copy of the original one with all characters in @b trimSet
at the beginning and at the end of the string removed, is returned.
If @b trimSet is not supplied, it defaults to space, tabulation characters,
new lines and carriage returns. The original string is unmodified.
*/
FALCON_FUNC mth_strBackTrim ( ::Falcon::VMachine *vm )
{
String *self;
Item *trimChars;
if ( vm->self().isMethodic() )
{
self = vm->self().asString();
trimChars = vm->param(0);
}
else
{
Item *i_str = vm->param( 0 );
if ( i_str == 0 || ! i_str->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
}
self = i_str->asString();
trimChars = vm->param(1);
}
if ( trimChars == 0 ) {
CoreString *cs = new CoreString( *self );
cs->backTrim();
vm->retval( cs );
}
else if ( ! trimChars->isString() ) {
throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
}
else
{
int32 pos = self->length()-1;
String *trim = trimChars->asString();
int32 tLen = trim->length();
while ( pos >= 0 )
{
uint32 chr = self->getCharAt( pos );
int found = 0;
for ( int32 tIdx=0; tIdx < tLen; tIdx++ )
if ( chr == trim->getCharAt( tIdx ) )
found = 1;
if ( found == 0 )
break;
pos--;
}
// has something to be trimmed?
if ( pos >= 0)
vm->retval( new CoreString( self->subString( 0, pos + 1 ) ) );
else
vm->retval( new CoreString );
}
}
/*#
@function strReplace
@brief Replaces the all the occurrences of a substring with another one.
@param string The string where the replace will take place.
@param substr The substring that will be replaced.
@param repstr The string that will take the place of substr.
@optparam start Optional start position in the string.
@optparam end Optional end position in the string.
@return A copy of the string with the occourences of the searched substring replaced.
This is a flexible function that allows to alter a string by changing all the
occurrences of a substring into another one. If the start parameter is given,
the search and replacement will take place only starting at the specified
position up to the end of the string, and if the end parameter is also provided,
the search will take place in the interval [start, end-1].
*/
/*#
@method replace String
@brief Replaces the all the occurrences of a substring with another one.
@param substr The substring that will be replaced.
@param repstr The string that will take the place of substr.
@optparam start Optional start position in the string.
@optparam end Optional end position in the string.
@return A copy of the string with the occourences of the searched substring replaced.
@see strReplace
*/
FALCON_FUNC mth_strReplace ( ::Falcon::VMachine *vm )
{
// Parameter checking;
Item *target;
Item *needle;
Item *replacer;
Item *start_item;
Item *end_item;
if( vm->self().isMethodic() )
{
target = &vm->self();
needle = vm->param(0);
replacer = vm->param(1);
start_item = vm->param(2);
end_item = vm->param(3);
}
else
{
target = vm->param(0);
needle = vm->param(1);
replacer = vm->param(2);
start_item = vm->param(3);
end_item = vm->param(4);
}
if ( target == 0 || ! target->isString()
|| needle == 0 || ! needle->isString()
|| replacer == 0 || ! replacer->isString()
|| (start_item != 0 && ! start_item->isOrdinal())
|| (end_item != 0 && ! end_item->isOrdinal())
)
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "S,S,[N],[N]" : "S,S,S,[N],[N]" ) );
}
// Parameter estraction.
String *tg_str = target->asString();
uint32 tg_len = target->asString()->length();
String *ned_str = needle->asString();
int32 ned_len = (int32) needle->asString()->length();
// Is the needle is empty
if ( ned_len == 0 ) {
// shallow copy the target
vm->retval( *target );
return;
}
String *rep_str = replacer->asString();
uint32 rep_len = replacer->asString()->length();
int32 start = start_item ? (int32) start_item->forceInteger(): 0;
if ( start < 0 ) start = 0;
int32 end = end_item ? (int32) end_item->forceInteger(): tg_len-1;
if ( end >= (int32) tg_len ) end = tg_len-1;
CoreString *ret = new CoreString;
if ( start > 0 )
ret->append( String( *tg_str, 0, start ) );
int32 old_start = start;
while ( start <= end )
{
int32 ned_pos = 0;
int32 pos = 0;
// skip matching pattern
while ( tg_str->getCharAt( start + pos ) == ned_str->getCharAt( ned_pos )
&& ned_pos < (int32) ned_len && start + ned_pos <= end )
{
ned_pos++;
pos++;
}
// a match?
if ( ned_pos == ned_len )
{
if ( start > old_start ) {
ret->append( String( *tg_str, old_start, start ) );
}
if ( rep_len > 0 ) {
ret->append( *rep_str );
}
start += ned_len;
old_start = start;
}
else
start++;
}
if ( old_start < (int32)tg_len )
ret->append( String( *tg_str, old_start ) );
vm->retval( ret );
}
/*#
@function strReplicate
@brief Returns a new string that is created by replicating the original one.
@param string The string to be replicaeted.
@param times Number of times the string must be replicated.
@return The new string.
A nice shortcut. Also, this function performs the work efficiently,
preallocating the needed space in one shot and avoiding the need
to grow the string while replicating the original value.
*/
/*#
@method replicate String
@brief Returns a new string that is created by replicating this one.
@param times Number of times the string must be replicated.
@return The new string.
A nice shortcut. Also, this function performs the work efficiently,
preallocating the needed space in one shot and avoiding the need
to grow the string while replicating the original value.
*/
FALCON_FUNC mth_strReplicate ( ::Falcon::VMachine *vm )
{
// Parameter checking;
Item *strrep;
Item *qty;
if ( vm->self().isMethodic() )
{
strrep = &vm->self();
qty = vm->param(0);
}
else
{
strrep = vm->param(0);
qty = vm->param(1);
}
if ( strrep == 0 || ! strrep->isString()
|| qty == 0 || ! qty->isOrdinal() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "N" : "S,N" ) );
}
int32 repl = (int32) qty->forceInteger();
String *replicated = strrep->asString();
int32 len = replicated->size() * repl;
if ( len <= 0 ) {
vm->retval( new CoreString("") );
return;
}
CoreString *target = new CoreString;
target->reserve( len );
int pos = 0;
while ( pos < len ) {
memcpy( target->getRawStorage() + pos, replicated->getRawStorage(), replicated->size() );
pos+= replicated->size();
}
target->manipulator( const_cast<Falcon::csh::Base*>(replicated->manipulator()->bufferedManipulator()) );
target->size( len );
vm->retval( target );
}
/*#
@function strBuffer
@brief Pre-allocates an empty string.
@param size Size of the pre-allocated string.
@return The new string.
The returned string is an empty string, and equals to "". However, the required
size is pre-allocated, and addition to this string (i.e. += operators)
takes place in a fraction of the time otherwise required, up tho the filling
of the pre-allocated buffer. Also, this string can be fed into file functions,
the pre-allocation size being used as the input read size.
*/
FALCON_FUNC strBuffer ( ::Falcon::VMachine *vm )
{
// Parameter checking;
Item *qty = vm->param(0);
if ( qty == 0 || ! qty->isOrdinal() ) {
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra("N") );
}
int32 size = (int32) qty->forceInteger();
if ( size <= 0 ) {
throw new ParamError( ErrorParam( e_param_range, __LINE__ ).origin( e_orig_runtime ) );
}
vm->retval( new CoreString( String( size ) ) );
}
/*#
@function strUpper
@brief Returns an upper case version of the string.
@param string String that must be uppercased.
@return The uppercased string.
All the Latin characters in the string are turned uppercase. Other characters
are left untouched.
*/
/*#
@method upper String
@brief Returns an upper case version of this string.
@return The uppercased string.
All the Latin characters in the string are turned uppercase. Other characters
are left untouched.
*/
FALCON_FUNC mth_strUpper ( ::Falcon::VMachine *vm )
{
Item *source;
// Parameter checking;
if ( vm->self().isMethodic() )
{
source = &vm->self();
}
else
{
source = vm->param(0);
if ( source == 0 || ! source->isString() ) {
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( "S" ) );
}
}
String *src = source->asString();
if ( src->size() == 0 )
{
vm->retval( new CoreString );
}
else {
CoreString *target = new CoreString( *src );
target->upper();
vm->retval( target );
}
}
/*#
@function strLower
@brief Returns a lowercase version of the given string.
@param string String that must be lowercased.
@return The lowercased string.
All the Latin characters in the string are turned lowercase. Other characters
are left untouched.
*/
/*#
@method lower String
@brief Returns a lowercase version of this string.
@return The lowercased string.
All the Latin characters in the string are turned lowercase. Other characters
are left untouched.
*/
FALCON_FUNC mth_strLower ( ::Falcon::VMachine *vm )
{
Item *source;
// Parameter checking;
if ( vm->self().isMethodic() )
{
source = &vm->self();
}
else
{
source = vm->param(0);
if ( source == 0 || ! source->isString() ) {
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( "S" ) );
}
}
String *src = source->asString();
if ( src->size() == 0 )
{
vm->retval( new CoreString );
}
else {
CoreString *target = new CoreString( *src );
target->lower();
vm->retval( target );
}
}
/*#
@method startsWith String
@brief Check if a strings starts with a substring.
@param token The substring that will be compared with this string.
@optparam icase If true, pefroms a case neutral check
@return True if @b token matches the beginning of this string, false otherwise.
This method performs a comparation check at the beginning of the string.
If this string starts with @b token, the function returns true. If @b token
is larger than the string, the method will always return false, and
if @b token is an empty string, it will always match.
The optional parameter @b icase can be provided as true to have this
method to perform a case insensitive match.
*/
/*#
@function strStartsWith
@brief Check if a strings starts with a substring.
@param string The string that is going to be tested for the given token.
@param token The substring that will be compared with this string.
@optparam icase If true, pefroms a case neutral check
@return True if @b token matches the beginning of @b string, false otherwise.
This functioin performs a comparation check at the beginning of the @b string.
If this string starts with @b token, the function returns true. If @b token
is larger than the string, the function will always return false, and
if @b token is an empty string, it will always match.
The optional parameter @b icase can be provided as true to have this
function to perform a case insensitive match.
*/
FALCON_FUNC mth_strStartsWith ( ::Falcon::VMachine *vm )
{
Item* source;
Item* i_token;
Item* i_icase;
// Parameter checking;
if ( vm->self().isMethodic() )
{
source = &vm->self();
i_token = vm->param(0);
i_icase = vm->param(1);
}
else
{
source = vm->param(0);
i_token = vm->param(1);
i_icase = vm->param(2);
}
if ( source == 0 || ! source->isString() ||
i_token == 0 || ! i_token->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "S,[B]" : "S,S,[B]" ) );
}
String *src = source->asString();
vm->regA().setBoolean( src->startsWith( *i_token->asString(), i_icase ? i_icase->isTrue():false ) );
}
/*#
@method endsWith String
@brief Check if a strings ends with a substring.
@param token The substring that will be compared with this string.
@optparam icase If true, pefroms a case neutral check
@return True if @b token matches the end of this string, false otherwise.
This method performs a comparation check at the end of the string.
If this string ends with @b token, the function returns true. If @b token
is larger than the string, the method will always return false, and
if @b token is an empty string, it will always match.
The optional parameter @b icase can be provided as true to have this
method to perform a case insensitive match.
*/
/*#
@function strEndsWith
@brief Check if a strings ends with a substring.
@param string The string that is going to be tested for the given token.
@param token The substring that will be compared with this string.
@optparam icase If true, pefroms a case neutral check
@return True if @b token matches the end of @b string, false otherwise.
This functioin performs a comparation check at the end of the @b string.
If this string ends with @b token, the function returns true. If @b token
is larger than the string, the function will always return false, and
if @b token is an empty string, it will always match.
The optional parameter @b icase can be provided as true to have this
function to perform a case insensitive match.
*/
FALCON_FUNC mth_strEndsWith ( ::Falcon::VMachine *vm )
{
Item* source;
Item* i_token;
Item* i_icase;
// Parameter checking;
if ( vm->self().isMethodic() )
{
source = &vm->self();
i_token = vm->param(0);
i_icase = vm->param(1);
}
else
{
source = vm->param(0);
i_token = vm->param(1);
i_icase = vm->param(2);
}
if ( source == 0 || ! source->isString() ||
i_token == 0 || ! i_token->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "S,[B]" : "S,S,[B]" ) );
}
String *src = source->asString();
vm->regA().setBoolean( src->endsWith( *i_token->asString(), i_icase ? i_icase->isTrue() : false ) );
}
/*#
@function strCmpIgnoreCase
@brief Performs a lexicographic comparation of two strings, ignoring character case.
@param string1 First string to be compared.
@param string2 Second string to be compared.
@return -1, 0 or 1.
The two strings are compared ignoring the case of latin characters contained in
the strings.
If the first string is greater than the second, the function returns a number
less than 0. If it's
smaller, it returns a positive number. If the two strings are the same,
ignoring the case of the characters, 0 is returned.
*/
/*#
@method cmpi String
@brief Performs a lexicographic comparation of two strings, ignoring character case.
@param string Second string to be compared with this one.
@return <0, 0 or >0, respectively if this string is less, equal or greater than
the @b string parameter.
The two strings are compared ignoring the case of latin characters contained in
the strings.
If the first string is greater than the second, the function returns a number
less than 0. If it's
smaller, it returns a positive number. If the two strings are the same,
ignoring the case of the characters, 0 is returned.
*/
FALCON_FUNC mth_strCmpIgnoreCase ( ::Falcon::VMachine *vm )
{
Item *s1_itm;
Item *s2_itm;
// Parameter checking;
if( vm->self().isMethodic() )
{
s1_itm = &vm->self();
s2_itm = vm->param(0);
}
else
{
s1_itm = vm->param(0);
s2_itm = vm->param(1);
}
if ( s1_itm == 0 || ! s1_itm->isString()
|| s2_itm == 0 || !s2_itm->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "S" : "S,S" ) );
}
String *str1 = s1_itm->asString();
String *str2 = s2_itm->asString();
int32 len1 = str1->length();
int32 len2 = str2->length();
int32 minlen = len1 > len2 ? len2 : len1;
for( int32 i = 0; i < minlen; i ++ )
{
int32 elem1 = str1->getCharAt( i );
int32 elem2 = str2->getCharAt( i );
if ( elem1 >= 'A' && elem1 <= 'Z' )
elem1 |= 0x20;
if ( elem2 >= 'A' && elem2 <= 'Z' )
elem2 |= 0x20;
if ( elem1 > elem2 ) {
vm->retval( 1 );
return;
}
if ( elem1 < elem2 ) {
vm->retval( -1 );
return;
}
}
if ( len1 > len2 ) {
vm->retval( 1 );
return;
}
if ( len1 < len2 ) {
vm->retval( -1 );
return;
}
// same!!!
vm->retval( 0 );
}
inline void internal_escape( int mode, VMachine* vm )
{
Item *i_string;
Item *i_onplace;
// Parameter checking;
if( vm->self().isMethodic() )
{
i_string = &vm->self();
i_onplace = vm->param(0);
}
else
{
i_string = vm->param(0);
i_onplace = vm->param(1);
if ( i_string == 0 || ! i_string->isString() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( "S,B" ) );
}
}
String* str;
if ( mode != 2 && i_onplace != 0 && i_onplace->isTrue() )
{
str = i_string->asString();
}
else
{
str = new CoreString( *i_string->asString() );
}
switch( mode )
{
case 0: //esq
str->escapeQuotes();
break;
case 1: //unesq
str->unescapeQuotes();
break;
case 2: //escape
{
CoreString* other = new CoreString( str->size() );
if ( i_onplace != 0 && i_onplace->isTrue() ) // actually it means "complete"
str->escapeFull( *other );
else
str->escape( *other );
vm->retval( other );
}
return;
case 3: //unescape
str->unescape();
break;
}
vm->retval( str );
}
/*#
@function strEsq
@brief Escape quotes in the given string.
@param string the String to be escaped.
@optparam inplace if true, the source string is modified, saving memory.
@return A new escaped string, if @b inplace is not given, or the @b string parameter
if @b inplace is true.
@see String.esq
@see strUnesq
*/
/*#
@method esq String
@brief Escapes the quotes in this string.
@optparam inplace if true, the source string is modified, saving memory.
@return A new escaped string, if @b inplace is not given, or this same string
if @b inplace is true.
@see String.unesq
@see strEsq
*/
FALCON_FUNC mth_strEsq ( ::Falcon::VMachine *vm )
{
internal_escape( 0, vm );
}
/*#
@function strUnesq
@brief Unescape the quotes in given string.
@param string the String to be unescaped.
@optparam inplace if true, the source string is modified, saving memory.
@return A new unescaped string, if @b inplace is not given, or the @b string parameter
if @b inplace is true.
This function transforms all the occourences of '\\"' and '\\'' into a double or
single quote, leaving all the other special escape sequences untouched.
@see String.unesq
@see strEsq
*/
/*#
@method unesq String
@brief Escapes the quotes in this string.
@optparam inplace if true, the source string is modified, saving memory.
@return A new escaped string, if @b inplace is not given, or this same string
if @b inplace is true.
@see String.esq
@see strUnesq
*/
FALCON_FUNC mth_strUnesq ( ::Falcon::VMachine *vm )
{
internal_escape( 1, vm );
}
/*#
@function strEscape
@brief Escape quotes and special characters in the string
@param string the String to be escaped.
@optparam full If true, characters above UNICODE 127 are escaped as well.
@return A new escaped string.
@see String.esq
@see strUnesq
*/
/*#
@method escape String
@brief Escapes all the special characters in the string.
@optparam full If true, characters above UNICODE 127 are escaped as well.
@return A new escaped string.
@see String.esq
@see strEsq
@see strUnescape
*/
FALCON_FUNC mth_strEscape ( ::Falcon::VMachine *vm )
{
internal_escape( 2, vm );
}
/*#
@function strUnescape
@brief Unescape quotes and special characters in the string
@param string the String to be escaped.
@optparam inplace if true, the source string is modified, saving memory.
@return A new unescaped string, if @b inplace is not given, or the @b string parameter
if @b inplace is true.
@see String.esq
@see strUnesq
@see String.unescape
*/
/*#
@method unescape String
@brief Unescapes all the special characters in the string.
@optparam inplace if true, the source string is modified, saving memory.
@return A new unescaped string, if @b inplace is not given, or the @b string parameter
if @b inplace is true.
@see String.esq
@see strEsq
@see strEscape
*/
FALCON_FUNC mth_strUnescape ( ::Falcon::VMachine *vm )
{
internal_escape( 3, vm );
}
/*#
@function strWildcardMatch
@brief Perform an old-style file-like jolly-based wildcard match.
@param string String that must match the wildcard.
@param wildcard A wildcard, possibly but not necessarily including a jolly character.
@optparam ignoreCase If true, the latin 26 base letters case is ignored in matches.
@return True if the string matches, false otherwise.
This function matches a wildcard that may contain jolly "*" or "?" characters against a
string, eventually ignoring the case of the characters. This is a practical function
to match file names against given patterns. A "?" in the wildcard represents any
single character, while a "*" represents an arbitrary sequence of characters.
The wildcard must match completely the given string for the function to return true.
For example:
- "*" matches everything
- "a?b" matches "aab", "adb" and so on
- "a*b" matches "ab", "annnb" and so on
@see String.wmatch
*/
/*#
@method wmatch String
@brief Perform an old-style file-like jolly-based wildcard match.
@param wildcard A wildcard, possibly but not necessarily including a jolly character.
@optparam ignoreCase If true, the latin 26 base letters case is ignored in matches.
@return True if the string matches, false otherwise.
This function matches a wildcard that may contain jolly "*" or "?" characters against a
string, eventually ignoring the case of the characters. This is a practical function
to match file names against given patterns. A "?" in the wildcard represents any
single character, while a "*" represents an arbitrary sequence of characters.
The wildcard must match completely the given string for the function to return true.
For example:
- "*" matches everything
- "a?b" matches "aab", "adb" and so on
- "a*b" matches "ab", "annnb" and so on
@see strWildcardMatch
*/
FALCON_FUNC mth_strWildcardMatch ( ::Falcon::VMachine *vm )
{
// Parameter checking;
Item *s1_itm = vm->param(0);
Item *s2_itm = vm->param(1);
Item *i_bIcase = vm->param(2);
if ( vm->self().isMethodic() )
{
s1_itm = &vm->self();
s2_itm = vm->param(0);
i_bIcase = vm->param(1);
}
else
{
s1_itm = vm->param(0);
s2_itm = vm->param(1);
i_bIcase = vm->param(2);
}
if ( s1_itm == 0 || ! s1_itm->isString() || s2_itm == 0 || !s2_itm->isString() ) {
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "S,[B]" : "S,S,[B]") );
}
// Ignore case?
bool bIcase = i_bIcase == 0 ? false : i_bIcase->isTrue();
// The first is the wildcard, the second is the matched thing.
String *cfr = s1_itm->asString();
String *wcard = s2_itm->asString();
vm->regA().setBoolean( cfr->wildcardMatch( *wcard, bIcase ) );
}
/*#
@function strToMemBuf
@brief Convets a string into a Memory Buffer
@param string String to be converted in a membuf.
@optparam wordWidth The memory buffer word width (defaults to string character size).
@return The resulting membuf.
This function creates a membuf from a string. The resulting membuf
has the same word width of the original string, which may be 1, 2 or 4
byte wide depending on the size needed to store its contents. It is possible
to specify a different word width; in that case the function will be much
less efficient (each character must be copied).
If wordWidth is set to zero, the resulting memory buffer will have 1 byte
long elements, but the content of the string will be copied as-is, bytewise,
regardless of its character size.
*/
/*#
@method toMemBuf String
@brief Convets this string into a Memory Buffer
@optparam wordWidth The memory buffer word width (defaults to string character size).
@return The resulting membuf.
This function creates a membuf from a string. The resulting membuf
has the same word width of the original string, which may be 1, 2 or 4
byte wide depending on the size needed to store its contents. It is possible
to specify a different word width; in that case the function will be much
less efficient (each character must be copied).
If wordWidth is set to zero, the resulting memory buffer will have 1 byte
long elements, but the content of the string will be copied as-is, bytewise,
regardless of its character size.
*/
FALCON_FUNC mth_strToMemBuf ( ::Falcon::VMachine *vm )
{
Item *i_string;
Item *i_wordWidth;
// Parameter checking;
if ( vm->self().isMethodic() )
{
i_string = &vm->self();
i_wordWidth = vm->param(0);
}
else
{
i_string = vm->param(0);
i_wordWidth = vm->param(1);
}
if( i_string == 0 || ! i_string->isString()
|| ( i_wordWidth != 0 && ! i_wordWidth->isOrdinal() )
)
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "[N]" : "S,[N]" ) );
}
String *string = i_string->asString();
int charSize = string->manipulator()->charSize();
int64 ww = i_wordWidth == 0 ? charSize : i_wordWidth->forceInteger();
MemBuf *result;
if ( ww == 0 )
{
result = new MemBuf_1( string->size() );
memcpy( result->data(), string->getRawStorage(), string->size() );
}
else
{
result = MemBuf::create( charSize, string->length() );
if ( result == 0 )
{
throw new ParamError( ErrorParam( e_param_range, __LINE__ )
.origin( e_orig_runtime )
.extra("0-4") );
}
if ( ww == charSize )
{
memcpy( result->data(), string->getRawStorage(), string->size() );
}
else
{
uint32 size = string->size();
for( uint32 p = 0; p < size; p++ )
{
result->set( p, string->getCharAt(p) );
}
}
}
vm->retval( result );
}
/*#
@function strFromMemBuf
@brief Convets a MemBuf to a string.
@param membuf A MemBuf that will be converted to a string.
@return The resulting string.
This string takes each element of the membuf and converts it into
a character in the final string. The contents of the buffer are
not transcoded. It is appropriate to say that this function considers
each element in the MemBuf as an Unicode value for the character in the
final string.
To create a string from a buffer that may come from an encoded source
(i.e. a file), use directly Transcode functions.
*/
FALCON_FUNC strFromMemBuf ( ::Falcon::VMachine *vm )
{
// Parameter checking;
Item *i_membuf = vm->param(0);
if( i_membuf == 0 || ! i_membuf->isMemBuf() )
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra("M") );
}
MemBuf *mb = i_membuf->asMemBuf();
// preallocating size instead of len, we won't have to resize the memory even
// if resizing character sizes.
CoreString *result = new CoreString( mb->size() );
uint32 size = mb->length();
for( uint32 p = 0; p < size; p++ )
{
result->append( mb->get( p ) );
}
vm->retval( result );
}
/*#
@function strFill
@brief Fills a string with a given character or substring.
@param string The string to be filled.
@param chr The character (unicode value) or substring used to refill the @b string.
@return The string itself.
This function fills the physical storage of the given string with a single
character or a repeated substring. This can be useful to clean a string used repeatedly
as input buffer.
The function returns the same string that has been passed as the parameter.
*/
/*#
@method fill String
@brief Fills a string with a given character or substring.
@param chr The character (unicode value) or substring used to refill this string.
@return The string itself.
This method fills the physical storage of the given string with a single
character or a repeated substring. This can be useful to clean a string used repeatedly
as input buffer.
*/
FALCON_FUNC mth_strFill ( ::Falcon::VMachine *vm )
{
Item *i_string;
Item *i_chr;
// Parameter checking;
if ( vm->self().isMethodic() )
{
i_string = &vm->self();
i_chr = vm->param(0);
}
else
{
i_string = vm->param(0);
i_chr = vm->param(1);
}
if( i_string == 0 || ! i_string->isString()
|| i_chr == 0 || ( ! i_chr->isOrdinal() && !i_chr->isString())
)
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->self().isMethodic() ? "N|S" : "S,N|S" ) );
}
CoreString *string = i_string->asCoreString();
if ( i_chr->isOrdinal() )
{
uint32 chr = (uint32) i_chr->forceInteger();
for( uint32 i = 0; i < string->length(); i ++ )
{
string->setCharAt( i, chr );
}
}
else
{
String* rep = i_chr->asString();
if ( rep->length() == 0 )
{
throw new ParamError( ErrorParam( e_param_range, __LINE__ )
.origin( e_orig_runtime )
.extra( vm->moduleString( rtl_string_empty ) ) );
}
uint32 pos = 0;
uint32 pos2 = 0;
while( pos < string->length() )
{
string->setCharAt( pos++, rep->getCharAt( pos2++ ) );
if ( pos2 >= rep->length() )
{
pos2 = 0;
}
}
}
vm->retval( string );
}
}}
/* end of string.cpp */
|