diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-06-03 11:31:24 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-06-03 11:31:24 +0200 |
commit | 56135c623a865c501ab31cc940c0e22ece2673f4 (patch) | |
tree | f69e04e82bbf75bdab0f624430ef265425e62b35 /src/pkg/strconv/quote.go | |
parent | 63d29fefab5290dc96e0a03ff70603aefa995887 (diff) | |
download | golang-56135c623a865c501ab31cc940c0e22ece2673f4.tar.gz |
Imported Upstream version 2011.06.02upstream-weekly/2011.06.02
Diffstat (limited to 'src/pkg/strconv/quote.go')
-rw-r--r-- | src/pkg/strconv/quote.go | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/src/pkg/strconv/quote.go b/src/pkg/strconv/quote.go index ed5889723..bbc0b2658 100644 --- a/src/pkg/strconv/quote.go +++ b/src/pkg/strconv/quote.go @@ -14,17 +14,14 @@ import ( const lowerhex = "0123456789abcdef" -// Quote returns a double-quoted Go string literal -// representing s. The returned string s uses Go escape -// sequences (\t, \n, \xFF, \u0100) for control characters -// and non-ASCII characters. -func Quote(s string) string { +func quoteWith(s string, quote byte) string { var buf bytes.Buffer - buf.WriteByte('"') + buf.WriteByte(quote) for ; len(s) > 0; s = s[1:] { switch c := s[0]; { - case c == '"': - buf.WriteString(`\"`) + case c == quote: + buf.WriteByte('\\') + buf.WriteByte(quote) case c == '\\': buf.WriteString(`\\`) case ' ' <= c && c <= '~': @@ -69,8 +66,26 @@ func Quote(s string) string { buf.WriteByte(lowerhex[c&0xF]) } } - buf.WriteByte('"') + buf.WriteByte(quote) return buf.String() + +} + +// Quote returns a double-quoted Go string literal +// representing s. The returned string uses Go escape +// sequences (\t, \n, \xFF, \u0100) for control characters +// and non-ASCII characters. +func Quote(s string) string { + return quoteWith(s, '"') +} + +// QuoteRune returns a single-quoted Go character literal +// representing the rune. The returned string uses Go escape +// sequences (\t, \n, \xFF, \u0100) for control characters +// and non-ASCII characters. +func QuoteRune(rune int) string { + // TODO: avoid the allocation here. + return quoteWith(string(rune), '\'') } // CanBackquote returns whether the string s would be |