Skip to content

Commit 7773bc9

Browse files
committed
account for legacy spacing
1 parent ae0e662 commit 7773bc9

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

stringwrap.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,23 @@ func stringWrap(
439439
case unicode.IsSpace(r):
440440
stateMachine.flushWordBuffer()
441441

442-
// All legacy whitespace is ignored and not written
443-
// to the line buffer.
442+
// Handle the different types of whitespace characters
443+
// in the string (e.g., space, newline, tab, etc.).
444444
switch r {
445445
case ' ':
446446
stateMachine.writeSpaceToLine(r)
447-
case '\n':
447+
case '\n', '\r', '\u0085', '\u2028', '\u2029':
448448
stateMachine.writeHardLine()
449449
positions.incrementOrigLine()
450450
positions.origLineSegment = 0
451451
case '\t':
452452
adjTabSize := stateMachine.writeTabToLine()
453453
positions.curLineWidth += adjTabSize
454+
case '\v', '\f':
455+
/* ignore */
456+
default:
457+
stateMachine.writeSpaceToLine(r)
458+
positions.curLineWidth += runewidth.RuneWidth(r) - 1
454459
}
455460
state = -1
456461
idx += rSize

stringwrap_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,34 @@ func TestStringWrap(t *testing.T) {
101101
trimWhitespace: false,
102102
splitWord: true,
103103
},
104+
{
105+
input: "hello\rworld",
106+
wrapped: "hello\nworld",
107+
limit: 10,
108+
trimWhitespace: true,
109+
splitWord: false,
110+
},
111+
{
112+
input: "foo\u2028bar baz",
113+
wrapped: "foo\nbar\nbaz",
114+
limit: 5,
115+
trimWhitespace: true,
116+
splitWord: false,
117+
},
118+
{
119+
input: "Supercalifragilisticexpialidocious\vness",
120+
wrapped: "Supercali-\nfragilist-\nicexpiali-\ndociousne-\nss",
121+
limit: 10,
122+
trimWhitespace: true,
123+
splitWord: true,
124+
},
125+
{
126+
input: "foo\u2028barbazbaz",
127+
wrapped: "foo\nbarb-\nazbaz",
128+
limit: 5,
129+
trimWhitespace: true,
130+
splitWord: true,
131+
},
104132
}
105133

106134
for idx, tt := range tests {

0 commit comments

Comments
 (0)