diff options
author | Xavier Leroy <xavier.leroy@inria.fr> | 1998-10-20 12:49:18 +0000 |
---|---|---|
committer | Xavier Leroy <xavier.leroy@inria.fr> | 1998-10-20 12:49:18 +0000 |
commit | 1568b2614a9addf74d0632f2beaaafc6127a2d20 (patch) | |
tree | 050ec6eb6088a8d81390cf51f34531fb78f0eb90 | |
parent | dcc7f0d600cb0f6d3c5b68a5378c5aaf74650758 (diff) |
Patches de Ian Zimmerman (string_partial_match, etc)
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@2127 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r-- | otherlibs/str/regex-0.12/regex.c | 13 | ||||
-rw-r--r-- | otherlibs/str/str.ml | 2 | ||||
-rw-r--r-- | otherlibs/str/str.mli | 4 | ||||
-rw-r--r-- | otherlibs/str/strstubs.c | 46 |
4 files changed, 53 insertions, 12 deletions
diff --git a/otherlibs/str/regex-0.12/regex.c b/otherlibs/str/regex-0.12/regex.c index 3f4ff815f..3798992d8 100644 --- a/otherlibs/str/regex-0.12/regex.c +++ b/otherlibs/str/regex-0.12/regex.c @@ -2261,7 +2261,9 @@ compile_range (p_ptr, pend, translate, syntax, b) exactly that if always used MAX_FAILURE_SPACE each time we failed. This is a variable only so users of regex can assign to it; we never change it ourselves. */ -int re_max_failures = 2000; +/* Xavier Leroy 14/10/1998: bumped from 2000 to 8000 because 2000 + causes failures even with relatively simple patterns */ +int re_max_failures = 8000; typedef const unsigned char *fail_stack_elt_t; @@ -3065,12 +3067,14 @@ typedef union /* Call before fetching a character with *d. This switches over to string2 if necessary. */ +/* itz Mon Sep 21 19:31:55 PDT 1998 set a flag in case we're at the + end, to indicate a partial match was possible. */ #define PREFETCH() \ while (d == dend) \ { \ /* End of string2 => fail. */ \ if (dend == end_match_2) \ - goto fail; \ + { partial = 1; goto fail; } \ /* End of string1 => advance to string2. */ \ d = string2; \ dend = end_match_2; \ @@ -3260,6 +3264,9 @@ re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop) const char **reg_dummy; register_info_type *reg_info_dummy; + /* itz Mon Sep 21 19:34:09 PDT 1998 record a partial match here */ + int partial = 0; + #ifdef DEBUG /* Counts the total number of registers pushed. */ unsigned num_regs_pushed = 0; @@ -4341,7 +4348,7 @@ re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop) FREE_VARIABLES (); - return -1; /* Failure to match. */ + return (partial ? -3 : -1); /* Failure to match. */ } /* re_match_2 */ /* Subroutine definitions for re_match_2. */ diff --git a/otherlibs/str/str.ml b/otherlibs/str/str.ml index 10e784ffc..8bb1a818c 100644 --- a/otherlibs/str/str.ml +++ b/otherlibs/str/str.ml @@ -15,6 +15,8 @@ type regexp external compile_regexp: string -> bool -> regexp = "str_compile_regexp" external string_match: regexp -> string -> int -> bool = "str_string_match" +external string_partial_match: regexp -> string -> int -> bool = + "str_string_partial_match" external search_forward: regexp -> string -> int -> int = "str_search_forward" external search_backward: regexp -> string -> int -> int = "str_search_backward" external beginning_group: int -> int = "str_beginning_group" diff --git a/otherlibs/str/str.mli b/otherlibs/str/str.mli index e80fd1bbb..5d8e5b4d5 100644 --- a/otherlibs/str/str.mli +++ b/otherlibs/str/str.mli @@ -68,6 +68,10 @@ external search_forward: regexp -> string -> int -> int = "str_search_forward" external search_backward: regexp -> string -> int -> int = "str_search_backward" (* Same as [search_forward], but the search proceeds towards the beginning of the string. *) +external string_partial_match: regexp -> string -> int -> bool = "str_string_partial_match" + (* Similar to [string_match], but succeeds whenever the argument + string is a prefix of a string that matches. This includes + the case of a true complete match. *) val matched_string: string -> string (* [matched_string s] returns the substring of [s] that was matched diff --git a/otherlibs/str/strstubs.c b/otherlibs/str/strstubs.c index 55ee4300e..56a205a78 100644 --- a/otherlibs/str/strstubs.c +++ b/otherlibs/str/strstubs.c @@ -72,10 +72,32 @@ static struct re_registers match_regs = { 10, start_regs, end_regs }; value str_string_match(regexp expr, value text, value pos) /* ML */ { - switch (re_match(&(expr->re), String_val(text), string_length(text), - Int_val(pos), &match_regs)) { - case -2: + int len = string_length(text); + int start = Int_val(pos); + if (start < 0 || start > len) invalid_argument("Str.string_match"); + switch (re_match(&(expr->re), String_val(text), len, + start, &match_regs)) { + case -2: + failwith("Str.string_match"); + case -1: + case -3: + return Val_false; + default: + return Val_true; + } +} + +value str_string_partial_match(regexp expr, value text, value pos) /* ML */ +{ + int len = string_length(text); + int start = Int_val(pos); + if (start < 0 || start > len) + invalid_argument("Str.string_partial_match"); + switch (re_match(&(expr->re), String_val(text), len, + start, &match_regs)) { + case -2: + failwith("Str.string_partial_match"); case -1: return Val_false; default: @@ -85,13 +107,16 @@ value str_string_match(regexp expr, value text, value pos) /* ML */ value str_search_forward(regexp expr, value text, value pos) /* ML */ { + int res; int len = string_length(text); int start = Int_val(pos); - int res = re_search(&(expr->re), String_val(text), len, start, len-start, - &match_regs); + if (start < 0 || start > len) + invalid_argument("Str.search_forward"); + res = re_search(&(expr->re), String_val(text), len, start, len-start, + &match_regs); switch(res) { case -2: - invalid_argument("Str.search_forward"); + failwith("Str.search_forward"); case -1: raise_not_found(); default: @@ -101,13 +126,16 @@ value str_search_forward(regexp expr, value text, value pos) /* ML */ value str_search_backward(regexp expr, value text, value pos) /* ML */ { + int res; int len = string_length(text); int start = Int_val(pos); - int res = re_search(&(expr->re), String_val(text), len, start, -start-1, - &match_regs); + if (start < 0 || start > len) + invalid_argument("Str.search_backward"); + res = re_search(&(expr->re), String_val(text), len, start, -start-1, + &match_regs); switch(res) { case -2: - invalid_argument("Str.search_backward"); + failwith("Str.search_backward"); case -1: raise_not_found(); default: |