From 79c0ea17ad932329c0c935c9e02c69d052ceed83 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Wed, 20 May 2020 16:47:08 -0400 Subject: [PATCH] Use CharacterCount over RuneCount --- internal/action/actions.go | 7 +++---- internal/action/bufpane.go | 2 +- internal/action/command.go | 5 ++--- internal/buffer/autocomplete.go | 13 ++++++------- internal/buffer/buffer.go | 3 +-- internal/buffer/buffer_test.go | 8 ++++---- internal/buffer/cursor.go | 34 ++++++++++++++++----------------- internal/buffer/eventhandler.go | 12 ++++++------ internal/buffer/line_array.go | 5 ++--- internal/buffer/loc.go | 10 ++++------ internal/buffer/save.go | 5 ++--- internal/buffer/search.go | 17 ++++++++--------- internal/display/infowindow.go | 8 +++----- internal/display/statusline.go | 5 ++--- internal/display/tabwindow.go | 6 ++---- internal/display/termwindow.go | 4 +--- internal/util/unicode.go | 3 +-- 17 files changed, 64 insertions(+), 83 deletions(-) diff --git a/internal/action/actions.go b/internal/action/actions.go index c42b6c8a..ae19a3f9 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -5,7 +5,6 @@ import ( "runtime" "strings" "time" - "unicode/utf8" shellquote "github.com/kballard/go-shellquote" "github.com/zyedidia/clipboard" @@ -175,7 +174,7 @@ func (h *BufPane) CursorRight() bool { if tabstospaces && tabmovement { tabsize := int(h.Buf.Settings["tabsize"].(float64)) line := h.Buf.LineBytes(h.Cursor.Y) - if h.Cursor.X+tabsize < utf8.RuneCount(line) && util.IsSpaces(line[h.Cursor.X:h.Cursor.X+tabsize]) && util.IsBytesWhitespace(line[0:h.Cursor.X]) { + if h.Cursor.X+tabsize < util.CharacterCount(line) && util.IsSpaces(line[h.Cursor.X:h.Cursor.X+tabsize]) && util.IsBytesWhitespace(line[0:h.Cursor.X]) { for i := 0; i < tabsize; i++ { h.Cursor.Right() } @@ -486,7 +485,7 @@ func (h *BufPane) InsertNewline() bool { // Remove the whitespaces if keepautoindent setting is off if util.IsSpacesOrTabs(h.Buf.LineBytes(h.Cursor.Y-1)) && !h.Buf.Settings["keepautoindent"].(bool) { line := h.Buf.LineBytes(h.Cursor.Y - 1) - h.Buf.Remove(buffer.Loc{X: 0, Y: h.Cursor.Y - 1}, buffer.Loc{X: utf8.RuneCount(line), Y: h.Cursor.Y - 1}) + h.Buf.Remove(buffer.Loc{X: 0, Y: h.Cursor.Y - 1}, buffer.Loc{X: util.CharacterCount(line), Y: h.Cursor.Y - 1}) } } h.Cursor.LastVisualX = h.Cursor.GetVisualX() @@ -511,7 +510,7 @@ func (h *BufPane) Backspace() bool { // tab (tabSize number of spaces) lineStart := util.SliceStart(h.Buf.LineBytes(h.Cursor.Y), h.Cursor.X) tabSize := int(h.Buf.Settings["tabsize"].(float64)) - if h.Buf.Settings["tabstospaces"].(bool) && util.IsSpaces(lineStart) && len(lineStart) != 0 && utf8.RuneCount(lineStart)%tabSize == 0 { + if h.Buf.Settings["tabstospaces"].(bool) && util.IsSpaces(lineStart) && len(lineStart) != 0 && util.CharacterCount(lineStart)%tabSize == 0 { loc := h.Cursor.Loc h.Buf.Remove(loc.Move(-tabSize, h.Buf), loc) } else { diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index 24086d74..51d4ebd1 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -190,7 +190,7 @@ type BufPane struct { tripleClick bool // Last search stores the last successful search for FindNext and FindPrev - lastSearch string + lastSearch string lastSearchRegex bool // Should the current multiple cursor selection search based on word or // based on selection (false for selection, true for word) diff --git a/internal/action/command.go b/internal/action/command.go index 8361e5a1..cae2b3f6 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -10,7 +10,6 @@ import ( "regexp" "strconv" "strings" - "unicode/utf8" shellquote "github.com/kballard/go-shellquote" "github.com/zyedidia/micro/v2/internal/buffer" @@ -703,7 +702,7 @@ func (h *BufPane) GotoCmd(args []string) { return } line = util.Clamp(line-1, 0, h.Buf.LinesNum()-1) - col = util.Clamp(col-1, 0, utf8.RuneCount(h.Buf.LineBytes(line))) + col = util.Clamp(col-1, 0, util.CharacterCount(h.Buf.LineBytes(line))) h.Cursor.GotoLoc(buffer.Loc{col, line}) } else { line, err := strconv.Atoi(args[0]) @@ -827,7 +826,7 @@ func (h *BufPane) ReplaceCmd(args []string) { nreplaced++ } else if !canceled && !yes { searchLoc = locs[0] - searchLoc.X += utf8.RuneCount(replace) + searchLoc.X += util.CharacterCount(replace) } else if canceled { h.Cursor.ResetSelection() h.Buf.RelocateCursors() diff --git a/internal/buffer/autocomplete.go b/internal/buffer/autocomplete.go index 3bb4d307..7af3f0b7 100644 --- a/internal/buffer/autocomplete.go +++ b/internal/buffer/autocomplete.go @@ -6,7 +6,6 @@ import ( "os" "sort" "strings" - "unicode/utf8" "github.com/zyedidia/micro/v2/internal/util" ) @@ -54,7 +53,7 @@ func (b *Buffer) CycleAutocomplete(forward bool) { start := c.Loc end := c.Loc if prevSuggestion < len(b.Suggestions) && prevSuggestion >= 0 { - start = end.Move(-utf8.RuneCountInString(b.Completions[prevSuggestion]), b) + start = end.Move(-util.CharacterCountInString(b.Completions[prevSuggestion]), b) } else { // end = start.Move(1, b) } @@ -82,7 +81,7 @@ func GetWord(b *Buffer) ([]byte, int) { args := bytes.FieldsFunc(l, util.IsNonAlphaNumeric) input := args[len(args)-1] - return input, c.X - utf8.RuneCount(input) + return input, c.X - util.CharacterCount(input) } // GetArg gets the most recent word (separated by ' ' only) @@ -98,7 +97,7 @@ func GetArg(b *Buffer) (string, int) { if i == len(args)-1 { break } - argstart += utf8.RuneCount(a) + 1 + argstart += util.CharacterCount(a) + 1 } return input, argstart @@ -162,7 +161,7 @@ func BufferComplete(b *Buffer) ([]string, []string) { return []string{}, []string{} } - inputLen := utf8.RuneCount(input) + inputLen := util.CharacterCount(input) suggestionsSet := make(map[string]struct{}) @@ -171,7 +170,7 @@ func BufferComplete(b *Buffer) ([]string, []string) { l := b.LineBytes(i) words := bytes.FieldsFunc(l, util.IsNonAlphaNumeric) for _, w := range words { - if bytes.HasPrefix(w, input) && utf8.RuneCount(w) > inputLen { + if bytes.HasPrefix(w, input) && util.CharacterCount(w) > inputLen { strw := string(w) if _, ok := suggestionsSet[strw]; !ok { suggestionsSet[strw] = struct{}{} @@ -184,7 +183,7 @@ func BufferComplete(b *Buffer) ([]string, []string) { l := b.LineBytes(i) words := bytes.FieldsFunc(l, util.IsNonAlphaNumeric) for _, w := range words { - if bytes.HasPrefix(w, input) && utf8.RuneCount(w) > inputLen { + if bytes.HasPrefix(w, input) && util.CharacterCount(w) > inputLen { strw := string(w) if _, ok := suggestionsSet[strw]; !ok { suggestionsSet[strw] = struct{}{} diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index ab9ada06..ebfdeabe 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -15,7 +15,6 @@ import ( "strings" "sync" "time" - "unicode/utf8" luar "layeh.com/gopher-luar" @@ -809,7 +808,7 @@ func (b *Buffer) MoveLinesUp(start int, end int) { if end == len(b.lines) { b.Insert( Loc{ - utf8.RuneCount(b.lines[end-1].data), + util.CharacterCount(b.lines[end-1].data), end - 1, }, "\n"+l, diff --git a/internal/buffer/buffer_test.go b/internal/buffer/buffer_test.go index f6ee3dee..305d1627 100644 --- a/internal/buffer/buffer_test.go +++ b/internal/buffer/buffer_test.go @@ -4,12 +4,12 @@ import ( "math/rand" "strings" "testing" - "unicode/utf8" "github.com/stretchr/testify/assert" lua "github.com/yuin/gopher-lua" - ulua "github.com/zyedidia/micro/v2/internal/lua" "github.com/zyedidia/micro/v2/internal/config" + ulua "github.com/zyedidia/micro/v2/internal/lua" + "github.com/zyedidia/micro/v2/internal/util" ) type operation struct { @@ -158,9 +158,9 @@ func benchEdit(testingB *testing.B, nLines, nCursors int) { operations := make([]operation, nCursors) for i := range operations { startLine := (i * regionSize) + rand.Intn(regionSize-5) - startColumn := rand.Intn(utf8.RuneCountInString(b.Line(startLine)) + 1) + startColumn := rand.Intn(util.CharacterCountInString(b.Line(startLine)) + 1) endLine := startLine + 1 + rand.Intn(5) - endColumn := rand.Intn(utf8.RuneCountInString(b.Line(endLine)) + 1) + endColumn := rand.Intn(util.CharacterCountInString(b.Line(endLine)) + 1) operations[i] = operation{ start: Loc{startColumn, startLine}, diff --git a/internal/buffer/cursor.go b/internal/buffer/cursor.go index 4bd94808..2d4d2da2 100644 --- a/internal/buffer/cursor.go +++ b/internal/buffer/cursor.go @@ -1,15 +1,13 @@ package buffer import ( - "unicode/utf8" - "github.com/zyedidia/clipboard" "github.com/zyedidia/micro/v2/internal/util" ) // InBounds returns whether the given location is a valid character position in the given buffer func InBounds(pos Loc, buf *Buffer) bool { - if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > utf8.RuneCount(buf.LineBytes(pos.Y)) { + if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > util.CharacterCount(buf.LineBytes(pos.Y)) { return false } @@ -76,8 +74,8 @@ func (c *Cursor) GetVisualX() int { bytes := c.buf.LineBytes(c.Y) tabsize := int(c.buf.Settings["tabsize"].(float64)) - if c.X > utf8.RuneCount(bytes) { - c.X = utf8.RuneCount(bytes) - 1 + if c.X > util.CharacterCount(bytes) { + c.X = util.CharacterCount(bytes) - 1 } return util.StringWidth(bytes, c.X, tabsize) @@ -102,7 +100,7 @@ func (c *Cursor) Start() { func (c *Cursor) StartOfText() { c.Start() for util.IsWhitespace(c.RuneUnder(c.X)) { - if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) { + if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { break } c.Right() @@ -114,7 +112,7 @@ func (c *Cursor) StartOfText() { func (c *Cursor) IsStartOfText() bool { x := 0 for util.IsWhitespace(c.RuneUnder(x)) { - if x == utf8.RuneCount(c.buf.LineBytes(c.Y)) { + if x == util.CharacterCount(c.buf.LineBytes(c.Y)) { break } x++ @@ -124,7 +122,7 @@ func (c *Cursor) IsStartOfText() bool { // End moves the cursor to the end of the line it is on func (c *Cursor) End() { - c.X = utf8.RuneCount(c.buf.LineBytes(c.Y)) + c.X = util.CharacterCount(c.buf.LineBytes(c.Y)) c.LastVisualX = c.GetVisualX() } @@ -242,8 +240,8 @@ func (c *Cursor) UpN(amount int) { bytes := c.buf.LineBytes(proposedY) c.X = c.GetCharPosInLine(bytes, c.LastVisualX) - if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) { - c.X = utf8.RuneCount(bytes) + if c.X > util.CharacterCount(bytes) || (amount < 0 && proposedY == c.Y) { + c.X = util.CharacterCount(bytes) } c.Y = proposedY @@ -285,7 +283,7 @@ func (c *Cursor) Right() { if c.Loc == c.buf.End() { return } - if c.X < utf8.RuneCount(c.buf.LineBytes(c.Y)) { + if c.X < util.CharacterCount(c.buf.LineBytes(c.Y)) { c.X++ } else { c.Down() @@ -306,8 +304,8 @@ func (c *Cursor) Relocate() { if c.X < 0 { c.X = 0 - } else if c.X > utf8.RuneCount(c.buf.LineBytes(c.Y)) { - c.X = utf8.RuneCount(c.buf.LineBytes(c.Y)) + } else if c.X > util.CharacterCount(c.buf.LineBytes(c.Y)) { + c.X = util.CharacterCount(c.buf.LineBytes(c.Y)) } } @@ -333,7 +331,7 @@ func (c *Cursor) SelectWord() { c.SetSelectionStart(Loc{backward, c.Y}) c.OrigSelection[0] = c.CurSelection[0] - lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1 + lineLen := util.CharacterCount(c.buf.LineBytes(c.Y)) - 1 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) { forward++ } @@ -365,7 +363,7 @@ func (c *Cursor) AddWordToSelection() { if c.Loc.GreaterThan(c.OrigSelection[1]) { forward := c.X - lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1 + lineLen := util.CharacterCount(c.buf.LineBytes(c.Y)) - 1 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) { forward++ } @@ -392,7 +390,7 @@ func (c *Cursor) SelectTo(loc Loc) { // WordRight moves the cursor one word to the right func (c *Cursor) WordRight() { for util.IsWhitespace(c.RuneUnder(c.X)) { - if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) { + if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { c.Right() return } @@ -400,7 +398,7 @@ func (c *Cursor) WordRight() { } c.Right() for util.IsWordChar(c.RuneUnder(c.X)) { - if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) { + if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { return } c.Right() @@ -429,7 +427,7 @@ func (c *Cursor) WordLeft() { // RuneUnder returns the rune under the given x position func (c *Cursor) RuneUnder(x int) rune { line := c.buf.LineBytes(c.Y) - if len(line) == 0 || x >= utf8.RuneCount(line) { + if len(line) == 0 || x >= util.CharacterCount(line) { return '\n' } else if x < 0 { x = 0 diff --git a/internal/buffer/eventhandler.go b/internal/buffer/eventhandler.go index 4ce30241..3862d696 100644 --- a/internal/buffer/eventhandler.go +++ b/internal/buffer/eventhandler.go @@ -3,12 +3,12 @@ package buffer import ( "bytes" "time" - "unicode/utf8" dmp "github.com/sergi/go-diff/diffmatchpatch" "github.com/zyedidia/micro/v2/internal/config" ulua "github.com/zyedidia/micro/v2/internal/lua" "github.com/zyedidia/micro/v2/internal/screen" + "github.com/zyedidia/micro/v2/internal/util" luar "layeh.com/gopher-luar" ) @@ -62,10 +62,10 @@ func (eh *EventHandler) DoTextEvent(t *TextEvent, useUndo bool) { var textX int if t.EventType == TextEventInsert { linecount := eh.buf.LinesNum() - oldl - textcount := utf8.RuneCount(text) + textcount := util.CharacterCount(text) lastnl = bytes.LastIndex(text, []byte{'\n'}) if lastnl >= 0 { - endX = utf8.RuneCount(text[lastnl+1:]) + endX = util.CharacterCount(text[lastnl+1:]) textX = endX } else { endX = start.X + textcount @@ -123,7 +123,7 @@ func ExecuteTextEvent(t *TextEvent, buf *SharedBuffer) { t.Deltas[i].Text = buf.remove(d.Start, d.End) buf.insert(d.Start, d.Text) t.Deltas[i].Start = d.Start - t.Deltas[i].End = Loc{d.Start.X + utf8.RuneCount(d.Text), d.Start.Y} + t.Deltas[i].End = Loc{d.Start.X + util.CharacterCount(d.Text), d.Start.Y} } for i, j := 0, len(t.Deltas)-1; i < j; i, j = i+1, j-1 { t.Deltas[i], t.Deltas[j] = t.Deltas[j], t.Deltas[i] @@ -166,12 +166,12 @@ func (eh *EventHandler) ApplyDiff(new string) { loc := eh.buf.Start() for _, d := range diff { if d.Type == dmp.DiffDelete { - eh.Remove(loc, loc.MoveLA(utf8.RuneCountInString(d.Text), eh.buf.LineArray)) + eh.Remove(loc, loc.MoveLA(util.CharacterCountInString(d.Text), eh.buf.LineArray)) } else { if d.Type == dmp.DiffInsert { eh.Insert(loc, d.Text) } - loc = loc.MoveLA(utf8.RuneCountInString(d.Text), eh.buf.LineArray) + loc = loc.MoveLA(util.CharacterCountInString(d.Text), eh.buf.LineArray) } } } diff --git a/internal/buffer/line_array.go b/internal/buffer/line_array.go index c47a3948..325e2f52 100644 --- a/internal/buffer/line_array.go +++ b/internal/buffer/line_array.go @@ -5,10 +5,9 @@ import ( "bytes" "io" "sync" - "unicode/utf8" - "github.com/zyedidia/micro/v2/pkg/highlight" "github.com/zyedidia/micro/v2/internal/util" + "github.com/zyedidia/micro/v2/pkg/highlight" ) // Finds the byte index of the nth rune in a byte slice @@ -300,7 +299,7 @@ func (la *LineArray) Start() Loc { // End returns the location of the last character in the buffer func (la *LineArray) End() Loc { numlines := len(la.lines) - return Loc{utf8.RuneCount(la.lines[numlines-1].data), numlines - 1} + return Loc{util.CharacterCount(la.lines[numlines-1].data), numlines - 1} } // LineBytes returns line n as an array of bytes diff --git a/internal/buffer/loc.go b/internal/buffer/loc.go index ce55f468..44f59c78 100644 --- a/internal/buffer/loc.go +++ b/internal/buffer/loc.go @@ -1,8 +1,6 @@ package buffer import ( - "unicode/utf8" - "github.com/zyedidia/micro/v2/internal/util" ) @@ -68,9 +66,9 @@ func DiffLA(a, b Loc, buf *LineArray) int { loc := 0 for i := a.Y + 1; i < b.Y; i++ { // + 1 for the newline - loc += utf8.RuneCount(buf.LineBytes(i)) + 1 + loc += util.CharacterCount(buf.LineBytes(i)) + 1 } - loc += utf8.RuneCount(buf.LineBytes(a.Y)) - a.X + b.X + 1 + loc += util.CharacterCount(buf.LineBytes(a.Y)) - a.X + b.X + 1 return loc } @@ -80,7 +78,7 @@ func (l Loc) right(buf *LineArray) Loc { return Loc{l.X + 1, l.Y} } var res Loc - if l.X < utf8.RuneCount(buf.LineBytes(l.Y)) { + if l.X < util.CharacterCount(buf.LineBytes(l.Y)) { res = Loc{l.X + 1, l.Y} } else { res = Loc{0, l.Y + 1} @@ -97,7 +95,7 @@ func (l Loc) left(buf *LineArray) Loc { if l.X > 0 { res = Loc{l.X - 1, l.Y} } else { - res = Loc{utf8.RuneCount(buf.LineBytes(l.Y - 1)), l.Y - 1} + res = Loc{util.CharacterCount(buf.LineBytes(l.Y - 1)), l.Y - 1} } return res } diff --git a/internal/buffer/save.go b/internal/buffer/save.go index 25b1f385..5716f22e 100644 --- a/internal/buffer/save.go +++ b/internal/buffer/save.go @@ -11,7 +11,6 @@ import ( "path/filepath" "runtime" "unicode" - "unicode/utf8" "github.com/zyedidia/micro/v2/internal/config" "github.com/zyedidia/micro/v2/internal/screen" @@ -100,9 +99,9 @@ func (b *Buffer) saveToFile(filename string, withSudo bool) error { b.UpdateRules() if b.Settings["rmtrailingws"].(bool) { for i, l := range b.lines { - leftover := utf8.RuneCount(bytes.TrimRightFunc(l.data, unicode.IsSpace)) + leftover := util.CharacterCount(bytes.TrimRightFunc(l.data, unicode.IsSpace)) - linelen := utf8.RuneCount(l.data) + linelen := util.CharacterCount(l.data) b.Remove(Loc{leftover, i}, Loc{linelen, i}) } diff --git a/internal/buffer/search.go b/internal/buffer/search.go index 9550c01a..157f119d 100644 --- a/internal/buffer/search.go +++ b/internal/buffer/search.go @@ -2,7 +2,6 @@ package buffer import ( "regexp" - "unicode/utf8" "github.com/zyedidia/micro/v2/internal/util" ) @@ -20,19 +19,19 @@ func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) { charpos := 0 if i == start.Y && start.Y == end.Y { - nchars := utf8.RuneCount(l) + nchars := util.CharacterCount(l) start.X = util.Clamp(start.X, 0, nchars) end.X = util.Clamp(end.X, 0, nchars) l = util.SliceStart(l, end.X) l = util.SliceEnd(l, start.X) charpos = start.X } else if i == start.Y { - nchars := utf8.RuneCount(l) + nchars := util.CharacterCount(l) start.X = util.Clamp(start.X, 0, nchars) l = util.SliceEnd(l, start.X) charpos = start.X } else if i == end.Y { - nchars := utf8.RuneCount(l) + nchars := util.CharacterCount(l) end.X = util.Clamp(end.X, 0, nchars) l = util.SliceStart(l, end.X) } @@ -61,19 +60,19 @@ func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) { charpos := 0 if i == start.Y && start.Y == end.Y { - nchars := utf8.RuneCount(l) + nchars := util.CharacterCount(l) start.X = util.Clamp(start.X, 0, nchars) end.X = util.Clamp(end.X, 0, nchars) l = util.SliceStart(l, end.X) l = util.SliceEnd(l, start.X) charpos = start.X } else if i == start.Y { - nchars := utf8.RuneCount(l) + nchars := util.CharacterCount(l) start.X = util.Clamp(start.X, 0, nchars) l = util.SliceEnd(l, start.X) charpos = start.X } else if i == end.Y { - nchars := utf8.RuneCount(l) + nchars := util.CharacterCount(l) end.X = util.Clamp(end.X, 0, nchars) l = util.SliceStart(l, end.X) } @@ -163,12 +162,12 @@ func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []b result = search.Expand(result, replace, in, submatches) } found++ - netrunes += utf8.RuneCount(in) - utf8.RuneCount(result) + netrunes += util.CharacterCount(in) - util.CharacterCount(result) return result }) from := Loc{charpos, i} - to := Loc{charpos + utf8.RuneCount(l), i} + to := Loc{charpos + util.CharacterCount(l), i} deltas = append(deltas, Delta{newText, from, to}) } diff --git a/internal/display/infowindow.go b/internal/display/infowindow.go index c3e016f7..32662be2 100644 --- a/internal/display/infowindow.go +++ b/internal/display/infowindow.go @@ -1,8 +1,6 @@ package display import ( - "unicode/utf8" - runewidth "github.com/mattn/go-runewidth" "github.com/zyedidia/micro/v2/internal/buffer" "github.com/zyedidia/micro/v2/internal/config" @@ -70,7 +68,7 @@ func (i *InfoWindow) IsActive() bool { return true } func (i *InfoWindow) LocFromVisual(vloc buffer.Loc) buffer.Loc { c := i.Buffer.GetActiveCursor() l := i.Buffer.LineBytes(0) - n := utf8.RuneCountInString(i.Msg) + n := util.CharacterCountInString(i.Msg) return buffer.Loc{c.GetCharPosInLine(l, vloc.X-n), 0} } @@ -86,7 +84,7 @@ func (i *InfoWindow) displayBuffer() { activeC := b.GetActiveCursor() blocX := 0 - vlocX := utf8.RuneCountInString(i.Msg) + vlocX := util.CharacterCountInString(i.Msg) tabsize := 4 line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, blocX, tabsize) @@ -192,7 +190,7 @@ func (i *InfoWindow) scrollToSuggestion() { s := i.totalSize() for j, n := range i.Suggestions { - c := utf8.RuneCountInString(n) + c := util.CharacterCountInString(n) if j == i.CurSuggestion { if x+c >= i.hscroll+i.Width { i.hscroll = util.Clamp(x+c+1-i.Width, 0, s-i.Width) diff --git a/internal/display/statusline.go b/internal/display/statusline.go index 63ac9c99..5c5f5512 100644 --- a/internal/display/statusline.go +++ b/internal/display/statusline.go @@ -6,7 +6,6 @@ import ( "regexp" "strconv" "strings" - "unicode/utf8" luar "layeh.com/gopher-luar" @@ -168,8 +167,8 @@ func (s *StatusLine) Display() { statusLineStyle = style } - leftLen := util.StringWidth(leftText, utf8.RuneCount(leftText), 1) - rightLen := util.StringWidth(rightText, utf8.RuneCount(rightText), 1) + leftLen := util.StringWidth(leftText, util.CharacterCount(leftText), 1) + rightLen := util.StringWidth(rightText, util.CharacterCount(rightText), 1) winX := s.win.X for x := 0; x < s.win.Width; x++ { diff --git a/internal/display/tabwindow.go b/internal/display/tabwindow.go index c16747a3..6263586d 100644 --- a/internal/display/tabwindow.go +++ b/internal/display/tabwindow.go @@ -1,8 +1,6 @@ package display import ( - "unicode/utf8" - runewidth "github.com/mattn/go-runewidth" "github.com/zyedidia/micro/v2/internal/buffer" "github.com/zyedidia/micro/v2/internal/config" @@ -34,7 +32,7 @@ func (w *TabWindow) LocFromVisual(vloc buffer.Loc) int { for i, n := range w.Names { x++ - s := utf8.RuneCountInString(n) + s := util.CharacterCountInString(n) if vloc.Y == w.Y && vloc.X < x+s { return i } @@ -75,7 +73,7 @@ func (w *TabWindow) SetActive(a int) { s := w.TotalSize() for i, n := range w.Names { - c := utf8.RuneCountInString(n) + c := util.CharacterCountInString(n) if i == a { if x+c >= w.hscroll+w.Width { w.hscroll = util.Clamp(x+c+1-w.Width, 0, s-w.Width) diff --git a/internal/display/termwindow.go b/internal/display/termwindow.go index ec161bcd..6250fea9 100644 --- a/internal/display/termwindow.go +++ b/internal/display/termwindow.go @@ -1,8 +1,6 @@ package display import ( - "unicode/utf8" - "github.com/zyedidia/micro/v2/internal/buffer" "github.com/zyedidia/micro/v2/internal/config" "github.com/zyedidia/micro/v2/internal/screen" @@ -99,7 +97,7 @@ func (w *TermWindow) Display() { } text := []byte(w.Name()) - textLen := utf8.RuneCount(text) + textLen := util.CharacterCount(text) for x := 0; x < w.Width; x++ { if x < textLen { r, combc, size := util.DecodeCharacter(text) diff --git a/internal/util/unicode.go b/internal/util/unicode.go index 98a17c42..477bc691 100644 --- a/internal/util/unicode.go +++ b/internal/util/unicode.go @@ -16,7 +16,6 @@ var combining = &unicode.RangeTable{ }, } - // DecodeCharacter returns the next character from an array of bytes // A character is a rune along with any accompanying combining runes func DecodeCharacter(b []byte) (rune, []rune, int) { @@ -32,7 +31,7 @@ func DecodeCharacter(b []byte) (rune, []rune, int) { b = b[s:] c, s = utf8.DecodeRune(b) } - + return r, combc, size }