callout.go 546 B

1234567891011121314151617181920212223242526272829
  1. package parser
  2. import (
  3. "bytes"
  4. "strconv"
  5. )
  6. // IsCallout detects a callout in the following format: <<N>> Where N is a integer > 0.
  7. func IsCallout(data []byte) (id []byte, consumed int) {
  8. if !bytes.HasPrefix(data, []byte("<<")) {
  9. return nil, 0
  10. }
  11. start := 2
  12. end := bytes.Index(data[start:], []byte(">>"))
  13. if end < 0 {
  14. return nil, 0
  15. }
  16. b := data[start : start+end]
  17. b = bytes.TrimSpace(b)
  18. i, err := strconv.Atoi(string(b))
  19. if err != nil {
  20. return nil, 0
  21. }
  22. if i <= 0 {
  23. return nil, 0
  24. }
  25. return b, start + end + 2 // 2 for >>
  26. }