Module Name: src Committed By: rillig Date: Mon Jun 21 18:12:49 UTC 2021
Modified Files: src/usr.bin/make: var.c Log Message: make: extract RegexReplace from ModifyWord_SubstRegex No functional change. To generate a diff of this commit: cvs rdiff -u -r1.936 -r1.937 src/usr.bin/make/var.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/var.c diff -u src/usr.bin/make/var.c:1.936 src/usr.bin/make/var.c:1.937 --- src/usr.bin/make/var.c:1.936 Mon Jun 21 17:52:33 2021 +++ src/usr.bin/make/var.c Mon Jun 21 18:12:49 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: var.c,v 1.936 2021/06/21 17:52:33 rillig Exp $ */ +/* $NetBSD: var.c,v 1.937 2021/06/21 18:12:49 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1993 @@ -140,7 +140,7 @@ #include "metachar.h" /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ -MAKE_RCSID("$NetBSD: var.c,v 1.936 2021/06/21 17:52:33 rillig Exp $"); +MAKE_RCSID("$NetBSD: var.c,v 1.937 2021/06/21 18:12:49 rillig Exp $"); /* * Variables are defined using one of the VAR=value assignments. Their @@ -1585,6 +1585,53 @@ VarREError(int reerr, const regex_t *pat free(errbuf); } +/* + * Replacement of regular expressions is not specified by POSIX, therefore + * re-implement it here. + */ +static void +RegexReplace(const char *replace, SepBuf *buf, const char *wp, + const regmatch_t *m, size_t nsub) +{ + const char *rp; + size_t n; + + for (rp = replace; *rp != '\0'; rp++) { + if (*rp == '\\' && (rp[1] == '&' || rp[1] == '\\')) { + SepBuf_AddBytes(buf, rp + 1, 1); + rp++; + continue; + } + + if (*rp == '&') { + SepBuf_AddBytesBetween(buf, + wp + m[0].rm_so, wp + m[0].rm_eo); + continue; + } + + if (*rp != '\\' || !ch_isdigit(rp[1])) { + SepBuf_AddBytes(buf, rp, 1); + continue; + } + + /* \0 to \9 backreference */ + n = (size_t)(rp[1] - '0'); + rp++; + + if (n >= nsub) { + Error("No subexpression \\%u", (unsigned)n); + } else if (m[n].rm_so == -1) { + if (opts.strict) { + Error("No match for subexpression \\%u", + (unsigned)n); + } + } else { + SepBuf_AddBytesBetween(buf, + wp + m[n].rm_so, wp + m[n].rm_eo); + } + } +} + struct ModifyWord_SubstRegexArgs { regex_t re; size_t nsub; @@ -1603,10 +1650,8 @@ ModifyWord_SubstRegex(Substring word, Se struct ModifyWord_SubstRegexArgs *args = data; int xrv; const char *wp; - const char *rp; int flags = 0; regmatch_t m[10]; - size_t n; assert(word.end[0] == '\0'); /* assume null-terminated word */ wp = word.start; @@ -1627,45 +1672,7 @@ ok: args->matched = true; SepBuf_AddBytes(buf, wp, (size_t)m[0].rm_so); - /* - * Replacement of regular expressions is not specified by - * POSIX, therefore re-implement it here. - */ - - for (rp = args->replace; *rp != '\0'; rp++) { - if (*rp == '\\' && (rp[1] == '&' || rp[1] == '\\')) { - SepBuf_AddBytes(buf, rp + 1, 1); - rp++; - continue; - } - - if (*rp == '&') { - SepBuf_AddBytesBetween(buf, - wp + m[0].rm_so, wp + m[0].rm_eo); - continue; - } - - if (*rp != '\\' || !ch_isdigit(rp[1])) { - SepBuf_AddBytes(buf, rp, 1); - continue; - } - - /* \0 to \9 backreference */ - n = (size_t)(rp[1] - '0'); - rp++; - - if (n >= args->nsub) { - Error("No subexpression \\%u", (unsigned)n); - } else if (m[n].rm_so == -1) { - if (opts.strict) { - Error("No match for subexpression \\%u", - (unsigned)n); - } - } else { - SepBuf_AddBytesBetween(buf, - wp + m[n].rm_so, wp + m[n].rm_eo); - } - } + RegexReplace(args->replace, buf, wp, m, args->nsub); wp += m[0].rm_eo; if (args->pflags.subGlobal) {