From 1563ab93dd29c3c3760254e6fecf9d274985e35d Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Mon, 14 Jan 2019 22:29:24 -0500 Subject: [PATCH] Use byte slice for insert --- cmd/micro/action/actions.go | 10 +++++----- cmd/micro/action/bufhandler.go | 4 ++-- cmd/micro/buffer/buffer.go | 9 +++++---- cmd/micro/buffer/eventhandler.go | 11 +++++------ cmd/micro/buffer/save.go | 2 +- cmd/micro/info/history.go | 4 ++-- cmd/micro/info/infobuffer.go | 4 ++-- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/cmd/micro/action/actions.go b/cmd/micro/action/actions.go index 32a22288..cd9e944b 100644 --- a/cmd/micro/action/actions.go +++ b/cmd/micro/action/actions.go @@ -386,14 +386,14 @@ func (h *BufHandler) InsertNewline() bool { ws := util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y)) cx := h.Cursor.X - h.Buf.Insert(h.Cursor.Loc, "\n") + h.Buf.Insert(h.Cursor.Loc, []byte{'\n'}) // h.Cursor.Right() if h.Buf.Settings["autoindent"].(bool) { if cx < len(ws) { ws = ws[0:cx] } - h.Buf.Insert(h.Cursor.Loc, string(ws)) + h.Buf.Insert(h.Cursor.Loc, ws) // for i := 0; i < len(ws); i++ { // h.Cursor.Right() // } @@ -733,10 +733,10 @@ func (h *BufHandler) Cut() bool { // DuplicateLine duplicates the current line or selection func (h *BufHandler) DuplicateLine() bool { if h.Cursor.HasSelection() { - h.Buf.Insert(h.Cursor.CurSelection[1], string(h.Cursor.GetSelection())) + h.Buf.Insert(h.Cursor.CurSelection[1], h.Cursor.GetSelection()) } else { h.Cursor.End() - h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y))) + h.Buf.Insert(h.Cursor.Loc, append([]byte{'\n'}, h.Buf.LineBytes(h.Cursor.Y)...)) // h.Cursor.Right() } @@ -794,7 +794,7 @@ func (h *BufHandler) paste(clip string) { h.Cursor.ResetSelection() } - h.Buf.Insert(h.Cursor.Loc, clip) + h.Buf.Insert(h.Cursor.Loc, []byte(clip)) // h.Cursor.Loc = h.Cursor.Loc.Move(Count(clip), h.Buf) h.freshClip = false InfoBar.Message("Pasted clipboard") diff --git a/cmd/micro/action/bufhandler.go b/cmd/micro/action/bufhandler.go index 23362eb0..e763b460 100644 --- a/cmd/micro/action/bufhandler.go +++ b/cmd/micro/action/bufhandler.go @@ -261,9 +261,9 @@ func (h *BufHandler) DoRuneInsert(r rune) { if h.isOverwriteMode { next := c.Loc next.X++ - h.Buf.Replace(c.Loc, next, string(r)) + h.Buf.Replace(c.Loc, next, []byte{byte(r)}) } else { - h.Buf.Insert(c.Loc, string(r)) + h.Buf.Insert(c.Loc, []byte{byte(r)}) } } } diff --git a/cmd/micro/buffer/buffer.go b/cmd/micro/buffer/buffer.go index f56f627b..c5c0f4bb 100644 --- a/cmd/micro/buffer/buffer.go +++ b/cmd/micro/buffer/buffer.go @@ -1,6 +1,7 @@ package buffer import ( + "bytes" "crypto/md5" "errors" "io" @@ -232,7 +233,7 @@ func (b *Buffer) SetName(s string) { b.name = s } -func (b *Buffer) Insert(start Loc, text string) { +func (b *Buffer) Insert(start Loc, text []byte) { b.EventHandler.cursors = b.cursors b.EventHandler.Insert(start, text) } @@ -409,11 +410,11 @@ func (b *Buffer) ClearMatches() { // IndentString returns this buffer's indent method (a tabstop or n spaces // depending on the settings) -func (b *Buffer) IndentString(tabsize int) string { +func (b *Buffer) IndentString(tabsize int) []byte { if b.Settings["tabstospaces"].(bool) { - return Spaces(tabsize) + return bytes.Repeat([]byte{' '}, tabsize) } - return "\t" + return []byte{'\t'} } // SetCursors resets this buffer's cursors to a new list diff --git a/cmd/micro/buffer/eventhandler.go b/cmd/micro/buffer/eventhandler.go index f04b78f9..81f1b67d 100644 --- a/cmd/micro/buffer/eventhandler.go +++ b/cmd/micro/buffer/eventhandler.go @@ -87,16 +87,16 @@ func NewEventHandler(buf *SharedBuffer, cursors []*Cursor) *EventHandler { // the buffer equal to that string // This means that we can transform the buffer into any string and still preserve undo/redo // through insert and delete events -func (eh *EventHandler) ApplyDiff(new string) { +func (eh *EventHandler) ApplyDiff(str string) { differ := dmp.New() - diff := differ.DiffMain(string(eh.buf.Bytes()), new, false) + diff := differ.DiffMain(string(eh.buf.Bytes()), str, false) 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)) } else { if d.Type == dmp.DiffInsert { - eh.Insert(loc, d.Text) + eh.Insert(loc, []byte(d.Text)) } loc = loc.MoveLA(utf8.RuneCountInString(d.Text), eh.buf.LineArray) } @@ -104,8 +104,7 @@ func (eh *EventHandler) ApplyDiff(new string) { } // Insert creates an insert text event and executes it -func (eh *EventHandler) Insert(start Loc, textStr string) { - text := []byte(textStr) +func (eh *EventHandler) Insert(start Loc, text []byte) { e := &TextEvent{ C: *eh.cursors[0], EventType: TextEventInsert, @@ -174,7 +173,7 @@ func (eh *EventHandler) MultipleReplace(deltas []Delta) { } // Replace deletes from start to end and replaces it with the given string -func (eh *EventHandler) Replace(start, end Loc, replace string) { +func (eh *EventHandler) Replace(start, end Loc, replace []byte) { eh.Remove(start, end) eh.Insert(start, replace) } diff --git a/cmd/micro/buffer/save.go b/cmd/micro/buffer/save.go index fe82a8e0..b8672dd7 100644 --- a/cmd/micro/buffer/save.go +++ b/cmd/micro/buffer/save.go @@ -67,7 +67,7 @@ func (b *Buffer) SaveAs(filename string) error { if b.Settings["eofnewline"].(bool) { end := b.End() if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' { - b.Insert(end, "\n") + b.Insert(end, []byte{'\n'}) } } diff --git a/cmd/micro/info/history.go b/cmd/micro/info/history.go index 18e6bbba..84a81e53 100644 --- a/cmd/micro/info/history.go +++ b/cmd/micro/info/history.go @@ -64,7 +64,7 @@ func (i *InfoBuf) SaveHistory() { func (i *InfoBuf) UpHistory(history []string) { if i.HistoryNum > 0 { i.HistoryNum-- - i.Replace(i.Start(), i.End(), history[i.HistoryNum]) + i.Replace(i.Start(), i.End(), []byte(history[i.HistoryNum])) i.Buffer.GetActiveCursor().GotoLoc(i.End()) } } @@ -73,7 +73,7 @@ func (i *InfoBuf) UpHistory(history []string) { func (i *InfoBuf) DownHistory(history []string) { if i.HistoryNum < len(history)-1 { i.HistoryNum++ - i.Replace(i.Start(), i.End(), history[i.HistoryNum]) + i.Replace(i.Start(), i.End(), []byte(history[i.HistoryNum])) i.Buffer.GetActiveCursor().GotoLoc(i.End()) } } diff --git a/cmd/micro/info/infobuffer.go b/cmd/micro/info/infobuffer.go index 438e3b52..58c8e428 100644 --- a/cmd/micro/info/infobuffer.go +++ b/cmd/micro/info/infobuffer.go @@ -111,7 +111,7 @@ func (i *InfoBuf) Prompt(prompt string, msg string, ptype string, eventcb func(s i.HasGutter = false i.PromptCallback = donecb i.EventCallback = eventcb - i.Buffer.Insert(i.Buffer.Start(), msg) + i.Buffer.Insert(i.Buffer.Start(), []byte(msg)) } func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) { @@ -151,7 +151,7 @@ func (i *InfoBuf) DonePrompt(canceled bool) { i.PromptCallback = nil i.EventCallback = nil i.YNCallback = nil - i.Replace(i.Start(), i.End(), "") + i.Replace(i.Start(), i.End(), []byte{}) } // Reset resets the infobuffer's msg and info