2022-08-14 21:39:53 +08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-08-14 23:36:01 +08:00
|
|
|
"bytes"
|
2022-08-14 21:39:53 +08:00
|
|
|
)
|
|
|
|
|
2022-08-14 22:57:14 +08:00
|
|
|
// ScanLines scan lines but keep the suffix \r and \n
|
2022-08-14 21:39:53 +08:00
|
|
|
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
|
|
if atEOF && len(data) == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
2022-08-15 09:11:10 +08:00
|
|
|
|
|
|
|
if i := bytes.IndexByte(data, '\n'); i >= 0 {
|
|
|
|
return i + 1, data[:i+1], nil
|
|
|
|
}
|
|
|
|
if i := bytes.IndexByte(data, '\r'); i >= 0 {
|
|
|
|
return i + 1, data[:i+1], nil
|
2022-08-14 21:39:53 +08:00
|
|
|
}
|
2022-08-15 09:11:10 +08:00
|
|
|
|
2022-08-14 21:39:53 +08:00
|
|
|
// If we're at EOF, we have a final, non-terminated line. Return it.
|
|
|
|
if atEOF {
|
2022-08-14 22:57:14 +08:00
|
|
|
return len(data), data, nil
|
2022-08-14 21:39:53 +08:00
|
|
|
}
|
|
|
|
// Request more data.
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|