From 9d258ee5ebe2773b477d553b62c0c67650a5a5cf Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 2 Apr 2026 21:39:21 +0300 Subject: task 005: use strings.Builder for string concatenation in loops Replace per-rune += string(r) heap allocations with strings.Builder in SanitizeFilename (internal/utils.go) and splitLines (internal/batch/processor.go). Both now call Grow/Reset appropriately to pre-allocate capacity. Co-Authored-By: Claude Sonnet 4.6 --- internal/utils.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'internal/utils.go') diff --git a/internal/utils.go b/internal/utils.go index 779b95a..c135f9b 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -4,6 +4,7 @@ import ( "crypto/md5" "encoding/hex" "fmt" + "strings" "time" ) @@ -22,17 +23,19 @@ func GenerateCardID(bulgarianWord string) string { return fmt.Sprintf("%d_%s", epochMillis, hashStr) } -// SanitizeFilename creates a safe filename from a string +// SanitizeFilename creates a safe filename from a string. +// Uses strings.Builder to avoid per-rune heap allocations. func SanitizeFilename(s string) string { - result := "" + var b strings.Builder + b.Grow(len(s)) for _, r := range s { if isAlphaNumeric(r) || r == '-' || r == '_' { - result += string(r) + b.WriteRune(r) } else { - result += "_" + b.WriteByte('_') } } - return result + return b.String() } // isAlphaNumeric checks if a rune is alphanumeric -- cgit v1.2.3