2021-06-02 17:25:04 +02:00
|
|
|
package txt
|
|
|
|
|
|
|
|
import (
|
2023-10-12 14:53:40 +02:00
|
|
|
"errors"
|
2021-06-02 17:25:04 +02:00
|
|
|
"strconv"
|
2021-09-20 23:32:35 +02:00
|
|
|
"strings"
|
2021-06-02 17:25:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Int converts a string to a signed integer or 0 if invalid.
|
|
|
|
func Int(s string) int {
|
|
|
|
if s == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-09-20 23:32:35 +02:00
|
|
|
result, err := strconv.ParseInt(strings.TrimSpace(s), 10, 32)
|
2021-06-02 17:25:04 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(result)
|
|
|
|
}
|
|
|
|
|
2022-03-25 16:31:09 +01:00
|
|
|
// IntVal converts a string to a validated integer or a default if invalid.
|
2022-07-22 12:38:25 +02:00
|
|
|
func IntVal(s string, min, max, def int) (i int) {
|
2022-03-25 16:31:09 +01:00
|
|
|
if s == "" {
|
2022-07-22 12:38:25 +02:00
|
|
|
return def
|
2022-04-01 13:25:25 +02:00
|
|
|
} else if s[0] == ' ' {
|
|
|
|
s = strings.TrimSpace(s)
|
2022-03-25 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result, err := strconv.ParseInt(s, 10, 32)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-07-22 12:38:25 +02:00
|
|
|
return def
|
2022-03-25 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
i = int(result)
|
|
|
|
|
|
|
|
if i < min {
|
2022-07-22 12:38:25 +02:00
|
|
|
return def
|
2022-03-25 16:31:09 +01:00
|
|
|
} else if max != 0 && i > max {
|
2022-07-22 12:38:25 +02:00
|
|
|
return def
|
2022-03-25 16:31:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2023-10-12 14:53:40 +02:00
|
|
|
// IntRange parses a string as integer range and returns an error if it's not a valid range.
|
|
|
|
func IntRange(s string, min, max int) (start int, end int, err error) {
|
|
|
|
if s == "" || len(s) > 40 {
|
|
|
|
return start, end, errors.New("invalid range")
|
|
|
|
}
|
|
|
|
|
|
|
|
valid := false
|
|
|
|
|
|
|
|
p := 0
|
|
|
|
v := [][]byte{make([]byte, 0, 20), make([]byte, 0, 20)}
|
|
|
|
|
|
|
|
for i, r := range s {
|
|
|
|
if r == 45 {
|
|
|
|
if i == 0 || p == 1 {
|
|
|
|
v[p] = append(v[p], byte(r))
|
|
|
|
} else if p == 0 {
|
|
|
|
p = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r == 46 || r >= 48 && r <= 57 {
|
|
|
|
valid = true
|
|
|
|
v[p] = append(v[p], byte(r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return start, end, errors.New("invalid range")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p == 0 {
|
|
|
|
start = Int(string(v[0]))
|
|
|
|
end = start
|
|
|
|
} else {
|
|
|
|
start = Int(string(v[0]))
|
|
|
|
end = Int(string(v[1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
if start > max {
|
|
|
|
start = max
|
|
|
|
} else if start < min {
|
|
|
|
start = min
|
|
|
|
}
|
|
|
|
|
|
|
|
if end > max {
|
|
|
|
end = max
|
|
|
|
} else if end < min {
|
|
|
|
end = min
|
|
|
|
}
|
|
|
|
|
|
|
|
return start, end, nil
|
|
|
|
}
|
|
|
|
|
2021-06-02 17:25:04 +02:00
|
|
|
// UInt converts a string to an unsigned integer or 0 if invalid.
|
|
|
|
func UInt(s string) uint {
|
|
|
|
if s == "" {
|
|
|
|
return 0
|
2022-04-01 13:25:25 +02:00
|
|
|
} else if s[0] == ' ' {
|
|
|
|
s = strings.TrimSpace(s)
|
2021-06-02 17:25:04 +02:00
|
|
|
}
|
|
|
|
|
2022-04-01 13:25:25 +02:00
|
|
|
result, err := strconv.ParseInt(s, 10, 32)
|
2021-06-02 17:25:04 +02:00
|
|
|
|
|
|
|
if err != nil || result < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint(result)
|
|
|
|
}
|
|
|
|
|
2023-10-12 14:53:40 +02:00
|
|
|
// Int64 converts a string to a signed 64-bit integer or 0 if invalid.
|
|
|
|
func Int64(s string) int64 {
|
|
|
|
if s == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
i := strings.SplitN(Numeric(s), ".", 2)
|
|
|
|
|
|
|
|
result, err := strconv.ParseInt(i[0], 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-06-02 17:25:04 +02:00
|
|
|
// IsUInt tests if a string represents an unsigned integer.
|
|
|
|
func IsUInt(s string) bool {
|
|
|
|
if s == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range s {
|
|
|
|
if r < 48 || r > 57 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2022-03-24 18:30:59 +01:00
|
|
|
|
|
|
|
// IsPosInt checks if a string represents an integer greater than 0.
|
|
|
|
func IsPosInt(s string) bool {
|
|
|
|
if s == "" || s == " " || s == "0" || s == "-1" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range s {
|
|
|
|
if r < 48 || r > 57 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|