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
|
/*
* re2 (https://github.com/mudge/re2)
* Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
* backtracking regular expression engines like those used in PCRE, Perl, and
* Python".
*
* Copyright (c) 2010, Paul Mucur (https://mudge.name)
* Released under the BSD Licence, please see LICENSE.txt
*/
#include <stdint.h>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <re2/re2.h>
#include <re2/set.h>
#include <ruby.h>
#include <ruby/encoding.h>
#define BOOL2RUBY(v) (v ? Qtrue : Qfalse)
typedef struct {
RE2 *pattern;
} re2_pattern;
typedef struct {
re2::StringPiece *matches;
int number_of_matches;
VALUE regexp, text;
} re2_matchdata;
typedef struct {
re2::StringPiece *input;
int number_of_capturing_groups;
bool eof;
VALUE regexp, text;
} re2_scanner;
typedef struct {
RE2::Set *set;
} re2_set;
VALUE re2_mRE2, re2_cRegexp, re2_cMatchData, re2_cScanner, re2_cSet,
re2_eSetMatchError, re2_eSetUnsupportedError, re2_eRegexpUnsupportedError;
/* Symbols used in RE2 options. */
static ID id_utf8, id_posix_syntax, id_longest_match, id_log_errors,
id_max_mem, id_literal, id_never_nl, id_case_sensitive,
id_perl_classes, id_word_boundary, id_one_line, id_unanchored,
id_anchor, id_anchor_start, id_anchor_both, id_exception,
id_submatches, id_startpos, id_endpos;
inline VALUE encoded_str_new(const char *str, long length, RE2::Options::Encoding encoding) {
if (encoding == RE2::Options::EncodingUTF8) {
return rb_utf8_str_new(str, length);
}
VALUE string = rb_str_new(str, length);
rb_enc_associate_index(string, rb_enc_find_index("ISO-8859-1"));
return string;
}
static void parse_re2_options(RE2::Options* re2_options, const VALUE options) {
if (TYPE(options) != T_HASH) {
rb_raise(rb_eArgError, "options should be a hash");
}
VALUE utf8 = rb_hash_aref(options, ID2SYM(id_utf8));
if (!NIL_P(utf8)) {
re2_options->set_encoding(RTEST(utf8) ? RE2::Options::EncodingUTF8 : RE2::Options::EncodingLatin1);
}
VALUE posix_syntax = rb_hash_aref(options, ID2SYM(id_posix_syntax));
if (!NIL_P(posix_syntax)) {
re2_options->set_posix_syntax(RTEST(posix_syntax));
}
VALUE longest_match = rb_hash_aref(options, ID2SYM(id_longest_match));
if (!NIL_P(longest_match)) {
re2_options->set_longest_match(RTEST(longest_match));
}
VALUE log_errors = rb_hash_aref(options, ID2SYM(id_log_errors));
if (!NIL_P(log_errors)) {
re2_options->set_log_errors(RTEST(log_errors));
}
VALUE max_mem = rb_hash_aref(options, ID2SYM(id_max_mem));
if (!NIL_P(max_mem)) {
re2_options->set_max_mem(NUM2INT(max_mem));
}
VALUE literal = rb_hash_aref(options, ID2SYM(id_literal));
if (!NIL_P(literal)) {
re2_options->set_literal(RTEST(literal));
}
VALUE never_nl = rb_hash_aref(options, ID2SYM(id_never_nl));
if (!NIL_P(never_nl)) {
re2_options->set_never_nl(RTEST(never_nl));
}
VALUE case_sensitive = rb_hash_aref(options, ID2SYM(id_case_sensitive));
if (!NIL_P(case_sensitive)) {
re2_options->set_case_sensitive(RTEST(case_sensitive));
}
VALUE perl_classes = rb_hash_aref(options, ID2SYM(id_perl_classes));
if (!NIL_P(perl_classes)) {
re2_options->set_perl_classes(RTEST(perl_classes));
}
VALUE word_boundary = rb_hash_aref(options, ID2SYM(id_word_boundary));
if (!NIL_P(word_boundary)) {
re2_options->set_word_boundary(RTEST(word_boundary));
}
VALUE one_line = rb_hash_aref(options, ID2SYM(id_one_line));
if (!NIL_P(one_line)) {
re2_options->set_one_line(RTEST(one_line));
}
}
static void re2_matchdata_mark(void *ptr) {
re2_matchdata *m = reinterpret_cast<re2_matchdata *>(ptr);
rb_gc_mark_movable(m->regexp);
rb_gc_mark(m->text);
}
static void re2_matchdata_compact(void *ptr) {
re2_matchdata *m = reinterpret_cast<re2_matchdata *>(ptr);
m->regexp = rb_gc_location(m->regexp);
}
static void re2_matchdata_free(void *ptr) {
re2_matchdata *m = reinterpret_cast<re2_matchdata *>(ptr);
if (m->matches) {
delete[] m->matches;
}
xfree(m);
}
static size_t re2_matchdata_memsize(const void *ptr) {
const re2_matchdata *m = reinterpret_cast<const re2_matchdata *>(ptr);
size_t size = sizeof(*m);
if (m->matches) {
size += sizeof(*m->matches) * m->number_of_matches;
}
return size;
}
static const rb_data_type_t re2_matchdata_data_type = {
"RE2::MatchData",
{
re2_matchdata_mark,
re2_matchdata_free,
re2_matchdata_memsize,
re2_matchdata_compact
},
0,
0,
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
// macro to update VALUE references, as to trigger write barriers.
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};
static void re2_scanner_mark(void *ptr) {
re2_scanner *s = reinterpret_cast<re2_scanner *>(ptr);
rb_gc_mark_movable(s->regexp);
rb_gc_mark(s->text);
}
static void re2_scanner_compact(void *ptr) {
re2_scanner *s = reinterpret_cast<re2_scanner *>(ptr);
s->regexp = rb_gc_location(s->regexp);
}
static void re2_scanner_free(void *ptr) {
re2_scanner *s = reinterpret_cast<re2_scanner *>(ptr);
if (s->input) {
delete s->input;
}
xfree(s);
}
static size_t re2_scanner_memsize(const void *ptr) {
const re2_scanner *s = reinterpret_cast<const re2_scanner *>(ptr);
size_t size = sizeof(*s);
if (s->input) {
size += sizeof(*s->input);
}
return size;
}
static const rb_data_type_t re2_scanner_data_type = {
"RE2::Scanner",
{
re2_scanner_mark,
re2_scanner_free,
re2_scanner_memsize,
re2_scanner_compact
},
0,
0,
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
// macro to update VALUE references, as to trigger write barriers.
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};
static void re2_regexp_free(void *ptr) {
re2_pattern *p = reinterpret_cast<re2_pattern *>(ptr);
if (p->pattern) {
delete p->pattern;
}
xfree(p);
}
static size_t re2_regexp_memsize(const void *ptr) {
const re2_pattern *p = reinterpret_cast<const re2_pattern *>(ptr);
size_t size = sizeof(*p);
if (p->pattern) {
size += sizeof(*p->pattern);
}
return size;
}
static const rb_data_type_t re2_regexp_data_type = {
"RE2::Regexp",
{
0,
re2_regexp_free,
re2_regexp_memsize,
},
0,
0,
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
// macro to update VALUE references, as to trigger write barriers.
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};
static VALUE re2_matchdata_allocate(VALUE klass) {
re2_matchdata *m;
return TypedData_Make_Struct(klass, re2_matchdata, &re2_matchdata_data_type,
m);
}
static VALUE re2_scanner_allocate(VALUE klass) {
re2_scanner *c;
return TypedData_Make_Struct(klass, re2_scanner, &re2_scanner_data_type, c);
}
/*
* Returns a frozen copy of the text supplied when matching.
*
* If the text was already a frozen string, returns the original.
*
* @return [String] a frozen string with the text supplied when matching
* @example
* m = RE2::Regexp.new('(\d+)').partial_match("bob 123")
* m.string #=> "bob 123"
*/
static VALUE re2_matchdata_string(const VALUE self) {
re2_matchdata *m;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
return m->text;
}
/*
* Returns a frozen copy of the text supplied when incrementally matching with
* {RE2::Regexp#scan}.
*
* If the text was already a frozen string, returns the original.
*
* @return [String] a frozen string with the text passed to {RE2::Regexp#scan}
* @example
* c = RE2::Regexp.new('(\d+)').scan("foo")
* c.string #=> "foo"
*/
static VALUE re2_scanner_string(const VALUE self) {
re2_scanner *c;
TypedData_Get_Struct(self, re2_scanner, &re2_scanner_data_type, c);
return c->text;
}
/*
* Returns whether the {RE2::Scanner} has consumed all input or not.
*
* @return [Boolean] whether the {RE2::Scanner} has consumed all input or not
* @example
* c = RE2::Regexp.new('(\d+)').scan("foo")
* c.eof? #=> true
*/
static VALUE re2_scanner_eof(const VALUE self) {
re2_scanner *c;
TypedData_Get_Struct(self, re2_scanner, &re2_scanner_data_type, c);
return BOOL2RUBY(c->eof);
}
/*
* Rewind the {RE2::Scanner} to the start of the string.
*
* @example
* s = RE2::Regexp.new('(\d+)').scan("1 2 3")
* e = s.to_enum
* e.scan #=> ["1"]
* e.scan #=> ["2"]
* s.rewind
* e.scan #=> ["1"]
*/
static VALUE re2_scanner_rewind(VALUE self) {
re2_scanner *c;
TypedData_Get_Struct(self, re2_scanner, &re2_scanner_data_type, c);
delete c->input;
c->input = new(std::nothrow) re2::StringPiece(
RSTRING_PTR(c->text), RSTRING_LEN(c->text));
if (c->input == 0) {
rb_raise(rb_eNoMemError,
"not enough memory to allocate StringPiece for input");
}
c->eof = false;
return self;
}
/*
* Scan the given text incrementally for matches using
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L447-L463
* `FindAndConsume`}, returning an array of submatches on each subsequent
* call. Returns `nil` if no matches are found or an empty array for every
* match if the pattern has no capturing groups.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @return [Array<String>] if the pattern has capturing groups
* @return [[]] if the pattern does not have capturing groups
* @return [nil] if no matches are found
* @example
* s = RE2::Regexp.new('(\w+)').scan("Foo bar baz")
* s.scan #=> ["Foo"]
* s.scan #=> ["bar"]
*/
static VALUE re2_scanner_scan(VALUE self) {
re2_pattern *p;
re2_scanner *c;
TypedData_Get_Struct(self, re2_scanner, &re2_scanner_data_type, c);
TypedData_Get_Struct(c->regexp, re2_pattern, &re2_regexp_data_type, p);
std::vector<RE2::Arg> argv(c->number_of_capturing_groups);
std::vector<RE2::Arg*> args(c->number_of_capturing_groups);
std::vector<re2::StringPiece> matches(c->number_of_capturing_groups);
if (c->eof) {
return Qnil;
}
re2::StringPiece::size_type original_input_size = c->input->size();
for (int i = 0; i < c->number_of_capturing_groups; ++i) {
argv[i] = &matches[i];
args[i] = &argv[i];
}
if (RE2::FindAndConsumeN(c->input, *p->pattern, args.data(),
c->number_of_capturing_groups)) {
re2::StringPiece::size_type new_input_size = c->input->size();
bool input_advanced = new_input_size < original_input_size;
VALUE result = rb_ary_new2(c->number_of_capturing_groups);
for (int i = 0; i < c->number_of_capturing_groups; ++i) {
if (matches[i].empty()) {
rb_ary_push(result, Qnil);
} else {
rb_ary_push(result, encoded_str_new(matches[i].data(),
matches[i].size(),
p->pattern->options().encoding()));
}
}
/* Check whether we've exhausted the input yet. */
c->eof = new_input_size == 0;
/* If the match didn't advance the input, we need to do this ourselves. */
if (!input_advanced && new_input_size > 0) {
c->input->remove_prefix(1);
}
return result;
} else {
return Qnil;
}
}
static re2::StringPiece *re2_matchdata_find_match(VALUE idx, const VALUE self) {
re2_matchdata *m;
re2_pattern *p;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
TypedData_Get_Struct(m->regexp, re2_pattern, &re2_regexp_data_type, p);
int id;
if (RB_INTEGER_TYPE_P(idx)) {
id = NUM2INT(idx);
} else if (SYMBOL_P(idx)) {
const std::map<std::string, int>& groups = p->pattern->NamedCapturingGroups();
std::map<std::string, int>::const_iterator search = groups.find(rb_id2name(SYM2ID(idx)));
if (search != groups.end()) {
id = search->second;
} else {
return NULL;
}
} else {
StringValue(idx);
const std::map<std::string, int>& groups = p->pattern->NamedCapturingGroups();
std::map<std::string, int>::const_iterator search = groups.find(std::string(RSTRING_PTR(idx), RSTRING_LEN(idx)));
if (search != groups.end()) {
id = search->second;
} else {
return NULL;
}
}
if (id >= 0 && id < m->number_of_matches) {
re2::StringPiece *match = &m->matches[id];
if (!match->empty()) {
return match;
}
}
return NULL;
}
/*
* Returns the number of elements in the {RE2::MatchData} (including the
* overall match, submatches and any `nils`).
*
* @return [Integer] the number of elements
* @example
* m = RE2::Regexp.new('(\d+)').match("bob 123")
* m.size #=> 2
* m.length #=> 2
*/
static VALUE re2_matchdata_size(const VALUE self) {
re2_matchdata *m;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
return INT2FIX(m->number_of_matches);
}
/*
* Returns the offset of the start of the nth element of the {RE2::MatchData}.
*
* @param [Integer, String, Symbol] n the name or number of the submatch
* @return [Integer, nil] the offset of the start of the match or `nil` if
* there is no such submatch
* @example
* m = RE2::Regexp.new('ob (\d+)').match("bob 123")
* m.begin(0) #=> 1
* m.begin(1) #=> 4
*/
static VALUE re2_matchdata_begin(const VALUE self, VALUE n) {
re2_matchdata *m;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
re2::StringPiece *match = re2_matchdata_find_match(n, self);
if (match == NULL) {
return Qnil;
} else {
long offset = match->data() - RSTRING_PTR(m->text);
return LONG2NUM(rb_str_sublen(m->text, offset));
}
}
/*
* Returns the offset of the character following the end of the nth element of
* the {RE2::MatchData}.
*
* @param [Integer, String, Symbol] n the name or number of the match
* @return [Integer, nil] the offset of the character following the end of the
* match or `nil` if there is no such match
* @example
* m = RE2::Regexp.new('ob (\d+) b').match("bob 123 bob")
* m.end(0) #=> 9
* m.end(1) #=> 7
*/
static VALUE re2_matchdata_end(const VALUE self, VALUE n) {
re2_matchdata *m;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
re2::StringPiece *match = re2_matchdata_find_match(n, self);
if (match == NULL) {
return Qnil;
} else {
long offset = (match->data() - RSTRING_PTR(m->text)) + match->size();
return LONG2NUM(rb_str_sublen(m->text, offset));
}
}
/*
* Returns the {RE2::Regexp} used in the match.
*
* @return [RE2::Regexp] the regular expression used in the match
* @example
* m = RE2::Regexp.new('(\d+)').match("bob 123")
* m.regexp #=> #<RE2::Regexp /(\d+)/>
*/
static VALUE re2_matchdata_regexp(const VALUE self) {
re2_matchdata *m;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
return m->regexp;
}
/*
* Returns the {RE2::Regexp} used in the {RE2::Scanner}.
*
* @return [RE2::Regexp] the regular expression used in the {RE2::Scanner}
* @example
* c = RE2::Regexp.new('(\d+)').scan("bob 123")
* c.regexp #=> #<RE2::Regexp /(\d+)/>
*/
static VALUE re2_scanner_regexp(const VALUE self) {
re2_scanner *c;
TypedData_Get_Struct(self, re2_scanner, &re2_scanner_data_type, c);
return c->regexp;
}
static VALUE re2_regexp_allocate(VALUE klass) {
re2_pattern *p;
return TypedData_Make_Struct(klass, re2_pattern, &re2_regexp_data_type, p);
}
/*
* Returns the array of matches including the overall match, submatches and any
* `nil`s.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @return [Array<String, nil>] the array of matches
* @example
* m = RE2::Regexp.new('(\d+)').match("bob 123")
* m.to_a #=> ["123", "123"]
*/
static VALUE re2_matchdata_to_a(const VALUE self) {
re2_matchdata *m;
re2_pattern *p;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
TypedData_Get_Struct(m->regexp, re2_pattern, &re2_regexp_data_type, p);
VALUE array = rb_ary_new2(m->number_of_matches);
for (int i = 0; i < m->number_of_matches; ++i) {
re2::StringPiece *match = &m->matches[i];
if (match->empty()) {
rb_ary_push(array, Qnil);
} else {
rb_ary_push(array, encoded_str_new(match->data(), match->size(),
p->pattern->options().encoding()));
}
}
return array;
}
static VALUE re2_matchdata_nth_match(int nth, const VALUE self) {
re2_matchdata *m;
re2_pattern *p;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
TypedData_Get_Struct(m->regexp, re2_pattern, &re2_regexp_data_type, p);
if (nth < 0 || nth >= m->number_of_matches) {
return Qnil;
} else {
re2::StringPiece *match = &m->matches[nth];
if (match->empty()) {
return Qnil;
} else {
return encoded_str_new(match->data(), match->size(),
p->pattern->options().encoding());
}
}
}
static VALUE re2_matchdata_named_match(const std::string &name, const VALUE self) {
re2_matchdata *m;
re2_pattern *p;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
TypedData_Get_Struct(m->regexp, re2_pattern, &re2_regexp_data_type, p);
const std::map<std::string, int>& groups = p->pattern->NamedCapturingGroups();
std::map<std::string, int>::const_iterator search = groups.find(name);
if (search != groups.end()) {
return re2_matchdata_nth_match(search->second, self);
} else {
return Qnil;
}
}
/*
* Retrieve zero, one or more matches by index or name.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @overload [](index)
* Access a particular match by index.
*
* @param [Integer] index the index of the match to fetch
* @return [String, nil] the specified match or `nil` if it isn't present
* @example
* m = RE2::Regexp.new('(\d+)').match("bob 123")
* m[0] #=> "123"
*
* @overload [](start, length)
* Access a range of matches by starting index and length.
*
* @param [Integer] start the index from which to start
* @param [Integer] length the number of elements to fetch
* @return [Array<String, nil>] the specified matches
* @example
* m = RE2::Regexp.new('(\d+)').match("bob 123")
* m[0, 1] #=> ["123"]
*
* @overload [](range)
* Access a range of matches by index.
*
* @param [Range] range the range of match indexes to fetch
* @return [Array<String, nil>] the specified matches
* @example
* m = RE2::Regexp.new('(\d+)').match("bob 123")
* m[0..1] #=> "[123", "123"]
*
* @overload [](name)
* Access a particular match by name.
*
* @param [String, Symbol] name the name of the match to fetch
* @return [String, nil] the specific match or `nil` if it isn't present
* @example
* m = RE2::Regexp.new('(?P<number>\d+)').match("bob 123")
* m["number"] #=> "123"
* m[:number] #=> "123"
*/
static VALUE re2_matchdata_aref(int argc, VALUE *argv, const VALUE self) {
VALUE idx, rest;
rb_scan_args(argc, argv, "11", &idx, &rest);
if (TYPE(idx) == T_STRING) {
return re2_matchdata_named_match(
std::string(RSTRING_PTR(idx), RSTRING_LEN(idx)), self);
} else if (SYMBOL_P(idx)) {
return re2_matchdata_named_match(rb_id2name(SYM2ID(idx)), self);
} else if (!NIL_P(rest) || !RB_INTEGER_TYPE_P(idx) || NUM2INT(idx) < 0) {
return rb_ary_aref(argc, argv, re2_matchdata_to_a(self));
} else {
return re2_matchdata_nth_match(NUM2INT(idx), self);
}
}
/*
* Returns the entire matched string.
*
* @return [String] the entire matched string
*/
static VALUE re2_matchdata_to_s(const VALUE self) {
return re2_matchdata_nth_match(0, self);
}
/*
* Returns a printable version of the match.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @return [String] a printable version of the match
* @example
* m = RE2::Regexp.new('(\d+)').match("bob 123")
* m.inspect #=> "#<RE2::MatchData \"123\" 1:\"123\">"
*/
static VALUE re2_matchdata_inspect(const VALUE self) {
re2_matchdata *m;
re2_pattern *p;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
TypedData_Get_Struct(m->regexp, re2_pattern, &re2_regexp_data_type, p);
std::ostringstream output;
output << "#<RE2::MatchData";
for (int i = 0; i < m->number_of_matches; ++i) {
output << " ";
if (i > 0) {
output << i << ":";
}
VALUE match = re2_matchdata_nth_match(i, self);
if (match == Qnil) {
output << "nil";
} else {
output << "\"";
output.write(RSTRING_PTR(match), RSTRING_LEN(match));
output << "\"";
}
}
output << ">";
return encoded_str_new(output.str().data(), output.str().length(),
p->pattern->options().encoding());
}
/*
* Returns the array of submatches for pattern matching.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is
* undefined).
*
* @return [Array<String, nil>] the array of submatches
* @example
* m = RE2::Regexp.new('(\d+)').match("bob 123")
* m.deconstruct #=> ["123"]
*
* @example pattern matching
* case RE2::Regexp.new('(\d+) (\d+)').match("bob 123 456")
* in x, y
* puts "Matched #{x} #{y}"
* else
* puts "Unrecognised match"
* end
*/
static VALUE re2_matchdata_deconstruct(const VALUE self) {
re2_matchdata *m;
re2_pattern *p;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
TypedData_Get_Struct(m->regexp, re2_pattern, &re2_regexp_data_type, p);
VALUE array = rb_ary_new2(m->number_of_matches - 1);
for (int i = 1; i < m->number_of_matches; ++i) {
re2::StringPiece *match = &m->matches[i];
if (match->empty()) {
rb_ary_push(array, Qnil);
} else {
rb_ary_push(array, encoded_str_new(match->data(), match->size(),
p->pattern->options().encoding()));
}
}
return array;
}
/*
* Returns a hash of capturing group names to submatches for pattern matching.
*
* As this is used by Ruby's pattern matching, it will return an empty hash if given
* more keys than there are capturing groups. Given keys will populate the hash in
* order but an invalid name will cause the hash to be immediately returned.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @return [Hash] a hash of capturing group names to submatches
* @param [Array<Symbol>, nil] keys an array of `Symbol` capturing group names
* or `nil` to return all names
* @example
* m = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc')
* m.deconstruct_keys(nil) #=> {numbers: "123", letters: "abc"}
* m.deconstruct_keys([:numbers]) #=> {numbers: "123"}
* m.deconstruct_keys([:fruit]) #=> {}
* m.deconstruct_keys([:letters, :fruit]) #=> {letters: "abc"}
*
* @example pattern matching
* case RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc')
* in numbers:, letters:
* puts "Numbers: #{numbers}, letters: #{letters}"
* else
* puts "Unrecognised match"
* end
*/
static VALUE re2_matchdata_deconstruct_keys(const VALUE self, const VALUE keys) {
re2_matchdata *m;
re2_pattern *p;
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, m);
TypedData_Get_Struct(m->regexp, re2_pattern, &re2_regexp_data_type, p);
const std::map<std::string, int>& groups = p->pattern->NamedCapturingGroups();
VALUE capturing_groups = rb_hash_new();
if (NIL_P(keys)) {
for (std::map<std::string, int>::const_iterator it = groups.begin(); it != groups.end(); ++it) {
rb_hash_aset(capturing_groups,
ID2SYM(rb_intern(it->first.data())),
re2_matchdata_nth_match(it->second, self));
}
} else {
Check_Type(keys, T_ARRAY);
if (p->pattern->NumberOfCapturingGroups() >= RARRAY_LEN(keys)) {
for (int i = 0; i < RARRAY_LEN(keys); ++i) {
VALUE key = rb_ary_entry(keys, i);
Check_Type(key, T_SYMBOL);
const char *name = rb_id2name(SYM2ID(key));
std::map<std::string, int>::const_iterator search = groups.find(name);
if (search != groups.end()) {
rb_hash_aset(capturing_groups, key, re2_matchdata_nth_match(search->second, self));
} else {
break;
}
}
}
}
return capturing_groups;
}
/*
* Shorthand to compile a new {RE2::Regexp}.
*
* @see RE2::Regexp#initialize
*/
static VALUE re2_re2(int argc, VALUE *argv, VALUE) {
return rb_class_new_instance(argc, argv, re2_cRegexp);
}
/*
* Returns a new {RE2::Regexp} object with a compiled version of
* `pattern` stored inside.
*
* @overload initialize(pattern)
* Returns a new {RE2::Regexp} object with a compiled version of
* `pattern` stored inside with the default options.
*
* @param [String] pattern the pattern to compile
* @return [RE2::Regexp] a {RE2::Regexp} with the specified pattern
* @raise [TypeError] if the given pattern can't be coerced to a `String`
* @raise [NoMemoryError] if memory could not be allocated for the compiled
* pattern
*
* @overload initialize(pattern, options)
* Returns a new {RE2::Regexp} object with a compiled version of
* `pattern` stored inside with the specified options.
*
* @param [String] pattern the pattern to compile
* @param [Hash] options the options with which to compile the pattern
* @option options [Boolean] :utf8 (true) text and pattern are UTF-8; otherwise Latin-1
* @option options [Boolean] :posix_syntax (false) restrict regexps to POSIX egrep syntax
* @option options [Boolean] :longest_match (false) search for longest match, not first match
* @option options [Boolean] :log_errors (true) log syntax and execution errors to ERROR
* @option options [Integer] :max_mem approx. max memory footprint of RE2
* @option options [Boolean] :literal (false) interpret string as literal, not regexp
* @option options [Boolean] :never_nl (false) never match `\n`, even if it is in regexp
* @option options [Boolean] :case_sensitive (true) match is case-sensitive (regexp can override with `(?i)` unless in `posix_syntax` mode)
* @option options [Boolean] :perl_classes (false) allow Perl's `\d` `\s` `\w` `\D` `\S` `\W` when in `posix_syntax` mode
* @option options [Boolean] :word_boundary (false) allow `\b` `\B` (word boundary and not) when in `posix_syntax` mode
* @option options [Boolean] :one_line (false) `^` and `$` only match beginning and end of text when in `posix_syntax` mode
* @return [RE2::Regexp] a {RE2::Regexp} with the specified pattern and options
* @raise [TypeError] if the given pattern can't be coerced to a `String`
* @raise [NoMemoryError] if memory could not be allocated for the compiled pattern
*/
static VALUE re2_regexp_initialize(int argc, VALUE *argv, VALUE self) {
VALUE pattern, options;
re2_pattern *p;
rb_scan_args(argc, argv, "11", &pattern, &options);
/* Ensure pattern is a string. */
StringValue(pattern);
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
if (RTEST(options)) {
RE2::Options re2_options;
parse_re2_options(&re2_options, options);
p->pattern = new(std::nothrow) RE2(
re2::StringPiece(RSTRING_PTR(pattern), RSTRING_LEN(pattern)), re2_options);
} else {
p->pattern = new(std::nothrow) RE2(
re2::StringPiece(RSTRING_PTR(pattern), RSTRING_LEN(pattern)));
}
if (p->pattern == 0) {
rb_raise(rb_eNoMemError, "not enough memory to allocate RE2 object");
}
return self;
}
/*
* Returns a printable version of the regular expression.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is
* undefined).
*
* @return [String] a printable version of the regular expression
* @example
* re2 = RE2::Regexp.new("woo?")
* re2.inspect #=> "#<RE2::Regexp /woo?/>"
*/
static VALUE re2_regexp_inspect(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
std::ostringstream output;
output << "#<RE2::Regexp /" << p->pattern->pattern() << "/>";
return encoded_str_new(output.str().data(), output.str().length(),
p->pattern->options().encoding());
}
/*
* Returns a string version of the regular expression.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @return [String] a string version of the regular expression
* @example
* re2 = RE2::Regexp.new("woo?")
* re2.to_s #=> "woo?"
*/
static VALUE re2_regexp_to_s(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return encoded_str_new(p->pattern->pattern().data(),
p->pattern->pattern().size(),
p->pattern->options().encoding());
}
/*
* Returns whether or not the regular expression was compiled successfully.
*
* @return [Boolean] whether or not compilation was successful
* @example
* re2 = RE2::Regexp.new("woo?")
* re2.ok? #=> true
*/
static VALUE re2_regexp_ok(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->ok());
}
/*
* Returns whether or not the regular expression was compiled with the `utf8`
* option set to `true`.
*
* @return [Boolean] the `utf8` option
* @example
* re2 = RE2::Regexp.new("woo?", utf8: true)
* re2.utf8? #=> true
*/
static VALUE re2_regexp_utf8(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().encoding() == RE2::Options::EncodingUTF8);
}
/*
* Returns whether or not the regular expression was compiled with the
* `posix_syntax` option set to `true`.
*
* @return [Boolean] the `posix_syntax` option
* @example
* re2 = RE2::Regexp.new("woo?", posix_syntax: true)
* re2.posix_syntax? #=> true
*/
static VALUE re2_regexp_posix_syntax(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().posix_syntax());
}
/*
* Returns whether or not the regular expression was compiled with the
* `longest_match` option set to `true`.
*
* @return [Boolean] the `longest_match` option
* @example
* re2 = RE2::Regexp.new("woo?", longest_match: true)
* re2.longest_match? #=> true
*/
static VALUE re2_regexp_longest_match(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().longest_match());
}
/*
* Returns whether or not the regular expression was compiled with the
* `log_errors` option set to `true`.
*
* @return [Boolean] the `log_errors` option
* @example
* re2 = RE2::Regexp.new("woo?", log_errors: true)
* re2.log_errors? #=> true
*/
static VALUE re2_regexp_log_errors(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().log_errors());
}
/*
* Returns the `max_mem` setting for the regular expression.
*
* @return [Integer] the `max_mem` option
* @example
* re2 = RE2::Regexp.new("woo?", max_mem: 1024)
* re2.max_mem #=> 1024
*/
static VALUE re2_regexp_max_mem(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return INT2FIX(p->pattern->options().max_mem());
}
/*
* Returns whether or not the regular expression was compiled with the
* `literal` option set to `true`.
*
* @return [Boolean] the `literal` option
* @example
* re2 = RE2::Regexp.new("woo?", literal: true)
* re2.literal? #=> true
*/
static VALUE re2_regexp_literal(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().literal());
}
/*
* Returns whether or not the regular expression was compiled with the
* `never_nl` option set to `true`.
*
* @return [Boolean] the `never_nl` option
* @example
* re2 = RE2::Regexp.new("woo?", never_nl: true)
* re2.never_nl? #=> true
*/
static VALUE re2_regexp_never_nl(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().never_nl());
}
/*
* Returns whether or not the regular expression was compiled with the
* `case_sensitive` option set to `true`.
*
* @return [Boolean] the `case_sensitive` option
* @example
* re2 = RE2::Regexp.new("woo?", case_sensitive: true)
* re2.case_sensitive? #=> true
*/
static VALUE re2_regexp_case_sensitive(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().case_sensitive());
}
/*
* Returns whether or not the regular expression was compiled with the
* `case_sensitive` option set to `false`.
*
* @return [Boolean] the inverse of the `case_sensitive` option
* @example
* re2 = RE2::Regexp.new("woo?", case_sensitive: true)
* re2.case_insensitive? #=> false
* re2.casefold? #=> false
*/
static VALUE re2_regexp_case_insensitive(const VALUE self) {
return BOOL2RUBY(re2_regexp_case_sensitive(self) != Qtrue);
}
/*
* Returns whether or not the regular expression was compiled with the
* perl_classes option set to `true`.
*
* @return [Boolean] the `perl_classes` option
* @example
* re2 = RE2::Regexp.new("woo?", perl_classes: true)
* re2.perl_classes? #=> true
*/
static VALUE re2_regexp_perl_classes(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().perl_classes());
}
/*
* Returns whether or not the regular expression was compiled with the
* `word_boundary` option set to `true`.
*
* @return [Boolean] the `word_boundary` option
* @example
* re2 = RE2::Regexp.new("woo?", word_boundary: true)
* re2.word_boundary? #=> true
*/
static VALUE re2_regexp_word_boundary(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().word_boundary());
}
/*
* Returns whether or not the regular expression was compiled with the
* `one_line` option set to `true`.
*
* @return [Boolean] the `one_line` option
* @example
* re2 = RE2::Regexp.new("woo?", one_line: true)
* re2.one_line? #=> true
*/
static VALUE re2_regexp_one_line(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(p->pattern->options().one_line());
}
/*
* If the {RE2::Regexp} could not be created properly, returns an error string
* otherwise returns `nil`.
*
* @return [String, nil] the error string or `nil`
*/
static VALUE re2_regexp_error(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
if (p->pattern->ok()) {
return Qnil;
} else {
return rb_str_new(p->pattern->error().data(), p->pattern->error().size());
}
}
/*
* If the {RE2::Regexp} could not be created properly, returns
* the offending portion of the regexp otherwise returns `nil`.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @return [String, nil] the offending portion of the regexp or `nil`
*/
static VALUE re2_regexp_error_arg(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
if (p->pattern->ok()) {
return Qnil;
} else {
return encoded_str_new(p->pattern->error_arg().data(),
p->pattern->error_arg().size(),
p->pattern->options().encoding());
}
}
/*
* Returns the program size, a very approximate measure
* of a regexp's "cost". Larger numbers are more expensive
* than smaller numbers.
*
* @return [Integer] the regexp "cost"
*/
static VALUE re2_regexp_program_size(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return INT2FIX(p->pattern->ProgramSize());
}
/*
* Returns a hash of the options currently set for the {RE2::Regexp}.
*
* @return [Hash] the options
*/
static VALUE re2_regexp_options(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
VALUE options = rb_hash_new();
rb_hash_aset(options, ID2SYM(id_utf8),
BOOL2RUBY(p->pattern->options().encoding() == RE2::Options::EncodingUTF8));
rb_hash_aset(options, ID2SYM(id_posix_syntax),
BOOL2RUBY(p->pattern->options().posix_syntax()));
rb_hash_aset(options, ID2SYM(id_longest_match),
BOOL2RUBY(p->pattern->options().longest_match()));
rb_hash_aset(options, ID2SYM(id_log_errors),
BOOL2RUBY(p->pattern->options().log_errors()));
rb_hash_aset(options, ID2SYM(id_max_mem),
INT2FIX(p->pattern->options().max_mem()));
rb_hash_aset(options, ID2SYM(id_literal),
BOOL2RUBY(p->pattern->options().literal()));
rb_hash_aset(options, ID2SYM(id_never_nl),
BOOL2RUBY(p->pattern->options().never_nl()));
rb_hash_aset(options, ID2SYM(id_case_sensitive),
BOOL2RUBY(p->pattern->options().case_sensitive()));
rb_hash_aset(options, ID2SYM(id_perl_classes),
BOOL2RUBY(p->pattern->options().perl_classes()));
rb_hash_aset(options, ID2SYM(id_word_boundary),
BOOL2RUBY(p->pattern->options().word_boundary()));
rb_hash_aset(options, ID2SYM(id_one_line),
BOOL2RUBY(p->pattern->options().one_line()));
/* This is a read-only hash after all... */
rb_obj_freeze(options);
return options;
}
/*
* Returns the number of capturing subpatterns, or -1 if the regexp
* wasn't valid on construction. The overall match (`$0`) does not
* count: if the regexp is `"(a)(b)"`, returns 2.
*
* @return [Integer] the number of capturing subpatterns
*/
static VALUE re2_regexp_number_of_capturing_groups(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return INT2FIX(p->pattern->NumberOfCapturingGroups());
}
/*
* Returns a hash of names to capturing indices of groups.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @return [Hash] a hash of names to capturing indices
*/
static VALUE re2_regexp_named_capturing_groups(const VALUE self) {
re2_pattern *p;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
const std::map<std::string, int>& groups = p->pattern->NamedCapturingGroups();
VALUE capturing_groups = rb_hash_new();
for (std::map<std::string, int>::const_iterator it = groups.begin(); it != groups.end(); ++it) {
rb_hash_aset(capturing_groups,
encoded_str_new(it->first.data(), it->first.size(),
p->pattern->options().encoding()),
INT2FIX(it->second));
}
return capturing_groups;
}
/*
* General matching: match the pattern against the given `text` using
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L562-L588
* `Match`} and return a {RE2::MatchData} instance with the specified number of
* submatches (defaults to the total number of capturing groups) or a boolean
* (if no submatches are required).
*
* The number of submatches has a significant impact on performance: requesting
* one submatch is much faster than requesting more than one and requesting
* zero submatches is faster still.
*
* @overload match(text)
* Returns a {RE2::MatchData} containing the matching pattern and all
* submatches resulting from looking for the regexp in `text` if the pattern
* contains capturing groups.
*
* Returns either `true` or `false` indicating whether a successful match was
* made if the pattern contains no capturing groups.
*
* @param [String] text the text to search
* @return [RE2::MatchData, nil] if the pattern contains capturing groups
* @return [Boolean] if the pattern does not contain capturing groups
* @raise [NoMemoryError] if there was not enough memory to allocate the submatches
* @raise [TypeError] if given text that cannot be coerced to a `String`
* @example Matching with capturing groups
* r = RE2::Regexp.new('w(o)(o)')
* r.match('woo') #=> #<RE2::MatchData "woo" 1:"o" 2:"o">
* @example Matching without capturing groups
* r = RE2::Regexp.new('woo')
* r.match('woo') #=> true
*
* @overload match(text, options)
* See `match(text)` but with customisable offsets for starting and ending
* matches, optional anchoring to the start or both ends of the text and a
* specific number of submatches to extract (padded with `nil`s if
* necessary).
*
* @param [String] text the text to search
* @param [Hash] options the options with which to perform the match
* @option options [Integer] :startpos (0) offset at which to start matching
* @option options [Integer] :endpos offset at which to stop matching, defaults to the text length
* @option options [Symbol] :anchor (:unanchored) one of :unanchored, :anchor_start, :anchor_both to anchor the match
* @option options [Integer] :submatches how many submatches to extract (0 is
* fastest), defaults to the number of capturing groups
* @return [RE2::MatchData, nil] if extracting any submatches
* @return [Boolean] if not extracting any submatches
* @raise [ArgumentError] if given a negative number of submatches, invalid
* anchor or invalid startpos, endpos pair
* @raise [NoMemoryError] if there was not enough memory to allocate the matches
* @raise [TypeError] if given non-String text, non-numeric number of
* submatches, non-symbol anchor or non-hash options
* @raise [RE2::Regexp::UnsupportedError] if given an endpos argument on a
* version of RE2 that does not support it
* @example Matching with capturing groups
* r = RE2::Regexp.new('w(o)(o)')
* r.match('woo', submatches: 1) #=> #<RE2::MatchData "woo" 1:"o">
* r.match('woo', submatches: 3) #=> #<RE2::MatchData "woo" 1:"o" 2:"o" 3:nil>
* r.match('woot', anchor: :anchor_both, submatches: 0)
* #=> false
* r.match('woot', anchor: :anchor_start, submatches: 0)
* #=> true
* @example Matching without capturing groups
* r = RE2::Regexp.new('wo+')
* r.match('woot', anchor: :anchor_both) #=> false
* r.match('woot', anchor: :anchor_start) #=> true
*
* @overload match(text, submatches)
* @deprecated Legacy syntax for matching against `text` with a specific
* number of submatches to extract. Use `match(text, submatches: n)` instead.
*
* @param [String] text the text to search
* @param [Integer] submatches the number of submatches to extract
* @return [RE2::MatchData, nil] if extracting any submatches
* @return [Boolean] if not extracting any submatches
* @raise [NoMemoryError] if there was not enough memory to allocate the submatches
* @raise [TypeError] if given non-numeric number of submatches
* @example
* r = RE2::Regexp.new('w(o)(o)')
* r.match('woo', 0) #=> true
* r.match('woo', 1) #=> #<RE2::MatchData "woo" 1:"o">
* r.match('woo', 2) #=> #<RE2::MatchData "woo" 1:"o" 2:"o">
*/
static VALUE re2_regexp_match(int argc, VALUE *argv, const VALUE self) {
re2_pattern *p;
re2_matchdata *m;
VALUE text, options;
rb_scan_args(argc, argv, "11", &text, &options);
/* Ensure text is a string. */
StringValue(text);
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
int n;
int startpos = 0;
int endpos = RSTRING_LEN(text);
RE2::Anchor anchor = RE2::UNANCHORED;
if (RTEST(options)) {
if (RB_INTEGER_TYPE_P(options)) {
n = NUM2INT(options);
if (n < 0) {
rb_raise(rb_eArgError, "number of matches should be >= 0");
}
} else {
if (TYPE(options) != T_HASH) {
options = rb_Hash(options);
}
VALUE endpos_option = rb_hash_aref(options, ID2SYM(id_endpos));
if (!NIL_P(endpos_option)) {
#ifdef HAVE_ENDPOS_ARGUMENT
endpos = NUM2INT(endpos_option);
if (endpos < 0) {
rb_raise(rb_eArgError, "endpos should be >= 0");
}
#else
rb_raise(re2_eRegexpUnsupportedError, "current version of RE2::Match() does not support endpos argument");
#endif
}
VALUE anchor_option = rb_hash_aref(options, ID2SYM(id_anchor));
if (!NIL_P(anchor_option)) {
Check_Type(anchor_option, T_SYMBOL);
ID id_anchor_option = SYM2ID(anchor_option);
if (id_anchor_option == id_unanchored) {
anchor = RE2::UNANCHORED;
} else if (id_anchor_option == id_anchor_start) {
anchor = RE2::ANCHOR_START;
} else if (id_anchor_option == id_anchor_both) {
anchor = RE2::ANCHOR_BOTH;
} else {
rb_raise(rb_eArgError, "anchor should be one of: :unanchored, :anchor_start, :anchor_both");
}
}
VALUE submatches_option = rb_hash_aref(options, ID2SYM(id_submatches));
if (!NIL_P(submatches_option)) {
n = NUM2INT(submatches_option);
if (n < 0) {
rb_raise(rb_eArgError, "number of matches should be >= 0");
}
} else {
if (!p->pattern->ok()) {
return Qnil;
}
n = p->pattern->NumberOfCapturingGroups();
}
VALUE startpos_option = rb_hash_aref(options, ID2SYM(id_startpos));
if (!NIL_P(startpos_option)) {
startpos = NUM2INT(startpos_option);
if (startpos < 0) {
rb_raise(rb_eArgError, "startpos should be >= 0");
}
}
}
} else {
if (!p->pattern->ok()) {
return Qnil;
}
n = p->pattern->NumberOfCapturingGroups();
}
if (startpos > endpos) {
rb_raise(rb_eArgError, "startpos should be <= endpos");
}
if (n == 0) {
#ifdef HAVE_ENDPOS_ARGUMENT
bool matched = p->pattern->Match(
re2::StringPiece(RSTRING_PTR(text), RSTRING_LEN(text)),
startpos, endpos, anchor, 0, 0);
#else
bool matched = p->pattern->Match(
re2::StringPiece(RSTRING_PTR(text), RSTRING_LEN(text)),
startpos, anchor, 0, 0);
#endif
return BOOL2RUBY(matched);
} else {
if (n == INT_MAX) {
rb_raise(rb_eRangeError, "number of matches should be < %d", INT_MAX);
}
/* Because match returns the whole match as well. */
n += 1;
re2::StringPiece *matches = new(std::nothrow) re2::StringPiece[n];
if (matches == 0) {
rb_raise(rb_eNoMemError,
"not enough memory to allocate StringPieces for matches");
}
text = rb_str_new_frozen(text);
#ifdef HAVE_ENDPOS_ARGUMENT
bool matched = p->pattern->Match(
re2::StringPiece(RSTRING_PTR(text), RSTRING_LEN(text)),
startpos, endpos, anchor, matches, n);
#else
bool matched = p->pattern->Match(
re2::StringPiece(RSTRING_PTR(text), RSTRING_LEN(text)),
startpos, anchor, matches, n);
#endif
if (matched) {
VALUE matchdata = rb_class_new_instance(0, 0, re2_cMatchData);
TypedData_Get_Struct(matchdata, re2_matchdata, &re2_matchdata_data_type, m);
RB_OBJ_WRITE(matchdata, &m->regexp, self);
RB_OBJ_WRITE(matchdata, &m->text, text);
m->matches = matches;
m->number_of_matches = n;
return matchdata;
} else {
delete[] matches;
return Qnil;
}
}
}
/*
* Returns true if the pattern matches any substring of the given text using
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L413-L427
* `PartialMatch`}.
*
* @return [Boolean] whether the match was successful
* @raise [TypeError] if text cannot be coerced to a `String`
*/
static VALUE re2_regexp_match_p(const VALUE self, VALUE text) {
re2_pattern *p;
/* Ensure text is a string. */
StringValue(text);
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(RE2::PartialMatch(
re2::StringPiece(RSTRING_PTR(text), RSTRING_LEN(text)), *p->pattern));
}
/*
* Returns true if the pattern matches the given text using
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L376-L411
* `FullMatch`}.
*
* @return [Boolean] whether the match was successful
* @raise [TypeError] if text cannot be coerced to a `String`
*/
static VALUE re2_regexp_full_match_p(const VALUE self, VALUE text) {
re2_pattern *p;
/* Ensure text is a string. */
StringValue(text);
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
return BOOL2RUBY(RE2::FullMatch(
re2::StringPiece(RSTRING_PTR(text), RSTRING_LEN(text)), *p->pattern));
}
/*
* Returns a {RE2::Scanner} for scanning the given text incrementally with
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L447-L463
* `FindAndConsume`}.
*
* @param [text] text the text to scan incrementally
* @return [RE2::Scanner] an `Enumerable` {RE2::Scanner} object
* @raise [TypeError] if `text` cannot be coerced to a `String`
* @example
* c = RE2::Regexp.new('(\w+)').scan("Foo bar baz")
* #=> #<RE2::Scanner:0x0000000000000001>
*/
static VALUE re2_regexp_scan(const VALUE self, VALUE text) {
/* Ensure text is a string. */
StringValue(text);
re2_pattern *p;
re2_scanner *c;
TypedData_Get_Struct(self, re2_pattern, &re2_regexp_data_type, p);
VALUE scanner = rb_class_new_instance(0, 0, re2_cScanner);
TypedData_Get_Struct(scanner, re2_scanner, &re2_scanner_data_type, c);
RB_OBJ_WRITE(scanner, &c->regexp, self);
RB_OBJ_WRITE(scanner, &c->text, rb_str_new_frozen(text));
c->input = new(std::nothrow) re2::StringPiece(
RSTRING_PTR(c->text), RSTRING_LEN(c->text));
if (c->input == 0) {
rb_raise(rb_eNoMemError,
"not enough memory to allocate StringPiece for input");
}
if (p->pattern->ok()) {
c->number_of_capturing_groups = p->pattern->NumberOfCapturingGroups();
} else {
c->number_of_capturing_groups = 0;
}
c->eof = false;
return scanner;
}
/*
* Returns whether the underlying RE2 version supports passing an `endpos`
* argument to
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L562-L588
* Match}. If not, {RE2::Regexp#match} will raise an error if attempting to
* pass an `endpos`.
*
* @return [Boolean] whether the underlying
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L562-L588
* Match} has an endpos argument
*/
static VALUE re2_regexp_match_has_endpos_argument_p(VALUE) {
#ifdef HAVE_ENDPOS_ARGUMENT
return Qtrue;
#else
return Qfalse;
#endif
}
/*
* Returns a copy of `str` with the first occurrence `pattern` replaced with
* `rewrite` using
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L465-L480
* `Replace`}.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @param [String] str the string to modify
* @param [String, RE2::Regexp] pattern a regexp matching text to be replaced
* @param [String] rewrite the string to replace with
* @return [String] the resulting string
* @raise [TypeError] if the given rewrite or pattern (if not provided as a
* {RE2::Regexp}) cannot be coerced to `String`s
* @example
* RE2.Replace("hello there", "hello", "howdy") #=> "howdy there"
* re2 = RE2::Regexp.new("hel+o")
* RE2.Replace("hello there", re2, "yo") #=> "yo there"
*/
static VALUE re2_Replace(VALUE, VALUE str, VALUE pattern,
VALUE rewrite) {
/* Ensure rewrite is a string. */
StringValue(rewrite);
re2_pattern *p;
/* Take a copy of str so it can be modified in-place by
* RE2::Replace.
*/
StringValue(str);
std::string str_as_string(RSTRING_PTR(str), RSTRING_LEN(str));
/* Do the replacement. */
if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
TypedData_Get_Struct(pattern, re2_pattern, &re2_regexp_data_type, p);
RE2::Replace(&str_as_string, *p->pattern,
re2::StringPiece(RSTRING_PTR(rewrite), RSTRING_LEN(rewrite)));
return encoded_str_new(str_as_string.data(), str_as_string.size(),
p->pattern->options().encoding());
} else {
/* Ensure pattern is a string. */
StringValue(pattern);
RE2::Replace(&str_as_string,
re2::StringPiece(RSTRING_PTR(pattern), RSTRING_LEN(pattern)),
re2::StringPiece(RSTRING_PTR(rewrite), RSTRING_LEN(rewrite)));
return encoded_str_new(str_as_string.data(), str_as_string.size(), RE2::Options::EncodingUTF8);
}
}
/*
* Return a copy of `str` with `pattern` replaced by `rewrite` using
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L482-L497
* `GlobalReplace`}.
*
* Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
* returned in UTF-8 by default or ISO-8859-1 if the `:utf8` option for the
* {RE2::Regexp} is set to `false` (any other encoding's behaviour is undefined).
*
* @param [String] str the string to modify
* @param [String, RE2::Regexp] pattern a regexp matching text to be replaced
* @param [String] rewrite the string to replace with
* @raise [TypeError] if the given rewrite or pattern (if not provided as a
* {RE2::Regexp}) cannot be coerced to `String`s
* @return [String] the resulting string
* @example
* re2 = RE2::Regexp.new("oo?")
* RE2.GlobalReplace("whoops-doops", re2, "e") #=> "wheps-deps"
* RE2.GlobalReplace("hello there", "e", "i") #=> "hillo thiri"
*/
static VALUE re2_GlobalReplace(VALUE, VALUE str, VALUE pattern,
VALUE rewrite) {
/* Ensure rewrite is a string. */
StringValue(rewrite);
/* Take a copy of str so it can be modified in-place by
* RE2::GlobalReplace.
*/
re2_pattern *p;
StringValue(str);
std::string str_as_string(RSTRING_PTR(str), RSTRING_LEN(str));
/* Do the replacement. */
if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
TypedData_Get_Struct(pattern, re2_pattern, &re2_regexp_data_type, p);
RE2::GlobalReplace(&str_as_string, *p->pattern,
re2::StringPiece(RSTRING_PTR(rewrite), RSTRING_LEN(rewrite)));
return encoded_str_new(str_as_string.data(), str_as_string.size(),
p->pattern->options().encoding());
} else {
/* Ensure pattern is a string. */
StringValue(pattern);
RE2::GlobalReplace(&str_as_string,
re2::StringPiece(RSTRING_PTR(pattern), RSTRING_LEN(pattern)),
re2::StringPiece(RSTRING_PTR(rewrite), RSTRING_LEN(rewrite)));
return encoded_str_new(str_as_string.data(), str_as_string.size(), RE2::Options::EncodingUTF8);
}
}
/*
* Returns a version of `str` with all potentially meaningful regexp characters
* escaped using
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/re2.h#L512-L518
* `QuoteMeta`}. The returned string, used as a regular expression, will
* exactly match the original string.
*
* @param [String] unquoted the unquoted string
* @raise [TypeError] if the given unquoted string cannot be coerced to a `String`
* @return [String] the escaped string
* @example
* RE2::Regexp.escape("1.5-2.0?") #=> "1\.5\-2\.0\?"
*/
static VALUE re2_QuoteMeta(VALUE, VALUE unquoted) {
StringValue(unquoted);
std::string quoted_string = RE2::QuoteMeta(
re2::StringPiece(RSTRING_PTR(unquoted), RSTRING_LEN(unquoted)));
return rb_str_new(quoted_string.data(), quoted_string.size());
}
static void re2_set_free(void *ptr) {
re2_set *s = reinterpret_cast<re2_set *>(ptr);
if (s->set) {
delete s->set;
}
xfree(s);
}
static size_t re2_set_memsize(const void *ptr) {
const re2_set *s = reinterpret_cast<const re2_set *>(ptr);
size_t size = sizeof(*s);
if (s->set) {
size += sizeof(*s->set);
}
return size;
}
static const rb_data_type_t re2_set_data_type = {
"RE2::Set",
{
0,
re2_set_free,
re2_set_memsize,
},
0,
0,
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
// macro to update VALUE references, as to trigger write barriers.
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};
static VALUE re2_set_allocate(VALUE klass) {
re2_set *s;
VALUE result = TypedData_Make_Struct(klass, re2_set, &re2_set_data_type, s);
return result;
}
/*
* Returns a new {RE2::Set} object, a collection of patterns that can be
* searched for simultaneously.
*
* @return [RE2::Set]
*
* @overload initialize
* Returns a new {RE2::Set} object for unanchored patterns with the default
* options.
*
* @raise [NoMemoryError] if memory could not be allocated for the compiled pattern
* @return [RE2::Set]
*
* @overload initialize(anchor)
* Returns a new {RE2::Set} object for the specified anchor with the default
* options.
*
* @param [Symbol] anchor one of `:unanchored`, `:anchor_start`, `:anchor_both`
* @raise [ArgumentError] if anchor is not `:unanchored`, `:anchor_start` or `:anchor_both`
* @raise [NoMemoryError] if memory could not be allocated for the compiled pattern
*
* @overload initialize(anchor, options)
* Returns a new {RE2::Set} object with the specified options.
*
* @param [Symbol] anchor one of `:unanchored`, `:anchor_start`, `:anchor_both`
* @param [Hash] options the options with which to compile the pattern
* @option options [Boolean] :utf8 (true) text and pattern are UTF-8; otherwise Latin-1
* @option options [Boolean] :posix_syntax (false) restrict regexps to POSIX egrep syntax
* @option options [Boolean] :longest_match (false) search for longest match, not first match
* @option options [Boolean] :log_errors (true) log syntax and execution errors to ERROR
* @option options [Integer] :max_mem approx. max memory footprint of RE2
* @option options [Boolean] :literal (false) interpret string as literal, not regexp
* @option options [Boolean] :never_nl (false) never match `\n`, even if it is in regexp
* @option options [Boolean] :case_sensitive (true) match is case-sensitive (regexp can override with `(?i)` unless in `posix_syntax` mode)
* @option options [Boolean] :perl_classes (false) allow Perl's `\d` `\s` `\w` `\D` `\S` `\W` when in `posix_syntax` mode
* @option options [Boolean] :word_boundary (false) allow `\b` `\B` (word boundary and not) when in `posix_syntax` mode
* @option options [Boolean] :one_line (false) `^` and `$` only match beginning and end of text when in `posix_syntax` mode
* @return [RE2::Set] a {RE2::Set} with the specified anchor and options
* @raise [ArgumentError] if `anchor` is not one of the accepted choices
* @raise [NoMemoryError] if memory could not be allocated for the compiled pattern
*/
static VALUE re2_set_initialize(int argc, VALUE *argv, VALUE self) {
VALUE anchor, options;
re2_set *s;
rb_scan_args(argc, argv, "02", &anchor, &options);
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
RE2::Anchor re2_anchor = RE2::UNANCHORED;
if (!NIL_P(anchor)) {
Check_Type(anchor, T_SYMBOL);
ID id_anchor_arg = SYM2ID(anchor);
if (id_anchor_arg == id_unanchored) {
re2_anchor = RE2::UNANCHORED;
} else if (id_anchor_arg == id_anchor_start) {
re2_anchor = RE2::ANCHOR_START;
} else if (id_anchor_arg == id_anchor_both) {
re2_anchor = RE2::ANCHOR_BOTH;
} else {
rb_raise(rb_eArgError, "anchor should be one of: :unanchored, :anchor_start, :anchor_both");
}
}
RE2::Options re2_options;
if (RTEST(options)) {
parse_re2_options(&re2_options, options);
}
s->set = new(std::nothrow) RE2::Set(re2_options, re2_anchor);
if (s->set == 0) {
rb_raise(rb_eNoMemError, "not enough memory to allocate RE2::Set object");
}
return self;
}
/*
* Adds a pattern to the set. Returns the index that will identify the pattern
* in the output of {RE2::Set#match}. Cannot be called after {RE2::Set#compile}
* has been called.
*
* @param [String] pattern the regex pattern
* @return [Integer] the index of the pattern in the set
* @raise [ArgumentError] if called after compile or the pattern is rejected
* @example
* set = RE2::Set.new
* set.add("abc") #=> 0
* set.add("def") #=> 1
*/
static VALUE re2_set_add(VALUE self, VALUE pattern) {
StringValue(pattern);
re2_set *s;
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
int index;
VALUE msg;
{
std::string err;
index = s->set->Add(
re2::StringPiece(RSTRING_PTR(pattern), RSTRING_LEN(pattern)), &err);
msg = rb_str_new(err.data(), err.size());
}
if (index < 0) {
rb_raise(rb_eArgError,
"str rejected by RE2::Set->Add(): %s", RSTRING_PTR(msg));
}
return INT2FIX(index);
}
/*
* Compiles a {RE2::Set} so it can be used to match against. Must be called
* after {RE2::Set#add} and before {RE2::Set#match}.
*
* @return [Boolean] whether compilation was a success
* @example
* set = RE2::Set.new
* set.add("abc")
* set.compile #=> true
*/
static VALUE re2_set_compile(VALUE self) {
re2_set *s;
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
return BOOL2RUBY(s->set->Compile());
}
/*
* Returns the size of the {RE2::Set}.
*
* @return [Integer] the number of patterns in the set
* @example
* set = RE2::Set.new
* set.add("abc")
* set.size #=> 1
*/
static VALUE re2_set_size(VALUE self) {
#ifdef HAVE_SET_SIZE
re2_set *s;
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
return INT2FIX(s->set->Size());
#else
rb_raise(re2_eSetUnsupportedError, "current version of RE2::Set does not have Size method");
#endif
}
/*
* Returns whether the underlying RE2 version outputs error information from
* {https://github.com/google/re2/blob/bc0faab533e2b27b85b8ad312abf061e33ed6b5d/re2/set.h#L62-L65
* `RE2::Set::Match`}. If not, {RE2::Set#match} will raise an error if attempting to set
* its `:exception` option to `true`.
*
* @return [Boolean] whether the underlying RE2 outputs error information from {RE2::Set} matches
*/
static VALUE re2_set_match_raises_errors_p(VALUE) {
#ifdef HAVE_ERROR_INFO_ARGUMENT
return Qtrue;
#else
return Qfalse;
#endif
}
/*
* Returns whether the underlying RE2 version has a Set::Size method.
*
* @return [Boolean] whether the underlying RE2 has a Set::Size method
*/
static VALUE re2_set_size_p(VALUE) {
#ifdef HAVE_SET_SIZE
return Qtrue;
#else
return Qfalse;
#endif
}
/*
* Matches the given text against patterns in the set, returning an array of
* integer indices of the matching patterns if matched or an empty array if
* there are no matches.
*
* @return [Array<Integer>]
*
* @overload match(str)
* Returns an array of integer indices of patterns matching the given string
* (if any). Raises exceptions if there are any errors while matching.
*
* @param [String] str the text to match against
* @return [Array<Integer>] the indices of matching regexps
* @raise [MatchError] if an error occurs while matching
* @raise [UnsupportedError] if the underlying version of RE2 does not output error information
* @example
* set = RE2::Set.new
* set.add("abc")
* set.add("def")
* set.compile
* set.match("abcdef") #=> [0, 1]
*
* @overload match(str, options)
* Returns an array of integer indices of patterns matching the given string
* (if any). Raises exceptions if there are any errors while matching and the
* `:exception` option is set to true.
*
* @param [String] str the text to match against
* @param [Hash] options the options with which to match
* @option options [Boolean] :exception (true) whether to raise exceptions with RE2's error information (not supported on ABI version 0 of RE2)
* @return [Array<Integer>] the indices of matching regexps
* @raise [MatchError] if an error occurs while matching
* @raise [UnsupportedError] if the underlying version of RE2 does not output error information
* @example
* set = RE2::Set.new
* set.add("abc")
* set.add("def")
* set.compile
* set.match("abcdef", exception: true) #=> [0, 1]
*/
static VALUE re2_set_match(int argc, VALUE *argv, const VALUE self) {
VALUE str, options;
bool raise_exception = true;
rb_scan_args(argc, argv, "11", &str, &options);
StringValue(str);
re2_set *s;
TypedData_Get_Struct(self, re2_set, &re2_set_data_type, s);
if (RTEST(options)) {
Check_Type(options, T_HASH);
VALUE exception_option = rb_hash_aref(options, ID2SYM(id_exception));
if (!NIL_P(exception_option)) {
raise_exception = RTEST(exception_option);
}
}
std::vector<int> v;
if (raise_exception) {
#ifdef HAVE_ERROR_INFO_ARGUMENT
RE2::Set::ErrorInfo e;
bool match_failed = !s->set->Match(
re2::StringPiece(RSTRING_PTR(str), RSTRING_LEN(str)), &v, &e);
VALUE result = rb_ary_new2(v.size());
if (match_failed) {
switch (e.kind) {
case RE2::Set::kNoError:
break;
case RE2::Set::kNotCompiled:
rb_raise(re2_eSetMatchError, "#match must not be called before #compile");
case RE2::Set::kOutOfMemory:
rb_raise(re2_eSetMatchError, "The DFA ran out of memory");
case RE2::Set::kInconsistent:
rb_raise(re2_eSetMatchError, "RE2::Prog internal error");
default: // Just in case a future version of libre2 adds new ErrorKinds
rb_raise(re2_eSetMatchError, "Unknown RE2::Set::ErrorKind: %d", e.kind);
}
} else {
for (std::vector<int>::size_type i = 0; i < v.size(); ++i) {
rb_ary_push(result, INT2FIX(v[i]));
}
}
return result;
#else
rb_raise(re2_eSetUnsupportedError, "current version of RE2::Set::Match() does not output error information, :exception option can only be set to false");
#endif
} else {
bool matched = s->set->Match(
re2::StringPiece(RSTRING_PTR(str), RSTRING_LEN(str)), &v);
VALUE result = rb_ary_new2(v.size());
if (matched) {
for (std::vector<int>::size_type i = 0; i < v.size(); ++i) {
rb_ary_push(result, INT2FIX(v[i]));
}
}
return result;
}
}
extern "C" void Init_re2(void) {
re2_mRE2 = rb_define_module("RE2");
re2_cRegexp = rb_define_class_under(re2_mRE2, "Regexp", rb_cObject);
re2_eRegexpUnsupportedError = rb_define_class_under(re2_cRegexp,
"UnsupportedError", rb_const_get(rb_cObject, rb_intern("StandardError")));
re2_cMatchData = rb_define_class_under(re2_mRE2, "MatchData", rb_cObject);
re2_cScanner = rb_define_class_under(re2_mRE2, "Scanner", rb_cObject);
re2_cSet = rb_define_class_under(re2_mRE2, "Set", rb_cObject);
re2_eSetMatchError = rb_define_class_under(re2_cSet, "MatchError",
rb_const_get(rb_cObject, rb_intern("StandardError")));
re2_eSetUnsupportedError = rb_define_class_under(re2_cSet, "UnsupportedError",
rb_const_get(rb_cObject, rb_intern("StandardError")));
rb_define_alloc_func(re2_cRegexp,
reinterpret_cast<VALUE (*)(VALUE)>(re2_regexp_allocate));
rb_define_alloc_func(re2_cMatchData,
reinterpret_cast<VALUE (*)(VALUE)>(re2_matchdata_allocate));
rb_define_alloc_func(re2_cScanner,
reinterpret_cast<VALUE (*)(VALUE)>(re2_scanner_allocate));
rb_define_alloc_func(re2_cSet,
reinterpret_cast<VALUE (*)(VALUE)>(re2_set_allocate));
rb_define_method(re2_cMatchData, "string",
RUBY_METHOD_FUNC(re2_matchdata_string), 0);
rb_define_method(re2_cMatchData, "regexp",
RUBY_METHOD_FUNC(re2_matchdata_regexp), 0);
rb_define_method(re2_cMatchData, "to_a",
RUBY_METHOD_FUNC(re2_matchdata_to_a), 0);
rb_define_method(re2_cMatchData, "size",
RUBY_METHOD_FUNC(re2_matchdata_size), 0);
rb_define_method(re2_cMatchData, "length",
RUBY_METHOD_FUNC(re2_matchdata_size), 0);
rb_define_method(re2_cMatchData, "begin",
RUBY_METHOD_FUNC(re2_matchdata_begin), 1);
rb_define_method(re2_cMatchData, "end",
RUBY_METHOD_FUNC(re2_matchdata_end), 1);
rb_define_method(re2_cMatchData, "[]", RUBY_METHOD_FUNC(re2_matchdata_aref),
-1);
rb_define_method(re2_cMatchData, "to_s",
RUBY_METHOD_FUNC(re2_matchdata_to_s), 0);
rb_define_method(re2_cMatchData, "inspect",
RUBY_METHOD_FUNC(re2_matchdata_inspect), 0);
rb_define_method(re2_cMatchData, "deconstruct",
RUBY_METHOD_FUNC(re2_matchdata_deconstruct), 0);
rb_define_method(re2_cMatchData, "deconstruct_keys",
RUBY_METHOD_FUNC(re2_matchdata_deconstruct_keys), 1);
rb_define_method(re2_cScanner, "string",
RUBY_METHOD_FUNC(re2_scanner_string), 0);
rb_define_method(re2_cScanner, "eof?",
RUBY_METHOD_FUNC(re2_scanner_eof), 0);
rb_define_method(re2_cScanner, "regexp",
RUBY_METHOD_FUNC(re2_scanner_regexp), 0);
rb_define_method(re2_cScanner, "scan",
RUBY_METHOD_FUNC(re2_scanner_scan), 0);
rb_define_method(re2_cScanner, "rewind",
RUBY_METHOD_FUNC(re2_scanner_rewind), 0);
rb_define_singleton_method(re2_cRegexp, "match_has_endpos_argument?",
RUBY_METHOD_FUNC(re2_regexp_match_has_endpos_argument_p), 0);
rb_define_method(re2_cRegexp, "initialize",
RUBY_METHOD_FUNC(re2_regexp_initialize), -1);
rb_define_method(re2_cRegexp, "ok?", RUBY_METHOD_FUNC(re2_regexp_ok), 0);
rb_define_method(re2_cRegexp, "error", RUBY_METHOD_FUNC(re2_regexp_error),
0);
rb_define_method(re2_cRegexp, "error_arg",
RUBY_METHOD_FUNC(re2_regexp_error_arg), 0);
rb_define_method(re2_cRegexp, "program_size",
RUBY_METHOD_FUNC(re2_regexp_program_size), 0);
rb_define_method(re2_cRegexp, "options",
RUBY_METHOD_FUNC(re2_regexp_options), 0);
rb_define_method(re2_cRegexp, "number_of_capturing_groups",
RUBY_METHOD_FUNC(re2_regexp_number_of_capturing_groups), 0);
rb_define_method(re2_cRegexp, "named_capturing_groups",
RUBY_METHOD_FUNC(re2_regexp_named_capturing_groups), 0);
rb_define_method(re2_cRegexp, "match", RUBY_METHOD_FUNC(re2_regexp_match),
-1);
rb_define_method(re2_cRegexp, "match?", RUBY_METHOD_FUNC(re2_regexp_match_p),
1);
rb_define_method(re2_cRegexp, "partial_match?",
RUBY_METHOD_FUNC(re2_regexp_match_p), 1);
rb_define_method(re2_cRegexp, "=~", RUBY_METHOD_FUNC(re2_regexp_match_p), 1);
rb_define_method(re2_cRegexp, "===", RUBY_METHOD_FUNC(re2_regexp_match_p), 1);
rb_define_method(re2_cRegexp, "full_match?",
RUBY_METHOD_FUNC(re2_regexp_full_match_p), 1);
rb_define_method(re2_cRegexp, "scan",
RUBY_METHOD_FUNC(re2_regexp_scan), 1);
rb_define_method(re2_cRegexp, "to_s", RUBY_METHOD_FUNC(re2_regexp_to_s), 0);
rb_define_method(re2_cRegexp, "to_str", RUBY_METHOD_FUNC(re2_regexp_to_s),
0);
rb_define_method(re2_cRegexp, "pattern", RUBY_METHOD_FUNC(re2_regexp_to_s),
0);
rb_define_method(re2_cRegexp, "source", RUBY_METHOD_FUNC(re2_regexp_to_s),
0);
rb_define_method(re2_cRegexp, "inspect",
RUBY_METHOD_FUNC(re2_regexp_inspect), 0);
rb_define_method(re2_cRegexp, "utf8?", RUBY_METHOD_FUNC(re2_regexp_utf8),
0);
rb_define_method(re2_cRegexp, "posix_syntax?",
RUBY_METHOD_FUNC(re2_regexp_posix_syntax), 0);
rb_define_method(re2_cRegexp, "longest_match?",
RUBY_METHOD_FUNC(re2_regexp_longest_match), 0);
rb_define_method(re2_cRegexp, "log_errors?",
RUBY_METHOD_FUNC(re2_regexp_log_errors), 0);
rb_define_method(re2_cRegexp, "max_mem",
RUBY_METHOD_FUNC(re2_regexp_max_mem), 0);
rb_define_method(re2_cRegexp, "literal?",
RUBY_METHOD_FUNC(re2_regexp_literal), 0);
rb_define_method(re2_cRegexp, "never_nl?",
RUBY_METHOD_FUNC(re2_regexp_never_nl), 0);
rb_define_method(re2_cRegexp, "case_sensitive?",
RUBY_METHOD_FUNC(re2_regexp_case_sensitive), 0);
rb_define_method(re2_cRegexp, "case_insensitive?",
RUBY_METHOD_FUNC(re2_regexp_case_insensitive), 0);
rb_define_method(re2_cRegexp, "casefold?",
RUBY_METHOD_FUNC(re2_regexp_case_insensitive), 0);
rb_define_method(re2_cRegexp, "perl_classes?",
RUBY_METHOD_FUNC(re2_regexp_perl_classes), 0);
rb_define_method(re2_cRegexp, "word_boundary?",
RUBY_METHOD_FUNC(re2_regexp_word_boundary), 0);
rb_define_method(re2_cRegexp, "one_line?",
RUBY_METHOD_FUNC(re2_regexp_one_line), 0);
rb_define_singleton_method(re2_cSet, "match_raises_errors?",
RUBY_METHOD_FUNC(re2_set_match_raises_errors_p), 0);
rb_define_singleton_method(re2_cSet, "size?",
RUBY_METHOD_FUNC(re2_set_size_p), 0);
rb_define_method(re2_cSet, "initialize",
RUBY_METHOD_FUNC(re2_set_initialize), -1);
rb_define_method(re2_cSet, "add", RUBY_METHOD_FUNC(re2_set_add), 1);
rb_define_method(re2_cSet, "compile", RUBY_METHOD_FUNC(re2_set_compile), 0);
rb_define_method(re2_cSet, "match", RUBY_METHOD_FUNC(re2_set_match), -1);
rb_define_method(re2_cSet, "size", RUBY_METHOD_FUNC(re2_set_size), 0);
rb_define_method(re2_cSet, "length", RUBY_METHOD_FUNC(re2_set_size), 0);
rb_define_module_function(re2_mRE2, "Replace",
RUBY_METHOD_FUNC(re2_Replace), 3);
rb_define_module_function(re2_mRE2, "GlobalReplace",
RUBY_METHOD_FUNC(re2_GlobalReplace), 3);
rb_define_module_function(re2_mRE2, "QuoteMeta",
RUBY_METHOD_FUNC(re2_QuoteMeta), 1);
rb_define_singleton_method(re2_cRegexp, "escape",
RUBY_METHOD_FUNC(re2_QuoteMeta), 1);
rb_define_singleton_method(re2_cRegexp, "quote",
RUBY_METHOD_FUNC(re2_QuoteMeta), 1);
// (see RE2::Regexp#initialize)
rb_define_singleton_method(re2_cRegexp, "compile",
RUBY_METHOD_FUNC(rb_class_new_instance), -1);
rb_define_module_function(rb_mKernel, "RE2", RUBY_METHOD_FUNC(re2_re2), -1);
/* Create the symbols used in options. */
id_utf8 = rb_intern("utf8");
id_posix_syntax = rb_intern("posix_syntax");
id_longest_match = rb_intern("longest_match");
id_log_errors = rb_intern("log_errors");
id_max_mem = rb_intern("max_mem");
id_literal = rb_intern("literal");
id_never_nl = rb_intern("never_nl");
id_case_sensitive = rb_intern("case_sensitive");
id_perl_classes = rb_intern("perl_classes");
id_word_boundary = rb_intern("word_boundary");
id_one_line = rb_intern("one_line");
id_unanchored = rb_intern("unanchored");
id_anchor = rb_intern("anchor");
id_anchor_start = rb_intern("anchor_start");
id_anchor_both = rb_intern("anchor_both");
id_exception = rb_intern("exception");
id_submatches = rb_intern("submatches");
id_startpos = rb_intern("startpos");
id_endpos = rb_intern("endpos");
}
|