1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
package util
import (
"fmt"
"strconv"
"strings"
)
// ParseUint32Range parses a uint32 range in the form "number" or "start-end".
// Returns the start number and the size of the range.
func ParseUint32Range(value string) (uint32, uint32, error) {
rangeParts := strings.SplitN(value, "-", 2)
rangeLen := len(rangeParts)
if rangeLen != 1 && rangeLen != 2 {
return 0, 0, fmt.Errorf("Range must contain a single number or start and end numbers")
}
startNum, err := strconv.ParseUint(rangeParts[0], 10, 32)
if err != nil {
return 0, 0, fmt.Errorf("Invalid number %q", value)
}
var rangeSize uint32 = 1
if rangeLen == 2 {
endNum, err := strconv.ParseUint(rangeParts[1], 10, 32)
if err != nil {
return 0, 0, fmt.Errorf("Invalid end number %q", value)
}
if startNum >= endNum {
return 0, 0, fmt.Errorf("Start number %d must be lower than end number %d", startNum, endNum)
}
rangeSize += uint32(endNum) - uint32(startNum)
}
return uint32(startNum), rangeSize, nil
}
// SplitNTrimSpace returns result of strings.SplitN() and then strings.TrimSpace() on each element.
// Accepts nilIfEmpty argument which if true, will return nil slice if s is empty (after trimming space).
func SplitNTrimSpace(s string, sep string, n int, nilIfEmpty bool) []string {
if nilIfEmpty && strings.TrimSpace(s) == "" {
return nil
}
parts := strings.SplitN(s, sep, n)
for i, v := range parts {
parts[i] = strings.TrimSpace(v)
}
return parts
}
// StringHasPrefix returns true if value has one of the supplied prefixes.
func StringHasPrefix(value string, prefixes ...string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(value, prefix) {
return true
}
}
return false
}
// StringPrefixInSlice returns true if any element in the list has the given prefix.
func StringPrefixInSlice(key string, list []string) bool {
for _, entry := range list {
if strings.HasPrefix(entry, key) {
return true
}
}
return false
}
|