"internal/runtime/gc",
"internal/runtime/maps",
"internal/runtime/math",
+ "internal/runtime/strconv",
"internal/runtime/sys",
"internal/runtime/syscall",
< internal/runtime/gc
< internal/runtime/math
< internal/runtime/maps
+ < internal/runtime/strconv
< runtime
< sync/atomic
< internal/sync
const (
MaxUint32 = ^uint32(0)
+ MaxUint64 = ^uint64(0)
MaxUintptr = ^uintptr(0)
+
+ MaxInt64 = int64(MaxUint64 >> 1)
)
// MulUintptr returns a * b and whether the multiplication overflowed.
--- /dev/null
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+import (
+ "internal/runtime/math"
+)
+
+// Atoi64 parses an int64 from a string s.
+// The bool result reports whether s is a number
+// representable by a value of type int64.
+func Atoi64(s string) (int64, bool) {
+ if s == "" {
+ return 0, false
+ }
+
+ neg := false
+ if s[0] == '-' {
+ neg = true
+ s = s[1:]
+ }
+
+ un := uint64(0)
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if c < '0' || c > '9' {
+ return 0, false
+ }
+ if un > math.MaxUint64/10 {
+ // overflow
+ return 0, false
+ }
+ un *= 10
+ un1 := un + uint64(c) - '0'
+ if un1 < un {
+ // overflow
+ return 0, false
+ }
+ un = un1
+ }
+
+ if !neg && un > uint64(math.MaxInt64) {
+ return 0, false
+ }
+ if neg && un > uint64(math.MaxInt64)+1 {
+ return 0, false
+ }
+
+ n := int64(un)
+ if neg {
+ n = -n
+ }
+
+ return n, true
+}
+
+// Atoi is like Atoi64 but for integers
+// that fit into an int.
+func Atoi(s string) (int, bool) {
+ if n, ok := Atoi64(s); n == int64(int(n)) {
+ return int(n), ok
+ }
+ return 0, false
+}
+
+// Atoi32 is like Atoi but for integers
+// that fit into an int32.
+func Atoi32(s string) (int32, bool) {
+ if n, ok := Atoi64(s); n == int64(int32(n)) {
+ return int32(n), ok
+ }
+ return 0, false
+}
+
--- /dev/null
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "internal/runtime/strconv"
+ "testing"
+)
+
+const intSize = 32 << (^uint(0) >> 63)
+
+type atoi64Test struct {
+ in string
+ out int64
+ ok bool
+}
+
+var atoi64tests = []atoi64Test{
+ {"", 0, false},
+ {"0", 0, true},
+ {"-0", 0, true},
+ {"1", 1, true},
+ {"-1", -1, true},
+ {"12345", 12345, true},
+ {"-12345", -12345, true},
+ {"012345", 12345, true},
+ {"-012345", -12345, true},
+ {"12345x", 0, false},
+ {"-12345x", 0, false},
+ {"98765432100", 98765432100, true},
+ {"-98765432100", -98765432100, true},
+ {"20496382327982653440", 0, false},
+ {"-20496382327982653440", 0, false},
+ {"9223372036854775807", 1<<63 - 1, true},
+ {"-9223372036854775807", -(1<<63 - 1), true},
+ {"9223372036854775808", 0, false},
+ {"-9223372036854775808", -1 << 63, true},
+ {"9223372036854775809", 0, false},
+ {"-9223372036854775809", 0, false},
+}
+
+func TestAtoi(t *testing.T) {
+ switch intSize {
+ case 32:
+ for i := range atoi32tests {
+ test := &atoi32tests[i]
+ out, ok := strconv.Atoi(test.in)
+ if test.out != int32(out) || test.ok != ok {
+ t.Errorf("Atoi(%q) = (%v, %v) want (%v, %v)",
+ test.in, out, ok, test.out, test.ok)
+ }
+ }
+ case 64:
+ for i := range atoi64tests {
+ test := &atoi64tests[i]
+ out, ok := strconv.Atoi(test.in)
+ if test.out != int64(out) || test.ok != ok {
+ t.Errorf("Atoi(%q) = (%v, %v) want (%v, %v)",
+ test.in, out, ok, test.out, test.ok)
+ }
+ }
+ }
+}
+
+type atoi32Test struct {
+ in string
+ out int32
+ ok bool
+}
+
+var atoi32tests = []atoi32Test{
+ {"", 0, false},
+ {"0", 0, true},
+ {"-0", 0, true},
+ {"1", 1, true},
+ {"-1", -1, true},
+ {"12345", 12345, true},
+ {"-12345", -12345, true},
+ {"012345", 12345, true},
+ {"-012345", -12345, true},
+ {"12345x", 0, false},
+ {"-12345x", 0, false},
+ {"987654321", 987654321, true},
+ {"-987654321", -987654321, true},
+ {"2147483647", 1<<31 - 1, true},
+ {"-2147483647", -(1<<31 - 1), true},
+ {"2147483648", 0, false},
+ {"-2147483648", -1 << 31, true},
+ {"2147483649", 0, false},
+ {"-2147483649", 0, false},
+}
+
+func TestAtoi32(t *testing.T) {
+ for i := range atoi32tests {
+ test := &atoi32tests[i]
+ out, ok := strconv.Atoi32(test.in)
+ if test.out != out || test.ok != ok {
+ t.Errorf("Atoi32(%q) = (%v, %v) want (%v, %v)",
+ test.in, out, ok, test.out, test.ok)
+ }
+ }
+}
+
var Fastlog2 = fastlog2
-var Atoi = atoi
-var Atoi32 = atoi32
var ParseByteCount = parseByteCount
var Nanotime = nanotime
}
// Initialize the memory limit here because the allocator is going to look at it
// but we haven't called gcinit yet and we're definitely going to allocate memory before then.
- gcController.memoryLimit.Store(maxInt64)
+ gcController.memoryLimit.Store(math.MaxInt64)
}
// sysAlloc allocates heap arena space for at least n bytes. The
func nextSample() int64 {
if MemProfileRate == 0 {
// Basically never sample.
- return maxInt64
+ return math.MaxInt64
}
if MemProfileRate == 1 {
// Sample immediately.
"internal/cpu"
"internal/goexperiment"
"internal/runtime/atomic"
+ "internal/runtime/math"
+ "internal/runtime/strconv"
_ "unsafe" // for go:linkname
)
if p == "off" {
return -1
}
- if n, ok := atoi32(p); ok {
+ if n, ok := strconv.Atoi32(p); ok {
return n
}
return 100
func readGOMEMLIMIT() int64 {
p := gogetenv("GOMEMLIMIT")
if p == "" || p == "off" {
- return maxInt64
+ return math.MaxInt64
}
n, ok := parseByteCount(p)
if !ok {
// that we expect to page in.
inuse := gcController.mappedReady.Load()
// Be careful about overflow, especially with uintptrs. Even on 32-bit platforms
- // someone can set a really big memory limit that isn't maxInt64.
+ // someone can set a really big memory limit that isn't math.MaxInt64.
if uint64(scav)+inuse > uint64(limit) {
bytesToScavenge = uintptr(uint64(scav) + inuse - uint64(limit))
forceScavenge = true
"internal/abi"
"internal/goarch"
"internal/runtime/atomic"
+ "internal/runtime/strconv"
"internal/runtime/syscall"
"unsafe"
)
return 0
}
n-- // remove trailing newline
- v, ok := atoi(slicebytetostringtmp((*byte)(ptr), int(n)))
+ v, ok := strconv.Atoi(slicebytetostringtmp((*byte)(ptr), int(n)))
if !ok || v < 0 {
v = 0
}
"internal/goos"
"internal/runtime/atomic"
"internal/runtime/exithook"
+ "internal/runtime/strconv"
"internal/runtime/sys"
"internal/stringslite"
"unsafe"
lock(&sched.lock)
sched.lastpoll.Store(nanotime())
procs := ncpu
- if n, ok := atoi32(gogetenv("GOMAXPROCS")); ok && n > 0 {
+ if n, ok := strconv.Atoi32(gogetenv("GOMAXPROCS")); ok && n > 0 {
procs = n
}
if procresize(procs) != nil {
"internal/bytealg"
"internal/goarch"
"internal/runtime/atomic"
+ "internal/runtime/strconv"
"unsafe"
)
// is int, not int32, and should only be updated
// if specified in GODEBUG.
if seen == nil && key == "memprofilerate" {
- if n, ok := atoi(value); ok {
+ if n, ok := strconv.Atoi(value); ok {
MemProfileRate = n
}
} else {
for _, v := range dbgvars {
if v.name == key {
- if n, ok := atoi32(value); ok {
+ if n, ok := strconv.Atoi32(value); ok {
if seen == nil && v.value != nil {
*v.value = n
} else if v.atomic != nil {
fallthrough
default:
t = tracebackAll
- if n, ok := atoi(level); ok && n == int(uint32(n)) {
+ if n, ok := strconv.Atoi(level); ok && n == int(uint32(n)) {
t |= uint32(n) << tracebackShift
}
}
"internal/abi"
"internal/bytealg"
"internal/goarch"
+ "internal/runtime/math"
+ "internal/runtime/strconv"
"internal/runtime/sys"
"unsafe"
)
return s
}
-const (
- maxUint64 = ^uint64(0)
- maxInt64 = int64(maxUint64 >> 1)
-)
-
-// atoi64 parses an int64 from a string s.
-// The bool result reports whether s is a number
-// representable by a value of type int64.
-func atoi64(s string) (int64, bool) {
- if s == "" {
- return 0, false
- }
-
- neg := false
- if s[0] == '-' {
- neg = true
- s = s[1:]
- }
-
- un := uint64(0)
- for i := 0; i < len(s); i++ {
- c := s[i]
- if c < '0' || c > '9' {
- return 0, false
- }
- if un > maxUint64/10 {
- // overflow
- return 0, false
- }
- un *= 10
- un1 := un + uint64(c) - '0'
- if un1 < un {
- // overflow
- return 0, false
- }
- un = un1
- }
-
- if !neg && un > uint64(maxInt64) {
- return 0, false
- }
- if neg && un > uint64(maxInt64)+1 {
- return 0, false
- }
-
- n := int64(un)
- if neg {
- n = -n
- }
-
- return n, true
-}
-
-// atoi is like atoi64 but for integers
-// that fit into an int.
-func atoi(s string) (int, bool) {
- if n, ok := atoi64(s); n == int64(int(n)) {
- return int(n), ok
- }
- return 0, false
-}
-
-// atoi32 is like atoi but for integers
-// that fit into an int32.
-func atoi32(s string) (int32, bool) {
- if n, ok := atoi64(s); n == int64(int32(n)) {
- return int32(n), ok
- }
- return 0, false
-}
-
// parseByteCount parses a string that represents a count of bytes.
//
// s must match the following regular expression:
// Handle the easy non-suffix case.
last := s[len(s)-1]
if last >= '0' && last <= '9' {
- n, ok := atoi64(s)
+ n, ok := strconv.Atoi64(s)
if !ok || n < 0 {
return 0, false
}
// The one before that must always be a digit or 'i'.
if c := s[len(s)-2]; c >= '0' && c <= '9' {
// Trivial 'B' suffix.
- n, ok := atoi64(s[:len(s)-1])
+ n, ok := strconv.Atoi64(s[:len(s)-1])
if !ok || n < 0 {
return 0, false
}
for i := 0; i < power; i++ {
m *= 1024
}
- n, ok := atoi64(s[:len(s)-3])
+ n, ok := strconv.Atoi64(s[:len(s)-3])
if !ok || n < 0 {
return 0, false
}
un := uint64(n)
- if un > maxUint64/m {
+ if un > math.MaxUint64/m {
// Overflow.
return 0, false
}
un *= m
- if un > uint64(maxInt64) {
+ if un > uint64(math.MaxInt64) {
// Overflow.
return 0, false
}
const intSize = 32 << (^uint(0) >> 63)
-type atoi64Test struct {
- in string
- out int64
- ok bool
-}
-
-var atoi64tests = []atoi64Test{
- {"", 0, false},
- {"0", 0, true},
- {"-0", 0, true},
- {"1", 1, true},
- {"-1", -1, true},
- {"12345", 12345, true},
- {"-12345", -12345, true},
- {"012345", 12345, true},
- {"-012345", -12345, true},
- {"12345x", 0, false},
- {"-12345x", 0, false},
- {"98765432100", 98765432100, true},
- {"-98765432100", -98765432100, true},
- {"20496382327982653440", 0, false},
- {"-20496382327982653440", 0, false},
- {"9223372036854775807", 1<<63 - 1, true},
- {"-9223372036854775807", -(1<<63 - 1), true},
- {"9223372036854775808", 0, false},
- {"-9223372036854775808", -1 << 63, true},
- {"9223372036854775809", 0, false},
- {"-9223372036854775809", 0, false},
-}
-
-func TestAtoi(t *testing.T) {
- switch intSize {
- case 32:
- for i := range atoi32tests {
- test := &atoi32tests[i]
- out, ok := runtime.Atoi(test.in)
- if test.out != int32(out) || test.ok != ok {
- t.Errorf("atoi(%q) = (%v, %v) want (%v, %v)",
- test.in, out, ok, test.out, test.ok)
- }
- }
- case 64:
- for i := range atoi64tests {
- test := &atoi64tests[i]
- out, ok := runtime.Atoi(test.in)
- if test.out != int64(out) || test.ok != ok {
- t.Errorf("atoi(%q) = (%v, %v) want (%v, %v)",
- test.in, out, ok, test.out, test.ok)
- }
- }
- }
-}
-
-type atoi32Test struct {
- in string
- out int32
- ok bool
-}
-
-var atoi32tests = []atoi32Test{
- {"", 0, false},
- {"0", 0, true},
- {"-0", 0, true},
- {"1", 1, true},
- {"-1", -1, true},
- {"12345", 12345, true},
- {"-12345", -12345, true},
- {"012345", 12345, true},
- {"-012345", -12345, true},
- {"12345x", 0, false},
- {"-12345x", 0, false},
- {"987654321", 987654321, true},
- {"-987654321", -987654321, true},
- {"2147483647", 1<<31 - 1, true},
- {"-2147483647", -(1<<31 - 1), true},
- {"2147483648", 0, false},
- {"-2147483648", -1 << 31, true},
- {"2147483649", 0, false},
- {"-2147483649", 0, false},
-}
-
-func TestAtoi32(t *testing.T) {
- for i := range atoi32tests {
- test := &atoi32tests[i]
- out, ok := runtime.Atoi32(test.in)
- if test.out != out || test.ok != ok {
- t.Errorf("atoi32(%q) = (%v, %v) want (%v, %v)",
- test.in, out, ok, test.out, test.ok)
- }
- }
-}
-
func TestParseByteCount(t *testing.T) {
for _, test := range []struct {
in string