diff options
author | Russ Cox <rsc@golang.org> | 2016-10-12 13:11:02 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2016-10-12 18:30:29 +0000 |
commit | 55ef67f2f85c51d415a030ae144a0b3301a097bd (patch) | |
tree | c0d130d65aa1e07f07f7a89ec530385955599fd6 /src/unicode | |
parent | 413afcafbfd45b6b58023a49484d8480143960e0 (diff) | |
download | go-55ef67f2f85c51d415a030ae144a0b3301a097bd.tar.gz go-55ef67f2f85c51d415a030ae144a0b3301a097bd.zip |
unicode: change SimpleFold to handle invalid runes
Functions like ToLower and ToUpper return the invalid rune back,
so we might as well do the same here.
I changed my mind about panicking when I tried to document the behavior.
Fixes #16690 (again).
Change-Id: If1c68bfcd66daea160fd19948e7672b0e1add106
Reviewed-on: https://go-review.googlesource.com/30935
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/unicode')
-rw-r--r-- | src/unicode/letter.go | 7 | ||||
-rw-r--r-- | src/unicode/letter_test.go | 14 |
2 files changed, 7 insertions, 14 deletions
diff --git a/src/unicode/letter.go b/src/unicode/letter.go index 462daf88b9..b43cc66e7d 100644 --- a/src/unicode/letter.go +++ b/src/unicode/letter.go @@ -320,6 +320,7 @@ type foldPair struct { // the Unicode-defined simple case folding. Among the code points // equivalent to rune (including rune itself), SimpleFold returns the // smallest rune > r if one exists, or else the smallest rune >= 0. +// If r is not a valid Unicode code point, SimpleFold(r) returns r. // // For example: // SimpleFold('A') = 'a' @@ -331,9 +332,11 @@ type foldPair struct { // // SimpleFold('1') = '1' // +// SimpleFold(-2) = -2 +// func SimpleFold(r rune) rune { - if r < 0 { - panic("unicode: negative rune is disallowed") + if r < 0 || r > MaxRune { + return r } if int(r) < len(asciiFold) { diff --git a/src/unicode/letter_test.go b/src/unicode/letter_test.go index 15e4ade2a3..3fe72ff13d 100644 --- a/src/unicode/letter_test.go +++ b/src/unicode/letter_test.go @@ -432,19 +432,9 @@ func TestSimpleFold(t *testing.T) { r = out } } -} -func TestSimpleFoldPanic(t *testing.T) { - got := func() (r interface{}) { - defer func() { r = recover() }() - SimpleFold(-1) - return nil - }() - want := "unicode: negative rune is disallowed" - - s, _ := got.(string) - if s != want { - t.Errorf("SimpleFold(-1) should panic, got: %q, want: %q", got, want) + if r := SimpleFold(-42); r != -42 { + t.Errorf("SimpleFold(-42) = %v, want -42", r) } } |