Debian Codegraph @

cue/literal/quote.go

raw symbols SCIP
1 // Copyright 2020 CUE Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package literalscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/
16
17 import (
18 "strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/"
19 "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/"
20 "unicode/utf8scip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/"
21 )
22
23 // Form defines how to quote a string or bytes literal.
24 type Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form# struct {
25 hashCountscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#hashCount. int
26 quotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#quote. byte
27 multilinescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#multiline. bool
28 autoscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#auto. bool
29 exactscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#exact. bool
30 asciiOnlyscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#asciiOnly. bool
31 graphicOnlyscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#graphicOnly. bool
32 indentscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#indent. string
33 tripleQuotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#tripleQuote. string
34 }
35
36 // TODO:
37 // - Fixed or max level of escape modifiers (#""#).
38 // - Option to fall back to bytes if value cannot be represented as string.
39 // E.g. ExactString.
40 // - QuoteExact that fails with an error if a string cannot be represented
41 // without loss.
42 // - Handle auto-breaking for long lines (Swift-style, \-terminated lines).
43 // This is not supported yet in CUE, but may, and should be considered as
44 // a possibility in API design.
45 // - Other possible convenience forms: Blob (auto-break bytes), String (bytes
46 // or string), Label.
47
48 // WithTabIndent returns a new Form with indentation set to the given number
49 // of tabs. The result will be a multiline string.
50 func (f Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) WithTabIndentscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#WithTabIndent().(n int) Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form# {
51 f.indentscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#indent. = stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.24.0 strings/Repeat().("\t", n)
52 f.multilinescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#multiline. = true
53 return f
54 }
55
56 // WithOptionalTabIndent is like [Form.WithTabIndent], but only returns a multiline
57 // strings if it doesn't contain any newline characters.
58 func (f Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) WithOptionalTabIndentscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#WithOptionalTabIndent().(tabs int) Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form# {
59 f.indentscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#indent. = stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.24.0 strings/Repeat().("\t", tabs)
60 f.autoscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#auto. = true
61 return f
62 }
63
64 // WithASCIIOnly ensures the quoted strings consists solely of valid ASCII
65 // characters.
66 func (f Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) WithASCIIOnlyscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#WithASCIIOnly().() Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form# {
67 f.asciiOnlyscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#asciiOnly. = true
68 return f
69 }
70
71 // WithGraphicOnly ensures the quoted strings consists solely of printable
72 // characters.
73 func (f Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) WithGraphicOnlyscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#WithGraphicOnly().() Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form# {
74 f.graphicOnlyscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#graphicOnly. = true
75 return f
76 }
77
78 var (
79 // String defines the format of a CUE string. Conversions may be lossy.
80 Stringscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/String. Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form# = stringFormscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/stringForm.
81
82 // TODO: ExactString: quotes to bytes type if the string cannot be
83 // represented without loss of accuracy.
84
85 // Label is like [String], but optimized for labels.
86 Labelscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Label. Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form# = stringFormscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/stringForm.
87
88 // Bytes defines the format of bytes literal.
89 Bytesscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Bytes. Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form# = bytesFormscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/bytesForm.
90
91 stringFormscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/stringForm. = Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#{
92 quotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#quote.: '"',
93 tripleQuotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#tripleQuote.: `"""`,
94 }
95 bytesFormscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/bytesForm. = Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#{
96 quotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#quote.: '\'',
97 tripleQuotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#tripleQuote.: `'''`,
98 exactscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#exact.: true,
99 }
100 )
101
102 // Quote returns CUE string literal representing s. The returned string uses CUE
103 // escape sequences (\t, \n, \u00FF, \u0100) for control characters and
104 // non-printable characters as defined by strconv.IsPrint.
105 //
106 // It reports an error if the string cannot be converted to the desired form.
107 func (f Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) Quotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#Quote().(s string) string {
108 return string(f.Appendscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#Append().(make([]byte, 0, 3*len(s)/2), s))
109 }
110
111 const (
112 lowerhexscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/lowerhex. = "0123456789abcdef"
113 )
114
115 // Append appends a CUE string literal representing s, as generated by Quote, to
116 // buf and returns the extended buffer.
117 func (f Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) Appendscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#Append().(buf []byte, s string) []byte {
118 if f.autoscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#auto. && stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.ContainsRunescip-go gomod github.com/golang/go/src go1.24.0 strings/ContainsRune().(s, '\n') {
119 f.multilinescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#multiline. = true
120 }
121 if f.multilinescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#multiline. {
122 f.hashCountscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#hashCount. = f.requiredHashCountscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#requiredHashCount().(s)
123 }
124
125 // Often called with big strings, so preallocate. If there's quoting,
126 // this is conservative but still helps a lot.
127 if cap(buf)-len(buf) < len(s) {
128 nBuf := make([]byte, len(buf), len(buf)+1+len(s)+1)
129 copy(nBuf, buf)
130 buf = nBuf
131 }
132 for range f.hashCountscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#hashCount. {
133 buf = append(buf, '#')
134 }
135 if f.multilinescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#multiline. {
136 buf = append(buf, f.tripleQuotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#tripleQuote....)
137 buf = append(buf, '\n')
138 if s == "" {
139 buf = append(buf, f.indentscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#indent....)
140 buf = append(buf, f.tripleQuotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#tripleQuote....)
141 return buf
142 }
143 if len(s) > 0 && s[0] != '\n' {
144 buf = append(buf, f.indentscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#indent....)
145 }
146 } else {
147 buf = append(buf, f.quotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#quote.)
148 }
149
150 buf = f.appendEscapedscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#appendEscaped().(buf, s)
151
152 if f.multilinescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#multiline. {
153 buf = append(buf, '\n')
154 buf = append(buf, f.indentscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#indent....)
155 buf = append(buf, f.tripleQuotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#tripleQuote....)
156 } else {
157 buf = append(buf, f.quotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#quote.)
158 }
159 for range f.hashCountscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#hashCount. {
160 buf = append(buf, '#')
161 }
162
163 return buf
164 }
165
166 // AppendEscaped appends a CUE string literal representing s, as generated by
167 // Quote but without the quotes, to buf and returns the extended buffer.
168 //
169 // It does not include the last indentation.
170 func (f Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) AppendEscapedscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#AppendEscaped().(buf []byte, s string) []byte {
171 if f.autoscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#auto. && stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.ContainsRunescip-go gomod github.com/golang/go/src go1.24.0 strings/ContainsRune().(s, '\n') {
172 f.multilinescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#multiline. = true
173 }
174
175 // Often called with big strings, so preallocate. If there's quoting,
176 // this is conservative but still helps a lot.
177 if cap(buf)-len(buf) < len(s) {
178 nBuf := make([]byte, len(buf), len(buf)+1+len(s)+1)
179 copy(nBuf, buf)
180 buf = nBuf
181 }
182
183 buf = f.appendEscapedscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#appendEscaped().(buf, s)
184
185 return buf
186 }
187
188 func (f Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) appendEscapedscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#appendEscaped().(buf []byte, s string) []byte {
189 for width := 0; len(s) > 0; s = s[width:] {
190 r := rune(s[0])
191 width = 1
192 if r >= utf8scip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/.RuneSelfscip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/RuneSelf. {
193 r, width = utf8scip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/.DecodeRuneInStringscip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/DecodeRuneInString().(s)
194 }
195 if f.exactscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#exact. && width == 1 && r == utf8scip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/.RuneErrorscip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/RuneError. {
196 buf = f.appendEscapescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#appendEscape().(buf)
197 buf = append(buf, 'x')
198 buf = append(buf, lowerhexscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/lowerhex.[s[0]>>4])
199 buf = append(buf, lowerhexscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/lowerhex.[s[0]&0xF])
200 continue
201 }
202 if f.multilinescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#multiline. && r == '\n' {
203 buf = append(buf, '\n')
204 if len(s) > 1 && s[1] != '\n' {
205 buf = append(buf, f.indentscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#indent....)
206 }
207 continue
208 }
209 buf = f.appendEscapedRunescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#appendEscapedRune().(buf, r)
210 }
211 return buf
212 }
213
214 func (f *Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) appendEscapedRunescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#appendEscapedRune().(buf []byte, r rune) []byte {
215 if (!f.multilinescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#multiline. && r == rune(f.quotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#quote.)) || r == '\\' { // always backslashed
216 buf = f.appendEscapescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#appendEscape().(buf)
217 buf = append(buf, byte(r))
218 return buf
219 }
220 if f.asciiOnlyscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#asciiOnly. {
221 if r < utf8scip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/.RuneSelfscip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/RuneSelf. && strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/.IsPrintscip-go gomod github.com/golang/go/src go1.24.0 strconv/IsPrint().(r) {
222 buf = append(buf, byte(r))
223 return buf
224 }
225 } else if strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/.IsPrintscip-go gomod github.com/golang/go/src go1.24.0 strconv/IsPrint().(r) || (f.graphicOnlyscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#graphicOnly. && strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/.IsGraphicscip-go gomod github.com/golang/go/src go1.24.0 strconv/IsGraphic().(r)) {
226 buf = utf8scip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/.AppendRunescip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/AppendRune().(buf, r)
227 return buf
228 }
229 buf = f.appendEscapescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#appendEscape().(buf)
230 switch r {
231 case '\a':
232 buf = append(buf, 'a')
233 case '\b':
234 buf = append(buf, 'b')
235 case '\f':
236 buf = append(buf, 'f')
237 case '\n':
238 buf = append(buf, 'n')
239 case '\r':
240 buf = append(buf, 'r')
241 case '\t':
242 buf = append(buf, 't')
243 case '\v':
244 buf = append(buf, 'v')
245 default:
246 switch {
247 case r < ' ' && f.exactscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#exact.:
248 buf = append(buf, 'x')
249 buf = append(buf, lowerhexscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/lowerhex.[byte(r)>>4])
250 buf = append(buf, lowerhexscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/lowerhex.[byte(r)&0xF])
251 case r > utf8scip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/.MaxRunescip-go gomod github.com/golang/go/src go1.24.0 `unicode/utf8`/MaxRune.:
252 r = 0xFFFD
253 fallthrough
254 case r < 0x10000:
255 buf = append(buf, 'u')
256 for s := 12; s >= 0; s -= 4 {
257 buf = append(buf, lowerhexscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/lowerhex.[r>>uint(s)&0xF])
258 }
259 default:
260 buf = append(buf, 'U')
261 for s := 28; s >= 0; s -= 4 {
262 buf = append(buf, lowerhexscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/lowerhex.[r>>uint(s)&0xF])
263 }
264 }
265 }
266 return buf
267 }
268
269 func (f *Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) appendEscapescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#appendEscape().(buf []byte) []byte {
270 buf = append(buf, '\\')
271 for range f.hashCountscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#hashCount. {
272 buf = append(buf, '#')
273 }
274 return buf
275 }
276
277 // requiredHashCount returns the number of # characters
278 // that are required to quote the multiline string s.
279 func (f *Formscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#) requiredHashCountscip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#requiredHashCount().(s string) int {
280 hashCount := 0
281 i := 0
282 // Find all occurrences of the triple-quote and count
283 // the maximum number of succeeding # characters.
284 for {
285 j := stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.24.0 strings/Index().(s[i:], f.tripleQuotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#tripleQuote.)
286 if j == -1 {
287 break
288 }
289 i += j + 3
290 // Absorb all extra quotes, so we
291 // get to the end of the sequence.
292 for ; i < len(s); i++ {
293 if s[i] != f.quotescip-go gomod cuelang.org/go . `cuelang.org/go/cue/literal`/Form#quote. {
294 break
295 }
296 }
297 e := i - 1
298 // Count succeeding # characters.
299 for ; i < len(s); i++ {
300 if s[i] != '#' {
301 break
302 }
303 }
304 if nhash := i - e; nhash > hashCount {
305 hashCount = nhash
306 }
307 }
308 return hashCount
309 }