TruncToWord truncates the given text to the provided limit.
(s string, l int)
| 68 | |
| 69 | // TruncToWord truncates the given text to the provided limit. |
| 70 | func TruncToWord(s string, l int) (string, bool) { |
| 71 | truncated := false |
| 72 | c := []rune(s) |
| 73 | if len(c) > l { |
| 74 | truncated = true |
| 75 | s = string(c[:l]) |
| 76 | spaceIdx := strings.LastIndexByte(s, ' ') |
| 77 | if spaceIdx > -1 { |
| 78 | s = s[:spaceIdx] |
| 79 | } |
| 80 | } |
| 81 | return s, truncated |
| 82 | } |
| 83 | |
| 84 | // Truncate truncates the given text to the provided limit, returning the original string if it's shorter than the limit. |
| 85 | func Truncate(s string, l int) string { |
no outgoing calls