* Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS * in length. */
| 39 | * in length. |
| 40 | */ |
| 41 | static void |
| 42 | ensure_minimum_exponent_length(char* buffer, size_t buf_size) |
| 43 | { |
| 44 | char *p = strpbrk(buffer, "eE"); |
| 45 | if (p && (*(p + 1) == '-' || *(p + 1) == '+')) { |
| 46 | char *start = p + 2; |
| 47 | int exponent_digit_cnt = 0; |
| 48 | int leading_zero_cnt = 0; |
| 49 | int in_leading_zeros = 1; |
| 50 | int significant_digit_cnt; |
| 51 | |
| 52 | /* Skip over the exponent and the sign. */ |
| 53 | p += 2; |
| 54 | |
| 55 | /* Find the end of the exponent, keeping track of leading zeros. */ |
| 56 | while (*p && isdigit(Py_CHARMASK(*p))) { |
| 57 | if (in_leading_zeros && *p == '0') { |
| 58 | ++leading_zero_cnt; |
| 59 | } |
| 60 | if (*p != '0') { |
| 61 | in_leading_zeros = 0; |
| 62 | } |
| 63 | ++p; |
| 64 | ++exponent_digit_cnt; |
| 65 | } |
| 66 | |
| 67 | significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt; |
| 68 | if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) { |
| 69 | /* |
| 70 | * If there are 2 exactly digits, we're done, |
| 71 | * regardless of what they contain |
| 72 | */ |
| 73 | } |
| 74 | else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) { |
| 75 | int extra_zeros_cnt; |
| 76 | |
| 77 | /* |
| 78 | * There are more than 2 digits in the exponent. See |
| 79 | * if we can delete some of the leading zeros |
| 80 | */ |
| 81 | if (significant_digit_cnt < MIN_EXPONENT_DIGITS) { |
| 82 | significant_digit_cnt = MIN_EXPONENT_DIGITS; |
| 83 | } |
| 84 | extra_zeros_cnt = exponent_digit_cnt - significant_digit_cnt; |
| 85 | |
| 86 | /* |
| 87 | * Delete extra_zeros_cnt worth of characters from the |
| 88 | * front of the exponent |
| 89 | */ |
| 90 | assert(extra_zeros_cnt >= 0); |
| 91 | |
| 92 | /* |
| 93 | * Add one to significant_digit_cnt to copy the |
| 94 | * trailing 0 byte, thus setting the length |
| 95 | */ |
| 96 | memmove(start, start + extra_zeros_cnt, significant_digit_cnt + 1); |
| 97 | } |
| 98 | else { |
no test coverage detected