From c93b644cdab5d882ea379d8cfa883d7b076343a5 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 15 Jul 2021 07:26:59 -0700 Subject: abs-path-p: rewrite in lower-level C. * stream.c (ap_regex): Static variable removed. (volume_prefix_p): New function. (abs_path_p, portable_abs_path_p): Get wchar_t * string from path and manipulate using C idioms. Use volume_prefix_p function for testing for drive letter or UNC prefix. (stream_init): Remove reference to ap_regex. --- stream.c | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/stream.c b/stream.c index 63bc3988..e3be10f1 100644 --- a/stream.c +++ b/stream.c @@ -4888,46 +4888,58 @@ static val open_files_star(val file_list, val substitute_stream, val mode) } } -static val ap_regex; +static val volume_prefix_p(const wchar_t *str) +{ + enum { init, slash } state; + + for (state = init; *str; str++) { + wchar_t ch = *str; + switch (state) { + case init: + if (iswalnum(ch)) + continue; + if (ch == ':') { + state = slash; + continue; + } + return nil; + case slash: + if (ch == '/' || ch == '\\') + return t; + return nil; + } + } + + return nil; +} val portable_abs_path_p(val path) { - val ch; + val self = lit("portable-abs-path-p"); + const wchar_t *str = c_str(path, self); - if (length(path) == zero) + if (*str == 0) return nil; - if ((ch = chr_str(path, zero)) == chr('/') || ch == chr('\\')) - return t; - - if (!ap_regex) - ap_regex = regex_compile(lit("[A-Za-z0-9]+:[/\\\\]"), nil); - - if (match_regex(path, ap_regex, zero)) + if (str[0] == '/' || str[0] == '\\') return t; - - return nil; + return volume_prefix_p(str); } val abs_path_p(val path) { + val self = lit("abs-path-p"); const wchar_t *psc = coerce(const wchar_t *, path_sep_chars); + const wchar_t *str = c_str(path, self); - if (length(path) == zero) + if (*str == 0) return nil; - - if (wcschr(psc, c_chr(chr_str(path, zero)))) + if (wcschr(psc, str[0])) return t; if (psc[0] != '\\') return nil; - if (!ap_regex) - ap_regex = regex_compile(lit("[A-Za-z0-9]+:[/\\\\]"), nil); - - if (match_regex(path, ap_regex, zero)) - return t; - - return nil; + return volume_prefix_p(str); } static val plp_regex; @@ -5366,7 +5378,6 @@ void iobuf_list_empty(void) void stream_init(void) { - prot1(&ap_regex); prot1(&plp_regex); prot1(&top_stderr); -- cgit v1.2.3