aboutsummaryrefslogtreecommitdiff
path: root/src/strconv
diff options
context:
space:
mode:
authorapocelipes <seve3r@outlook.com>2024-03-11 17:41:43 +0000
committerGopher Robot <gobot@golang.org>2024-03-11 18:56:17 +0000
commitb4b9746e5170d50475b9c0d60073ce5e74b7aae1 (patch)
tree7c3e50092ae69be9d8bec27f070c295813ba3a80 /src/strconv
parentedbb5a13b4d89adcfdce3704615e2d33b5991794 (diff)
downloadgo-b4b9746e5170d50475b9c0d60073ce5e74b7aae1.tar.gz
go-b4b9746e5170d50475b9c0d60073ce5e74b7aae1.zip
strconv: use slices.BinarySearch to simplify makeisprint.go
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>
Diffstat (limited to 'src/strconv')
-rw-r--r--src/strconv/makeisprint.go43
1 files changed, 7 insertions, 36 deletions
diff --git a/src/strconv/makeisprint.go b/src/strconv/makeisprint.go
index ff361e7b41..767448067b 100644
--- a/src/strconv/makeisprint.go
+++ b/src/strconv/makeisprint.go
@@ -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) {