summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2014-07-19 19:19:21 -0700
committerKaz Kylheku <kaz@kylheku.com>2014-07-19 19:19:21 -0700
commit8fe8e46c6dce8292f2c12660d0c383e521f9233a (patch)
tree347e192c467d2140b61ecc77356e18b544535fef /lib.c
parent5f9d1d22af102a8e9d2a769ea02243fb5763fe74 (diff)
downloadtxr-8fe8e46c6dce8292f2c12660d0c383e521f9233a.tar.gz
txr-8fe8e46c6dce8292f2c12660d0c383e521f9233a.tar.bz2
txr-8fe8e46c6dce8292f2c12660d0c383e521f9233a.zip
* lib.c (search_str): Support negative starting index.
Hoist uselessly repeated c_str operation out of loop. * txr.1: Document negative starting index for search-str.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/lib.c b/lib.c
index 0f705211..3f38ab97 100644
--- a/lib.c
+++ b/lib.c
@@ -2145,11 +2145,14 @@ val search_str(val haystack, val needle, val start_num, val from_end)
const wchar_t *n = c_str(needle), *h;
if (!h_is_lazy) {
- do {
- const wchar_t *f;
- h = c_str(haystack);
+ h = c_str(haystack);
+
+ if (start < 0)
+ start += wcslen(h);
- f = wcsstr(h + start, n);
+ nonlazy:
+ do {
+ const wchar_t *f = wcsstr(h + start, n);
if (f)
pos = f - h;
@@ -2159,6 +2162,13 @@ val search_str(val haystack, val needle, val start_num, val from_end)
} else {
size_t ln = c_num(length_str(needle));
+ if (start < 0) {
+ lazy_str_force(haystack);
+ h = c_str(haystack->ls.prefix);
+ start += wcslen(h);
+ goto nonlazy;
+ }
+
do {
lazy_str_force_upto(haystack, plus(num(start + 1), length_str(needle)));
h = c_str(haystack->ls.prefix);