summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-07-15 07:26:59 -0700
committerKaz Kylheku <kaz@kylheku.com>2021-07-15 07:26:59 -0700
commit613ad713e515d0bb0c8c90426194e10501beeaa9 (patch)
tree47c63cb9c9de198992c9a23e5e9407adee937c0c
parent6627e21d09ac492c51059c268a3eb4341e0658ea (diff)
downloadtxr-613ad713e515d0bb0c8c90426194e10501beeaa9.tar.gz
txr-613ad713e515d0bb0c8c90426194e10501beeaa9.tar.bz2
txr-613ad713e515d0bb0c8c90426194e10501beeaa9.zip
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.
-rw-r--r--stream.c57
1 files 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);