MCPcopy Create free account
hub / github.com/andybalholm/cascadia / parseString

Method parseString

parser.go:135–193  ·  view source on GitHub ↗

parseString parses a single- or double-quoted string.

()

Source from the content-addressed store, hash-verified

133
134// parseString parses a single- or double-quoted string.
135func (p *parser) parseString() (result string, err error) {
136 i := p.i
137 if len(p.s) < i+2 {
138 return "", errors.New("expected string, found EOF instead")
139 }
140
141 quote := p.s[i]
142 i++
143
144loop:
145 for i < len(p.s) {
146 switch p.s[i] {
147 case '\\':
148 if len(p.s) > i+1 {
149 switch c := p.s[i+1]; c {
150 case '\r':
151 if len(p.s) > i+2 && p.s[i+2] == '\n' {
152 i += 3
153 continue loop
154 }
155 fallthrough
156 case '\n', '\f':
157 i += 2
158 continue loop
159 }
160 }
161 p.i = i
162 val, err := p.parseEscape()
163 if err != nil {
164 return "", err
165 }
166 i = p.i
167 result += val
168 case quote:
169 break loop
170 case '\r', '\n', '\f':
171 return "", errors.New("unexpected end of line in string")
172 default:
173 start := i
174 for i < len(p.s) {
175 if c := p.s[i]; c == quote || c == '\\' || c == '\r' || c == '\n' || c == '\f' {
176 break
177 }
178 i++
179 }
180 result += p.s[start:i]
181 }
182 }
183
184 if i >= len(p.s) {
185 return "", errors.New("EOF in string")
186 }
187
188 // Consume the final quote.
189 i++
190
191 p.i = i
192 return result, nil

Callers 3

TestParseStringFunction · 0.95

Calls 1

parseEscapeMethod · 0.95

Tested by 1

TestParseStringFunction · 0.76