diff options
| author | Russ Cox <rsc@golang.org> | 2008-11-24 13:04:27 -0800 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2008-11-24 13:04:27 -0800 |
| commit | b65a930453232646b1511414bcdbc6e05b9db476 (patch) | |
| tree | dcbf89c2abddd807510ee54812d5493c6f3c182b /src/lib/strconv | |
| parent | 508277debe6230ef210cf0165c393e6ff2fd0d4b (diff) | |
| download | go-b65a930453232646b1511414bcdbc6e05b9db476.tar.xz | |
utf8: add InString routines for decoding in strings
reflect: add InterfaceValue.Get(), remove Empty
strconv: add Quote, CanBackquote
fmt:
* %q go-quoted " string
* %#q go-quoted ` string if possible, " string otherwise
* %x hexadecimal string
* anywhere a string is okay, *[]byte is okay
* flags # 0 - + space
* print value inside interface, not interface itself
* tests
R=r
DELTA=756 (597 added, 121 deleted, 38 changed)
OCL=19888
CL=19916
Diffstat (limited to 'src/lib/strconv')
| -rw-r--r-- | src/lib/strconv/Makefile | 5 | ||||
| -rw-r--r-- | src/lib/strconv/ftoa_test.go | 1 | ||||
| -rw-r--r-- | src/lib/strconv/quote.go | 76 | ||||
| -rw-r--r-- | src/lib/strconv/quote_test.go | 87 |
4 files changed, 167 insertions, 2 deletions
diff --git a/src/lib/strconv/Makefile b/src/lib/strconv/Makefile index 399360a936..cf74d58f73 100644 --- a/src/lib/strconv/Makefile +++ b/src/lib/strconv/Makefile @@ -33,8 +33,9 @@ coverage: packages O1=\ atoi.$O\ - decimal.$O\ itoa.$O\ + decimal.$O\ + quote.$O\ O2=\ ftoa.$O\ @@ -45,7 +46,7 @@ O3=\ strconv.a: a1 a2 a3 a1: $(O1) - $(AR) grc strconv.a atoi.$O decimal.$O itoa.$O + $(AR) grc strconv.a atoi.$O itoa.$O decimal.$O quote.$O rm -f $(O1) a2: $(O2) diff --git a/src/lib/strconv/ftoa_test.go b/src/lib/strconv/ftoa_test.go index 914ecd9e33..643abb0dd4 100644 --- a/src/lib/strconv/ftoa_test.go +++ b/src/lib/strconv/ftoa_test.go @@ -25,6 +25,7 @@ var ftests = []Test { Test{ 1, 'g', 5, "1" }, Test{ 1, 'g', -1, "1" }, Test{ 20, 'g', -1, "20" }, + Test{ 1234567.8, 'g', -1, "1.2345678e+06" }, Test{ 200000, 'g', -1, "200000" }, Test{ 2000000, 'g', -1, "2e+06" }, diff --git a/src/lib/strconv/quote.go b/src/lib/strconv/quote.go new file mode 100644 index 0000000000..122af92d72 --- /dev/null +++ b/src/lib/strconv/quote.go @@ -0,0 +1,76 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package strconv + +import ( + "utf8"; +) + +const ldigits = "0123456789abcdef" +const udigits = "0123456789ABCDEF" + +export func Quote(s string) string { + t := `"`; + for i := 0; i < len(s); i++ { + switch { + case s[i] == '"': + t += `\"`; + case s[i] == '\\': + t += `\\`; + case ' ' <= s[i] && s[i] <= '~': + t += string(s[i]); + case s[i] == '\a': + t += `\a`; + case s[i] == '\b': + t += `\b`; + case s[i] == '\f': + t += `\f`; + case s[i] == '\n': + t += `\n`; + case s[i] == '\r': + t += `\r`; + case s[i] == '\t': + t += `\t`; + case s[i] == '\v': + t += `\v`; + + case utf8.FullRuneInString(s, i): + r, size := utf8.DecodeRuneInString(s, i); + if r == utf8.RuneError && size == 1 { + goto EscX; + } + i += size-1; // i++ on next iteration + if r < 0x10000 { + t += `\u`; + for j:=uint(0); j<4; j++ { + t += string(ldigits[(r>>(12-4*j))&0xF]); + } + } else { + t += `\U`; + for j:=uint(0); j<8; j++ { + t += string(ldigits[(r>>(28-4*j))&0xF]); + } + } + + default: + EscX: + t += `\x`; + t += string(ldigits[s[i]>>4]); + t += string(ldigits[s[i]&0xF]); + } + } + t += `"`; + return t; +} + +export func CanBackquote(s string) bool { + for i := 0; i < len(s); i++ { + if s[i] < ' ' || s[i] == '`' { + return false; + } + } + return true; +} + diff --git a/src/lib/strconv/quote_test.go b/src/lib/strconv/quote_test.go new file mode 100644 index 0000000000..2c0e98ed52 --- /dev/null +++ b/src/lib/strconv/quote_test.go @@ -0,0 +1,87 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package strconv + +import ( + "strconv"; + "testing"; +) + +type QuoteTest struct { + in string; + out string; +} + +var quotetests = []QuoteTest { + QuoteTest{ "\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"` }, + QuoteTest{ "\\", `"\\"` }, + QuoteTest{ "abc\xffdef", `"abc\xffdef"` }, + QuoteTest{ "\u263a", `"\u263a"` }, + QuoteTest{ "\U0010ffff", `"\U0010ffff"` }, +} + +export func TestQuote(t *testing.T) { + for i := 0; i < len(quotetests); i++ { + tt := quotetests[i]; + if out := Quote(tt.in); out != tt.out { + t.Errorf("Quote(%s) = %s, want %s", tt.in, out, tt.out); + } + } +} + +type CanBackquoteTest struct { + in string; + out bool; +} + +var canbackquotetests = []CanBackquoteTest { + CanBackquoteTest{ "`", false }, + CanBackquoteTest{ string(0), false }, + CanBackquoteTest{ string(1), false }, + CanBackquoteTest{ string(2), false }, + CanBackquoteTest{ string(3), false }, + CanBackquoteTest{ string(4), false }, + CanBackquoteTest{ string(5), false }, + CanBackquoteTest{ string(6), false }, + CanBackquoteTest{ string(7), false }, + CanBackquoteTest{ string(8), false }, + CanBackquoteTest{ string(9), false }, + CanBackquoteTest{ string(10), false }, + CanBackquoteTest{ string(11), false }, + CanBackquoteTest{ string(12), false }, + CanBackquoteTest{ string(13), false }, + CanBackquoteTest{ string(14), false }, + CanBackquoteTest{ string(15), false }, + CanBackquoteTest{ string(16), false }, + CanBackquoteTest{ string(17), false }, + CanBackquoteTest{ string(18), false }, + CanBackquoteTest{ string(19), false }, + CanBackquoteTest{ string(20), false }, + CanBackquoteTest{ string(21), false }, + CanBackquoteTest{ string(22), false }, + CanBackquoteTest{ string(23), false }, + CanBackquoteTest{ string(24), false }, + CanBackquoteTest{ string(25), false }, + CanBackquoteTest{ string(26), false }, + CanBackquoteTest{ string(27), false }, + CanBackquoteTest{ string(28), false }, + CanBackquoteTest{ string(29), false }, + CanBackquoteTest{ string(30), false }, + CanBackquoteTest{ string(31), false }, + CanBackquoteTest{ `' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true }, + CanBackquoteTest{ `0123456789`, true }, + CanBackquoteTest{ `ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true }, + CanBackquoteTest{ `abcdefghijklmnopqrstuvwxyz`, true }, + CanBackquoteTest{ `☺`, true }, +} + +export func TestCanBackquote(t *testing.T) { + for i := 0; i < len(canbackquotetests); i++ { + tt := canbackquotetests[i]; + if out := CanBackquote(tt.in); out != tt.out { + t.Errorf("CanBackquote(%q) = %v, want %v", tt.in, out, tt.out); + } + } +} |
