2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-16 21:25:38 +00:00
cointop/vendor/github.com/mitchellh/go-wordwrap/wordwrap.go

84 lines
1.8 KiB
Go
Raw Normal View History

2019-02-26 06:34:53 +00:00
package wordwrap
import (
"bytes"
"unicode"
)
2021-09-04 20:31:08 +00:00
const nbsp = 0xA0
2019-02-26 06:34:53 +00:00
// WrapString wraps the given string within lim width in characters.
//
// Wrapping is currently naive and only happens at white-space. A future
// version of the library will implement smarter wrapping. This means that
// pathological cases can dramatically reach past the limit, such as a very
// long word.
func WrapString(s string, lim uint) string {
// Initialize a buffer with a slightly larger size to account for breaks
init := make([]byte, 0, len(s))
buf := bytes.NewBuffer(init)
var current uint
var wordBuf, spaceBuf bytes.Buffer
2021-09-04 20:31:08 +00:00
var wordBufLen, spaceBufLen uint
2019-02-26 06:34:53 +00:00
for _, char := range s {
if char == '\n' {
if wordBuf.Len() == 0 {
2021-09-04 20:31:08 +00:00
if current+spaceBufLen > lim {
2019-02-26 06:34:53 +00:00
current = 0
} else {
2021-09-04 20:31:08 +00:00
current += spaceBufLen
2019-02-26 06:34:53 +00:00
spaceBuf.WriteTo(buf)
}
spaceBuf.Reset()
2021-09-04 20:31:08 +00:00
spaceBufLen = 0
2019-02-26 06:34:53 +00:00
} else {
2021-09-04 20:31:08 +00:00
current += spaceBufLen + wordBufLen
2019-02-26 06:34:53 +00:00
spaceBuf.WriteTo(buf)
spaceBuf.Reset()
2021-09-04 20:31:08 +00:00
spaceBufLen = 0
2019-02-26 06:34:53 +00:00
wordBuf.WriteTo(buf)
wordBuf.Reset()
2021-09-04 20:31:08 +00:00
wordBufLen = 0
2019-02-26 06:34:53 +00:00
}
buf.WriteRune(char)
current = 0
2021-09-04 20:31:08 +00:00
} else if unicode.IsSpace(char) && char != nbsp {
2019-02-26 06:34:53 +00:00
if spaceBuf.Len() == 0 || wordBuf.Len() > 0 {
2021-09-04 20:31:08 +00:00
current += spaceBufLen + wordBufLen
2019-02-26 06:34:53 +00:00
spaceBuf.WriteTo(buf)
spaceBuf.Reset()
2021-09-04 20:31:08 +00:00
spaceBufLen = 0
2019-02-26 06:34:53 +00:00
wordBuf.WriteTo(buf)
wordBuf.Reset()
2021-09-04 20:31:08 +00:00
wordBufLen = 0
2019-02-26 06:34:53 +00:00
}
spaceBuf.WriteRune(char)
2021-09-04 20:31:08 +00:00
spaceBufLen++
2019-02-26 06:34:53 +00:00
} else {
wordBuf.WriteRune(char)
2021-09-04 20:31:08 +00:00
wordBufLen++
2019-02-26 06:34:53 +00:00
2021-09-04 20:31:08 +00:00
if current+wordBufLen+spaceBufLen > lim && wordBufLen < lim {
2019-02-26 06:34:53 +00:00
buf.WriteRune('\n')
current = 0
spaceBuf.Reset()
2021-09-04 20:31:08 +00:00
spaceBufLen = 0
2019-02-26 06:34:53 +00:00
}
}
}
if wordBuf.Len() == 0 {
2021-09-04 20:31:08 +00:00
if current+spaceBufLen <= lim {
2019-02-26 06:34:53 +00:00
spaceBuf.WriteTo(buf)
}
} else {
spaceBuf.WriteTo(buf)
wordBuf.WriteTo(buf)
}
return buf.String()
}