diff --git a/server/public/model/utils.go b/server/public/model/utils.go index ff86ff5bf6c..e9b2e7f89de 100644 --- a/server/public/model/utils.go +++ b/server/public/model/utils.go @@ -876,3 +876,23 @@ func SliceToMapKey(s ...string) map[string]any { return m } + +// LimitRunes limits the number of runes in a string to the given maximum. +// It returns the potentially truncated string and a boolean indicating whether truncation occurred. +func LimitRunes(s string, maxRunes int) (string, bool) { + runes := []rune(s) + if len(runes) > maxRunes { + return string(runes[:maxRunes]), true + } + + return s, false +} + +// LimitBytes limits the number of bytes in a string to the given maximum. +// It returns the potentially truncated string and a boolean indicating whether truncation occurred. +func LimitBytes(s string, maxBytes int) (string, bool) { + if len(s) > maxBytes { + return s[:maxBytes], true + } + return s, false +}