Skip to content

Commit

Permalink
text: process Carriage-Return codes correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Jan 8, 2024
1 parent 460e895 commit 7c17f9c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
4 changes: 2 additions & 2 deletions table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func TestTable_Render_CRLF(t *testing.T) {
tw := NewWriter()
tw.AppendHeader(testHeader)
tw.AppendRows(testRows)
tw.AppendRow(Row{5000, "Night", "King", 10000, "Was once a\r\nMortal \rMan"})
tw.AppendRow(Row{5000, "Night", "King", 10000, "Was once a\r\nMortal \rMen"})
tw.AppendFooter(testFooter)

compareOutput(t, tw.Render(), `
Expand All @@ -631,7 +631,7 @@ func TestTable_Render_CRLF(t *testing.T) {
| 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! |
| 300 | Tyrion | Lannister | 5000 | |
| 5000 | Night | King | 10000 | Was once a |
| | | | | Man |
| | | | | Mental |
+------+------------+-----------+--------+-----------------------------+
| | | TOTAL | 10000 | |
+------+------------+-----------+--------+-----------------------------+`)
Expand Down
38 changes: 27 additions & 11 deletions text/string.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package text

import (
"regexp"
"strings"
"unicode/utf8"

Expand Down Expand Up @@ -108,21 +107,38 @@ func Pad(str string, maxLen int, paddingChar rune) string {
return str
}

var (
reCarriageReturn = regexp.MustCompile(`(.*)\r`)
)

// ProcessCRLF converts "\r\n" to "\n", and erases everything preceding a lone
// "\r" in each line of the string.
// ProcessCRLF converts "\r\n" to "\n", and processes lone "\r" by moving the
// cursor/carriage to the start of the line and overwrites the contents
// accordingly. Ex.:
//
// ProcessCRLF("abc") == "abc"
// ProcessCRLF("abc\r\ndef") == "abc\ndef"
// ProcessCRLF("abc\r\ndef\rghi") == "abc\nghi"
// ProcessCRLF("abc\r\ndef\rghi\njkl") == "abc\nghi\njkl"
// ProcessCRLF("abc\r\ndef\rghi\njkl\r") == "abc\nghi\njkl"
// ProcessCRLF("abc\r\ndef\rghi\rjkl\rmn") == "abc\nmnl"
func ProcessCRLF(str string) string {
str = strings.ReplaceAll(str, "\r\n", "\n")

// process \r by erasing everything preceding it in the line
if strings.Contains(str, "\r") {
lines := strings.Split(str, "\n")
for idx := range lines {
for reCarriageReturn.MatchString(lines[idx]) {
lines[idx] = reCarriageReturn.ReplaceAllString(lines[idx], "")
for lineIdx, line := range lines {
if strings.Contains(line, "\r") {
lineRunes, newLineRunes := []rune(line), make([]rune, 0)
for idx, realIdx := 0, 0; idx < len(lineRunes); idx++ {
if lineRunes[idx] == '\r' {
realIdx = 0
continue
}

if realIdx < len(newLineRunes) {
newLineRunes[realIdx] = lineRunes[idx]
} else {
newLineRunes = append(newLineRunes, lineRunes[idx])
}
realIdx++
}
lines[lineIdx] = string(newLineRunes)
}
}
str = strings.Join(lines, "\n")
Expand Down
7 changes: 5 additions & 2 deletions text/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,23 @@ func ExampleProcessCRLF() {
fmt.Printf("%#v\n", ProcessCRLF("abc\r\ndef\rghi"))
fmt.Printf("%#v\n", ProcessCRLF("abc\r\ndef\rghi\njkl"))
fmt.Printf("%#v\n", ProcessCRLF("abc\r\ndef\rghi\njkl\r"))
fmt.Printf("%#v\n", ProcessCRLF("abc\r\ndef\rghi\rjkl\rmn"))

// Output: "abc"
// "abc\ndef"
// "abc\nghi"
// "abc\nghi\njkl"
// "abc\nghi\n"
// "abc\nghi\njkl"
// "abc\nmnl"
}

func TestProcessCRLF(t *testing.T) {
assert.Equal(t, "abc", ProcessCRLF("abc"))
assert.Equal(t, "abc\ndef", ProcessCRLF("abc\r\ndef"))
assert.Equal(t, "abc\nghi", ProcessCRLF("abc\r\ndef\rghi"))
assert.Equal(t, "abc\nghi\njkl", ProcessCRLF("abc\r\ndef\rghi\njkl"))
assert.Equal(t, "abc\nghi\n", ProcessCRLF("abc\r\ndef\rghi\njkl\r"))
assert.Equal(t, "abc\nghi\njkl", ProcessCRLF("abc\r\ndef\rghi\njkl\r"))
assert.Equal(t, "abc\nmnl", ProcessCRLF("abc\r\ndef\rghi\rjkl\rmn"))
}

func ExampleRepeatAndTrim() {
Expand Down

0 comments on commit 7c17f9c

Please sign in to comment.