podman
161 строка · 4.8 Кб
1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package text
6
7import (
8"bytes"
9"strconv"
10"strings"
11"unicode"
12"unicode/utf16"
13"unicode/utf8"
14
15"google.golang.org/protobuf/internal/strs"
16)
17
18// parseStringValue parses string field token.
19// This differs from parseString since the text format allows
20// multiple back-to-back string literals where they are semantically treated
21// as a single large string with all values concatenated.
22//
23// E.g., `"foo" "bar" "baz"` => "foobarbaz"
24func (d *Decoder) parseStringValue() (Token, error) {
25// Note that the ending quote is sufficient to unambiguously mark the end
26// of a string. Thus, the text grammar does not require intervening
27// whitespace or control characters in-between strings.
28// Thus, the following is valid:
29// `"foo"'bar'"baz"` => "foobarbaz"
30in0 := d.in
31var ss []string
32for len(d.in) > 0 && (d.in[0] == '"' || d.in[0] == '\'') {
33s, err := d.parseString()
34if err != nil {
35return Token{}, err
36}
37ss = append(ss, s)
38}
39// d.in already points to the end of the value at this point.
40return Token{
41kind: Scalar,
42attrs: stringValue,
43pos: len(d.orig) - len(in0),
44raw: in0[:len(in0)-len(d.in)],
45str: strings.Join(ss, ""),
46}, nil
47}
48
49// parseString parses a string value enclosed in " or '.
50func (d *Decoder) parseString() (string, error) {
51in := d.in
52if len(in) == 0 {
53return "", ErrUnexpectedEOF
54}
55quote := in[0]
56in = in[1:]
57i := indexNeedEscapeInBytes(in)
58in, out := in[i:], in[:i:i] // set cap to prevent mutations
59for len(in) > 0 {
60switch r, n := utf8.DecodeRune(in); {
61case r == utf8.RuneError && n == 1:
62return "", d.newSyntaxError("invalid UTF-8 detected")
63case r == 0 || r == '\n':
64return "", d.newSyntaxError("invalid character %q in string", r)
65case r == rune(quote):
66in = in[1:]
67d.consume(len(d.in) - len(in))
68return string(out), nil
69case r == '\\':
70if len(in) < 2 {
71return "", ErrUnexpectedEOF
72}
73switch r := in[1]; r {
74case '"', '\'', '\\', '?':
75in, out = in[2:], append(out, r)
76case 'a':
77in, out = in[2:], append(out, '\a')
78case 'b':
79in, out = in[2:], append(out, '\b')
80case 'n':
81in, out = in[2:], append(out, '\n')
82case 'r':
83in, out = in[2:], append(out, '\r')
84case 't':
85in, out = in[2:], append(out, '\t')
86case 'v':
87in, out = in[2:], append(out, '\v')
88case 'f':
89in, out = in[2:], append(out, '\f')
90case '0', '1', '2', '3', '4', '5', '6', '7':
91// One, two, or three octal characters.
92n := len(in[1:]) - len(bytes.TrimLeft(in[1:], "01234567"))
93if n > 3 {
94n = 3
95}
96v, err := strconv.ParseUint(string(in[1:1+n]), 8, 8)
97if err != nil {
98return "", d.newSyntaxError("invalid octal escape code %q in string", in[:1+n])
99}
100in, out = in[1+n:], append(out, byte(v))
101case 'x':
102// One or two hexadecimal characters.
103n := len(in[2:]) - len(bytes.TrimLeft(in[2:], "0123456789abcdefABCDEF"))
104if n > 2 {
105n = 2
106}
107v, err := strconv.ParseUint(string(in[2:2+n]), 16, 8)
108if err != nil {
109return "", d.newSyntaxError("invalid hex escape code %q in string", in[:2+n])
110}
111in, out = in[2+n:], append(out, byte(v))
112case 'u', 'U':
113// Four or eight hexadecimal characters
114n := 6
115if r == 'U' {
116n = 10
117}
118if len(in) < n {
119return "", ErrUnexpectedEOF
120}
121v, err := strconv.ParseUint(string(in[2:n]), 16, 32)
122if utf8.MaxRune < v || err != nil {
123return "", d.newSyntaxError("invalid Unicode escape code %q in string", in[:n])
124}
125in = in[n:]
126
127r := rune(v)
128if utf16.IsSurrogate(r) {
129if len(in) < 6 {
130return "", ErrUnexpectedEOF
131}
132v, err := strconv.ParseUint(string(in[2:6]), 16, 16)
133r = utf16.DecodeRune(r, rune(v))
134if in[0] != '\\' || in[1] != 'u' || r == unicode.ReplacementChar || err != nil {
135return "", d.newSyntaxError("invalid Unicode escape code %q in string", in[:6])
136}
137in = in[6:]
138}
139out = append(out, string(r)...)
140default:
141return "", d.newSyntaxError("invalid escape code %q in string", in[:2])
142}
143default:
144i := indexNeedEscapeInBytes(in[n:])
145in, out = in[n+i:], append(out, in[:n+i]...)
146}
147}
148return "", ErrUnexpectedEOF
149}
150
151// indexNeedEscapeInString returns the index of the character that needs
152// escaping. If no characters need escaping, this returns the input length.
153func indexNeedEscapeInBytes(b []byte) int { return indexNeedEscapeInString(strs.UnsafeString(b)) }
154
155// UnmarshalString returns an unescaped string given a textproto string value.
156// String value needs to contain single or double quotes. This is only used by
157// internal/encoding/defval package for unmarshaling bytes.
158func UnmarshalString(s string) (string, error) {
159d := NewDecoder([]byte(s))
160return d.parseString()
161}
162