-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7f01f4
commit 3862c80
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Package number provides utility functions for working with numbers. | ||
package number | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ParseNumbers parses a comma-separated list of numbers into a slice of unit64. | ||
func ParseNumbers(numbersParam string) ([]uint64, error) { | ||
var numbers []uint64 | ||
numStrings := splitAndTrim(numbersParam, ",") | ||
|
||
for _, numStr := range numStrings { | ||
num, err := strconv.ParseUint(numStr, 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
numbers = append(numbers, num) | ||
} | ||
|
||
return numbers, nil | ||
} | ||
|
||
// splitAndTrim splits a string by a separator and trims the resulting strings. | ||
func splitAndTrim(s, sep string) []string { | ||
var result []string | ||
for _, val := range strings.Split(s, sep) { | ||
trimmed := strings.TrimSpace(val) | ||
if trimmed != "" { | ||
result = append(result, trimmed) | ||
} | ||
} | ||
return result | ||
} |