isValidNumber reports whether s is a valid JSON number literal.
(s string)
| 200 | |
| 201 | // isValidNumber reports whether s is a valid JSON number literal. |
| 202 | func isValidNumber(s string) bool { |
| 203 | // This function implements the JSON numbers grammar. |
| 204 | // See https://tools.ietf.org/html/rfc7159#section-6 |
| 205 | // and https://json.org/number.gif |
| 206 | |
| 207 | if s == "" { |
| 208 | return false |
| 209 | } |
| 210 | |
| 211 | // Optional - |
| 212 | if s[0] == '-' { |
| 213 | s = s[1:] |
| 214 | if s == "" { |
| 215 | return false |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Digits |
| 220 | switch { |
| 221 | default: |
| 222 | return false |
| 223 | |
| 224 | case s[0] == '0': |
| 225 | s = s[1:] |
| 226 | |
| 227 | case '1' <= s[0] && s[0] <= '9': |
| 228 | s = s[1:] |
| 229 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
| 230 | s = s[1:] |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // . followed by 1 or more digits. |
| 235 | if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' { |
| 236 | s = s[2:] |
| 237 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
| 238 | s = s[1:] |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // e or E followed by an optional - or + and |
| 243 | // 1 or more digits. |
| 244 | if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') { |
| 245 | s = s[1:] |
| 246 | if s[0] == '+' || s[0] == '-' { |
| 247 | s = s[1:] |
| 248 | if s == "" { |
| 249 | return false |
| 250 | } |
| 251 | } |
| 252 | for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { |
| 253 | s = s[1:] |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Make sure we are at the end. |
| 258 | return s == "" |
| 259 | } |
no outgoing calls
no test coverage detected