summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--lib.c31
-rw-r--r--txr.119
3 files changed, 44 insertions, 13 deletions
diff --git a/ChangeLog b/ChangeLog
index d60aac61..2fab8a4d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2014-06-09 Kaz Kylheku <kaz@kylheku.com>
+ * lib.c (match_str): Extended to suffix testing, with a negative
+ start argument.
+
+ * txr.1: Documented.
+
+2014-06-09 Kaz Kylheku <kaz@kylheku.com>
+
* Makefile: fix broken tests; numerous test cases output
bindings, and need the -B option. One test case does not
need the -l option which now implies -B.
diff --git a/lib.c b/lib.c
index d46ab2e6..b0118f45 100644
--- a/lib.c
+++ b/lib.c
@@ -2077,15 +2077,30 @@ val match_str(val bigstr, val str, val pos)
pos = default_arg(pos, zero);
- for (i = zero;
- length_str_gt(bigstr, p = plus(pos, i)) && length_str_gt(str, i);
- i = plus(i, one))
- {
- if (chr_str(bigstr, p) != chr_str(str, i))
- return nil;
- }
+ if (ge(pos, zero)) {
+ for (i = zero;
+ length_str_gt(bigstr, p = plus(pos, i)) && length_str_gt(str, i);
+ i = plus(i, one))
+ {
+ if (chr_str(bigstr, p) != chr_str(str, i))
+ return nil;
+ }
- return length_str_le(str, i) ? t : nil;
+ return length_str_le(str, i) ? t : nil;
+ } else {
+ pos = plus(pos, length(bigstr));
+ pos = plus(minus(pos, length(str)), one);
+
+ for (i = minus(length(str), one);
+ ge(i, zero) && ge(p = plus(pos, i), zero);
+ i = minus(i, one))
+ {
+ if (chr_str(bigstr, p) != chr_str(str, i))
+ return nil;
+ }
+
+ return lt(i, zero) ? t : nil;
+ }
}
val match_str_tree(val bigstr, val tree, val pos)
diff --git a/txr.1 b/txr.1
index 9ac40b64..1d8a617b 100644
--- a/txr.1
+++ b/txr.1
@@ -8884,12 +8884,21 @@ Syntax:
.TP
Description:
-The match-str function determines how many characters of <littlestring> match a
-prefix of <bigstring>.
+Without the <start> argument, the match-str function determines whether
+<littlestring> is a prefix of <bigstring>, returning a t or nil
+indication.
-If the <start> argument is specified, then the function tests how many
-characters of <littlestring> match a prefix of that portion of <bigstring>
-which starts at the given position.
+If the <start> argument is specified, and is a non-negative integer, then the
+function tests whether <littlestring> matches a prefix of that portion of
+<bigstring> which starts at the given position.
+
+If the <start> argument is a negative integer, then match-str determines
+whether <littlestring> is a suffix of <bigstring>, ending on that position
+of bigstring, where -1 denotes the last character of <bigstring>,
+-2 the second last one and so on.
+
+If <start> is -1, then this corresponds to testing whether <littlestring>
+is a suffix of <bigstring>.
.SS Function match-str-tree