Use CharacterCount over RuneCount

This commit is contained in:
Zachary Yedidia
2020-05-20 16:47:08 -04:00
parent bdff221870
commit 79c0ea17ad
17 changed files with 64 additions and 83 deletions

View File

@@ -5,7 +5,6 @@ import (
"runtime" "runtime"
"strings" "strings"
"time" "time"
"unicode/utf8"
shellquote "github.com/kballard/go-shellquote" shellquote "github.com/kballard/go-shellquote"
"github.com/zyedidia/clipboard" "github.com/zyedidia/clipboard"
@@ -175,7 +174,7 @@ func (h *BufPane) CursorRight() bool {
if tabstospaces && tabmovement { if tabstospaces && tabmovement {
tabsize := int(h.Buf.Settings["tabsize"].(float64)) tabsize := int(h.Buf.Settings["tabsize"].(float64))
line := h.Buf.LineBytes(h.Cursor.Y) 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++ { for i := 0; i < tabsize; i++ {
h.Cursor.Right() h.Cursor.Right()
} }
@@ -486,7 +485,7 @@ func (h *BufPane) InsertNewline() bool {
// Remove the whitespaces if keepautoindent setting is off // Remove the whitespaces if keepautoindent setting is off
if util.IsSpacesOrTabs(h.Buf.LineBytes(h.Cursor.Y-1)) && !h.Buf.Settings["keepautoindent"].(bool) { if util.IsSpacesOrTabs(h.Buf.LineBytes(h.Cursor.Y-1)) && !h.Buf.Settings["keepautoindent"].(bool) {
line := h.Buf.LineBytes(h.Cursor.Y - 1) 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() h.Cursor.LastVisualX = h.Cursor.GetVisualX()
@@ -511,7 +510,7 @@ func (h *BufPane) Backspace() bool {
// tab (tabSize number of spaces) // tab (tabSize number of spaces)
lineStart := util.SliceStart(h.Buf.LineBytes(h.Cursor.Y), h.Cursor.X) lineStart := util.SliceStart(h.Buf.LineBytes(h.Cursor.Y), h.Cursor.X)
tabSize := int(h.Buf.Settings["tabsize"].(float64)) 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 loc := h.Cursor.Loc
h.Buf.Remove(loc.Move(-tabSize, h.Buf), loc) h.Buf.Remove(loc.Move(-tabSize, h.Buf), loc)
} else { } else {

View File

@@ -190,7 +190,7 @@ type BufPane struct {
tripleClick bool tripleClick bool
// Last search stores the last successful search for FindNext and FindPrev // Last search stores the last successful search for FindNext and FindPrev
lastSearch string lastSearch string
lastSearchRegex bool lastSearchRegex bool
// Should the current multiple cursor selection search based on word or // Should the current multiple cursor selection search based on word or
// based on selection (false for selection, true for word) // based on selection (false for selection, true for word)

View File

@@ -10,7 +10,6 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"unicode/utf8"
shellquote "github.com/kballard/go-shellquote" shellquote "github.com/kballard/go-shellquote"
"github.com/zyedidia/micro/v2/internal/buffer" "github.com/zyedidia/micro/v2/internal/buffer"
@@ -703,7 +702,7 @@ func (h *BufPane) GotoCmd(args []string) {
return return
} }
line = util.Clamp(line-1, 0, h.Buf.LinesNum()-1) 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}) h.Cursor.GotoLoc(buffer.Loc{col, line})
} else { } else {
line, err := strconv.Atoi(args[0]) line, err := strconv.Atoi(args[0])
@@ -827,7 +826,7 @@ func (h *BufPane) ReplaceCmd(args []string) {
nreplaced++ nreplaced++
} else if !canceled && !yes { } else if !canceled && !yes {
searchLoc = locs[0] searchLoc = locs[0]
searchLoc.X += utf8.RuneCount(replace) searchLoc.X += util.CharacterCount(replace)
} else if canceled { } else if canceled {
h.Cursor.ResetSelection() h.Cursor.ResetSelection()
h.Buf.RelocateCursors() h.Buf.RelocateCursors()

View File

@@ -6,7 +6,6 @@ import (
"os" "os"
"sort" "sort"
"strings" "strings"
"unicode/utf8"
"github.com/zyedidia/micro/v2/internal/util" "github.com/zyedidia/micro/v2/internal/util"
) )
@@ -54,7 +53,7 @@ func (b *Buffer) CycleAutocomplete(forward bool) {
start := c.Loc start := c.Loc
end := c.Loc end := c.Loc
if prevSuggestion < len(b.Suggestions) && prevSuggestion >= 0 { 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 { } else {
// end = start.Move(1, b) // end = start.Move(1, b)
} }
@@ -82,7 +81,7 @@ func GetWord(b *Buffer) ([]byte, int) {
args := bytes.FieldsFunc(l, util.IsNonAlphaNumeric) args := bytes.FieldsFunc(l, util.IsNonAlphaNumeric)
input := args[len(args)-1] 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) // GetArg gets the most recent word (separated by ' ' only)
@@ -98,7 +97,7 @@ func GetArg(b *Buffer) (string, int) {
if i == len(args)-1 { if i == len(args)-1 {
break break
} }
argstart += utf8.RuneCount(a) + 1 argstart += util.CharacterCount(a) + 1
} }
return input, argstart return input, argstart
@@ -162,7 +161,7 @@ func BufferComplete(b *Buffer) ([]string, []string) {
return []string{}, []string{} return []string{}, []string{}
} }
inputLen := utf8.RuneCount(input) inputLen := util.CharacterCount(input)
suggestionsSet := make(map[string]struct{}) suggestionsSet := make(map[string]struct{})
@@ -171,7 +170,7 @@ func BufferComplete(b *Buffer) ([]string, []string) {
l := b.LineBytes(i) l := b.LineBytes(i)
words := bytes.FieldsFunc(l, util.IsNonAlphaNumeric) words := bytes.FieldsFunc(l, util.IsNonAlphaNumeric)
for _, w := range words { 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) strw := string(w)
if _, ok := suggestionsSet[strw]; !ok { if _, ok := suggestionsSet[strw]; !ok {
suggestionsSet[strw] = struct{}{} suggestionsSet[strw] = struct{}{}
@@ -184,7 +183,7 @@ func BufferComplete(b *Buffer) ([]string, []string) {
l := b.LineBytes(i) l := b.LineBytes(i)
words := bytes.FieldsFunc(l, util.IsNonAlphaNumeric) words := bytes.FieldsFunc(l, util.IsNonAlphaNumeric)
for _, w := range words { 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) strw := string(w)
if _, ok := suggestionsSet[strw]; !ok { if _, ok := suggestionsSet[strw]; !ok {
suggestionsSet[strw] = struct{}{} suggestionsSet[strw] = struct{}{}

View File

@@ -15,7 +15,6 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"unicode/utf8"
luar "layeh.com/gopher-luar" luar "layeh.com/gopher-luar"
@@ -809,7 +808,7 @@ func (b *Buffer) MoveLinesUp(start int, end int) {
if end == len(b.lines) { if end == len(b.lines) {
b.Insert( b.Insert(
Loc{ Loc{
utf8.RuneCount(b.lines[end-1].data), util.CharacterCount(b.lines[end-1].data),
end - 1, end - 1,
}, },
"\n"+l, "\n"+l,

View File

@@ -4,12 +4,12 @@ import (
"math/rand" "math/rand"
"strings" "strings"
"testing" "testing"
"unicode/utf8"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
ulua "github.com/zyedidia/micro/v2/internal/lua"
"github.com/zyedidia/micro/v2/internal/config" "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 { type operation struct {
@@ -158,9 +158,9 @@ func benchEdit(testingB *testing.B, nLines, nCursors int) {
operations := make([]operation, nCursors) operations := make([]operation, nCursors)
for i := range operations { for i := range operations {
startLine := (i * regionSize) + rand.Intn(regionSize-5) 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) 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{ operations[i] = operation{
start: Loc{startColumn, startLine}, start: Loc{startColumn, startLine},

View File

@@ -1,15 +1,13 @@
package buffer package buffer
import ( import (
"unicode/utf8"
"github.com/zyedidia/clipboard" "github.com/zyedidia/clipboard"
"github.com/zyedidia/micro/v2/internal/util" "github.com/zyedidia/micro/v2/internal/util"
) )
// InBounds returns whether the given location is a valid character position in the given buffer // InBounds returns whether the given location is a valid character position in the given buffer
func InBounds(pos Loc, buf *Buffer) bool { 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 return false
} }
@@ -76,8 +74,8 @@ func (c *Cursor) GetVisualX() int {
bytes := c.buf.LineBytes(c.Y) bytes := c.buf.LineBytes(c.Y)
tabsize := int(c.buf.Settings["tabsize"].(float64)) tabsize := int(c.buf.Settings["tabsize"].(float64))
if c.X > utf8.RuneCount(bytes) { if c.X > util.CharacterCount(bytes) {
c.X = utf8.RuneCount(bytes) - 1 c.X = util.CharacterCount(bytes) - 1
} }
return util.StringWidth(bytes, c.X, tabsize) return util.StringWidth(bytes, c.X, tabsize)
@@ -102,7 +100,7 @@ func (c *Cursor) Start() {
func (c *Cursor) StartOfText() { func (c *Cursor) StartOfText() {
c.Start() c.Start()
for util.IsWhitespace(c.RuneUnder(c.X)) { 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 break
} }
c.Right() c.Right()
@@ -114,7 +112,7 @@ func (c *Cursor) StartOfText() {
func (c *Cursor) IsStartOfText() bool { func (c *Cursor) IsStartOfText() bool {
x := 0 x := 0
for util.IsWhitespace(c.RuneUnder(x)) { 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 break
} }
x++ x++
@@ -124,7 +122,7 @@ func (c *Cursor) IsStartOfText() bool {
// End moves the cursor to the end of the line it is on // End moves the cursor to the end of the line it is on
func (c *Cursor) End() { 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() c.LastVisualX = c.GetVisualX()
} }
@@ -242,8 +240,8 @@ func (c *Cursor) UpN(amount int) {
bytes := c.buf.LineBytes(proposedY) bytes := c.buf.LineBytes(proposedY)
c.X = c.GetCharPosInLine(bytes, c.LastVisualX) c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) { if c.X > util.CharacterCount(bytes) || (amount < 0 && proposedY == c.Y) {
c.X = utf8.RuneCount(bytes) c.X = util.CharacterCount(bytes)
} }
c.Y = proposedY c.Y = proposedY
@@ -285,7 +283,7 @@ func (c *Cursor) Right() {
if c.Loc == c.buf.End() { if c.Loc == c.buf.End() {
return return
} }
if c.X < utf8.RuneCount(c.buf.LineBytes(c.Y)) { if c.X < util.CharacterCount(c.buf.LineBytes(c.Y)) {
c.X++ c.X++
} else { } else {
c.Down() c.Down()
@@ -306,8 +304,8 @@ func (c *Cursor) Relocate() {
if c.X < 0 { if c.X < 0 {
c.X = 0 c.X = 0
} else if c.X > utf8.RuneCount(c.buf.LineBytes(c.Y)) { } else if c.X > util.CharacterCount(c.buf.LineBytes(c.Y)) {
c.X = utf8.RuneCount(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.SetSelectionStart(Loc{backward, c.Y})
c.OrigSelection[0] = c.CurSelection[0] 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)) { for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
forward++ forward++
} }
@@ -365,7 +363,7 @@ func (c *Cursor) AddWordToSelection() {
if c.Loc.GreaterThan(c.OrigSelection[1]) { if c.Loc.GreaterThan(c.OrigSelection[1]) {
forward := c.X 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)) { for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
forward++ forward++
} }
@@ -392,7 +390,7 @@ func (c *Cursor) SelectTo(loc Loc) {
// WordRight moves the cursor one word to the right // WordRight moves the cursor one word to the right
func (c *Cursor) WordRight() { func (c *Cursor) WordRight() {
for util.IsWhitespace(c.RuneUnder(c.X)) { 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() c.Right()
return return
} }
@@ -400,7 +398,7 @@ func (c *Cursor) WordRight() {
} }
c.Right() c.Right()
for util.IsWordChar(c.RuneUnder(c.X)) { 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 return
} }
c.Right() c.Right()
@@ -429,7 +427,7 @@ func (c *Cursor) WordLeft() {
// RuneUnder returns the rune under the given x position // RuneUnder returns the rune under the given x position
func (c *Cursor) RuneUnder(x int) rune { func (c *Cursor) RuneUnder(x int) rune {
line := c.buf.LineBytes(c.Y) line := c.buf.LineBytes(c.Y)
if len(line) == 0 || x >= utf8.RuneCount(line) { if len(line) == 0 || x >= util.CharacterCount(line) {
return '\n' return '\n'
} else if x < 0 { } else if x < 0 {
x = 0 x = 0

View File

@@ -3,12 +3,12 @@ package buffer
import ( import (
"bytes" "bytes"
"time" "time"
"unicode/utf8"
dmp "github.com/sergi/go-diff/diffmatchpatch" dmp "github.com/sergi/go-diff/diffmatchpatch"
"github.com/zyedidia/micro/v2/internal/config" "github.com/zyedidia/micro/v2/internal/config"
ulua "github.com/zyedidia/micro/v2/internal/lua" ulua "github.com/zyedidia/micro/v2/internal/lua"
"github.com/zyedidia/micro/v2/internal/screen" "github.com/zyedidia/micro/v2/internal/screen"
"github.com/zyedidia/micro/v2/internal/util"
luar "layeh.com/gopher-luar" luar "layeh.com/gopher-luar"
) )
@@ -62,10 +62,10 @@ func (eh *EventHandler) DoTextEvent(t *TextEvent, useUndo bool) {
var textX int var textX int
if t.EventType == TextEventInsert { if t.EventType == TextEventInsert {
linecount := eh.buf.LinesNum() - oldl linecount := eh.buf.LinesNum() - oldl
textcount := utf8.RuneCount(text) textcount := util.CharacterCount(text)
lastnl = bytes.LastIndex(text, []byte{'\n'}) lastnl = bytes.LastIndex(text, []byte{'\n'})
if lastnl >= 0 { if lastnl >= 0 {
endX = utf8.RuneCount(text[lastnl+1:]) endX = util.CharacterCount(text[lastnl+1:])
textX = endX textX = endX
} else { } else {
endX = start.X + textcount endX = start.X + textcount
@@ -123,7 +123,7 @@ func ExecuteTextEvent(t *TextEvent, buf *SharedBuffer) {
t.Deltas[i].Text = buf.remove(d.Start, d.End) t.Deltas[i].Text = buf.remove(d.Start, d.End)
buf.insert(d.Start, d.Text) buf.insert(d.Start, d.Text)
t.Deltas[i].Start = d.Start 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 { 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] 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() loc := eh.buf.Start()
for _, d := range diff { for _, d := range diff {
if d.Type == dmp.DiffDelete { 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 { } else {
if d.Type == dmp.DiffInsert { if d.Type == dmp.DiffInsert {
eh.Insert(loc, d.Text) 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)
} }
} }
} }

View File

@@ -5,10 +5,9 @@ import (
"bytes" "bytes"
"io" "io"
"sync" "sync"
"unicode/utf8"
"github.com/zyedidia/micro/v2/pkg/highlight"
"github.com/zyedidia/micro/v2/internal/util" "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 // 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 // End returns the location of the last character in the buffer
func (la *LineArray) End() Loc { func (la *LineArray) End() Loc {
numlines := len(la.lines) 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 // LineBytes returns line n as an array of bytes

View File

@@ -1,8 +1,6 @@
package buffer package buffer
import ( import (
"unicode/utf8"
"github.com/zyedidia/micro/v2/internal/util" "github.com/zyedidia/micro/v2/internal/util"
) )
@@ -68,9 +66,9 @@ func DiffLA(a, b Loc, buf *LineArray) int {
loc := 0 loc := 0
for i := a.Y + 1; i < b.Y; i++ { for i := a.Y + 1; i < b.Y; i++ {
// + 1 for the newline // + 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 return loc
} }
@@ -80,7 +78,7 @@ func (l Loc) right(buf *LineArray) Loc {
return Loc{l.X + 1, l.Y} return Loc{l.X + 1, l.Y}
} }
var res Loc 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} res = Loc{l.X + 1, l.Y}
} else { } else {
res = Loc{0, l.Y + 1} res = Loc{0, l.Y + 1}
@@ -97,7 +95,7 @@ func (l Loc) left(buf *LineArray) Loc {
if l.X > 0 { if l.X > 0 {
res = Loc{l.X - 1, l.Y} res = Loc{l.X - 1, l.Y}
} else { } 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 return res
} }

View File

@@ -11,7 +11,6 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"unicode" "unicode"
"unicode/utf8"
"github.com/zyedidia/micro/v2/internal/config" "github.com/zyedidia/micro/v2/internal/config"
"github.com/zyedidia/micro/v2/internal/screen" "github.com/zyedidia/micro/v2/internal/screen"
@@ -100,9 +99,9 @@ func (b *Buffer) saveToFile(filename string, withSudo bool) error {
b.UpdateRules() b.UpdateRules()
if b.Settings["rmtrailingws"].(bool) { if b.Settings["rmtrailingws"].(bool) {
for i, l := range b.lines { 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}) b.Remove(Loc{leftover, i}, Loc{linelen, i})
} }

View File

@@ -2,7 +2,6 @@ package buffer
import ( import (
"regexp" "regexp"
"unicode/utf8"
"github.com/zyedidia/micro/v2/internal/util" "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 charpos := 0
if i == start.Y && start.Y == end.Y { if i == start.Y && start.Y == end.Y {
nchars := utf8.RuneCount(l) nchars := util.CharacterCount(l)
start.X = util.Clamp(start.X, 0, nchars) start.X = util.Clamp(start.X, 0, nchars)
end.X = util.Clamp(end.X, 0, nchars) end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X) l = util.SliceStart(l, end.X)
l = util.SliceEnd(l, start.X) l = util.SliceEnd(l, start.X)
charpos = start.X charpos = start.X
} else if i == start.Y { } else if i == start.Y {
nchars := utf8.RuneCount(l) nchars := util.CharacterCount(l)
start.X = util.Clamp(start.X, 0, nchars) start.X = util.Clamp(start.X, 0, nchars)
l = util.SliceEnd(l, start.X) l = util.SliceEnd(l, start.X)
charpos = start.X charpos = start.X
} else if i == end.Y { } else if i == end.Y {
nchars := utf8.RuneCount(l) nchars := util.CharacterCount(l)
end.X = util.Clamp(end.X, 0, nchars) end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X) 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 charpos := 0
if i == start.Y && start.Y == end.Y { if i == start.Y && start.Y == end.Y {
nchars := utf8.RuneCount(l) nchars := util.CharacterCount(l)
start.X = util.Clamp(start.X, 0, nchars) start.X = util.Clamp(start.X, 0, nchars)
end.X = util.Clamp(end.X, 0, nchars) end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X) l = util.SliceStart(l, end.X)
l = util.SliceEnd(l, start.X) l = util.SliceEnd(l, start.X)
charpos = start.X charpos = start.X
} else if i == start.Y { } else if i == start.Y {
nchars := utf8.RuneCount(l) nchars := util.CharacterCount(l)
start.X = util.Clamp(start.X, 0, nchars) start.X = util.Clamp(start.X, 0, nchars)
l = util.SliceEnd(l, start.X) l = util.SliceEnd(l, start.X)
charpos = start.X charpos = start.X
} else if i == end.Y { } else if i == end.Y {
nchars := utf8.RuneCount(l) nchars := util.CharacterCount(l)
end.X = util.Clamp(end.X, 0, nchars) end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X) 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) result = search.Expand(result, replace, in, submatches)
} }
found++ found++
netrunes += utf8.RuneCount(in) - utf8.RuneCount(result) netrunes += util.CharacterCount(in) - util.CharacterCount(result)
return result return result
}) })
from := Loc{charpos, i} 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}) deltas = append(deltas, Delta{newText, from, to})
} }

View File

@@ -1,8 +1,6 @@
package display package display
import ( import (
"unicode/utf8"
runewidth "github.com/mattn/go-runewidth" runewidth "github.com/mattn/go-runewidth"
"github.com/zyedidia/micro/v2/internal/buffer" "github.com/zyedidia/micro/v2/internal/buffer"
"github.com/zyedidia/micro/v2/internal/config" "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 { func (i *InfoWindow) LocFromVisual(vloc buffer.Loc) buffer.Loc {
c := i.Buffer.GetActiveCursor() c := i.Buffer.GetActiveCursor()
l := i.Buffer.LineBytes(0) 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} return buffer.Loc{c.GetCharPosInLine(l, vloc.X-n), 0}
} }
@@ -86,7 +84,7 @@ func (i *InfoWindow) displayBuffer() {
activeC := b.GetActiveCursor() activeC := b.GetActiveCursor()
blocX := 0 blocX := 0
vlocX := utf8.RuneCountInString(i.Msg) vlocX := util.CharacterCountInString(i.Msg)
tabsize := 4 tabsize := 4
line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, blocX, tabsize) line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, blocX, tabsize)
@@ -192,7 +190,7 @@ func (i *InfoWindow) scrollToSuggestion() {
s := i.totalSize() s := i.totalSize()
for j, n := range i.Suggestions { for j, n := range i.Suggestions {
c := utf8.RuneCountInString(n) c := util.CharacterCountInString(n)
if j == i.CurSuggestion { if j == i.CurSuggestion {
if x+c >= i.hscroll+i.Width { if x+c >= i.hscroll+i.Width {
i.hscroll = util.Clamp(x+c+1-i.Width, 0, s-i.Width) i.hscroll = util.Clamp(x+c+1-i.Width, 0, s-i.Width)

View File

@@ -6,7 +6,6 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"unicode/utf8"
luar "layeh.com/gopher-luar" luar "layeh.com/gopher-luar"
@@ -168,8 +167,8 @@ func (s *StatusLine) Display() {
statusLineStyle = style statusLineStyle = style
} }
leftLen := util.StringWidth(leftText, utf8.RuneCount(leftText), 1) leftLen := util.StringWidth(leftText, util.CharacterCount(leftText), 1)
rightLen := util.StringWidth(rightText, utf8.RuneCount(rightText), 1) rightLen := util.StringWidth(rightText, util.CharacterCount(rightText), 1)
winX := s.win.X winX := s.win.X
for x := 0; x < s.win.Width; x++ { for x := 0; x < s.win.Width; x++ {

View File

@@ -1,8 +1,6 @@
package display package display
import ( import (
"unicode/utf8"
runewidth "github.com/mattn/go-runewidth" runewidth "github.com/mattn/go-runewidth"
"github.com/zyedidia/micro/v2/internal/buffer" "github.com/zyedidia/micro/v2/internal/buffer"
"github.com/zyedidia/micro/v2/internal/config" "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 { for i, n := range w.Names {
x++ x++
s := utf8.RuneCountInString(n) s := util.CharacterCountInString(n)
if vloc.Y == w.Y && vloc.X < x+s { if vloc.Y == w.Y && vloc.X < x+s {
return i return i
} }
@@ -75,7 +73,7 @@ func (w *TabWindow) SetActive(a int) {
s := w.TotalSize() s := w.TotalSize()
for i, n := range w.Names { for i, n := range w.Names {
c := utf8.RuneCountInString(n) c := util.CharacterCountInString(n)
if i == a { if i == a {
if x+c >= w.hscroll+w.Width { if x+c >= w.hscroll+w.Width {
w.hscroll = util.Clamp(x+c+1-w.Width, 0, s-w.Width) w.hscroll = util.Clamp(x+c+1-w.Width, 0, s-w.Width)

View File

@@ -1,8 +1,6 @@
package display package display
import ( import (
"unicode/utf8"
"github.com/zyedidia/micro/v2/internal/buffer" "github.com/zyedidia/micro/v2/internal/buffer"
"github.com/zyedidia/micro/v2/internal/config" "github.com/zyedidia/micro/v2/internal/config"
"github.com/zyedidia/micro/v2/internal/screen" "github.com/zyedidia/micro/v2/internal/screen"
@@ -99,7 +97,7 @@ func (w *TermWindow) Display() {
} }
text := []byte(w.Name()) text := []byte(w.Name())
textLen := utf8.RuneCount(text) textLen := util.CharacterCount(text)
for x := 0; x < w.Width; x++ { for x := 0; x < w.Width; x++ {
if x < textLen { if x < textLen {
r, combc, size := util.DecodeCharacter(text) r, combc, size := util.DecodeCharacter(text)

View File

@@ -16,7 +16,6 @@ var combining = &unicode.RangeTable{
}, },
} }
// DecodeCharacter returns the next character from an array of bytes // DecodeCharacter returns the next character from an array of bytes
// A character is a rune along with any accompanying combining runes // A character is a rune along with any accompanying combining runes
func DecodeCharacter(b []byte) (rune, []rune, int) { func DecodeCharacter(b []byte) (rune, []rune, int) {