2021-06-02 17:25:04 +02:00
|
|
|
package txt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UInt converts a string to an unsigned integer or 0 if invalid.
|
|
|
|
func UInt(s string) uint {
|
|
|
|
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 || result < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|