WIP incorrect_line_wrapping_endash #37

Manually merged
sloum merged 5 commits from asdf/bombadillo:incorrect_line_wrapping_endash into master 2019-09-15 00:57:31 +00:00
1 changed files with 10 additions and 6 deletions

View File

@ -81,21 +81,25 @@ func Clear(dir string) {
}
// takes the document content (as a slice) and modifies any lines that are longer
// than the specified console width, splitting them over two lines. returns the
// amended document content as a slice.
// word wrapping uses a "greedy" algorithm
// Takes the document content (as a slice of strings) and wraps any lines that
// are longer than the specified console width. returns the amended document
// content as a slice of strings.
// Word wrapping uses a "greedy" algorithm, where long lines are split in to
// words, and then rebuilt word by word to fill the available space. any
// leftover words overflow to the next line.
// To offer some support for unicode, some lengths are calculated using a slice
// of runes in the following way: len([]rune(string))
func wrapLines(s []string, consolewidth int) []string {
Outdated
Review

Looking at the benchmarks this seems to get the best combination of speed and function (len on its own being faster). I like the count rune in string method for its clarity, if we go with this maybe add a code comment for why we are casting to rune. I could see myself forgetting, or someone looking at it not knowing in the first place.

If you agree with the assessment, please remove the other two and update the branch and I'll complete the pull request into master :)
Otherwise, let me know and we can talk out the other options.

Looking at the benchmarks this seems to get the best combination of speed and function (len on its own being faster). I like the count rune in string method for its clarity, if we go with this maybe add a code comment for why we are casting to rune. I could see myself forgetting, or someone looking at it not knowing in the first place. If you agree with the assessment, please remove the other two and update the branch and I'll complete the pull request into master :) Otherwise, let me know and we can talk out the other options.
out := []string{}
for _, ln := range s {
if len(ln) <= consolewidth {
if len([]rune(ln)) <= consolewidth {
out = append(out, ln)
} else {
words := strings.SplitAfter(ln, " ")
var subout bytes.Buffer
for i, wd := range words {
sublen := subout.Len()
wdlen := len(wd)
wdlen := len([]rune(wd))
if sublen+wdlen <= consolewidth {
subout.WriteString(wd)
if i == len(words)-1 {