mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-23 10:57:44 +08:00
Fix stream compatibility and vision model exposure
This commit is contained in:
46
internal/util/text.go
Normal file
46
internal/util/text.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package util
|
||||
|
||||
import "unicode/utf8"
|
||||
|
||||
// TruncateRunes trims a string to at most limit Unicode code points.
|
||||
func TruncateRunes(text string, limit int) (string, bool) {
|
||||
if limit < 0 {
|
||||
return text, false
|
||||
}
|
||||
if limit == 0 {
|
||||
return "", text != ""
|
||||
}
|
||||
|
||||
count := 0
|
||||
for i := range text {
|
||||
if count == limit {
|
||||
return text[:i], true
|
||||
}
|
||||
count++
|
||||
}
|
||||
return text, false
|
||||
}
|
||||
|
||||
// TruncateUTF8Bytes trims a string to fit within limit bytes without cutting
|
||||
// through a UTF-8 code point boundary.
|
||||
func TruncateUTF8Bytes(text string, limit int) (string, bool) {
|
||||
if limit < 0 {
|
||||
return text, false
|
||||
}
|
||||
if len(text) <= limit {
|
||||
return text, false
|
||||
}
|
||||
if limit == 0 {
|
||||
return "", true
|
||||
}
|
||||
|
||||
raw := []byte(text)
|
||||
cut := limit
|
||||
if cut > len(raw) {
|
||||
cut = len(raw)
|
||||
}
|
||||
for cut > 0 && cut < len(raw) && !utf8.RuneStart(raw[cut]) {
|
||||
cut--
|
||||
}
|
||||
return string(raw[:cut]), true
|
||||
}
|
||||
Reference in New Issue
Block a user