Skip to content

Commit 55ef67f

Browse files
committed
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 <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent 413afca commit 55ef67f

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

src/unicode/letter.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ type foldPair struct {
320320
// the Unicode-defined simple case folding. Among the code points
321321
// equivalent to rune (including rune itself), SimpleFold returns the
322322
// smallest rune > r if one exists, or else the smallest rune >= 0.
323+
// If r is not a valid Unicode code point, SimpleFold(r) returns r.
323324
//
324325
// For example:
325326
// SimpleFold('A') = 'a'
@@ -331,9 +332,11 @@ type foldPair struct {
331332
//
332333
// SimpleFold('1') = '1'
333334
//
335+
// SimpleFold(-2) = -2
336+
//
334337
func SimpleFold(r rune) rune {
335-
if r < 0 {
336-
panic("unicode: negative rune is disallowed")
338+
if r < 0 || r > MaxRune {
339+
return r
337340
}
338341

339342
if int(r) < len(asciiFold) {

src/unicode/letter_test.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -432,19 +432,9 @@ func TestSimpleFold(t *testing.T) {
432432
r = out
433433
}
434434
}
435-
}
436435

437-
func TestSimpleFoldPanic(t *testing.T) {
438-
got := func() (r interface{}) {
439-
defer func() { r = recover() }()
440-
SimpleFold(-1)
441-
return nil
442-
}()
443-
want := "unicode: negative rune is disallowed"
444-
445-
s, _ := got.(string)
446-
if s != want {
447-
t.Errorf("SimpleFold(-1) should panic, got: %q, want: %q", got, want)
436+
if r := SimpleFold(-42); r != -42 {
437+
t.Errorf("SimpleFold(-42) = %v, want -42", r)
448438
}
449439
}
450440

0 commit comments

Comments
 (0)