@@ -493,34 +493,20 @@ func unquote(in string, unescape bool) (out, rem string, err error) {
493
493
}
494
494
}
495
495
496
- // bsearch16 returns the smallest i such that a[i] >= x.
497
- // If there is no such i, bsearch16 returns len(a).
498
- func bsearch16 (a []uint16 , x uint16 ) int {
499
- i , j := 0 , len (a )
496
+ // bsearch is semantically the same as [slices.BinarySearch] (without NaN checks)
497
+ // We copied this function because we can not import "slices" here.
498
+ func bsearch [S ~ []E , E ~ uint16 | ~ uint32 ](s S , v E ) (int , bool ) {
499
+ n := len (s )
500
+ i , j := 0 , n
500
501
for i < j {
501
502
h := i + (j - i )>> 1
502
- if a [h ] < x {
503
+ if s [h ] < v {
503
504
i = h + 1
504
505
} else {
505
506
j = h
506
507
}
507
508
}
508
- return i
509
- }
510
-
511
- // bsearch32 returns the smallest i such that a[i] >= x.
512
- // If there is no such i, bsearch32 returns len(a).
513
- func bsearch32 (a []uint32 , x uint32 ) int {
514
- i , j := 0 , len (a )
515
- for i < j {
516
- h := i + (j - i )>> 1
517
- if a [h ] < x {
518
- i = h + 1
519
- } else {
520
- j = h
521
- }
522
- }
523
- return i
509
+ return i , i < n && s [i ] == v
524
510
}
525
511
526
512
// TODO: IsPrint is a local implementation of unicode.IsPrint, verified by the tests
@@ -554,25 +540,25 @@ func IsPrint(r rune) bool {
554
540
555
541
if 0 <= r && r < 1 << 16 {
556
542
rr , isPrint , isNotPrint := uint16 (r ), isPrint16 , isNotPrint16
557
- i := bsearch16 (isPrint , rr )
543
+ i , _ := bsearch (isPrint , rr )
558
544
if i >= len (isPrint ) || rr < isPrint [i &^1 ] || isPrint [i | 1 ] < rr {
559
545
return false
560
546
}
561
- j := bsearch16 (isNotPrint , rr )
562
- return j >= len ( isNotPrint ) || isNotPrint [ j ] != rr
547
+ _ , found := bsearch (isNotPrint , rr )
548
+ return ! found
563
549
}
564
550
565
551
rr , isPrint , isNotPrint := uint32 (r ), isPrint32 , isNotPrint32
566
- i := bsearch32 (isPrint , rr )
552
+ i , _ := bsearch (isPrint , rr )
567
553
if i >= len (isPrint ) || rr < isPrint [i &^1 ] || isPrint [i | 1 ] < rr {
568
554
return false
569
555
}
570
556
if r >= 0x20000 {
571
557
return true
572
558
}
573
559
r -= 0x10000
574
- j := bsearch16 (isNotPrint , uint16 (r ))
575
- return j >= len ( isNotPrint ) || isNotPrint [ j ] != uint16 ( r )
560
+ _ , found := bsearch (isNotPrint , uint16 (r ))
561
+ return ! found
576
562
}
577
563
578
564
// IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such
@@ -593,7 +579,6 @@ func isInGraphicList(r rune) bool {
593
579
if r > 0xFFFF {
594
580
return false
595
581
}
596
- rr := uint16 (r )
597
- i := bsearch16 (isGraphic , rr )
598
- return i < len (isGraphic ) && rr == isGraphic [i ]
582
+ _ , found := bsearch (isGraphic , uint16 (r ))
583
+ return found
599
584
}
0 commit comments