diff options
author | Kei Son <hey.calmdown@gmail.com> | 2009-12-11 10:37:48 -0800 |
---|---|---|
committer | Kei Son <hey.calmdown@gmail.com> | 2009-12-11 10:37:48 -0800 |
commit | 42506b7ab20df820f52da6a1ab0edd97231feba2 (patch) | |
tree | 3016eecdbaea6a8f70831cb6a56929843bcceaa8 /src/pkg/strings | |
parent | 2201430392d52a5f43d3808b5856deeb4a8e8914 (diff) | |
download | go-42506b7ab20df820f52da6a1ab0edd97231feba2.tar.gz |
bytes, strings: allow -1 in Map to mean "drop this character".
xml: drop invalid characters in attribute names
when constructing struct field names.
R=rsc
CC=r
http://codereview.appspot.com/157104
Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/pkg/strings')
-rw-r--r-- | src/pkg/strings/strings.go | 29 | ||||
-rw-r--r-- | src/pkg/strings/strings_test.go | 13 |
2 files changed, 29 insertions, 13 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go index 7be98e6c1..4e375b4d5 100644 --- a/src/pkg/strings/strings.go +++ b/src/pkg/strings/strings.go @@ -178,7 +178,8 @@ func HasSuffix(s, suffix string) bool { } // Map returns a copy of the string s with all its characters modified -// according to the mapping function. +// according to the mapping function. If mapping returns a negative value, the character is +// dropped from the string with no replacement. func Map(mapping func(rune int) int, s string) string { // In the worst case, the string can grow when mapped, making // things unpleasant. But it's so rare we barge in assuming it's @@ -188,20 +189,22 @@ func Map(mapping func(rune int) int, s string) string { b := make([]byte, maxbytes); for _, c := range s { rune := mapping(c); - wid := 1; - if rune >= utf8.RuneSelf { - wid = utf8.RuneLen(rune) - } - if nbytes+wid > maxbytes { - // Grow the buffer. - maxbytes = maxbytes*2 + utf8.UTFMax; - nb := make([]byte, maxbytes); - for i, c := range b[0:nbytes] { - nb[i] = c + if rune >= 0 { + wid := 1; + if rune >= utf8.RuneSelf { + wid = utf8.RuneLen(rune) + } + if nbytes+wid > maxbytes { + // Grow the buffer. + maxbytes = maxbytes*2 + utf8.UTFMax; + nb := make([]byte, maxbytes); + for i, c := range b[0:nbytes] { + nb[i] = c + } + b = nb; } - b = nb; + nbytes += utf8.EncodeRune(rune, b[nbytes:maxbytes]); } - nbytes += utf8.EncodeRune(rune, b[nbytes:maxbytes]); } return string(b[0:nbytes]); } diff --git a/src/pkg/strings/strings_test.go b/src/pkg/strings/strings_test.go index ce77c5c2f..e3e7f38ae 100644 --- a/src/pkg/strings/strings_test.go +++ b/src/pkg/strings/strings_test.go @@ -279,6 +279,19 @@ func TestMap(t *testing.T) { if m != expect { t.Errorf("rot13: expected %q got %q", expect, m) } + + // 5. Drop + dropNotLatin := func(rune int) int { + if unicode.Is(unicode.Latin, rune) { + return rune + } + return -1; + }; + m = Map(dropNotLatin, "Hello, 세계"); + expect = "Hello"; + if m != expect { + t.Errorf("drop: expected %q got %q", expect, m) + } } func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) } |