Skip to content

Commit

Permalink
mutate2/filter2: fix value with single or double quotes. #174
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Nov 18, 2021
1 parent 7e54d7a commit 9ca0313
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 19 additions & 4 deletions csvtk/cmd/filter2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Expand All @@ -351,6 +357,8 @@ Custom functions:
col = "$" + col
}

quote = `'`

if reDigitals.MatchString(value) {
if digitsAsString || containCustomFuncs {
parameters[col] = quote + value + quote
Expand All @@ -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
}
}
Expand Down
23 changes: 19 additions & 4 deletions csvtk/cmd/mutate2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -406,6 +412,8 @@ Custom functions:
col = "$" + col
}

quote = `'`

if reDigitals.MatchString(value) {
if digitsAsString || containCustomFuncs {
parameters[col] = quote + value + quote
Expand All @@ -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
}
}
Expand Down

0 comments on commit 9ca0313

Please sign in to comment.