1
0
mirror of https://github.com/zu1k/nali.git synced 2025-02-02 10:22:41 +08:00

optimize: remove regexp and use bytes.IndexByte to improve the performance

This commit is contained in:
mzz2017 2022-08-14 23:36:01 +08:00
parent 5b722a6afe
commit 24cdbcfe44

View File

@ -1,23 +1,34 @@
package common package common
import ( import (
"regexp" "bytes"
) )
var newlineReg = regexp.MustCompile(`\r?\n|\r\n?`)
// ScanLines scan lines but keep the suffix \r and \n // ScanLines scan lines but keep the suffix \r and \n
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) { func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 { if atEOF && len(data) == 0 {
return 0, nil, nil return 0, nil, nil
} }
if loc := newlineReg.FindIndex(data); len(loc) > 0 { if i, j := bytes.IndexByte(data, '\r'), bytes.IndexByte(data, '\n'); i >= 0 || j >= 0 {
delimiterLen := 1 // case 1: TOKEN\r\nTOKEN
i := loc[0] if i >= 0 && j >= 0 {
if i+1 < len(data) && data[i] == '\r' && data[i+1] == '\n' { if i+1 == j {
delimiterLen = 2 return i + 2, data[:i+2], nil
}
if i < j {
// case 2: TOKEN\rTOKEN\nTOKEN
return i + 1, data[:i+1], nil
} else {
// case 3: TOKEN\nTOKEN\rTOKEN
return j + 1, data[:j+1], nil
}
} else if i >= 0 {
// case 4: TOKEN\rTOKEN
return i + 1, data[:i+1], nil
} else {
// case 5: TOKEN\nTOKEN
return j + 1, data[:j+1], nil
} }
return i + delimiterLen, data[:i+delimiterLen], nil
} }
// If we're at EOF, we have a final, non-terminated line. Return it. // If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF { if atEOF {