| 1 | //go:build tinygo.wasm || re2_cgo |
| 2 | |
| 3 | /* |
| 4 | Source file for CRE2, a C language wrapper for RE2: a regular |
| 5 | expressions library by Google. |
| 6 | |
| 7 | Copyright (c) 2012, 2016, 2019 Marco Maggi <[email protected]> |
| 8 | Copyright (c) 2011 Keegan McAllister |
| 9 | All rights reserved. |
| 10 | |
| 11 | For the license notice see the COPYING file. |
| 12 | */ |
| 13 | |
| 14 | #ifdef HAVE_CONFIG_H |
| 15 | # include <config.h> |
| 16 | #endif |
| 17 | #include <re2/re2.h> |
| 18 | #include <re2/set.h> |
| 19 | #include "cre2.h" |
| 20 | |
| 21 | #include <cstdlib> |
| 22 | #include <cstdio> |
| 23 | #include <cstring> |
| 24 | #include <vector> |
| 25 | |
| 26 | |
| 27 | /** -------------------------------------------------------------------- |
| 28 | ** Options objects. |
| 29 | ** ----------------------------------------------------------------- */ |
| 30 | |
| 31 | /* Cast the pointer argument "opt" to a pointer of type |
| 32 | "RE2::Options*". */ |
| 33 | #define TO_OPT(opt) (reinterpret_cast<RE2::Options *>(opt)) |
| 34 | |
| 35 | cre2_options_t * |
| 36 | cre2_opt_new(void) |
| 37 | /* Allocate and return a new options object. */ |
| 38 | { |
| 39 | // FIXME: is this use of "nothrow" good to avoid raising exceptions |
| 40 | // when memory allocation fails and to return NULL instead? |
| 41 | return reinterpret_cast<void*>(new (std::nothrow) RE2::Options()); |
| 42 | } |
| 43 | void |
| 44 | cre2_opt_delete (cre2_options_t *opt) |
| 45 | /* Finalise an options object. */ |
| 46 | { |
| 47 | delete TO_OPT(opt); |
| 48 | } |
| 49 | |
| 50 | /* Set or unset option flags in an options object. */ |
| 51 | #define OPT_BOOL(name) \ |
| 52 | void cre2_opt_set_##name (cre2_options_t *opt, int flag) \ |
| 53 | { \ |
| 54 | TO_OPT(opt)->set_##name(bool(flag)); \ |
| 55 | } \ |
| 56 | int cre2_opt_##name (cre2_options_t *opt) \ |
| 57 | { \ |
| 58 | return TO_OPT(opt)->name(); \ |
| 59 | } |
| 60 | OPT_BOOL(posix_syntax) |
| 61 | OPT_BOOL(longest_match) |
| 62 | OPT_BOOL(log_errors) |
| 63 | OPT_BOOL(literal) |
| 64 | OPT_BOOL(never_nl) |
| 65 | OPT_BOOL(dot_nl) |
| 66 | OPT_BOOL(never_capture) |
| 67 | OPT_BOOL(case_sensitive) |
| 68 | OPT_BOOL(perl_classes) |
| 69 | OPT_BOOL(word_boundary) |
| 70 | OPT_BOOL(one_line) |
| 71 | #undef OPT_BOOL |
| 72 | |
| 73 | void |
| 74 | cre2_opt_set_encoding (cre2_options_t *opt, cre2_encoding_t enc) |
| 75 | /* Select the encoding in an options object. */ |
| 76 | { |
| 77 | switch (enc) { |
| 78 | case CRE2_UTF8: |
| 79 | TO_OPT(opt)->set_encoding(RE2::Options::EncodingUTF8); |
| 80 | break; |
| 81 | case CRE2_Latin1: |
| 82 | TO_OPT(opt)->set_encoding(RE2::Options::EncodingLatin1); |
| 83 | break; |
| 84 | default: |
| 85 | fprintf(stderr, "CRE2: internal error: unknown encoding %d\n", enc); |
| 86 | exit(EXIT_FAILURE); |
| 87 | } |
| 88 | } |
| 89 | cre2_encoding_t |
| 90 | cre2_opt_encoding (cre2_options_t *opt) |
| 91 | { |
| 92 | RE2::Options::Encoding E = TO_OPT(opt)->encoding(); |
| 93 | switch (E) { |
| 94 | case RE2::Options::EncodingUTF8: |
| 95 | return CRE2_UTF8; |
| 96 | case RE2::Options::EncodingLatin1: |
| 97 | return CRE2_Latin1; |
| 98 | default: |
| 99 | return CRE2_UNKNOWN; |
| 100 | } |
| 101 | } |
| 102 | void |
| 103 | cre2_opt_set_max_mem (cre2_options_t *opt, int64_t m) |
| 104 | /* Configure the maximum amount of memory in an options object. */ |
| 105 | { |
| 106 | TO_OPT(opt)->set_max_mem(m); |
| 107 | } |
| 108 | int64_t |
| 109 | cre2_opt_max_mem (cre2_options_t *opt) |
| 110 | { |
| 111 | return TO_OPT(opt)->max_mem(); |
| 112 | } |
| 113 | void |
| 114 | cre2_opt_set_latin1_encoding(cre2_options_t *opt) |
| 115 | { |
| 116 | TO_OPT(opt)->set_encoding(RE2::Options::EncodingLatin1); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** -------------------------------------------------------------------- |
| 121 | ** Precompiled regular expressions objects. |
| 122 | ** ----------------------------------------------------------------- */ |
| 123 | |
| 124 | #define TO_RE2(re) (reinterpret_cast<RE2 *>(re)) |
| 125 | #define TO_CONST_RE2(re) (reinterpret_cast<const RE2 *>(re)) |
| 126 | |
| 127 | cre2_regexp_t * |
| 128 | cre2_new (const char *pattern, int pattern_len, const cre2_options_t *opt) |
| 129 | { |
| 130 | re2::StringPiece pattern_re2(pattern, pattern_len); |
| 131 | if (opt) { |
| 132 | // FIXME: is this use of "nothrow" enough to avoid raising |
| 133 | // exceptions when memory allocation fails and to return NULL |
| 134 | // instead? |
| 135 | return reinterpret_cast<void*> |
| 136 | (new (std::nothrow) RE2(pattern_re2, *reinterpret_cast<const RE2::Options *>(opt))); |
| 137 | } else { |
| 138 | return reinterpret_cast<void*> (new (std::nothrow) RE2(pattern_re2)); |
| 139 | } |
| 140 | } |
| 141 | void |
| 142 | cre2_delete (cre2_regexp_t *re) |
| 143 | { |
| 144 | delete TO_RE2(re); |
| 145 | } |
| 146 | const char * |
| 147 | cre2_pattern (const cre2_regexp_t *re) |
| 148 | { |
| 149 | return TO_CONST_RE2(re)->pattern().c_str(); |
| 150 | } |
| 151 | int |
| 152 | cre2_error_code (const cre2_regexp_t *re) |
| 153 | { |
| 154 | return int(TO_CONST_RE2(re)->error_code()); |
| 155 | } |
| 156 | const char * |
| 157 | cre2_error_string (const cre2_regexp_t *re) |
| 158 | { |
| 159 | return TO_CONST_RE2(re)->error().c_str(); |
| 160 | } |
| 161 | const char * |
| 162 | cre2_error_arg (const cre2_regexp_t *re) |
| 163 | { |
| 164 | return TO_CONST_RE2(re)->error_arg().c_str(); |
| 165 | } |
| 166 | int |
| 167 | cre2_num_capturing_groups (const cre2_regexp_t *re) |
| 168 | { |
| 169 | return TO_CONST_RE2(re)->NumberOfCapturingGroups(); |
| 170 | } |
| 171 | int |
| 172 | cre2_find_named_capturing_groups (const cre2_regexp_t *re, const char* group_name) |
| 173 | { |
| 174 | const std::map<std::string, int>& m = TO_CONST_RE2(re)->NamedCapturingGroups(); |
| 175 | std::map<std::string, int>::const_iterator it = m.find(group_name); |
| 176 | |
| 177 | if(it != m.end()) { |
| 178 | return it->second; |
| 179 | } |
| 180 | else { |
| 181 | return -1; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | int |
| 186 | cre2_program_size (const cre2_regexp_t *re) |
| 187 | { |
| 188 | return TO_CONST_RE2(re)->ProgramSize(); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /** -------------------------------------------------------------------- |
| 193 | ** Named capture group iteration. |
| 194 | ** ----------------------------------------------------------------- */ |
| 195 | |
| 196 | struct cre2_named_groups_iter_t |
| 197 | { |
| 198 | const RE2 * re; |
| 199 | std::map<int, std::string>::const_iterator it; |
| 200 | }; |
| 201 | |
| 202 | cre2_named_groups_iter_t * |
| 203 | cre2_named_groups_iter_new(const cre2_regexp_t *re) |
| 204 | { |
| 205 | cre2_named_groups_iter_t * iter = new (std::nothrow)cre2_named_groups_iter_t; |
| 206 | iter->re = TO_CONST_RE2(re); |
| 207 | iter->it = iter->re->CapturingGroupNames().begin(); |
| 208 | return iter; |
| 209 | } |
| 210 | bool |
| 211 | cre2_named_groups_iter_next(cre2_named_groups_iter_t* iter, const char ** name, int *index) |
| 212 | { |
| 213 | if (iter->it == iter->re->CapturingGroupNames().end()) { |
| 214 | *name = NULL; |
| 215 | *index = -1; |
| 216 | return false; |
| 217 | } else { |
| 218 | *index = iter->it->first; |
| 219 | *name = iter->it->second.c_str(); |
| 220 | ++iter->it; |
| 221 | return true; |
| 222 | } |
| 223 | } |
| 224 | void |
| 225 | cre2_named_groups_iter_delete(cre2_named_groups_iter_t *iter) |
| 226 | { |
| 227 | delete iter; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /** -------------------------------------------------------------------- |
| 232 | ** Matching with precompiled regular expressions objects. |
| 233 | ** ----------------------------------------------------------------- */ |
| 234 | |
| 235 | static RE2::Anchor |
| 236 | to_cre2_anchor(cre2_anchor_t anchor) |
| 237 | { |
| 238 | RE2::Anchor anchor_re2 = RE2::UNANCHORED; |
| 239 | switch (anchor) { |
| 240 | case CRE2_ANCHOR_START: |
| 241 | anchor_re2 = RE2::ANCHOR_START; |
| 242 | break; |
| 243 | case CRE2_ANCHOR_BOTH: |
| 244 | anchor_re2 = RE2::ANCHOR_BOTH; |
| 245 | break; |
| 246 | case CRE2_UNANCHORED: |
| 247 | break; |
| 248 | } |
| 249 | return anchor_re2; |
| 250 | } |
| 251 | |
| 252 | int |
| 253 | cre2_match (const cre2_regexp_t *re , const char *text, |
| 254 | int textlen, int startpos, int endpos, cre2_anchor_t anchor, |
| 255 | cre2_string_t *match, int nmatch) |
| 256 | { |
| 257 | re2::StringPiece text_re2(text, textlen); |
| 258 | std::vector<re2::StringPiece> match_re2(nmatch); |
| 259 | RE2::Anchor anchor_re2 = to_cre2_anchor(anchor); |
| 260 | bool retval; // 0 for no match |
| 261 | // 1 for successful matching |
| 262 | retval = TO_CONST_RE2(re)->Match(text_re2, startpos, endpos, anchor_re2, match_re2.data(), nmatch); |
| 263 | if (retval) { |
| 264 | for (int i=0; i<nmatch; i++) { |
| 265 | match[i].data = match_re2[i].data(); |
| 266 | match[i].length = match_re2[i].length(); |
| 267 | } |
| 268 | } |
| 269 | return (retval)? 1 : 0; |
| 270 | } |
| 271 | int |
| 272 | cre2_easy_match (const char * pattern, int pattern_len, |
| 273 | const char *text, int text_len, |
| 274 | cre2_string_t *match, int nmatch) |
| 275 | { |
| 276 | cre2_regexp_t * rex; |
| 277 | cre2_options_t * opt; |
| 278 | int retval; // 0 for no match, 1 for successful |
| 279 | // matching, 2 for wrong regexp |
| 280 | opt = cre2_opt_new(); |
| 281 | if (!opt) return 2; |
| 282 | cre2_opt_set_log_errors(opt, 0); |
| 283 | rex = cre2_new(pattern, pattern_len, opt); |
| 284 | if (!rex) { |
| 285 | cre2_opt_delete(opt); |
| 286 | return 2; |
| 287 | } |
| 288 | { |
| 289 | if (!cre2_error_code(rex)) { |
| 290 | retval = cre2_match(rex, text, text_len, 0, text_len, CRE2_UNANCHORED, match, nmatch); |
| 291 | } else { |
| 292 | retval = 2; |
| 293 | } |
| 294 | } |
| 295 | cre2_delete(rex); |
| 296 | cre2_opt_delete(opt); |
| 297 | return retval; |
| 298 | } |
| 299 | void |
| 300 | cre2_strings_to_ranges (const char * text, cre2_range_t * ranges, cre2_string_t * strings, int nmatch) |
| 301 | { |
| 302 | for (int i=0; i<nmatch; ++i) { |
| 303 | ranges[i].start = strings[i].data - text; |
| 304 | ranges[i].past = ranges[i].start + strings[i].length; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /** -------------------------------------------------------------------- |
| 310 | ** Other matching functions: stringz pattern. |
| 311 | ** ----------------------------------------------------------------- */ |
| 312 | |
| 313 | #define DEFINE_MATCH_ZSTRING_FUN(NAME,FUN) \ |
| 314 | int \ |
| 315 | NAME (const char * pattern, const cre2_string_t * text, \ |
| 316 | cre2_string_t * match, int nmatch) \ |
| 317 | { \ |
| 318 | re2::StringPiece input(text->data, text->length); \ |
| 319 | std::vector<re2::StringPiece> strv(nmatch); \ |
| 320 | std::vector<RE2::Arg> argv(nmatch); \ |
| 321 | std::vector<RE2::Arg*> args(nmatch); \ |
| 322 | bool retval; \ |
| 323 | for (int i=0; i<nmatch; ++i) { \ |
| 324 | argv[i] = &(strv.data())[i]; \ |
| 325 | args[i] = &(argv.data())[i]; \ |
| 326 | } \ |
| 327 | retval = RE2::FUN(input, pattern, args.data(), nmatch); \ |
| 328 | if (retval) { \ |
| 329 | for (int i=0; i<nmatch; ++i) { \ |
| 330 | match[i].data = strv[i].data(); \ |
| 331 | match[i].length = strv[i].length(); \ |
| 332 | } \ |
| 333 | } \ |
| 334 | return int(retval); \ |
| 335 | } |
| 336 | |
| 337 | DEFINE_MATCH_ZSTRING_FUN(cre2_full_match,FullMatchN) |
| 338 | DEFINE_MATCH_ZSTRING_FUN(cre2_partial_match,PartialMatchN) |
| 339 | |
| 340 | /* This is different from the above in that the "input" argument is |
| 341 | mutated to reference the text after the mathing portion. */ |
| 342 | #define DEFINE_MATCH_ZSTRING_FUN2(NAME,FUN) \ |
| 343 | int \ |
| 344 | NAME (const char * pattern, cre2_string_t * text, \ |
| 345 | cre2_string_t * match, int nmatch) \ |
| 346 | { \ |
| 347 | re2::StringPiece input(text->data, text->length); \ |
| 348 | std::vector<re2::StringPiece> strv(nmatch); \ |
| 349 | std::vector<RE2::Arg> argv(nmatch); \ |
| 350 | std::vector<RE2::Arg*> args(nmatch); \ |
| 351 | bool retval; \ |
| 352 | for (int i=0; i<nmatch; ++i) { \ |
| 353 | argv[i] = &(strv.data())[i]; \ |
| 354 | args[i] = &(argv.data())[i]; \ |
| 355 | } \ |
| 356 | retval = RE2::FUN(&input, pattern, args.data(), nmatch); \ |
| 357 | if (retval) { \ |
| 358 | text->data = input.data(); \ |
| 359 | text->length = input.length(); \ |
| 360 | for (int i=0; i<nmatch; ++i) { \ |
| 361 | match[i].data = strv[i].data(); \ |
| 362 | match[i].length = strv[i].length(); \ |
| 363 | } \ |
| 364 | } \ |
| 365 | return int(retval); \ |
| 366 | } |
| 367 | |
| 368 | DEFINE_MATCH_ZSTRING_FUN2(cre2_consume,ConsumeN) |
| 369 | DEFINE_MATCH_ZSTRING_FUN2(cre2_find_and_consume,FindAndConsumeN) |
| 370 | |
| 371 | |
| 372 | /** -------------------------------------------------------------------- |
| 373 | ** Other matching functions: rex pattern. |
| 374 | ** ----------------------------------------------------------------- */ |
| 375 | |
| 376 | #define DEFINE_MATCH_REX_FUN(NAME,FUN) \ |
| 377 | int \ |
| 378 | NAME (cre2_regexp_t * rex, const cre2_string_t * text, \ |
| 379 | cre2_string_t * match, int nmatch) \ |
| 380 | { \ |
| 381 | re2::StringPiece input(text->data, text->length); \ |
| 382 | std::vector<re2::StringPiece> strv(nmatch); \ |
| 383 | std::vector<RE2::Arg> argv(nmatch); \ |
| 384 | std::vector<RE2::Arg*> args(nmatch); \ |
| 385 | bool retval; \ |
| 386 | for (int i=0; i<nmatch; ++i) { \ |
| 387 | argv[i] = &(strv.data())[i]; \ |
| 388 | args[i] = &(argv.data())[i]; \ |
| 389 | } \ |
| 390 | retval = RE2::FUN(input, *TO_RE2(rex), args.data(), nmatch); \ |
| 391 | if (retval) { \ |
| 392 | for (int i=0; i<nmatch; ++i) { \ |
| 393 | match[i].data = strv[i].data(); \ |
| 394 | match[i].length = strv[i].length(); \ |
| 395 | } \ |
| 396 | } \ |
| 397 | return int(retval); \ |
| 398 | } |
| 399 | |
| 400 | DEFINE_MATCH_REX_FUN(cre2_full_match_re,FullMatchN) |
| 401 | DEFINE_MATCH_REX_FUN(cre2_partial_match_re,PartialMatchN) |
| 402 | |
| 403 | /* This is different from the above in that the "input" argument is |
| 404 | mutated to reference the text after the mathing portion. */ |
| 405 | #define DEFINE_MATCH_REX_FUN2(NAME,FUN) \ |
| 406 | int \ |
| 407 | NAME (cre2_regexp_t * rex, cre2_string_t * text, \ |
| 408 | cre2_string_t * match, int nmatch) \ |
| 409 | { \ |
| 410 | re2::StringPiece input(text->data, text->length); \ |
| 411 | std::vector<re2::StringPiece> strv(nmatch); \ |
| 412 | std::vector<RE2::Arg> argv(nmatch); \ |
| 413 | std::vector<RE2::Arg*> args(nmatch); \ |
| 414 | bool retval; \ |
| 415 | for (int i=0; i<nmatch; ++i) { \ |
| 416 | argv[i] = &(strv.data())[i]; \ |
| 417 | args[i] = &(argv.data())[i]; \ |
| 418 | } \ |
| 419 | retval = RE2::FUN(&input, *TO_RE2(rex), args.data(), nmatch); \ |
| 420 | if (retval) { \ |
| 421 | text->data = input.data(); \ |
| 422 | text->length = input.length(); \ |
| 423 | for (int i=0; i<nmatch; ++i) { \ |
| 424 | match[i].data = strv[i].data(); \ |
| 425 | match[i].length = strv[i].length(); \ |
| 426 | } \ |
| 427 | } \ |
| 428 | return int(retval); \ |
| 429 | } |
| 430 | |
| 431 | DEFINE_MATCH_REX_FUN2(cre2_consume_re,ConsumeN) |
| 432 | DEFINE_MATCH_REX_FUN2(cre2_find_and_consume_re,FindAndConsumeN) |
| 433 | |
| 434 | |
| 435 | /** -------------------------------------------------------------------- |
| 436 | ** Problematic functions. |
| 437 | ** ----------------------------------------------------------------- */ |
| 438 | |
| 439 | /* The following functions rely on C++ memory allocation. It is not |
| 440 | clear how they can be written to allow a correct API towards C. */ |
| 441 | |
| 442 | int |
| 443 | cre2_replace (const char * pattern, cre2_string_t * text_and_target, cre2_string_t * rewrite) |
| 444 | { |
| 445 | std::string S(text_and_target->data, text_and_target->length); |
| 446 | re2::StringPiece R(rewrite->data, rewrite->length); |
| 447 | char * buffer; /* this exists to make GCC shut up about const */ |
| 448 | bool retval; |
| 449 | retval = RE2::Replace(&S, pattern, R); |
| 450 | text_and_target->length = S.length(); |
| 451 | buffer = (char *)malloc(1+text_and_target->length); |
| 452 | if (buffer) { |
| 453 | S.copy(buffer, text_and_target->length); |
| 454 | buffer[text_and_target->length] = '\0'; |
| 455 | text_and_target->data = buffer; |
| 456 | } else |
| 457 | return -1; |
| 458 | return int(retval); |
| 459 | } |
| 460 | int |
| 461 | cre2_replace_re (cre2_regexp_t * rex, cre2_string_t * text_and_target, cre2_string_t * rewrite) |
| 462 | { |
| 463 | std::string S(text_and_target->data, text_and_target->length); |
| 464 | re2::StringPiece R(rewrite->data, rewrite->length); |
| 465 | char * buffer; /* this exists to make GCC shut up about const */ |
| 466 | bool retval; |
| 467 | retval = RE2::Replace(&S, *TO_RE2(rex), R); |
| 468 | text_and_target->length = S.length(); |
| 469 | buffer = (char *)malloc(1+text_and_target->length); |
| 470 | if (buffer) { |
| 471 | S.copy(buffer, text_and_target->length); |
| 472 | buffer[text_and_target->length] = '\0'; |
| 473 | text_and_target->data = buffer; |
| 474 | } else |
| 475 | return -1; |
| 476 | return int(retval); |
| 477 | } |
| 478 | |
| 479 | /* ------------------------------------------------------------------ */ |
| 480 | |
| 481 | int |
| 482 | cre2_global_replace (const char * pattern, cre2_string_t * text_and_target, cre2_string_t * rewrite) |
| 483 | { |
| 484 | std::string S(text_and_target->data, text_and_target->length); |
| 485 | re2::StringPiece R(rewrite->data, rewrite->length); |
| 486 | char * buffer; /* this exists to make GCC shut up about const */ |
| 487 | int retval; |
| 488 | retval = RE2::GlobalReplace(&S, pattern, R); |
| 489 | text_and_target->length = S.length(); |
| 490 | buffer = (char *)malloc(1+text_and_target->length); |
| 491 | if (buffer) { |
| 492 | S.copy(buffer, text_and_target->length); |
| 493 | buffer[text_and_target->length] = '\0'; |
| 494 | text_and_target->data = buffer; |
| 495 | } else |
| 496 | return -1; |
| 497 | return int(retval); |
| 498 | } |
| 499 | int |
| 500 | cre2_global_replace_re (cre2_regexp_t * rex, cre2_string_t * text_and_target, cre2_string_t * rewrite) |
| 501 | { |
| 502 | std::string S(text_and_target->data, text_and_target->length); |
| 503 | re2::StringPiece R(rewrite->data, rewrite->length); |
| 504 | char * buffer; /* this exists to make GCC shut up about const */ |
| 505 | int retval; |
| 506 | retval = RE2::GlobalReplace(&S, *TO_RE2(rex), R); |
| 507 | text_and_target->length = S.length(); |
| 508 | buffer = (char *)malloc(1+text_and_target->length); |
| 509 | if (buffer) { |
| 510 | S.copy(buffer, text_and_target->length); |
| 511 | buffer[text_and_target->length] = '\0'; |
| 512 | text_and_target->data = buffer; |
| 513 | } else |
| 514 | return -1; |
| 515 | return retval; |
| 516 | } |
| 517 | |
| 518 | /* ------------------------------------------------------------------ */ |
| 519 | |
| 520 | int |
| 521 | cre2_extract (const char * pattern, cre2_string_t * text, |
| 522 | cre2_string_t * rewrite, cre2_string_t * target) |
| 523 | { |
| 524 | re2::StringPiece T(text->data, text->length); |
| 525 | re2::StringPiece R(rewrite->data, rewrite->length); |
| 526 | std::string O; |
| 527 | char * buffer; /* this exists to make GCC shut up about const */ |
| 528 | bool retval; |
| 529 | retval = RE2::Extract(T, pattern, R, &O); |
| 530 | target->length = O.length(); |
| 531 | buffer = (char *)malloc(1+target->length); |
| 532 | if (buffer) { |
| 533 | O.copy(buffer, target->length); |
| 534 | buffer[target->length] = '\0'; |
| 535 | target->data = buffer; |
| 536 | } else |
| 537 | return -1; |
| 538 | return int(retval); |
| 539 | } |
| 540 | int |
| 541 | cre2_extract_re (cre2_regexp_t * rex, cre2_string_t * text, |
| 542 | cre2_string_t * rewrite, cre2_string_t * target) |
| 543 | { |
| 544 | re2::StringPiece T(text->data, text->length); |
| 545 | re2::StringPiece R(rewrite->data, rewrite->length); |
| 546 | std::string O; |
| 547 | char * buffer; /* this exists to make GCC shut up about const */ |
| 548 | bool retval; |
| 549 | retval = RE2::Extract(T, *TO_RE2(rex), R, &O); |
| 550 | target->length = O.length(); |
| 551 | buffer = (char *)malloc(1+target->length); |
| 552 | if (buffer) { |
| 553 | O.copy(buffer, target->length); |
| 554 | buffer[target->length] = '\0'; |
| 555 | target->data = buffer; |
| 556 | } else |
| 557 | return -1; |
| 558 | return int(retval); |
| 559 | } |
| 560 | |
| 561 | /* ------------------------------------------------------------------ */ |
| 562 | |
| 563 | int |
| 564 | cre2_quote_meta (cre2_string_t * quoted, cre2_string_t * original) |
| 565 | { |
| 566 | re2::StringPiece O(original->data, original->length); |
| 567 | std::string Q; |
| 568 | char * buffer; /* this exists to make GCC shut up about const */ |
| 569 | Q = RE2::QuoteMeta(O); |
| 570 | quoted->length = Q.length(); |
| 571 | buffer = (char *)malloc(1+quoted->length); |
| 572 | if (buffer) { |
| 573 | Q.copy(buffer, quoted->length); |
| 574 | buffer[quoted->length] = '\0'; |
| 575 | quoted->data = buffer; |
| 576 | return 0; |
| 577 | } else |
| 578 | return -1; |
| 579 | } |
| 580 | int |
| 581 | cre2_possible_match_range (cre2_regexp_t * rex, |
| 582 | cre2_string_t * min_, cre2_string_t * max_, int maxlen) |
| 583 | { |
| 584 | std::string MIN, MAX; |
| 585 | cre2_string_t min, max; |
| 586 | char * buffer; /* this exists to make GCC shut up about const */ |
| 587 | bool retval; |
| 588 | retval = TO_RE2(rex)->PossibleMatchRange(&MIN, &MAX, maxlen); |
| 589 | if (retval) { |
| 590 | /* copy MIN */ |
| 591 | min.length = MIN.length(); |
| 592 | buffer = (char *)malloc(1+min.length); |
| 593 | if (buffer) { |
| 594 | MIN.copy(buffer, min.length); |
| 595 | buffer[min.length] = '\0'; |
| 596 | min.data = buffer; |
| 597 | } else |
| 598 | return -1; |
| 599 | /* copy MAX */ |
| 600 | max.length = MAX.length(); |
| 601 | buffer = (char *)malloc(1+max.length); |
| 602 | if (buffer) { |
| 603 | MAX.copy(buffer, max.length); |
| 604 | buffer[max.length] = '\0'; |
| 605 | max.data = buffer; |
| 606 | } else { |
| 607 | free((void *)min.data); |
| 608 | min.data = NULL; |
| 609 | return -1; |
| 610 | } |
| 611 | *min_ = min; |
| 612 | *max_ = max; |
| 613 | return 1; |
| 614 | } else |
| 615 | return 0; |
| 616 | } |
| 617 | int |
| 618 | cre2_check_rewrite_string (cre2_regexp_t * rex, cre2_string_t * rewrite, cre2_string_t * errmsg) |
| 619 | { |
| 620 | re2::StringPiece R(rewrite->data, rewrite->length); |
| 621 | std::string E; |
| 622 | char * buffer; /* this exists to make GCC shut up about const */ |
| 623 | bool retval; |
| 624 | retval = TO_RE2(rex)->CheckRewriteString(R, &E); |
| 625 | if (retval) { |
| 626 | errmsg->data = NULL; |
| 627 | errmsg->length = 0; |
| 628 | return 1; |
| 629 | } else { |
| 630 | errmsg->length = E.length(); |
| 631 | buffer = (char *)malloc(1+errmsg->length); |
| 632 | if (buffer) { |
| 633 | E.copy(buffer, errmsg->length); |
| 634 | buffer[errmsg->length] = '\0'; |
| 635 | errmsg->data = buffer; |
| 636 | } else |
| 637 | return -1; |
| 638 | return 0; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | |
| 643 | /** -------------------------------------------------------------------- |
| 644 | ** Set match. |
| 645 | ** ----------------------------------------------------------------- */ |
| 646 | |
| 647 | #define TO_RE2_SET(set) (reinterpret_cast<RE2::Set *>(set)) |
| 648 | #define TO_CRE2_SET(set) (reinterpret_cast<cre2_set *>(set)) |
| 649 | |
| 650 | // RE2::Set constructor and destructor |
| 651 | cre2_set* |
| 652 | cre2_set_new(cre2_options_t *opt, cre2_anchor_t anchor) |
| 653 | { |
| 654 | return TO_CRE2_SET(new (std::nothrow) RE2::Set(*TO_OPT(opt), to_cre2_anchor(anchor))); |
| 655 | } |
| 656 | |
| 657 | void |
| 658 | cre2_set_delete(cre2_set *set) |
| 659 | { |
| 660 | delete TO_RE2_SET(set); |
| 661 | } |
| 662 | |
| 663 | static char* ok = "ok"; |
| 664 | |
| 665 | // Add a regex to the set. If invalid: return error message that must be freed. If valid, return "ok". |
| 666 | char* |
| 667 | cre2_set_add(cre2_set *set, const char *pattern, size_t pattern_len) |
| 668 | { |
| 669 | RE2::Set *s = TO_RE2_SET(set); |
| 670 | re2::StringPiece regex(pattern, static_cast<int>(pattern_len)); |
| 671 | std::string err; |
| 672 | int regex_index = s->Add(regex, &err); |
| 673 | if (regex_index >= 0) { |
| 674 | return ok; |
| 675 | } |
| 676 | size_t len = err.size() + 1; |
| 677 | void* msg = malloc(len); |
| 678 | if (msg == NULL) { |
| 679 | return NULL; |
| 680 | } |
| 681 | memcpy(msg, err.c_str(), len); |
| 682 | return (char*)msg; |
| 683 | } |
| 684 | |
| 685 | // Add pattern without NULL byte. Don't store error message. |
| 686 | int |
| 687 | cre2_set_add_simple(cre2_set *set, const char *pattern) |
| 688 | { |
| 689 | RE2::Set *s = TO_RE2_SET(set); |
| 690 | re2::StringPiece regex(pattern, static_cast<int>(strlen(pattern))); |
| 691 | return s->Add(regex, NULL); |
| 692 | } |
| 693 | |
| 694 | |
| 695 | // Compile the regex set into a DFA. Must be called after add and before match. |
| 696 | int |
| 697 | cre2_set_compile(cre2_set *set) |
| 698 | { |
| 699 | RE2::Set *s = TO_RE2_SET(set); |
| 700 | return static_cast<int>(s->Compile()); |
| 701 | } |
| 702 | |
| 703 | // Match the set of regex against text and store indices of matching regexes in match array. |
| 704 | // Returns the number of regexes which match. |
| 705 | size_t |
| 706 | cre2_set_match(cre2_set *set, const char *text, size_t text_len, int *match, size_t match_len) |
| 707 | { |
| 708 | RE2::Set *s = TO_RE2_SET(set); |
| 709 | re2::StringPiece data(text, static_cast<int>(text_len)); |
| 710 | std::vector<int> v; |
| 711 | bool does_match = s->Match(data, &v); |
| 712 | if (!does_match) { |
| 713 | return 0; |
| 714 | } |
| 715 | size_t min = v.size() < match_len ? v.size() : match_len; |
| 716 | std::copy(v.begin(), v.begin() + min, match); |
| 717 | return v.size(); |
| 718 | } |
| 719 | |
| 720 | /* end of file */ |