MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / SplitString

Function SplitString

internal/util/util.go:185–240  ·  view source on GitHub ↗
(input string, lineWidth int)

Source from the content-addressed store, hash-verified

183}
184
185func SplitString(input string, lineWidth int) []string {
186 var result []string
187 var currentLine string
188 currentLen := 0
189
190 parts := strings.Split(input, "\n")
191
192 for _, textLine := range parts {
193 words := wordRegex.FindAllString(textLine, -1)
194
195 l := len(words)
196
197 skip := false
198 for idx, word := range words {
199 if skip {
200 skip = false
201 continue
202 }
203
204 wordLen := runewidth.StringWidth(word)
205
206 if idx < l-1 && punctuationRegex.MatchString(words[idx+1]) {
207 wordLen += runewidth.StringWidth(words[idx+1])
208 word += words[idx+1]
209 skip = true
210 } else {
211 skip = false
212 }
213
214 if wordLen > lineWidth {
215 result = append(result, word)
216 continue
217 }
218
219 if currentLen+wordLen > lineWidth {
220 if currentLine != "" {
221 result = append(result, strings.TrimRight(currentLine, " "))
222 }
223 // clear spaces at the beginning of the line
224 currentLine = strings.TrimLeft(word, " ")
225 currentLen = runewidth.StringWidth(currentLine)
226 } else {
227 currentLine += word
228 currentLen += wordLen
229 }
230 }
231
232 if currentLine != "" {
233 result = append(result, currentLine)
234 currentLine = ""
235 currentLen = 0
236 }
237 }
238
239 return result
240}
241
242// Splits a string by adding line breaks at the end of each line

Callers 3

GetDetailsFunction · 0.92
TestSplitStringFunction · 0.85
SplitStringNLFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestSplitStringFunction · 0.68