diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-08 10:15:35 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-08 10:15:35 +0300 |
| commit | fc1857e1b485e7011fb80b40ee7e5146fe2350e7 (patch) | |
| tree | 5fba41b61f5ed801350a5a63bab1cca4a8d37b2c | |
| parent | f7deedf50f6fa00c7dfe989ed381989e202b3057 (diff) | |
batch: pre-allocate WordEntry slice from line count in ReadBatchFile
Use make([]WordEntry, 0, len(lines)) after splitting normalized content
to avoid repeated growth on append. Return nil when no entries are
parsed so empty batch files still yield a nil slice like before.
Made-with: Cursor
| -rw-r--r-- | internal/batch/processor.go | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/internal/batch/processor.go b/internal/batch/processor.go index c785a91..3432c8b 100644 --- a/internal/batch/processor.go +++ b/internal/batch/processor.go @@ -30,11 +30,12 @@ func ReadBatchFile(filename string) ([]WordEntry, error) { return nil, fmt.Errorf("failed to read batch file: %w", err) } - var entries []WordEntry // Normalize \r\n to \n before splitting so both Windows and Unix line // endings are handled uniformly by strings.Split. normalized := strings.ReplaceAll(string(content), "\r\n", "\n") - for _, line := range strings.Split(normalized, "\n") { + lines := strings.Split(normalized, "\n") + entries := make([]WordEntry, 0, len(lines)) + for _, line := range lines { if line = strings.TrimSpace(line); line != "" { entry := parseBatchLine(line) if entry != nil { @@ -43,6 +44,9 @@ func ReadBatchFile(filename string) ([]WordEntry, error) { } } + if len(entries) == 0 { + return nil, nil + } return entries, nil } |
