aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorqiulaidongfeng <2645477756@qq.com>2024-05-04 02:01:19 +0000
committerGopher Robot <gobot@golang.org>2024-05-05 00:24:26 +0000
commitc3dff93a8c927c426770b27fee0457a544ef4be6 (patch)
tree342e3c5ce2e78d57bd6b092a0dda4fb9f52df283 /src
parenta80543a987c4201f05842ea7d46cc8c999a01b09 (diff)
downloadgo-c3dff93a8c927c426770b27fee0457a544ef4be6.tar.gz
go-c3dff93a8c927c426770b27fee0457a544ef4be6.zip
strconv: use stringslite.Clone
Change-Id: Ifa3c022ad5453301573593a3d05e7b1d42b931ff GitHub-Last-Rev: a7468b068b5c7bcaa573042cf9d5c2732c0385f1 GitHub-Pull-Request: golang/go#67167 Reviewed-on: https://go-review.googlesource.com/c/go/+/583215 Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Commit-Queue: Ian Lance Taylor <iant@google.com> Auto-Submit: Keith Randall <khr@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/strconv/atoc.go4
-rw-r--r--src/strconv/atoi.go23
2 files changed, 12 insertions, 15 deletions
diff --git a/src/strconv/atoc.go b/src/strconv/atoc.go
index 8cf975d3e1..560bd7920d 100644
--- a/src/strconv/atoc.go
+++ b/src/strconv/atoc.go
@@ -4,6 +4,8 @@
package strconv
+import "internal/stringslite"
+
const fnParseComplex = "ParseComplex"
// convErr splits an error returned by parseFloatPrefix
@@ -11,7 +13,7 @@ const fnParseComplex = "ParseComplex"
func convErr(err error, s string) (syntax, range_ error) {
if x, ok := err.(*NumError); ok {
x.Func = fnParseComplex
- x.Num = cloneString(s)
+ x.Num = stringslite.Clone(s)
if x.Err == ErrRange {
return nil, x
}
diff --git a/src/strconv/atoi.go b/src/strconv/atoi.go
index 45341820cd..599ad9b895 100644
--- a/src/strconv/atoi.go
+++ b/src/strconv/atoi.go
@@ -4,7 +4,10 @@
package strconv
-import "errors"
+import (
+ "errors"
+ "internal/stringslite"
+)
// lower(c) is a lower-case letter if and only if
// c is either that lower-case letter or the equivalent upper-case letter.
@@ -33,8 +36,6 @@ func (e *NumError) Error() string {
func (e *NumError) Unwrap() error { return e.Err }
-// cloneString returns a string copy of x.
-//
// All ParseXXX functions allow the input string to escape to the error value.
// This hurts strconv.ParseXXX(string(b)) calls where b is []byte since
// the conversion from []byte must allocate a string on the heap.
@@ -42,27 +43,21 @@ func (e *NumError) Unwrap() error { return e.Err }
// back to the output by copying it first. This allows the compiler to call
// strconv.ParseXXX without a heap allocation for most []byte to string
// conversions, since it can now prove that the string cannot escape Parse.
-//
-// TODO: Use strings.Clone instead? However, we cannot depend on "strings"
-// since it incurs a transitive dependency on "unicode".
-// Either move strings.Clone to an internal/bytealg or make the
-// "strings" to "unicode" dependency lighter (see https://go.dev/issue/54098).
-func cloneString(x string) string { return string([]byte(x)) }
func syntaxError(fn, str string) *NumError {
- return &NumError{fn, cloneString(str), ErrSyntax}
+ return &NumError{fn, stringslite.Clone(str), ErrSyntax}
}
func rangeError(fn, str string) *NumError {
- return &NumError{fn, cloneString(str), ErrRange}
+ return &NumError{fn, stringslite.Clone(str), ErrRange}
}
func baseError(fn, str string, base int) *NumError {
- return &NumError{fn, cloneString(str), errors.New("invalid base " + Itoa(base))}
+ return &NumError{fn, stringslite.Clone(str), errors.New("invalid base " + Itoa(base))}
}
func bitSizeError(fn, str string, bitSize int) *NumError {
- return &NumError{fn, cloneString(str), errors.New("invalid bit size " + Itoa(bitSize))}
+ return &NumError{fn, stringslite.Clone(str), errors.New("invalid bit size " + Itoa(bitSize))}
}
const intSize = 32 << (^uint(0) >> 63)
@@ -221,7 +216,7 @@ func ParseInt(s string, base int, bitSize int) (i int64, err error) {
un, err = ParseUint(s, base, bitSize)
if err != nil && err.(*NumError).Err != ErrRange {
err.(*NumError).Func = fnParseInt
- err.(*NumError).Num = cloneString(s0)
+ err.(*NumError).Num = stringslite.Clone(s0)
return 0, err
}