Debian Codegraph @

stdlib_quote.go

raw symbols SCIP
1 // Copied and trimmed down from https://github.com/golang/go/blob/e3769299cd3484e018e0e2a6e1b95c2b18ce4f41/src/strconv/quote.go
2 // We want to use the standard library's private "quoteWith" function rather than write our own so that we get robust unicode support.
3 // Every private function called by quoteWith was copied.
4 // There are 2 modifications to simplify the code:
5 // 1. The unicode.IsPrint function was substituted for the custom implementation of IsPrint
6 // 2. All code paths reachable only when ASCIIonly or grphicOnly are set to true were removed.
7
8 // Copyright 2009 The Go Authors. All rights reserved.
9 // Use of this source code is governed by a BSD-style
10 // license that can be found in the LICENSE file.
11
12 package yaml
13
14 import (
15 "unicode"
16 "unicode/utf8"
17 )
18
19 const (
20 lowerhex = "0123456789abcdef"
21 )
22
23 func quoteWith(s string, quote byte) string {
24 return string(appendQuotedWith(make([]byte, 0, 3*len(s)/2), s, quote))
25 }
26
27 func appendQuotedWith(buf []byte, s string, quote byte) []byte {
28 // Often called with big strings, so preallocate. If there's quoting,
29 // this is conservative but still helps a lot.
30 if cap(buf)-len(buf) < len(s) {
31 nBuf := make([]byte, len(buf), len(buf)+1+len(s)+1)
32 copy(nBuf, buf)
33 buf = nBuf
34 }
35 buf = append(buf, quote)
36 for width := 0; len(s) > 0; s = s[width:] {
37 r := rune(s[0])
38 width = 1
39 if r >= utf8.RuneSelf {
40 r, width = utf8.DecodeRuneInString(s)
41 }
42 if width == 1 && r == utf8.RuneError {
43 buf = append(buf, `\x`...)
44 buf = append(buf, lowerhex[s[0]>>4])
45 buf = append(buf, lowerhex[s[0]&0xF])
46 continue
47 }
48 buf = appendEscapedRune(buf, r, quote)
49 }
50 buf = append(buf, quote)
51 return buf
52 }
53
54 func appendEscapedRune(buf []byte, r rune, quote byte) []byte {
55 var runeTmp [utf8.UTFMax]byte
56 // goccy/go-yaml patch on top of the standard library's appendEscapedRune function.
57 //
58 // We use this to implement the YAML single-quoted string, where the only escape sequence is '', which represents a single quote.
59 // The below snippet from the standard library is for escaping e.g. \ with \\, which is not what we want for the single-quoted string.
60 //
61 // if r == rune(quote) || r == '\\' { // always backslashed
62 // buf = append(buf, '\\')
63 // buf = append(buf, byte(r))
64 // return buf
65 // }
66 if r == rune(quote) {
67 buf = append(buf, byte(r))
68 buf = append(buf, byte(r))
69 return buf
70 }
71 if unicode.IsPrint(r) {
72 n := utf8.EncodeRune(runeTmp[:], r)
73 buf = append(buf, runeTmp[:n]...)
74 return buf
75 }
76 switch r {
77 case '\a':
78 buf = append(buf, `\a`...)
79 case '\b':
80 buf = append(buf, `\b`...)
81 case '\f':
82 buf = append(buf, `\f`...)
83 case '\n':
84 buf = append(buf, `\n`...)
85 case '\r':
86 buf = append(buf, `\r`...)
87 case '\t':
88 buf = append(buf, `\t`...)
89 case '\v':
90 buf = append(buf, `\v`...)
91 default:
92 switch {
93 case r < ' ':
94 buf = append(buf, `\x`...)
95 buf = append(buf, lowerhex[byte(r)>>4])
96 buf = append(buf, lowerhex[byte(r)&0xF])
97 case r > utf8.MaxRune:
98 r = 0xFFFD
99 fallthrough
100 case r < 0x10000:
101 buf = append(buf, `\u`...)
102 for s := 12; s >= 0; s -= 4 {
103 buf = append(buf, lowerhex[r>>uint(s)&0xF])
104 }
105 default:
106 buf = append(buf, `\U`...)
107 for s := 28; s >= 0; s -= 4 {
108 buf = append(buf, lowerhex[r>>uint(s)&0xF])
109 }
110 }
111 }
112 return buf
113 }