mirror of
https://github.com/bloeys/nterm.git
synced 2025-12-29 06:28:20 +00:00
Make ParseLines much faster using bytes.IndexByte
This commit is contained in:
28
main.go
28
main.go
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
@ -667,19 +668,24 @@ func (p *program) HandleReturn() {
|
||||
|
||||
func (p *program) ParseLines(bs []byte) {
|
||||
|
||||
for i := uint64(0); i < uint64(len(bs)); i++ {
|
||||
checkedBytes := uint64(0)
|
||||
for len(bs) > 0 {
|
||||
|
||||
b := bs[i]
|
||||
if b == '\n' {
|
||||
|
||||
if p.CurrLineValid {
|
||||
p.CurrLine.EndIndex = p.textBuf.WrittenElements + i
|
||||
p.WriteLine(&p.CurrLine)
|
||||
p.CurrLine.StartIndex = p.textBuf.WrittenElements + i
|
||||
} else {
|
||||
p.CurrLine.StartIndex = p.textBuf.WrittenElements + i
|
||||
p.CurrLineValid = true
|
||||
// IndexByte is assembly optimized for different platforms and is much faster than checking one byte at a time
|
||||
index := bytes.IndexByte(bs, '\n')
|
||||
if index == -1 {
|
||||
break
|
||||
}
|
||||
bs = bs[index+1:]
|
||||
|
||||
checkedBytes += uint64(index + 1)
|
||||
if p.CurrLineValid {
|
||||
p.CurrLine.EndIndex = p.textBuf.WrittenElements + checkedBytes - 1
|
||||
p.WriteLine(&p.CurrLine)
|
||||
p.CurrLine.StartIndex = p.textBuf.WrittenElements + checkedBytes - 1
|
||||
} else {
|
||||
p.CurrLine.StartIndex = p.textBuf.WrittenElements + checkedBytes - 1
|
||||
p.CurrLineValid = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user