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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
@ -667,19 +668,24 @@ func (p *program) HandleReturn() {
|
|||||||
|
|
||||||
func (p *program) ParseLines(bs []byte) {
|
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]
|
// IndexByte is assembly optimized for different platforms and is much faster than checking one byte at a time
|
||||||
if b == '\n' {
|
index := bytes.IndexByte(bs, '\n')
|
||||||
|
if index == -1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
bs = bs[index+1:]
|
||||||
|
|
||||||
if p.CurrLineValid {
|
checkedBytes += uint64(index + 1)
|
||||||
p.CurrLine.EndIndex = p.textBuf.WrittenElements + i
|
if p.CurrLineValid {
|
||||||
p.WriteLine(&p.CurrLine)
|
p.CurrLine.EndIndex = p.textBuf.WrittenElements + checkedBytes - 1
|
||||||
p.CurrLine.StartIndex = p.textBuf.WrittenElements + i
|
p.WriteLine(&p.CurrLine)
|
||||||
} else {
|
p.CurrLine.StartIndex = p.textBuf.WrittenElements + checkedBytes - 1
|
||||||
p.CurrLine.StartIndex = p.textBuf.WrittenElements + i
|
} else {
|
||||||
p.CurrLineValid = true
|
p.CurrLine.StartIndex = p.textBuf.WrittenElements + checkedBytes - 1
|
||||||
}
|
p.CurrLineValid = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user