]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: use slices.BinarySearch to simplify makeisprint.go
authorapocelipes <seve3r@outlook.com>
Mon, 11 Mar 2024 17:41:43 +0000 (17:41 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 11 Mar 2024 18:56:17 +0000 (18:56 +0000)
Change-Id: I9886a99f730b7616f6f8a5e6154e1beb7d3c79e6
GitHub-Last-Rev: 3f9dc7707377f79968e2dfcd206b83db21e60e60
GitHub-Pull-Request: golang/go#66242
Reviewed-on: https://go-review.googlesource.com/c/go/+/570535
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/strconv/makeisprint.go

index ff361e7b41fb3c0abe0be10a1cbba40afe087fb1..767448067b7296b8b8992cb3441ab0af7c8593af 100644 (file)
@@ -19,6 +19,7 @@ import (
        "go/format"
        "log"
        "os"
+       "slices"
        "unicode"
 )
 
@@ -31,36 +32,6 @@ var (
        except32 []uint32
 )
 
-// bsearch16 returns the smallest i such that a[i] >= x.
-// If there is no such i, bsearch16 returns len(a).
-func bsearch16(a []uint16, x uint16) int {
-       i, j := 0, len(a)
-       for i < j {
-               h := i + (j-i)>>1
-               if a[h] < x {
-                       i = h + 1
-               } else {
-                       j = h
-               }
-       }
-       return i
-}
-
-// bsearch32 returns the smallest i such that a[i] >= x.
-// If there is no such i, bsearch32 returns len(a).
-func bsearch32(a []uint32, x uint32) int {
-       i, j := 0, len(a)
-       for i < j {
-               h := i + (j-i)>>1
-               if a[h] < x {
-                       i = h + 1
-               } else {
-                       j = h
-               }
-       }
-       return i
-}
-
 func isPrint(r rune) bool {
        // Same algorithm, either on uint16 or uint32 value.
        // First, find first i such that rang[i] >= x.
@@ -70,21 +41,21 @@ func isPrint(r rune) bool {
 
        if 0 <= r && r < 1<<16 {
                rr, rang, except := uint16(r), range16, except16
-               i := bsearch16(rang, rr)
+               i, _ := slices.BinarySearch(rang, rr)
                if i >= len(rang) || rr < rang[i&^1] || rang[i|1] < rr {
                        return false
                }
-               j := bsearch16(except, rr)
-               return j >= len(except) || except[j] != rr
+               _, found := slices.BinarySearch(except, rr)
+               return !found
        }
 
        rr, rang, except := uint32(r), range32, except32
-       i := bsearch32(rang, rr)
+       i, _ := slices.BinarySearch(rang, rr)
        if i >= len(rang) || rr < rang[i&^1] || rang[i|1] < rr {
                return false
        }
-       j := bsearch32(except, rr)
-       return j >= len(except) || except[j] != rr
+       _, found := slices.BinarySearch(except, rr)
+       return !found
 }
 
 func scan(min, max rune) (rang, except []uint32) {