From b89c0358f41a8b6f6acb291d80ff7345e8650bde Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 27 May 2024 00:14:29 +0100 Subject: [PATCH] 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. --- internal/utilities/utilities.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go index e2e4b0e..31d7df1 100644 --- a/internal/utilities/utilities.go +++ b/internal/utilities/utilities.go @@ -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 }