Skip to content

Commit

Permalink
refactor(translate.go): optimize trimDividers function for clarity an…
Browse files Browse the repository at this point in the history
…d efficiency

- Move the condition to check the length of lines before cloning to avoid unnecessary operation.
- Change return value to use strings.TrimSpace for consistency and to ensure the returned text does not have leading or trailing whitespace when no lines exist.
  • Loading branch information
bounoable committed Apr 26, 2024
1 parent b8264a1 commit 4c17993
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ func (t *Translator) translateChunk(ctx context.Context, chunk string, params Tr

func trimDividers(text string) string {
lines := strings.Split(text, "\n")
out := slices.Clone(lines)

if len(out) < 1 {
return text
if len(lines) < 1 {
return strings.TrimSpace(text)
}

out := slices.Clone(lines)

if out[0] == "---<DOC_BEGIN>---" {
out = out[1:]
}
Expand Down

0 comments on commit 4c17993

Please sign in to comment.