LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package strconv | 5 package strconv |
6 | 6 |
7 import ( | 7 import ( |
8 "unicode/utf8" | 8 "unicode/utf8" |
9 ) | 9 ) |
10 | 10 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 // AppendQuoteRuneToASCII appends a single-quoted Go character literal represent
ing the rune, | 136 // AppendQuoteRuneToASCII appends a single-quoted Go character literal represent
ing the rune, |
137 // as generated by QuoteRuneToASCII, to dst and returns the extended buffer. | 137 // as generated by QuoteRuneToASCII, to dst and returns the extended buffer. |
138 func AppendQuoteRuneToASCII(dst []byte, r rune) []byte { | 138 func AppendQuoteRuneToASCII(dst []byte, r rune) []byte { |
139 return append(dst, QuoteRuneToASCII(r)...) | 139 return append(dst, QuoteRuneToASCII(r)...) |
140 } | 140 } |
141 | 141 |
142 // CanBackquote reports whether the string s can be represented | 142 // CanBackquote reports whether the string s can be represented |
143 // unchanged as a single-line backquoted string without control | 143 // unchanged as a single-line backquoted string without control |
144 // characters other than space and tab. | 144 // characters other than space and tab. |
145 func CanBackquote(s string) bool { | 145 func CanBackquote(s string) bool { |
146 » i := 0 | 146 » for len(s) > 0 { |
147 » for i < len(s) { | 147 » » r, wid := utf8.DecodeRuneInString(s) |
148 » » c := s[i] | 148 » » s = s[wid:] |
149 » » if c < utf8.RuneSelf { | 149 » » if wid > 1 { |
150 » » » if (c < ' ' && c != '\t') || c == '`' || c == '\u007F' { | 150 » » » continue // All multibyte runes are correctly encoded an
d assumed printable. |
151 » » » » return false | 151 » » } |
152 » » » } | 152 » » if r == utf8.RuneError { |
153 » » » i++ | 153 » » » return false |
154 » » } else { | 154 » » } |
155 » » » _, size := utf8.DecodeRuneInString(s[i:]) | 155 » » if (r < ' ' && r != '\t') || r == '`' || r == '\u007F' { |
156 » » » if size == 1 { | 156 » » » return false |
157 » » » » // A RuneError as all runes of size 1 have | |
158 » » » » // been handled above. | |
159 » » » » return false | |
160 » » » } | |
161 » » » i += size | |
162 } | 157 } |
163 } | 158 } |
164 return true | 159 return true |
165 } | 160 } |
166 | 161 |
167 func unhex(b byte) (v rune, ok bool) { | 162 func unhex(b byte) (v rune, ok bool) { |
168 c := rune(b) | 163 c := rune(b) |
169 switch { | 164 switch { |
170 case '0' <= c && c <= '9': | 165 case '0' <= c && c <= '9': |
171 return c - '0', true | 166 return c - '0', true |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 if i >= len(isPrint) || rr < isPrint[i&^1] || isPrint[i|1] < rr { | 441 if i >= len(isPrint) || rr < isPrint[i&^1] || isPrint[i|1] < rr { |
447 return false | 442 return false |
448 } | 443 } |
449 if r >= 0x20000 { | 444 if r >= 0x20000 { |
450 return true | 445 return true |
451 } | 446 } |
452 r -= 0x10000 | 447 r -= 0x10000 |
453 j := bsearch16(isNotPrint, uint16(r)) | 448 j := bsearch16(isNotPrint, uint16(r)) |
454 return j >= len(isNotPrint) || isNotPrint[j] != uint16(r) | 449 return j >= len(isNotPrint) || isNotPrint[j] != uint16(r) |
455 } | 450 } |
LEFT | RIGHT |