MCPcopy Create free account
hub / github.com/uptrace/bun / readSubstring

Method readSubstring

dialect/pgdialect/parser.go:35–92  ·  view source on GitHub ↗
(ch byte, escaped bool)

Source from the content-addressed store, hash-verified

33}
34
35func (p *pgparser) readSubstring(ch byte, escaped bool) ([]byte, error) {
36 ch, err := p.ReadByte()
37 if err != nil {
38 return nil, err
39 }
40
41 p.buf = p.buf[:0]
42 for {
43 if ch == '"' {
44 break
45 }
46
47 next, err := p.ReadByte()
48 if err != nil {
49 return nil, err
50 }
51
52 if ch == '\\' {
53 switch next {
54 case '\\', '"':
55 p.buf = append(p.buf, next)
56
57 ch, err = p.ReadByte()
58 if err != nil {
59 return nil, err
60 }
61 default:
62 p.buf = append(p.buf, '\\')
63 ch = next
64 }
65 continue
66 }
67
68 if escaped && ch == '\'' && next == '\'' {
69 p.buf = append(p.buf, next)
70 ch, err = p.ReadByte()
71 if err != nil {
72 return nil, err
73 }
74 continue
75 }
76
77 p.buf = append(p.buf, ch)
78 ch = next
79 }
80
81 if bytes.HasPrefix(p.buf, []byte("\\x")) && len(p.buf)%2 == 0 {
82 data := p.buf[2:]
83 buf := make([]byte, hex.DecodedLen(len(data)))
84 n, err := hex.Decode(buf, data)
85 if err != nil {
86 return nil, err
87 }
88 return buf[:n], nil
89 }
90
91 return p.buf, nil
92}

Callers 2

ReadSubstringMethod · 0.95

Calls 2

ReadByteMethod · 0.80
DecodeMethod · 0.80

Tested by

no test coverage detected