fix: resolve infinite loop issue in wrapLine fn
The wrap line function breaks a line at the position of the last white space before reaching the line limit, however sometimes a single word (e.g. a very long url) exceeds the character limit itself which threw the function into an infinite loop. This commit fixes that by breaking said word at the last letter before the character limit is reached.
This commit is contained in:
parent
f3d9d34f3c
commit
b89c0358f4
1 changed files with 7 additions and 1 deletions
|
@ -94,9 +94,15 @@ func wrapLine(line, separator string, charLimit int) string {
|
|||
|
||||
for rightcursor < (len(line) - charLimit) {
|
||||
rightcursor += charLimit
|
||||
for !unicode.IsSpace(rune(line[rightcursor-1])) {
|
||||
|
||||
for !unicode.IsSpace(rune(line[rightcursor-1])) && (rightcursor > leftcursor) {
|
||||
rightcursor--
|
||||
}
|
||||
|
||||
if rightcursor == leftcursor {
|
||||
rightcursor = leftcursor + charLimit
|
||||
}
|
||||
|
||||
builder.WriteString(line[leftcursor:rightcursor] + separator)
|
||||
leftcursor = rightcursor
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue