Skip to content

Commit 934bba8

Browse files
committed
refactor: move strings.Replacer to package scope
Moved the `strings.NewReplacer` instance to a package-level variable to avoid repeated allocations on every call to `escapeControl`. Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 787cad8 commit 934bba8

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

godump.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,17 @@ func isNil(v reflect.Value) bool {
369369
}
370370
}
371371

372+
var replacer = strings.NewReplacer(
373+
"\n", `\n`,
374+
"\t", `\t`,
375+
"\r", `\r`,
376+
"\v", `\v`,
377+
"\f", `\f`,
378+
"\x1b", `\x1b`,
379+
)
380+
372381
// escapeControl escapes control characters in a string for safe display.
373382
func escapeControl(s string) string {
374-
replacer := strings.NewReplacer(
375-
"\n", `\n`,
376-
"\t", `\t`,
377-
"\r", `\r`,
378-
"\v", `\v`,
379-
"\f", `\f`,
380-
"\x1b", `\x1b`,
381-
)
382383
return replacer.Replace(s)
383384
}
384385

0 commit comments

Comments
 (0)