callout.go

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