-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from vimeo/parse_integral_type_slice_helpers
parse: add integral-slice type parsing support
- Loading branch information
Showing
2 changed files
with
473 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,46 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"unsafe" | ||
) | ||
|
||
// SignedIntegralSlice splits on commas and parses into a slice of integers | ||
// Parses with strconv.ParseInt and the base set to 0 so base prefixes are available. | ||
// Whitespace is trimmed around the integers before parsing to allow for reasonable separtion (shell word-splitting aside) | ||
func SignedIntegralSlice[I int | int64 | int32 | int16 | int8](s string) ([]I, error) { | ||
parts := strings.Split(s, ",") | ||
out := make([]I, len(parts)) | ||
|
||
bitSize := int(unsafe.Sizeof(I(0)) * 8) | ||
|
||
for i, p := range parts { | ||
val, parseErr := strconv.ParseInt(strings.TrimSpace(p), 0, bitSize) | ||
if parseErr != nil { | ||
return nil, fmt.Errorf("failed to parse integer index %d: %w", i, parseErr) | ||
} | ||
out[i] = I(val) | ||
} | ||
return out, nil | ||
} | ||
|
||
// UnsignedIntegralSlice splits on commas and parses into a slice of integers | ||
// Parses with strconv.ParseInt and the base set to 0 so base prefixes are available. | ||
// Whitespace is trimmed around the integers before parsing to allow for reasonable separtion (shell word-splitting aside) | ||
func UnsignedIntegralSlice[I uint | uint64 | uint32 | uint16 | uint8 | uintptr](s string) ([]I, error) { | ||
parts := strings.Split(s, ",") | ||
out := make([]I, len(parts)) | ||
|
||
bitSize := int(unsafe.Sizeof(I(0)) * 8) | ||
|
||
for i, p := range parts { | ||
val, parseErr := strconv.ParseUint(strings.TrimSpace(p), 0, bitSize) | ||
if parseErr != nil { | ||
return nil, fmt.Errorf("failed to parse integer index %d: %w", i, parseErr) | ||
} | ||
out[i] = I(val) | ||
} | ||
return out, nil | ||
} |
Oops, something went wrong.