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)
| 58 | // isTerminatingChar, returning the resulting string, not including the |
| 59 | // termination character. |
| 60 | func (p *parseState) gobbleString(isTerminatingChar func(ch byte) bool) (out string, err error) { |
| 61 | var result bytes.Buffer |
| 62 | start := 0 |
| 63 | i := 0 |
| 64 | for i < len(p.s) && !isTerminatingChar(p.s[i]) { |
| 65 | // In these strings, we just encode directly the character following a |
| 66 | // '\', even if it would normally be an escape sequence. |
| 67 | if i < len(p.s) && p.s[i] == '\\' { |
| 68 | result.WriteString(p.s[start:i]) |
| 69 | i++ |
| 70 | if i < len(p.s) { |
| 71 | result.WriteByte(p.s[i]) |
| 72 | i++ |
| 73 | } |
| 74 | start = i |
| 75 | } else { |
| 76 | i++ |
| 77 | } |
| 78 | } |
| 79 | if i >= len(p.s) { |
| 80 | return "", malformedError |
| 81 | } |
| 82 | result.WriteString(p.s[start:i]) |
| 83 | p.s = p.s[i:] |
| 84 | return result.String(), nil |
| 85 | } |
| 86 | |
| 87 | type parseState struct { |
| 88 | s string |
no test coverage detected