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

Method parseString

parser.go:160–218  ·  view source on GitHub ↗

parseString parses a single- or double-quoted string.

()

Source from the content-addressed store, hash-verified

158
159// parseString parses a single- or double-quoted string.
160func (p *parser) parseString() (result string, err error) {
161 i := p.i
162 if len(p.s) < i+2 {
163 return "", errors.New("expected string, found EOF instead")
164 }
165
166 quote := p.s[i]
167 i++
168
169loop:
170 for i < len(p.s) {
171 switch p.s[i] {
172 case '\\':
173 if len(p.s) > i+1 {
174 switch c := p.s[i+1]; c {
175 case '\r':
176 if len(p.s) > i+2 && p.s[i+2] == '\n' {
177 i += 3
178 continue loop
179 }
180 fallthrough
181 case '\n', '\f':
182 i += 2
183 continue loop
184 }
185 }
186 p.i = i
187 val, err := p.parseEscape()
188 if err != nil {
189 return "", err
190 }
191 i = p.i
192 result += val
193 case quote:
194 break loop
195 case '\r', '\n', '\f':
196 return "", errors.New("unexpected end of line in string")
197 default:
198 start := i
199 for i < len(p.s) {
200 if c := p.s[i]; c == quote || c == '\\' || c == '\r' || c == '\n' || c == '\f' {
201 break
202 }
203 i++
204 }
205 result += p.s[start:i]
206 }
207 }
208
209 if i >= len(p.s) {
210 return "", errors.New("EOF in string")
211 }
212
213 // Consume the final quote.
214 i++
215
216 p.i = i
217 return result, nil

Callers 3

TestParseStringFunction · 0.95

Calls 1

parseEscapeMethod · 0.95

Tested by 1

TestParseStringFunction · 0.76