Skip to content

Commit

Permalink
simplify for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Jan 8, 2024
1 parent 5b0efc3 commit 4c406d1
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions text/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,32 +119,35 @@ func Pad(str string, maxLen int, paddingChar rune) string {
// ProcessCRLF("abc\r\ndef\rghi\rjkl\rmn") == "abc\nmnl"
func ProcessCRLF(str string) string {
str = strings.ReplaceAll(str, "\r\n", "\n")
if !strings.Contains(str, "\r") {
return str
}

lines := strings.Split(str, "\n")
for lineIdx, line := range lines {
if !strings.Contains(line, "\r") {
continue
}

if strings.Contains(str, "\r") {
lines := strings.Split(str, "\n")
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
}
lineRunes, newLineRunes := []rune(line), make([]rune, 0)
for idx, realIdx := 0, 0; idx < len(lineRunes); idx++ {
// if a CR, move "cursor" back to beginning of line
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)
// if cursor is not at end, overwrite
if realIdx < len(newLineRunes) {
newLineRunes[realIdx] = lineRunes[idx]
} else { // else append
newLineRunes = append(newLineRunes, lineRunes[idx])
}
realIdx++
}
str = strings.Join(lines, "\n")
lines[lineIdx] = string(newLineRunes)
}

return str
return strings.Join(lines, "\n")
}

// RepeatAndTrim repeats the given string until it is as long as maxRunes.
Expand Down

0 comments on commit 4c406d1

Please sign in to comment.