MCPcopy Create free account
hub / github.com/crawl/crawl / parse_number

Function parse_number

crawl-ref/source/json.cc:986–1029  ·  view source on GitHub ↗

* The JSON spec says that a number shall follow this precise pattern * (spaces and quotes added for readability): * '-'? (0 | [1-9][0-9]*) ('.' [0-9]+)? ([Ee] [+-]? [0-9]+)? * * However, some JSON parsers are more liberal. For instance, PHP accepts * '.5' and '1.'. JSON.parse accepts '+3'. * * This function takes the strict approach. */

Source from the content-addressed store, hash-verified

984 * This function takes the strict approach.
985 */
986bool parse_number(const char **sp, double *out)
987{
988 const char *s = *sp;
989
990 /* '-'? */
991 if (*s == '-')
992 s++;
993
994 /* (0 | [1-9][0-9]*) */
995 if (*s == '0')
996 s++;
997 else
998 {
999 if (!is_digit(*s))
1000 return false;
1001 do { s++; } while (is_digit(*s));
1002 }
1003
1004 /* ('.' [0-9]+)? */
1005 if (*s == '.')
1006 {
1007 s++;
1008 if (!is_digit(*s))
1009 return false;
1010 do { s++; } while (is_digit(*s));
1011 }
1012
1013 /* ([Ee] [+-]? [0-9]+)? */
1014 if (*s == 'E' || *s == 'e')
1015 {
1016 s++;
1017 if (*s == '+' || *s == '-')
1018 s++;
1019 if (!is_digit(*s))
1020 return false;
1021 do { s++; } while (is_digit(*s));
1022 }
1023
1024 if (out)
1025 *out = strtod(*sp, nullptr);
1026
1027 *sp = s;
1028 return true;
1029}
1030
1031static void skip_space(const char **sp)
1032{

Callers 2

parse_valueFunction · 0.85
number_is_validFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected