LEFT | RIGHT |
(no file at all) | |
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 "bytes" | 8 "bytes" |
9 "os" | |
10 "strings" | 9 "strings" |
11 "unicode" | 10 "unicode" |
12 "utf8" | 11 "utf8" |
13 ) | 12 ) |
14 | 13 |
15 const lowerhex = "0123456789abcdef" | 14 const lowerhex = "0123456789abcdef" |
16 | 15 |
17 func quoteWith(s string, quote byte, ASCIIonly bool) string { | 16 func quoteWith(s string, quote byte, ASCIIonly bool) string { |
18 var buf bytes.Buffer | 17 var buf bytes.Buffer |
19 buf.WriteByte(quote) | 18 buf.WriteByte(quote) |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // 1) value, the decoded Unicode code point or byte value; | 149 // 1) value, the decoded Unicode code point or byte value; |
151 // 2) multibyte, a boolean indicating whether the decoded character require
s a multibyte UTF-8 representation; | 150 // 2) multibyte, a boolean indicating whether the decoded character require
s a multibyte UTF-8 representation; |
152 // 3) tail, the remainder of the string after the character; and | 151 // 3) tail, the remainder of the string after the character; and |
153 // 4) an error that will be nil if the character is syntactically valid. | 152 // 4) an error that will be nil if the character is syntactically valid. |
154 // | 153 // |
155 // The second argument, quote, specifies the type of literal being parsed | 154 // The second argument, quote, specifies the type of literal being parsed |
156 // and therefore which escaped quote character is permitted. | 155 // and therefore which escaped quote character is permitted. |
157 // If set to a single quote, it permits the sequence \' and disallows unescaped
'. | 156 // If set to a single quote, it permits the sequence \' and disallows unescaped
'. |
158 // If set to a double quote, it permits \" and disallows unescaped ". | 157 // If set to a double quote, it permits \" and disallows unescaped ". |
159 // If set to zero, it does not permit either escape and allows both quote charac
ters to appear unescaped. | 158 // If set to zero, it does not permit either escape and allows both quote charac
ters to appear unescaped. |
160 func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string,
err os.Error) { | 159 func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string,
err error) { |
161 // easy cases | 160 // easy cases |
162 switch c := s[0]; { | 161 switch c := s[0]; { |
163 case c == quote && (quote == '\'' || quote == '"'): | 162 case c == quote && (quote == '\'' || quote == '"'): |
164 err = ErrSyntax | 163 err = ErrSyntax |
165 return | 164 return |
166 case c >= utf8.RuneSelf: | 165 case c >= utf8.RuneSelf: |
167 r, size := utf8.DecodeRuneInString(s) | 166 r, size := utf8.DecodeRuneInString(s) |
168 return r, true, s[size:], nil | 167 return r, true, s[size:], nil |
169 case c != '\\': | 168 case c != '\\': |
170 return rune(s[0]), false, s[1:], nil | 169 return rune(s[0]), false, s[1:], nil |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } | 260 } |
262 tail = s | 261 tail = s |
263 return | 262 return |
264 } | 263 } |
265 | 264 |
266 // Unquote interprets s as a single-quoted, double-quoted, | 265 // Unquote interprets s as a single-quoted, double-quoted, |
267 // or backquoted Go string literal, returning the string value | 266 // or backquoted Go string literal, returning the string value |
268 // that s quotes. (If s is single-quoted, it would be a Go | 267 // that s quotes. (If s is single-quoted, it would be a Go |
269 // character literal; Unquote returns the corresponding | 268 // character literal; Unquote returns the corresponding |
270 // one-character string.) | 269 // one-character string.) |
271 func Unquote(s string) (t string, err os.Error) { | 270 func Unquote(s string) (t string, err error) { |
272 n := len(s) | 271 n := len(s) |
273 if n < 2 { | 272 if n < 2 { |
274 return "", ErrSyntax | 273 return "", ErrSyntax |
275 } | 274 } |
276 quote := s[0] | 275 quote := s[0] |
277 if quote != s[n-1] { | 276 if quote != s[n-1] { |
278 return "", ErrSyntax | 277 return "", ErrSyntax |
279 } | 278 } |
280 s = s[1 : n-1] | 279 s = s[1 : n-1] |
281 | 280 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 } else { | 316 } else { |
318 buf.WriteString(string(c)) | 317 buf.WriteString(string(c)) |
319 } | 318 } |
320 if quote == '\'' && len(s) != 0 { | 319 if quote == '\'' && len(s) != 0 { |
321 // single-quoted must be single character | 320 // single-quoted must be single character |
322 return "", ErrSyntax | 321 return "", ErrSyntax |
323 } | 322 } |
324 } | 323 } |
325 return buf.String(), nil | 324 return buf.String(), nil |
326 } | 325 } |
LEFT | RIGHT |