diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-09-21 21:14:13 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-09-21 21:14:13 -0700 |
commit | cbe8ab16302c0224c2e90dc0dd28e64e80b3fe2b (patch) | |
tree | c23e5679559579ffd2f6dbd30983a45e17e9c80a | |
parent | 9f039218345989a770322611704c3d7ff9a76a12 (diff) | |
download | txr-cbe8ab16302c0224c2e90dc0dd28e64e80b3fe2b.tar.gz txr-cbe8ab16302c0224c2e90dc0dd28e64e80b3fe2b.tar.bz2 txr-cbe8ab16302c0224c2e90dc0dd28e64e80b3fe2b.zip |
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.
-rw-r--r-- | regex.c | 10 | ||||
-rw-r--r-- | txr.1 | 21 |
2 files changed, 29 insertions, 2 deletions
@@ -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)); @@ -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. |