gobbleString advances the parser for the remainder of the current string until it sees a non-escaped termination character, as specified by isTerminatingChar, returning the resulting string, not including the termination character.
(isTerminatingChar func(ch byte) bool)
| 44 | // isTerminatingChar, returning the resulting string, not including the |
| 45 | // termination character. |
| 46 | func (p *parseState) gobbleString(isTerminatingChar func(ch byte) bool) (out string, err error) { |
| 47 | var result bytes.Buffer |
| 48 | start := 0 |
| 49 | i := 0 |
| 50 | for i < len(p.s) && !isTerminatingChar(p.s[i]) { |
| 51 | // In these strings, we just encode directly the character following a |
| 52 | // '\', even if it would normally be an escape sequence. |
| 53 | if i < len(p.s) && p.s[i] == '\\' { |
| 54 | result.WriteString(p.s[start:i]) |
| 55 | i++ |
| 56 | if i < len(p.s) { |
| 57 | result.WriteByte(p.s[i]) |
| 58 | i++ |
| 59 | } |
| 60 | start = i |
| 61 | } else { |
| 62 | i++ |
| 63 | } |
| 64 | } |
| 65 | if i >= len(p.s) { |
| 66 | return "", malformedError |
| 67 | } |
| 68 | result.WriteString(p.s[start:i]) |
| 69 | p.s = p.s[i:] |
| 70 | return result.String(), nil |
| 71 | } |
| 72 | |
| 73 | type parseState struct { |
| 74 | s string |
no test coverage detected