Skip to content

Commit

Permalink
improve allocation resulting from replacer build in trimQuotes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux committed Oct 7, 2024
1 parent cbc90c6 commit 1f8f8a2
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion sqllexer_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,30 @@ func replaceDigits(input string, placeholder string) string {
return builder.String()
}

var (
doubleQuotesReplacer = strings.NewReplacer("\"", "")
backQuotesReplacer = strings.NewReplacer("`", "")
bracketQuotesReplacer = strings.NewReplacer("[", "", "]", "")
)

func trimQuotes(input string, delim string, closingDelim string) string {
replacer := strings.NewReplacer(delim, "", closingDelim, "")
var replacer *strings.Replacer
switch {
// common quote types get an already allocated replacer
case delim == closingDelim && delim == "\"":
replacer = doubleQuotesReplacer
case delim == closingDelim && delim == "`":
replacer = backQuotesReplacer
case delim == "[" && closingDelim == "]":
replacer = bracketQuotesReplacer

// common case of `delim` and `closingDelim` being the same, gets a simpler replacer
case delim == closingDelim:
replacer = strings.NewReplacer(delim, "")

default:
replacer = strings.NewReplacer(delim, "", closingDelim, "")
}

return replacer.Replace(input)
}

0 comments on commit 1f8f8a2

Please sign in to comment.