diff --git a/strconv/strconvutil/slice.go b/strconv/strconvutil/slice.go index 9745732..ada3d5a 100644 --- a/strconv/strconvutil/slice.go +++ b/strconv/strconvutil/slice.go @@ -1,41 +1,61 @@ package strconvutil import ( - "sort" "strconv" + "github.com/grokify/mogo/type/slicesutil" "golang.org/x/exp/constraints" ) // SliceAtoi converts a slice of string integers. -func SliceAtoi(strings []string) ([]int, error) { - ints := []int{} - for _, s := range strings { - thisInt, err := strconv.Atoi(s) +func SliceAtoi(s []string, dedupe, sort bool) ([]int, error) { + out := []int{} + for _, si := range s { + sv, err := strconv.Atoi(si) if err != nil { - return ints, err + return []int{}, err } - ints = append(ints, thisInt) + out = append(out, sv) } - return ints, nil + if dedupe { + out = slicesutil.Dedupe(out) + } + if sort { + slicesutil.Sort(out) + } + return out, nil } -// SliceAtoiSort converts and sorts a slice of string integers. -func SliceAtoiSort(strings []string) ([]int, error) { - ints, err := SliceAtoi(strings) - if err != nil { - return ints, err - } - intSlice := sort.IntSlice(ints) - intSlice.Sort() - return intSlice, nil +func SliceAtou(s []string, dedupe, sort bool) ([]uint, error) { + out := []uint{} + for _, si := range s { + sv, err := Atou(si) + if err != nil { + return []uint{}, err + } else { + out = append(out, sv) + } + } + if dedupe { + out = slicesutil.Dedupe(out) + } + if sort { + slicesutil.Sort(out) + } + return out, nil } // SliceItoa converts a slice of `constraints.Integer` to a slice of `string`. -func SliceItoa[S ~[]E, E constraints.Integer](elems S) []string { - strs := []string{} - for _, v := range elems { - strs = append(strs, strconv.Itoa(int(v))) +func SliceItoa[S ~[]E, E constraints.Integer](s S, dedupe, sort bool) []string { + out := []string{} + for _, v := range s { + out = append(out, strconv.Itoa(int(v))) + } + if dedupe { + out = slicesutil.Dedupe(out) + } + if sort { + slicesutil.Sort(out) } - return strs + return out } diff --git a/type/slicesutil/strconv.go b/type/slicesutil/strconv.go deleted file mode 100644 index d1202d2..0000000 --- a/type/slicesutil/strconv.go +++ /dev/null @@ -1,14 +0,0 @@ -package slicesutil - -import ( - "github.com/grokify/mogo/strconv/strconvutil" - "golang.org/x/exp/constraints" -) - -func Itoa[S ~[]E, E constraints.Integer](s S) []string { - out := []string{} - for _, e := range s { - out = append(out, strconvutil.Itoa(e)) - } - return out -}