From 031384f551286723a1e82b8b729905f22bc15822 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 21 Sep 2016 21:14:13 -0700 Subject: Support negative positions in regex matching funs. * regex.c (match_regex, match_regex_right): Detect a negative start or end position, respectively, and add the string length to it. If it is still negative, bail with nil. * txr.1: Documented. --- regex.c | 10 +++++++++- txr.1 | 21 ++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/regex.c b/regex.c index 2930e2e9..c7a5e132 100644 --- a/regex.c +++ b/regex.c @@ -2464,7 +2464,13 @@ val match_regex(val str, val reg, val pos) val i, retval; regm_result_t last_res = REGM_INCOMPLETE; - pos = default_arg(pos, zero); + if (null_or_missing_p(pos)) { + pos = zero; + } else if (lt(pos, zero)) { + pos = plus(pos, length_str(str)); + if (lt(pos, zero)) + return nil; + } regex_machine_init(®m, reg); @@ -2497,6 +2503,8 @@ val match_regex_right(val str, val regex, val end) if (null_or_missing_p(end) || gt(end, slen)) end = slen; + else if (lt(end, zero)) + end = plus(end, slen); while (le(pos, end)) { cons_bind (from, len, search_regex(str, regex, pos, nil)); diff --git a/txr.1 b/txr.1 index 237be808..205f25cc 100644 --- a/txr.1 +++ b/txr.1 @@ -31748,9 +31748,18 @@ matches at .meta position in .metn string . + If .meta position -is not specified, it is taken to be zero. +is not specified, it is taken to be zero. Negative values +of +.meta position +index from the right end of the string such that -1 +refers to the last character. Excessively negative +values which index before the first character cause +.code nil +to be returned. + If the regex matches, then the length of the match is returned. If it does not match, then .code nil @@ -31778,10 +31787,20 @@ function tests whether contains a match which ends precisely on the character just before .metn end-position . + If .meta end-position is not specified, it defaults to the length of the string, and the function performs a right-anchored regex match. +The +.meta end-position +argument can be a negative integer, in which case it denotes +positions from the end of the string, such that -1 refers +to the last character. If the value is excessively negative such +that the position immediately before it is before the start +of the string, then +.code nil +is returned. If a match is found, then the length of the match is returned. -- cgit v1.2.3