splitter.go 515 B

12345678910111213141516171819202122232425
  1. package wsshell
  2. import "strings"
  3. func customSplitter(data []byte, atEOF bool) (advance int, token []byte, err error) {
  4. // Return nothing if at end of file and no data passed
  5. if atEOF && len(data) == 0 {
  6. return 0, nil, nil
  7. }
  8. i := strings.Index(string(data), "\n")
  9. j := strings.Index(string(data), "\r")
  10. if i >= 0 {
  11. return i + 1, data[0:i], nil
  12. } else if j >= 0 {
  13. return j + 1, data[0:j], nil
  14. }
  15. // If at end of file with data return the data
  16. if atEOF {
  17. return len(data), data, nil
  18. }
  19. return
  20. }