Commit 55ef67f2 authored by Russ Cox's avatar Russ Cox

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: default avatarRob Pike <r@golang.org>
parent 413afcaf
...@@ -320,6 +320,7 @@ type foldPair struct { ...@@ -320,6 +320,7 @@ type foldPair struct {
// the Unicode-defined simple case folding. Among the code points // the Unicode-defined simple case folding. Among the code points
// equivalent to rune (including rune itself), SimpleFold returns the // equivalent to rune (including rune itself), SimpleFold returns the
// smallest rune > r if one exists, or else the smallest rune >= 0. // 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: // For example:
// SimpleFold('A') = 'a' // SimpleFold('A') = 'a'
...@@ -331,9 +332,11 @@ type foldPair struct { ...@@ -331,9 +332,11 @@ type foldPair struct {
// //
// SimpleFold('1') = '1' // SimpleFold('1') = '1'
// //
// SimpleFold(-2) = -2
//
func SimpleFold(r rune) rune { func SimpleFold(r rune) rune {
if r < 0 { if r < 0 || r > MaxRune {
panic("unicode: negative rune is disallowed") return r
} }
if int(r) < len(asciiFold) { if int(r) < len(asciiFold) {
......
...@@ -432,19 +432,9 @@ func TestSimpleFold(t *testing.T) { ...@@ -432,19 +432,9 @@ func TestSimpleFold(t *testing.T) {
r = out r = out
} }
} }
}
func TestSimpleFoldPanic(t *testing.T) { if r := SimpleFold(-42); r != -42 {
got := func() (r interface{}) { t.Errorf("SimpleFold(-42) = %v, want -42", r)
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)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment