searxng-mcp-go/internal/searxng/answer/truncate.go
xlion-bot-renovate[bot] 6e4c553a9e
chore(deps): update dependency go to v1.27.1 (#588)
* chore(deps): update dependency go to v1.27.1

* Fixed all lints; just verify passes.

Co-authored-by: xlionjuan <[email protected]>

* Fixed `.golangci.yml` paths→path; `just verify` passes.

Co-authored-by: xlionjuan <[email protected]>

* Parallelized golden & error-path tests; verify passes.

Co-authored-by: xlionjuan <[email protected]>

* Verify passes; HTTPClient/Logger nil set; exhaustruct covers Config.

Co-authored-by: xlionjuan <[email protected]>

---------

Co-authored-by: xlion-bot-renovate[bot] <218815870+xlion-bot-renovate[bot]@users.noreply.github.com>
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: xlionjuan <[email protected]>
2026-09-06 23:14:09 +08:00

33 lines
1 KiB
Go

package answer
// TruncateRunes returns s truncated to at most maxRunes runes in a single pass
// over the underlying bytes. Multi-byte UTF-8 runes are never split: the
// function walks the string with `for i := range s` and slices on the byte
// offset of the rune boundary, which is the standard Go idiom for rune-safe
// truncation without an intermediate []rune conversion.
//
// The function returns the original string unchanged when its rune count is
// at or below maxRunes. A non-positive maxRunes returns the empty string,
// matching the pre-refactor behavior of the two callers it replaces.
//
// This helper is the single source of truth for rune-safe truncation. It is
// used both by the internal deduplication prefix match (answer package) and
// by the CLI text formatter (root package) for content-length bounding per
// MaxContentRunes.
func TruncateRunes(str string, maxRunes int) string {
if maxRunes <= 0 {
return ""
}
count := 0
for i := range str {
count++
if count > maxRunes {
return str[:i]
}
}
return str
}