From ccb998581d8ba1e2f8f9c56768d57e32c51777c5 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 3 Jul 2021 16:06:17 -0700 Subject: suffix functions: ignore trailing slashes. Another requirements tweak to short-suffix and long-suffix: ignore one or more trailing slashes, instead of just one. This harmonizes with base-name, which does same, that requirement being copies from the POSIX basename utility. * stream.c (short_suffix, long_suffix): If sl points to a trailing slash which is the start of a suffix that consists of nothing but trailing slashes, then we pretend it isn't there. * tests/018/path.tl: Adjusted two existing test cases, and added more. * txr.1: Documented. --- stream.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'stream.c') diff --git a/stream.c b/stream.c index a2f4baf1..4f4edbe3 100644 --- a/stream.c +++ b/stream.c @@ -5042,8 +5042,9 @@ val short_suffix(val name, val alt_in) const wchar_t *str = c_str(name, self); const wchar_t *dot = wcsrchr(str, '.'); const wchar_t *sl = if3(dot, wcspbrk(dot + 1, psc), 0); + int sl_trail = if3(sl, sl[wcsspn(sl, psc)] == 0, 0); - if (!dot || (sl && sl[1]) || dot == str || wcschr(psc, dot[-1])) { + if (!dot || (sl && sl[1] && !sl_trail) || dot == str || wcschr(psc, dot[-1])) { return default_null_arg(alt_in); } else { wchar_t *suff = chk_strdup(dot); @@ -5063,7 +5064,7 @@ val long_suffix(val name, val alt_in) { const wchar_t *sl; - while (dot && (sl = wcspbrk(dot, psc)) && sl[1]) + while (dot && (sl = wcspbrk(dot, psc)) && sl[1] && sl[wcsspn(sl, psc)] != 0) dot = wcschr(sl + 1, '.'); if (dot && (dot == str || wcschr(psc, dot[-1]))) @@ -5074,7 +5075,7 @@ val long_suffix(val name, val alt_in) } else { wchar_t *suff = chk_strdup(dot); if (sl) - suff[sl - dot] = 0; + suff[sl - dot] = 0; return string_own(suff); } } -- cgit v1.2.3