Decrease allocs when wrapping
This commit is contained in:
parent
18b58c5cf9
commit
e42b4fd9a6
@ -248,12 +248,12 @@ func (self hyperlink_state) as_escape_codes(for_close bool) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type line_builder struct {
|
type line_builder struct {
|
||||||
buf strings.Builder
|
buf []byte
|
||||||
last_text_pos, cursor_pos int
|
last_text_pos, cursor_pos int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *line_builder) reset() string {
|
func (self *line_builder) reset() string {
|
||||||
ans := self.buf.String()
|
ans := string(self.buf)
|
||||||
if len(ans) > self.last_text_pos {
|
if len(ans) > self.last_text_pos {
|
||||||
prefix := ans[:self.last_text_pos]
|
prefix := ans[:self.last_text_pos]
|
||||||
suffix := ans[self.last_text_pos:]
|
suffix := ans[self.last_text_pos:]
|
||||||
@ -264,15 +264,9 @@ func (self *line_builder) reset() string {
|
|||||||
} else {
|
} else {
|
||||||
ans = strings.TrimRightFunc(ans, unicode.IsSpace)
|
ans = strings.TrimRightFunc(ans, unicode.IsSpace)
|
||||||
}
|
}
|
||||||
sz := self.buf.Len()
|
self.buf = self.buf[:0]
|
||||||
self.buf.Reset()
|
|
||||||
self.last_text_pos = 0
|
self.last_text_pos = 0
|
||||||
self.cursor_pos = 0
|
self.cursor_pos = 0
|
||||||
if sz > 1024 {
|
|
||||||
self.buf.Grow(sz)
|
|
||||||
} else {
|
|
||||||
self.buf.Grow(1024)
|
|
||||||
}
|
|
||||||
return ans
|
return ans
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -281,25 +275,25 @@ func (self *line_builder) has_space_for_width(w, max_width int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *line_builder) add_char(ch rune) {
|
func (self *line_builder) add_char(ch rune) {
|
||||||
self.buf.WriteRune(ch)
|
self.buf = utf8.AppendRune(self.buf, ch)
|
||||||
self.last_text_pos = self.buf.Len()
|
self.last_text_pos = len(self.buf)
|
||||||
self.cursor_pos += wcswidth.Runewidth(ch)
|
self.cursor_pos += wcswidth.Runewidth(ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *line_builder) add_word(word string, width int) {
|
func (self *line_builder) add_word(word []byte, width int) {
|
||||||
self.buf.WriteString(word)
|
self.buf = append(self.buf, word...)
|
||||||
self.last_text_pos = self.buf.Len()
|
self.last_text_pos = len(self.buf)
|
||||||
self.cursor_pos += width
|
self.cursor_pos += width
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *line_builder) add_escape_code(code string) {
|
func (self *line_builder) add_escape_code(code string) {
|
||||||
self.buf.WriteString(code)
|
self.buf = append(self.buf, code...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *line_builder) add_escape_code2(prefix string, body []byte, suffix string) {
|
func (self *line_builder) add_escape_code2(prefix string, body []byte, suffix string) {
|
||||||
self.buf.WriteString(prefix)
|
self.buf = append(self.buf, prefix...)
|
||||||
self.buf.Write(body)
|
self.buf = append(self.buf, body...)
|
||||||
self.buf.WriteString(suffix)
|
self.buf = append(self.buf, suffix...)
|
||||||
}
|
}
|
||||||
|
|
||||||
type escape_code_ struct {
|
type escape_code_ struct {
|
||||||
@ -313,14 +307,12 @@ type word_builder struct {
|
|||||||
wcswidth *wcswidth.WCWidthIterator
|
wcswidth *wcswidth.WCWidthIterator
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *word_builder) reset() string {
|
func (self *word_builder) reset(copy_current_word func([]byte)) {
|
||||||
ans := utils.UnsafeBytesToString(self.buf)
|
copy_current_word(self.buf)
|
||||||
sz := utils.Min(utils.Max(64, len(ans)), 4096)
|
self.buf = self.buf[:0]
|
||||||
self.buf = make([]byte, 0, sz)
|
|
||||||
self.escape_codes = self.escape_codes[:0]
|
self.escape_codes = self.escape_codes[:0]
|
||||||
self.text_start_position = 0
|
self.text_start_position = 0
|
||||||
self.wcswidth.Reset()
|
self.wcswidth.Reset()
|
||||||
return ans
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *word_builder) is_empty() bool {
|
func (self *word_builder) is_empty() bool {
|
||||||
@ -401,7 +393,7 @@ type wrapper struct {
|
|||||||
func (self *wrapper) newline_prefix() {
|
func (self *wrapper) newline_prefix() {
|
||||||
self.current_line.add_escape_code(self.sgr.as_escape_codes(true))
|
self.current_line.add_escape_code(self.sgr.as_escape_codes(true))
|
||||||
self.current_line.add_escape_code(self.hyperlink.as_escape_codes(true))
|
self.current_line.add_escape_code(self.hyperlink.as_escape_codes(true))
|
||||||
self.current_line.add_word(self.indent, self.indent_width)
|
self.current_line.add_word(utils.UnsafeStringToBytes(self.indent), self.indent_width)
|
||||||
self.current_line.add_escape_code(self.sgr.as_escape_codes(false))
|
self.current_line.add_escape_code(self.sgr.as_escape_codes(false))
|
||||||
self.current_line.add_escape_code(self.hyperlink.as_escape_codes(false))
|
self.current_line.add_escape_code(self.hyperlink.as_escape_codes(false))
|
||||||
}
|
}
|
||||||
@ -438,7 +430,9 @@ func (self *wrapper) print_word() {
|
|||||||
self.sgr.apply_csi(e.body)
|
self.sgr.apply_csi(e.body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.current_line.add_word(self.current_word.reset(), w)
|
self.current_word.reset(func(word []byte) {
|
||||||
|
self.current_line.add_word(word, w)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *wrapper) handle_rune(ch rune) error {
|
func (self *wrapper) handle_rune(ch rune) error {
|
||||||
@ -474,9 +468,9 @@ func (self *wrapper) wrap_text(text string) []string {
|
|||||||
return []string{""}
|
return []string{""}
|
||||||
}
|
}
|
||||||
self.current_line.reset()
|
self.current_line.reset()
|
||||||
self.current_word.reset()
|
self.current_word.reset(func([]byte) {})
|
||||||
self.lines = self.lines[:0]
|
self.lines = self.lines[:0]
|
||||||
self.current_line.add_word(self.indent, self.indent_width)
|
self.current_line.add_word(utils.UnsafeStringToBytes(self.indent), self.indent_width)
|
||||||
self.ep.ParseString(text)
|
self.ep.ParseString(text)
|
||||||
if !self.current_word.is_empty() {
|
if !self.current_word.is_empty() {
|
||||||
self.print_word()
|
self.print_word()
|
||||||
|
|||||||
@ -122,6 +122,10 @@ func (self *WCWidthIterator) Parse(b []byte) (ans int) {
|
|||||||
return self.current_width
|
return self.current_width
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *WCWidthIterator) CurrentWidth() int {
|
||||||
|
return self.current_width
|
||||||
|
}
|
||||||
|
|
||||||
func Stringwidth(text string) int {
|
func Stringwidth(text string) int {
|
||||||
w := CreateWCWidthIterator()
|
w := CreateWCWidthIterator()
|
||||||
return w.Parse(utils.UnsafeStringToBytes(text))
|
return w.Parse(utils.UnsafeStringToBytes(text))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user