summaryrefslogtreecommitdiffstats
path: root/up.go
diff options
context:
space:
mode:
authorMateusz Czapliński <czapkofan@gmail.com>2018-10-20 00:47:48 +0200
committerMateusz Czapliński <czapkofan@gmail.com>2018-10-20 00:47:48 +0200
commit206168df50f432405b2440a434a5fb58aa43ce38 (patch)
tree129fe8f00ebaa0cf66ecee9a1dca55bca75c3b4d /up.go
parenta4d8437bb158ffcbe0ce6719edc4bd667d102873 (diff)
delete Buf.Snapshot function
Diffstat (limited to 'up.go')
-rw-r--r--up.go19
1 files changed, 12 insertions, 7 deletions
diff --git a/up.go b/up.go
index 77b58fd..8d94e05 100644
--- a/up.go
+++ b/up.go
@@ -394,7 +394,7 @@ func (v *BufView) HandleKey(ev *tcell.EventKey) bool {
}
func (v *BufView) normalizeY() {
- nlines := bytes.Count(v.Buf.Snapshot(), []byte{'\n'}) + 1
+ nlines := count(v.Buf.NewReader(false), '\n') + 1
if v.Y >= nlines {
v.Y = nlines - 1
}
@@ -403,6 +403,17 @@ func (v *BufView) normalizeY() {
}
}
+func count(r io.Reader, b byte) (n int) {
+ buf := [256]byte{}
+ for {
+ i, err := r.Read(buf[:])
+ n += bytes.Count(buf[:i], []byte{b})
+ if err != nil {
+ return
+ }
+ }
+}
+
func NewBuf() *Buf {
// TODO: make buffer size dynamic (growable by pressing a key)
const bufsize = 40 * 1024 * 1024 // 40 MB
@@ -450,12 +461,6 @@ func (b *Buf) capture(r io.Reader, notify func()) {
}
}
-func (b *Buf) Snapshot() []byte {
- b.mu.Lock()
- defer b.mu.Unlock()
- return b.bytes[:b.n]
-}
-
func (b *Buf) NewReader(blocking bool) io.Reader {
i := 0
return funcReader(func(p []byte) (n int, err error) {