MCPcopy Create free account
hub / github.com/F-Stack/f-stack / parse_number

Function parse_number

freebsd/kern/kern_fail.c:1073–1111  ·  view source on GitHub ↗

* Internal helper function to parse a numeric for a failpoint term. */

Source from the content-addressed store, hash-verified

1071 * Internal helper function to parse a numeric for a failpoint term.
1072 */
1073static char *
1074parse_number(int *out_units, int *out_decimal, char *p)
1075{
1076 char *old_p;
1077
1078 /**
1079 * <number> ::
1080 * <integer> [ "." <integer> ] |
1081 * "." <integer>
1082 */
1083
1084 /* whole part */
1085 old_p = p;
1086 *out_units = strtol(p, &p, 10);
1087 if (p == old_p && *p != '.')
1088 return (NULL);
1089
1090 /* fractional part */
1091 *out_decimal = 0;
1092 if (*p == '.') {
1093 int digits = 0;
1094 p++;
1095 while (isdigit(*p)) {
1096 int digit = *p - '0';
1097 if (digits < PROB_DIGITS - 2)
1098 *out_decimal = *out_decimal * 10 + digit;
1099 else if (digits == PROB_DIGITS - 2 && digit >= 5)
1100 (*out_decimal)++;
1101 digits++;
1102 p++;
1103 }
1104 if (!digits) /* need at least one digit after '.' */
1105 return (NULL);
1106 while (digits++ < PROB_DIGITS - 2) /* add implicit zeros */
1107 *out_decimal *= 10;
1108 }
1109
1110 return (p); /* success */
1111}
1112
1113/**
1114 * Internal helper function to parse an individual type for a failpoint term.

Callers 1

parse_termFunction · 0.85

Calls 2

strtolFunction · 0.85
isdigitFunction · 0.85

Tested by

no test coverage detected