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)
| 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. |
| 64 | func 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 | } |
no outgoing calls