mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-08 10:41:19 +00:00
da20785685
Former-commit-id: 23f542f43b4b493e38f5aa4c29788ed93a63b43b [formerly 71b23a6a28eb045fcfeab6329de69f1e5455486b] Former-commit-id: d12b3a6decc876f010a71f98e11df7387c1aaf2a
74 lines
1.6 KiB
Go
74 lines
1.6 KiB
Go
package wordwrap
|
|
|
|
import (
|
|
"bytes"
|
|
"unicode"
|
|
)
|
|
|
|
// 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
|
|
|
|
for _, char := range s {
|
|
if char == '\n' {
|
|
if wordBuf.Len() == 0 {
|
|
if current+uint(spaceBuf.Len()) > lim {
|
|
current = 0
|
|
} else {
|
|
current += uint(spaceBuf.Len())
|
|
spaceBuf.WriteTo(buf)
|
|
}
|
|
spaceBuf.Reset()
|
|
} else {
|
|
current += uint(spaceBuf.Len() + wordBuf.Len())
|
|
spaceBuf.WriteTo(buf)
|
|
spaceBuf.Reset()
|
|
wordBuf.WriteTo(buf)
|
|
wordBuf.Reset()
|
|
}
|
|
buf.WriteRune(char)
|
|
current = 0
|
|
} else if unicode.IsSpace(char) {
|
|
if spaceBuf.Len() == 0 || wordBuf.Len() > 0 {
|
|
current += uint(spaceBuf.Len() + wordBuf.Len())
|
|
spaceBuf.WriteTo(buf)
|
|
spaceBuf.Reset()
|
|
wordBuf.WriteTo(buf)
|
|
wordBuf.Reset()
|
|
}
|
|
|
|
spaceBuf.WriteRune(char)
|
|
} else {
|
|
|
|
wordBuf.WriteRune(char)
|
|
|
|
if current+uint(spaceBuf.Len()+wordBuf.Len()) > lim && uint(wordBuf.Len()) < lim {
|
|
buf.WriteRune('\n')
|
|
current = 0
|
|
spaceBuf.Reset()
|
|
}
|
|
}
|
|
}
|
|
|
|
if wordBuf.Len() == 0 {
|
|
if current+uint(spaceBuf.Len()) <= lim {
|
|
spaceBuf.WriteTo(buf)
|
|
}
|
|
} else {
|
|
spaceBuf.WriteTo(buf)
|
|
wordBuf.WriteTo(buf)
|
|
}
|
|
|
|
return buf.String()
|
|
}
|