MCPcopy Index your code
hub / github.com/coder/coder / truncate

Function truncate

coderd/util/namesgenerator/namesgenerator.go:64–80  ·  view source on GitHub ↗

truncate truncates a name to maxLen characters. It assumes the name ends with a numeric suffix and preserves it, truncating the base name portion instead.

(name string, maxLen int)

Source from the content-addressed store, hash-verified

62// truncate truncates a name to maxLen characters. It assumes the name ends with
63// a numeric suffix and preserves it, truncating the base name portion instead.
64func truncate(name string, maxLen int) string {
65 if len(name) <= maxLen {
66 return name
67 }
68 // Find where the numeric suffix starts.
69 suffixStart := len(name)
70 for suffixStart > 0 && name[suffixStart-1] >= '0' && name[suffixStart-1] <= '9' {
71 suffixStart--
72 }
73 base := name[:suffixStart]
74 suffix := name[suffixStart:]
75 truncateAt := maxLen - len(suffix)
76 if truncateAt <= 0 {
77 return strconv.Itoa(maxLen) // Fallback, shouldn't happen in practice.
78 }
79 return base[:truncateAt] + suffix
80}

Callers 3

NameDigitWithFunction · 0.85
UniqueNameWithFunction · 0.85
TestTruncateFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestTruncateFunction · 0.68