MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / TruncateString

Function TruncateString

pkg/util/strings.go:46–68  ·  view source on GitHub ↗

TruncateString truncates a string to a given number of runes.

(s string, maxRunes int)

Source from the content-addressed store, hash-verified

44
45// TruncateString truncates a string to a given number of runes.
46func TruncateString(s string, maxRunes int) string {
47 // This is a fast path (len(s) is an upper bound for RuneCountInString).
48 if len(s) <= maxRunes {
49 return s
50 }
51 n := utf8.RuneCountInString(s)
52 if n <= maxRunes {
53 return s
54 }
55 // Fast path for ASCII strings.
56 if len(s) == n {
57 return s[:maxRunes]
58 }
59 i := 0
60 for pos := range s {
61 if i == maxRunes {
62 return s[:pos]
63 }
64 i++
65 }
66 // This code should be unreachable.
67 return s
68}
69
70// RemoveTrailingSpaces splits the input string into lines, trims any trailing
71// spaces from each line, then puts the lines back together.

Callers 2

AdjustValueToTypeFunction · 0.92
truncateStringFunction · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…