From 382d6ca50c4cf29246fb30dc45b3833fe77346e5 Mon Sep 17 00:00:00 2001 From: Tom Conder Date: Sat, 25 Jul 2026 15:59:35 -0500 Subject: [PATCH] fix(linebreak): allow Knuth-Plass to place words longer than width Words longer than the target width could never be assigned a finite cost, leaving minCost stuck at +Inf and collapsing the whole input onto a single line instead of wrapping. --- pkg/linebreak/knuthplass.go | 2 +- test/linebreak/knuthplass_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/linebreak/knuthplass.go b/pkg/linebreak/knuthplass.go index 50c860f..1838b9c 100644 --- a/pkg/linebreak/knuthplass.go +++ b/pkg/linebreak/knuthplass.go @@ -38,7 +38,7 @@ func KnuthPlass(text string, width int) []string { spaces := j - i - 1 totalLen := lineLength + spaces - if totalLen > width { + if totalLen > width && j > i+1 { break } diff --git a/test/linebreak/knuthplass_test.go b/test/linebreak/knuthplass_test.go index b8275df..a37f5fa 100644 --- a/test/linebreak/knuthplass_test.go +++ b/test/linebreak/knuthplass_test.go @@ -67,6 +67,14 @@ func TestKnuthPlass(t *testing.T) { expected: []string{"The lazy", "yellow dog", "was caught by", "the slow red", "fox as he lay", "sleeping in", "the sun"}, }, }, + { + name: "Word longer than width", + args: args{ + text: "a supercalifragilisticexpialidocious word", + width: 10, + expected: []string{"a", "supercalifragilisticexpialidocious", "word"}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {