diff --git a/src/util.go b/src/util.go index b0f5a58..7fe24a8 100644 --- a/src/util.go +++ b/src/util.go @@ -16,6 +16,7 @@ package main import ( "database/sql" "regexp" + "strings" "time" "github.com/pingcap/errors" @@ -51,6 +52,22 @@ func OpenDBWithRetry(driverName, dataSourceName string, retryCount int) (mdb *sq return } +func processEscapes(str string) string { + escapeMap := map[string]string{ + `\n`: "\n", + `\t`: "\t", + `\r`: "\r", + `\\`: "\\", + `\/`: "/", + } + + for escape, replacement := range escapeMap { + str = strings.ReplaceAll(str, escape, replacement) + } + + return str +} + func ParseReplaceRegex(originalString string) ([]*ReplaceRegex, error) { var begin, middle, end, cnt int ret := make([]*ReplaceRegex, 0) @@ -63,10 +80,13 @@ func ParseReplaceRegex(originalString string) ([]*ReplaceRegex, error) { } cnt++ switch cnt % 3 { + // The first '/' case 1: begin = i + // The second '/' case 2: middle = i + // The last '/', we could compile regex and process replace string case 0: end = i reg, err := regexp.Compile(originalString[begin+1 : middle]) @@ -75,7 +95,7 @@ func ParseReplaceRegex(originalString string) ([]*ReplaceRegex, error) { } ret = append(ret, &ReplaceRegex{ regex: reg, - replace: originalString[middle+1 : end], + replace: processEscapes(originalString[middle+1 : end]), }) } } diff --git a/src/util_test.go b/src/util_test.go index 01e10fc..2b0e428 100644 --- a/src/util_test.go +++ b/src/util_test.go @@ -56,14 +56,12 @@ func TestParseReplaceRegex(t *testing.T) { input: "Some infos [conn=xxx]", output: "Some infos [conn=xxx]", }, - /* TODO: support replaced with '\t', '\n', '\/' etc { succ: true, - regexpStr: `/a/\/b/`, + regexpStr: `/a/\/b\r\t/`, input: "a", - output: "/b", + output: "/b\r\t", }, - */ { succ: false, regexpStr: `/conn=[0-9]+/conn=`,