diff options
Diffstat (limited to 'src/pkg/strconv/quote.go')
-rw-r--r-- | src/pkg/strconv/quote.go | 134 |
1 files changed, 67 insertions, 67 deletions
diff --git a/src/pkg/strconv/quote.go b/src/pkg/strconv/quote.go index 5b5911db0..ccd106264 100644 --- a/src/pkg/strconv/quote.go +++ b/src/pkg/strconv/quote.go @@ -5,11 +5,11 @@ package strconv import ( - "bytes"; - "os"; - "strings"; - "unicode"; - "utf8"; + "bytes" + "os" + "strings" + "unicode" + "utf8" ) const lowerhex = "0123456789abcdef" @@ -19,8 +19,8 @@ const lowerhex = "0123456789abcdef" // sequences (\t, \n, \xFF, \u0100) for control characters // and non-ASCII characters. func Quote(s string) string { - var buf bytes.Buffer; - buf.WriteByte('"'); + var buf bytes.Buffer + buf.WriteByte('"') for ; len(s) > 0; s = s[1:] { switch c := s[0]; { case c == '"': @@ -45,18 +45,18 @@ func Quote(s string) string { buf.WriteString(`\v`) case c >= utf8.RuneSelf && utf8.FullRuneInString(s): - r, size := utf8.DecodeRuneInString(s); + r, size := utf8.DecodeRuneInString(s) if r == utf8.RuneError && size == 1 { goto EscX } - s = s[size-1:]; // next iteration will slice off 1 more + s = s[size-1:] // next iteration will slice off 1 more if r < 0x10000 { - buf.WriteString(`\u`); + buf.WriteString(`\u`) for j := uint(0); j < 4; j++ { buf.WriteByte(lowerhex[(r>>(12-4*j))&0xF]) } } else { - buf.WriteString(`\U`); + buf.WriteString(`\U`) for j := uint(0); j < 8; j++ { buf.WriteByte(lowerhex[(r>>(28-4*j))&0xF]) } @@ -64,13 +64,13 @@ func Quote(s string) string { default: EscX: - buf.WriteString(`\x`); - buf.WriteByte(lowerhex[c>>4]); - buf.WriteByte(lowerhex[c&0xF]); + buf.WriteString(`\x`) + buf.WriteByte(lowerhex[c>>4]) + buf.WriteByte(lowerhex[c&0xF]) } } - buf.WriteByte('"'); - return buf.String(); + buf.WriteByte('"') + return buf.String() } // CanBackquote returns whether the string s would be @@ -81,11 +81,11 @@ func CanBackquote(s string) bool { return false } } - return true; + return true } func unhex(b byte) (v int, ok bool) { - c := int(b); + c := int(b) switch { case '0' <= c && c <= '9': return c - '0', true @@ -94,7 +94,7 @@ func unhex(b byte) (v int, ok bool) { case 'A' <= c && c <= 'F': return c - 'A' + 10, true } - return; + return } // UnquoteChar decodes the first character or byte in the escaped string @@ -114,22 +114,22 @@ func UnquoteChar(s string, quote byte) (value int, multibyte bool, tail string, // easy cases switch c := s[0]; { case c == quote && (quote == '\'' || quote == '"'): - err = os.EINVAL; - return; + err = os.EINVAL + return case c >= utf8.RuneSelf: - r, size := utf8.DecodeRuneInString(s); - return r, true, s[size:], nil; + r, size := utf8.DecodeRuneInString(s) + return r, true, s[size:], nil case c != '\\': return int(s[0]), false, s[1:], nil } // hard case: c is backslash if len(s) <= 1 { - err = os.EINVAL; - return; + err = os.EINVAL + return } - c := s[1]; - s = s[2:]; + c := s[1] + s = s[2:] switch c { case 'a': @@ -147,7 +147,7 @@ func UnquoteChar(s string, quote byte) (value int, multibyte bool, tail string, case 'v': value = '\v' case 'x', 'u', 'U': - n := 0; + n := 0 switch c { case 'x': n = 2 @@ -156,64 +156,64 @@ func UnquoteChar(s string, quote byte) (value int, multibyte bool, tail string, case 'U': n = 8 } - v := 0; + v := 0 if len(s) < n { - err = os.EINVAL; - return; + err = os.EINVAL + return } for j := 0; j < n; j++ { - x, ok := unhex(s[j]); + x, ok := unhex(s[j]) if !ok { - err = os.EINVAL; - return; + err = os.EINVAL + return } - v = v<<4 | x; + v = v<<4 | x } - s = s[n:]; + s = s[n:] if c == 'x' { // single-byte string, possibly not UTF-8 - value = v; - break; + value = v + break } if v > unicode.MaxRune { - err = os.EINVAL; - return; + err = os.EINVAL + return } - value = v; - multibyte = true; + value = v + multibyte = true case '0', '1', '2', '3', '4', '5', '6', '7': - v := int(c) - '0'; + v := int(c) - '0' if len(s) < 2 { - err = os.EINVAL; - return; + err = os.EINVAL + return } - for j := 0; j < 2; j++ { // one digit already; two more - x := int(s[j]) - '0'; + for j := 0; j < 2; j++ { // one digit already; two more + x := int(s[j]) - '0' if x < 0 || x > 7 { return } - v = (v << 3) | x; + v = (v << 3) | x } - s = s[2:]; + s = s[2:] if v > 255 { - err = os.EINVAL; - return; + err = os.EINVAL + return } - value = v; + value = v case '\\': value = '\\' case '\'', '"': if c != quote { - err = os.EINVAL; - return; + err = os.EINVAL + return } - value = int(c); + value = int(c) default: - err = os.EINVAL; - return; + err = os.EINVAL + return } - tail = s; - return; + tail = s + return } // Unquote interprets s as a single-quoted, double-quoted, @@ -222,33 +222,33 @@ func UnquoteChar(s string, quote byte) (value int, multibyte bool, tail string, // character literal; Unquote returns the corresponding // one-character string.) func Unquote(s string) (t string, err os.Error) { - n := len(s); + n := len(s) if n < 2 { return "", os.EINVAL } - quote := s[0]; + quote := s[0] if quote != s[n-1] { return "", os.EINVAL } - s = s[1 : n-1]; + s = s[1 : n-1] if quote == '`' { if strings.Index(s, "`") >= 0 { return "", os.EINVAL } - return s, nil; + return s, nil } if quote != '"' && quote != '\'' { return "", err } - var buf bytes.Buffer; + var buf bytes.Buffer for len(s) > 0 { - c, multibyte, ss, err := UnquoteChar(s, quote); + c, multibyte, ss, err := UnquoteChar(s, quote) if err != nil { return "", err } - s = ss; + s = ss if c < utf8.RuneSelf || !multibyte { buf.WriteByte(byte(c)) } else { @@ -259,5 +259,5 @@ func Unquote(s string) (t string, err os.Error) { return "", os.EINVAL } } - return buf.String(), nil; + return buf.String(), nil } |