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:
Dan Anglin 2024-05-27 00:14:29 +01:00
parent f3d9d34f3c
commit b89c0358f4
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

View file

@ -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
}