Compare commits

...

6 Commits

Author SHA1 Message Date
Ricardo Gerardi 6bf6c09440
Merge 5e269c4527 into 487df00934 2024-11-22 14:13:18 +00:00
Steve Francia 487df00934
Merge pull request #213 from skyjerry/fix-uint64
Fix ToUint64 issue with string input exceeding maximum int64
2024-10-18 13:53:17 -04:00
Steve Francia 955c718bf6
Merge pull request #224 from lesichkovm/patch-1
Update README.md
2024-10-18 13:51:17 -04:00
Milan Lesichkov 184982508e
Update README.md
Fixed the workflow status badge to point to the correct test file.
2024-09-01 06:22:58 +01:00
skyjerry 6e9731d50e Title: Fix ToUint64E function in cast library for Go
Detail:
The ToUint6E4 function in the Go cast library was fixed to correctly handle strings that overflow the maximum value of int64.

Previously, the function incorrectly returned zero for these input values because it was using strconv.ParseInt internally, which doesn't handle values greater than the max int64. The fix replaces the use of strconv.ParseInt with strconv.ParseUint.
2024-03-12 22:17:08 +08:00
Ricardo Gerardi 5e269c4527 Added space separated string to IntSlice conversion 2020-05-13 23:18:19 -04:00
3 changed files with 13 additions and 3 deletions

View File

@ -1,6 +1,6 @@
# cast
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/spf13/cast/ci.yaml?branch=master&style=flat-square)](https://github.com/spf13/cast/actions/workflows/ci.yaml)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/spf13/cast/test.yaml?branch=master&style=flat-square)](https://github.com/spf13/cast/actions/workflows/test.yaml)
[![PkgGoDev](https://pkg.go.dev/badge/mod/github.com/spf13/cast)](https://pkg.go.dev/mod/github.com/spf13/cast)
![Go Version](https://img.shields.io/badge/go%20version-%3E=1.16-61CFDD.svg?style=flat-square)
[![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast?style=flat-square)](https://goreportcard.com/report/github.com/spf13/cast)

View File

@ -111,6 +111,12 @@ func TestToUintE(t *testing.T) {
func TestToUint64E(t *testing.T) {
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(8))
// Maximum value of uint64
tests = append(tests,
testStep{"18446744073709551615", uint64(18446744073709551615), false},
testStep{"18446744073709551616", uint64(0), true},
)
runNumberTest(
qt.New(t),
tests,
@ -648,10 +654,12 @@ func TestToIntSliceE(t *testing.T) {
{[]interface{}{1.2, 3.2}, []int{1, 3}, false},
{[]string{"2", "3"}, []int{2, 3}, false},
{[2]string{"2", "3"}, []int{2, 3}, false},
{"2 3", []int{2, 3}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{[]string{"foo", "bar"}, nil, true},
{"2 a", []int{}, true},
}
for i, test := range tests {

View File

@ -613,12 +613,12 @@ func ToUint64E(i interface{}) (uint64, error) {
switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
}
return uint64(v), nil
return v, nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
case json.Number:
@ -1335,6 +1335,8 @@ func ToIntSliceE(i interface{}) ([]int, error) {
switch v := i.(type) {
case []int:
return v, nil
case string:
return ToIntSliceE(strings.Fields(v))
}
kind := reflect.TypeOf(i).Kind()