From 9c5e3ee1900fbc978498d38ba09caa11f0dbd603 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Wed, 19 Jan 2022 02:49:00 +0100 Subject: [PATCH] Use raw strings to avoid escapes (#885) Especially for regexes, raw strings improve readability since they aren't escaped. While we're at it, use MustCompile instead of handling the error and exiting. Signed-off-by: Stephen Kitt --- internal/pkg/auxents/unhex.go | 6 +----- internal/pkg/bifs/strings.go | 2 +- internal/pkg/lib/regex.go | 4 ++-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/internal/pkg/auxents/unhex.go b/internal/pkg/auxents/unhex.go index 48b04ca73..69a9d03ee 100644 --- a/internal/pkg/auxents/unhex.go +++ b/internal/pkg/auxents/unhex.go @@ -54,11 +54,7 @@ func unhexFile(istream *os.File, ostream *os.File) { var scanValue int byteArray := make([]byte, 1) - re, err := regexp.Compile("\\s+") - if err != nil { - fmt.Fprintln(os.Stderr, "mlr unhex: internal coding error detected.") - os.Exit(1) - } + re := regexp.MustCompile(`\s+`) eof := false for !eof { diff --git a/internal/pkg/bifs/strings.go b/internal/pkg/bifs/strings.go index 06e3e19f1..cba62879a 100644 --- a/internal/pkg/bifs/strings.go +++ b/internal/pkg/bifs/strings.go @@ -238,7 +238,7 @@ func BIF_collapse_whitespace_regexp(input1 *mlrval.Mlrval, whitespaceRegexp *reg } func WhitespaceRegexp() *regexp.Regexp { - return regexp.MustCompile("\\s+") + return regexp.MustCompile(`\s+`) } // ================================================================ diff --git a/internal/pkg/lib/regex.go b/internal/pkg/lib/regex.go index fe9810f5b..532f62b50 100644 --- a/internal/pkg/lib/regex.go +++ b/internal/pkg/lib/regex.go @@ -38,11 +38,11 @@ import ( // captureDetector is used to see if a string literal interpolates previous // captures (like "\2:\1") or not (like "2:1"). -var captureDetector = regexp.MustCompile("\\\\[0-9]") +var captureDetector = regexp.MustCompile(`\\[0-9]`) // captureSplitter is used to precompute an offsets matrix for strings like // "\2:\1" so they don't need to be recomputed on every record. -var captureSplitter = regexp.MustCompile("(\\\\[0-9])") +var captureSplitter = regexp.MustCompile(`(\\[0-9])`) // CompileMillerRegex wraps Go regex-compile with some Miller-specific syntax // which predate the port of Miller from C to Go. Miller regexes use a final