Debian Codegraph @

internal/parser/parser.go

raw
1 package parser
2
3 import (
4 "bytes"
5
6 "github.com/vmihailenco/tagparser/v2/internal"
7 )
8
9 type Parser struct {
10 b []byte
11 i int
12 }
13
14 func New(b []byte) *Parser {
15 return &Parser{
16 b: b,
17 }
18 }
19
20 func NewString(s string) *Parser {
21 return New(internal.StringToBytes(s))
22 }
23
24 func (p *Parser) Bytes() []byte {
25 return p.b[p.i:]
26 }
27
28 func (p *Parser) Valid() bool {
29 return p.i < len(p.b)
30 }
31
32 func (p *Parser) Read() byte {
33 if p.Valid() {
34 c := p.b[p.i]
35 p.Advance()
36 return c
37 }
38 return 0
39 }
40
41 func (p *Parser) Peek() byte {
42 if p.Valid() {
43 return p.b[p.i]
44 }
45 return 0
46 }
47
48 func (p *Parser) Advance() {
49 p.i++
50 }
51
52 func (p *Parser) Skip(skip byte) bool {
53 if p.Peek() == skip {
54 p.Advance()
55 return true
56 }
57 return false
58 }
59
60 func (p *Parser) SkipBytes(skip []byte) bool {
61 if len(skip) > len(p.b[p.i:]) {
62 return false
63 }
64 if !bytes.Equal(p.b[p.i:p.i+len(skip)], skip) {
65 return false
66 }
67 p.i += len(skip)
68 return true
69 }
70
71 func (p *Parser) ReadSep(sep byte) ([]byte, bool) {
72 ind := bytes.IndexByte(p.b[p.i:], sep)
73 if ind == -1 {
74 b := p.b[p.i:]
75 p.i = len(p.b)
76 return b, false
77 }
78
79 b := p.b[p.i : p.i+ind]
80 p.i += ind + 1
81 return b, true
82 }