TruncateString truncates a string to a given number of runes.
(s string, maxRunes int)
| 44 | |
| 45 | // TruncateString truncates a string to a given number of runes. |
| 46 | func 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. |
no outgoing calls
no test coverage detected
searching dependent graphs…