IsPositiveInteger checks if a string is a positive integer. Returns true for strings like "1", "123", "999" but false for: - Zero ("0") - Negative numbers ("-5") - Numbers with leading zeros ("007") - Floating point numbers ("3.14") - Non-numeric strings ("abc") - Empty strings ("")
(s string)
| 148 | // - Non-numeric strings ("abc") |
| 149 | // - Empty strings ("") |
| 150 | func IsPositiveInteger(s string) bool { |
| 151 | // Must not be empty |
| 152 | if s == "" { |
| 153 | return false |
| 154 | } |
| 155 | |
| 156 | // Must not have leading zeros (except "0" itself, but that's not positive) |
| 157 | if len(s) > 1 && s[0] == '0' { |
| 158 | return false |
| 159 | } |
| 160 | |
| 161 | // Must be numeric and > 0 |
| 162 | num, err := strconv.ParseInt(s, 10, 64) |
| 163 | return err == nil && num > 0 |
| 164 | } |
no outgoing calls