From 8fe8e46c6dce8292f2c12660d0c383e521f9233a Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 19 Jul 2014 19:19:21 -0700 Subject: * 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. --- lib.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'lib.c') 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); -- cgit v1.2.3