From 3ff4cb6c21c4908cf6d1f561282ac6e9cb95270e Mon Sep 17 00:00:00 2001 From: Ray Gardner Date: Fri, 12 Apr 2024 17:08:25 -0600 Subject: [PATCH] Merge rx_escape_str() into escape_str(), remove new_zstring_cap() Cleanup to remove/combine code Co-Authored-By: Oliver Webb --- toys/pending/awk.c | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/toys/pending/awk.c b/toys/pending/awk.c index 63e5ffdb..fb824803 100644 --- a/toys/pending/awk.c +++ b/toys/pending/awk.c @@ -2493,15 +2493,16 @@ static struct zstring *num_to_zstring(double n, char *fmt) //// regex routines //////////////////// -static char *rx_escape_str(char *s) +static char *escape_str(char *s, int is_regex) { - char *p, *escapes = "abfnrtv\"/"; // FIXME TODO should / be in there? + char *p, *escapes = is_regex ? "abfnrtv\"/" : "\\abfnrtv\"/"; + // FIXME TODO should / be in there? char *s0 = s, *to = s; while ((*to = *s)) { if (*s != '\\') { to++, s++; } else if ((p = strchr(escapes, *++s))) { // checking char after \ for known escapes - int c = "\a\b\f\n\r\t\v\"/"[p-escapes]; + int c = (is_regex?"\a\b\f\n\r\t\v\"/":"\\\a\b\f\n\r\t\v\"/")[p-escapes]; if (c) *to = c, s++; // else final backslash to++; } else if ('0' <= *s && *s <= '9') { @@ -2515,7 +2516,10 @@ static char *rx_escape_str(char *s) if (isxdigit(s[1])) c = c * 16 + hexval(*++s); *to++ = c, s++; } - } else *to++ = '\\', *to++ = *s++; + } else { + if (is_regex) *to++ = '\\'; + *to++ = *s++; + } } return s0; } @@ -2526,7 +2530,7 @@ static void rx_zvalue_compile(regex_t **rx, struct zvalue *pat) else { val_to_str(pat); zvalue_dup_zstring(pat); - rx_escape_str(pat->vst->str); + escape_str(pat->vst->str, 1); xregcomp(*rx, pat->vst->str, REG_EXTENDED); } } @@ -3169,33 +3173,6 @@ static void varprint(int(*fpvar)(FILE *, const char *, ...), FILE *outfp, int na } } -static char *escape_str(char *s) -{ - char *p, *escapes = "\\abfnrtv\"/"; // FIXME TODO should / be in there? - char *s0 = s, *to = s; - while ((*to = *s)) { - if (*s != '\\') to++, s++; - else if ((p = strchr(escapes, *++s))) { - // checking char after \ for known escapes - int c = "\\\a\b\f\n\r\t\v\"/"[p-escapes]; - if (c) *to = c, s++; // else final backslash - to++; - } else if ('0' <= *s && *s <= '9') { - int k, c = *s++ - '0'; - for (k = 0; k < 2 && '0' <= *s && *s <= '9'; k++) - c = c * 8 + *s++ - '0'; - *to++ = c; - } else if (*s == 'x') { - if (isxdigit(s[1])) { - int c = hexval(*++s); - if (isxdigit(s[1])) c = c * 16 + hexval(*++s); - *to++ = c, s++; - } - } else *to++ = *s++; - } - return s0; -} - static int is_ok_varname(char *v) { char *ok = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; @@ -3227,7 +3204,7 @@ static int assign_global(char *var, char *value) zvalue_release_zstring(v); value = xstrdup(value); - *v = new_str_val(escape_str(value)); + *v = new_str_val(escape_str(value, 0)); xfree(value); check_numeric_string(v); return 1; @@ -4501,7 +4478,7 @@ static int awk(char *sepstring, char *progstring, struct arg_list *prog_args, void awk_main(void) { - char *sepstring = TT.F ? escape_str(TT.F) : " "; + char *sepstring = TT.F ? escape_str(TT.F, 0) : " "; int optind = 0; char *progstring = NULL; -- 2.39.2