Skip to content

Commit

Permalink
perf: use replacer instead of replace
Browse files Browse the repository at this point in the history
Signed-off-by: rosenlo <rosenluov@gmail.com>
  • Loading branch information
rosenlo authored and lzf575 committed Jun 20, 2024
1 parent a06616c commit 664e1bf
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions server/libs/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,10 @@ func IsNil(i interface{}) bool {

// EscapeJSONString is used to escape special characters in JSON strings
func EscapeJSONString(value string) string {
value = strings.ReplaceAll(value, `\`, `\\"`)
value = strings.ReplaceAll(value, `"`, `\"`)
value = strings.ReplaceAll(value, "\n", `\n`)
return value
replacer := strings.NewReplacer(
`\`, `\\`,
`"`, `\"`,
"\n", `\n`,
)
return replacer.Replace(value)
}

0 comments on commit 664e1bf

Please sign in to comment.