From 9ca03137eabc30f2518f466b38177d7fb6e4a79e Mon Sep 17 00:00:00 2001 From: Wei Shen Date: Thu, 18 Nov 2021 09:32:40 +0800 Subject: [PATCH] mutate2/filter2: fix value with single or double quotes. #174 --- CHANGELOG.md | 1 + csvtk/cmd/filter2.go | 23 +++++++++++++++++++---- csvtk/cmd/mutate2.go | 23 +++++++++++++++++++---- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbadc84..7f42a73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - change the way of rexpression evaluation. - add custom functions: `len()`. [#153](https://github.com/shenwei356/csvtk/issues/153) - fix bug when using two or more columns with common prefixes in column names. [#173](https://github.com/shenwei356/csvtk/issues/173) + - fix value with single or double quotes. [#174](https://github.com/shenwei356/csvtk/issues/174) - `csvtk cut`: new flags `-m/--allow-missing-col` and `-b/--blank-missing-col`. [#156](https://github.com/shenwei356/csvtk/issues/156) - `csvtk pretty`: still add header row for empty column. - `csvtk csv2md`: better format. diff --git a/csvtk/cmd/filter2.go b/csvtk/cmd/filter2.go index d8e64cc..0b6e16c 100644 --- a/csvtk/cmd/filter2.go +++ b/csvtk/cmd/filter2.go @@ -145,10 +145,7 @@ Custom functions: hasNullCoalescence := reNullCoalescence.MatchString(filterStr) - var quote string = `'` - if strings.Contains(filterStr, `"`) { - quote = `"` - } + var quote string filterStr0 := filterStr filterStr = reFiler2VarSymbolStartsWithDigits.ReplaceAllString(filterStr, "shenwei_$1$2") filterStr = reFilter2VarField.ReplaceAllString(filterStr, "shenwei$1") @@ -326,6 +323,8 @@ Custom functions: value = record[fieldTmp-1] col = fmt.Sprintf("shenwei%d", fieldTmp) + quote = `'` + if reDigitals.MatchString(value) { if digitsAsString || containCustomFuncs { parameters[col] = quote + value + quote @@ -337,6 +336,13 @@ Custom functions: if value == "" && hasNullCoalescence { parameters[col] = "shenweiNULL" } else { + if strings.Contains(value, `'`) { + value = strings.ReplaceAll(value, `'`, `\'`) + } + if strings.Contains(value, `"`) { + value = strings.ReplaceAll(value, `"`, `\"`) + } + parameters[col] = quote + value + quote } } @@ -351,6 +357,8 @@ Custom functions: col = "$" + col } + quote = `'` + if reDigitals.MatchString(value) { if digitsAsString || containCustomFuncs { parameters[col] = quote + value + quote @@ -362,6 +370,13 @@ Custom functions: if value == "" && hasNullCoalescence { parameters[col] = "shenweiNULL" } else { + if strings.Contains(value, `'`) { + value = strings.ReplaceAll(value, `'`, `\'`) + } + if strings.Contains(value, `"`) { + value = strings.ReplaceAll(value, `"`, `\"`) + } + parameters[col] = quote + value + quote } } diff --git a/csvtk/cmd/mutate2.go b/csvtk/cmd/mutate2.go index f69d8ff..e5d5da8 100644 --- a/csvtk/cmd/mutate2.go +++ b/csvtk/cmd/mutate2.go @@ -209,10 +209,7 @@ Custom functions: hasNullCoalescence := reNullCoalescence.MatchString(exprStr) - var quote string = `'` - if strings.Contains(exprStr, `"`) { - quote = `"` - } + var quote string exprStr = reFiler2VarSymbolStartsWithDigits.ReplaceAllString(exprStr, "shenwei_$1$2") exprStr = reFilter2VarField.ReplaceAllString(exprStr, "shenwei$1") // exprStr = reFilter2VarSymbol.ReplaceAllString(exprStr, "") @@ -381,6 +378,8 @@ Custom functions: value = record[fieldTmp-1] col = fmt.Sprintf("shenwei%d", fieldTmp) + quote = `'` + if reDigitals.MatchString(value) { if digitsAsString || containCustomFuncs { parameters[col] = quote + value + quote @@ -389,6 +388,13 @@ Custom functions: parameters[col] = fmt.Sprintf("%.16f", valueFloat) } } else { + if strings.Contains(value, `'`) { + value = strings.ReplaceAll(value, `'`, `\'`) + } + if strings.Contains(value, `"`) { + value = strings.ReplaceAll(value, `"`, `\"`) + } + if value == "" && hasNullCoalescence { parameters[col] = "shenweiNULL" } else { @@ -406,6 +412,8 @@ Custom functions: col = "$" + col } + quote = `'` + if reDigitals.MatchString(value) { if digitsAsString || containCustomFuncs { parameters[col] = quote + value + quote @@ -417,6 +425,13 @@ Custom functions: if value == "" && hasNullCoalescence { parameters[col] = "shenweiNULL" } else { + if strings.Contains(value, `'`) { + value = strings.ReplaceAll(value, `'`, `\'`) + } + if strings.Contains(value, `"`) { + value = strings.ReplaceAll(value, `"`, `\"`) + } + parameters[col] = quote + value + quote } }