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 | 4faa9da32a986b1c10ad8b02e6bde4e6f06552ff (patch) | |
| tree | 3016eecdbaea6a8f70831cb6a56929843bcceaa8 /src/pkg/bytes | |
| parent | e584e6d2da734864fab7bbc340aa83636a4e5739 (diff) | |
| download | golang-4faa9da32a986b1c10ad8b02e6bde4e6f06552ff.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/bytes')
| -rw-r--r-- | src/pkg/bytes/bytes.go | 21 | ||||
| -rw-r--r-- | src/pkg/bytes/bytes_test.go | 13 |
2 files changed, 25 insertions, 9 deletions
diff --git a/src/pkg/bytes/bytes.go b/src/pkg/bytes/bytes.go index 9ab199ceb..85d4f9fd7 100644 --- a/src/pkg/bytes/bytes.go +++ b/src/pkg/bytes/bytes.go @@ -207,7 +207,8 @@ func HasSuffix(s, suffix []byte) bool { } // Map returns a copy of the byte array 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 []byte) []byte { // In the worst case, the array can grow when mapped, making // things unpleasant. But it's so rare we barge in assuming it's @@ -222,16 +223,18 @@ func Map(mapping func(rune int) int, s []byte) []byte { rune, wid = utf8.DecodeRune(s[i:]) } rune = mapping(rune); - if nbytes+utf8.RuneLen(rune) > 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 { + if nbytes+utf8.RuneLen(rune) > 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]); i += wid; } return b[0:nbytes]; diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go index 553ceb7c5..3f77e6e9f 100644 --- a/src/pkg/bytes/bytes_test.go +++ b/src/pkg/bytes/bytes_test.go @@ -365,6 +365,19 @@ func TestMap(t *testing.T) { if string(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, Bytes("Hello, 세계")); + expect = "Hello"; + if string(m) != expect { + t.Errorf("drop: expected %q got %q", expect, m) + } } func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) } |
