Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 1 | /* -*- Mode: C; c-file-style: "python" -*- */ |
| 2 | |
| 3 | #include <Python.h> |
| 4 | #include <locale.h> |
| 5 | |
| 6 | /* ascii character tests (as opposed to locale tests) */ |
| 7 | #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \ |
| 8 | (c) == '\r' || (c) == '\t' || (c) == '\v') |
| 9 | #define ISDIGIT(c) ((c) >= '0' && (c) <= '9') |
| 10 | #define ISXDIGIT(c) (ISDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F')) |
| 11 | |
| 12 | |
| 13 | /** |
| 14 | * PyOS_ascii_strtod: |
| 15 | * @nptr: the string to convert to a numeric value. |
| 16 | * @endptr: if non-%NULL, it returns the character after |
| 17 | * the last character used in the conversion. |
| 18 | * |
| 19 | * Converts a string to a #gdouble value. |
| 20 | * This function behaves like the standard strtod() function |
| 21 | * does in the C locale. It does this without actually |
| 22 | * changing the current locale, since that would not be |
| 23 | * thread-safe. |
| 24 | * |
| 25 | * This function is typically used when reading configuration |
| 26 | * files or other non-user input that should be locale independent. |
| 27 | * To handle input from the user you should normally use the |
| 28 | * locale-sensitive system strtod() function. |
| 29 | * |
| 30 | * If the correct value would cause overflow, plus or minus %HUGE_VAL |
| 31 | * is returned (according to the sign of the value), and %ERANGE is |
| 32 | * stored in %errno. If the correct value would cause underflow, |
| 33 | * zero is returned and %ERANGE is stored in %errno. |
Georg Brandl | b569ee4 | 2006-05-29 14:28:05 +0000 | [diff] [blame] | 34 | * If memory allocation fails, %ENOMEM is stored in %errno. |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 35 | * |
| 36 | * This function resets %errno before calling strtod() so that |
| 37 | * you can reliably detect overflow and underflow. |
| 38 | * |
| 39 | * Return value: the #gdouble value. |
| 40 | **/ |
| 41 | double |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 42 | PyOS_ascii_strtod(const char *nptr, char **endptr) |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 43 | { |
| 44 | char *fail_pos; |
Neal Norwitz | 0e7a0ed | 2005-12-18 05:37:36 +0000 | [diff] [blame] | 45 | double val = -1.0; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 46 | struct lconv *locale_data; |
| 47 | const char *decimal_point; |
Neal Norwitz | d39d861 | 2006-01-08 01:03:36 +0000 | [diff] [blame] | 48 | size_t decimal_point_len; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 49 | const char *p, *decimal_point_pos; |
| 50 | const char *end = NULL; /* Silence gcc */ |
Guido van Rossum | 3b83549 | 2008-01-05 00:59:59 +0000 | [diff] [blame] | 51 | const char *digits_pos = NULL; |
| 52 | int negate = 0; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 53 | |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 54 | assert(nptr != NULL); |
| 55 | |
| 56 | fail_pos = NULL; |
| 57 | |
| 58 | locale_data = localeconv(); |
| 59 | decimal_point = locale_data->decimal_point; |
| 60 | decimal_point_len = strlen(decimal_point); |
| 61 | |
| 62 | assert(decimal_point_len != 0); |
| 63 | |
| 64 | decimal_point_pos = NULL; |
Guido van Rossum | 3b83549 | 2008-01-05 00:59:59 +0000 | [diff] [blame] | 65 | |
| 66 | /* We process any leading whitespace and the optional sign manually, |
| 67 | then pass the remainder to the system strtod. This ensures that |
| 68 | the result of an underflow has the correct sign. (bug #1725) */ |
| 69 | |
| 70 | p = nptr; |
| 71 | /* Skip leading space */ |
| 72 | while (ISSPACE(*p)) |
| 73 | p++; |
| 74 | |
| 75 | /* Process leading sign, if present */ |
| 76 | if (*p == '-') { |
| 77 | negate = 1; |
| 78 | p++; |
| 79 | } else if (*p == '+') { |
| 80 | p++; |
| 81 | } |
| 82 | |
| 83 | /* What's left should begin with a digit, a decimal point, or one of |
| 84 | the letters i, I, n, N. It should not begin with 0x or 0X */ |
| 85 | if ((!ISDIGIT(*p) && |
| 86 | *p != '.' && *p != 'i' && *p != 'I' && *p != 'n' && *p != 'N') |
| 87 | || |
| 88 | (*p == '0' && (p[1] == 'x' || p[1] == 'X'))) |
| 89 | { |
| 90 | if (endptr) |
| 91 | *endptr = (char*)nptr; |
| 92 | errno = EINVAL; |
| 93 | return val; |
| 94 | } |
| 95 | digits_pos = p; |
| 96 | |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 97 | if (decimal_point[0] != '.' || |
| 98 | decimal_point[1] != 0) |
| 99 | { |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 100 | while (ISDIGIT(*p)) |
| 101 | p++; |
| 102 | |
| 103 | if (*p == '.') |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 104 | { |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 105 | decimal_point_pos = p++; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 106 | |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 107 | while (ISDIGIT(*p)) |
| 108 | p++; |
| 109 | |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 110 | if (*p == 'e' || *p == 'E') |
| 111 | p++; |
| 112 | if (*p == '+' || *p == '-') |
| 113 | p++; |
| 114 | while (ISDIGIT(*p)) |
| 115 | p++; |
| 116 | end = p; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 117 | } |
Martin v. Löwis | fcfff0a | 2006-07-03 12:19:50 +0000 | [diff] [blame] | 118 | else if (strncmp(p, decimal_point, decimal_point_len) == 0) |
| 119 | { |
| 120 | /* Python bug #1417699 */ |
Guido van Rossum | 3b83549 | 2008-01-05 00:59:59 +0000 | [diff] [blame] | 121 | if (endptr) |
| 122 | *endptr = (char*)nptr; |
Martin v. Löwis | fcfff0a | 2006-07-03 12:19:50 +0000 | [diff] [blame] | 123 | errno = EINVAL; |
| 124 | return val; |
| 125 | } |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 126 | /* For the other cases, we need not convert the decimal point */ |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 129 | /* Set errno to zero, so that we can distinguish zero results |
| 130 | and underflows */ |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 131 | errno = 0; |
| 132 | |
| 133 | if (decimal_point_pos) |
| 134 | { |
| 135 | char *copy, *c; |
| 136 | |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 137 | /* We need to convert the '.' to the locale specific decimal point */ |
Guido van Rossum | 3b83549 | 2008-01-05 00:59:59 +0000 | [diff] [blame] | 138 | copy = (char *)PyMem_MALLOC(end - digits_pos + |
| 139 | 1 + decimal_point_len); |
Georg Brandl | b569ee4 | 2006-05-29 14:28:05 +0000 | [diff] [blame] | 140 | if (copy == NULL) { |
| 141 | if (endptr) |
Georg Brandl | 80181e2 | 2006-05-29 14:33:55 +0000 | [diff] [blame] | 142 | *endptr = (char *)nptr; |
Georg Brandl | b569ee4 | 2006-05-29 14:28:05 +0000 | [diff] [blame] | 143 | errno = ENOMEM; |
| 144 | return val; |
| 145 | } |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 146 | |
| 147 | c = copy; |
Guido van Rossum | 3b83549 | 2008-01-05 00:59:59 +0000 | [diff] [blame] | 148 | memcpy(c, digits_pos, decimal_point_pos - digits_pos); |
| 149 | c += decimal_point_pos - digits_pos; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 150 | memcpy(c, decimal_point, decimal_point_len); |
| 151 | c += decimal_point_len; |
| 152 | memcpy(c, decimal_point_pos + 1, end - (decimal_point_pos + 1)); |
| 153 | c += end - (decimal_point_pos + 1); |
| 154 | *c = 0; |
| 155 | |
| 156 | val = strtod(copy, &fail_pos); |
| 157 | |
| 158 | if (fail_pos) |
| 159 | { |
| 160 | if (fail_pos > decimal_point_pos) |
Guido van Rossum | 3b83549 | 2008-01-05 00:59:59 +0000 | [diff] [blame] | 161 | fail_pos = (char *)digits_pos + |
| 162 | (fail_pos - copy) - |
| 163 | (decimal_point_len - 1); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 164 | else |
Guido van Rossum | 3b83549 | 2008-01-05 00:59:59 +0000 | [diff] [blame] | 165 | fail_pos = (char *)digits_pos + |
| 166 | (fail_pos - copy); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Brett Cannon | 0ed0587 | 2006-05-25 20:44:08 +0000 | [diff] [blame] | 169 | PyMem_FREE(copy); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 170 | |
| 171 | } |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 172 | else { |
Guido van Rossum | 3b83549 | 2008-01-05 00:59:59 +0000 | [diff] [blame] | 173 | val = strtod(digits_pos, &fail_pos); |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 174 | } |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | 3b83549 | 2008-01-05 00:59:59 +0000 | [diff] [blame] | 176 | if (fail_pos == digits_pos) |
| 177 | fail_pos = (char *)nptr; |
| 178 | |
| 179 | if (negate && fail_pos != nptr) |
| 180 | val = -val; |
| 181 | |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 182 | if (endptr) |
| 183 | *endptr = fail_pos; |
| 184 | |
| 185 | return val; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /** |
| 190 | * PyOS_ascii_formatd: |
| 191 | * @buffer: A buffer to place the resulting string in |
| 192 | * @buf_len: The length of the buffer. |
| 193 | * @format: The printf()-style format to use for the |
| 194 | * code to use for converting. |
| 195 | * @d: The #gdouble to convert |
| 196 | * |
| 197 | * Converts a #gdouble to a string, using the '.' as |
| 198 | * decimal point. To format the number you pass in |
| 199 | * a printf()-style format string. Allowed conversion |
| 200 | * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'. |
| 201 | * |
| 202 | * Return value: The pointer to the buffer with the converted string. |
| 203 | **/ |
| 204 | char * |
| 205 | PyOS_ascii_formatd(char *buffer, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 206 | size_t buf_len, |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 207 | const char *format, |
| 208 | double d) |
| 209 | { |
| 210 | struct lconv *locale_data; |
| 211 | const char *decimal_point; |
Neal Norwitz | d39d861 | 2006-01-08 01:03:36 +0000 | [diff] [blame] | 212 | size_t decimal_point_len, rest_len; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 213 | char *p; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 214 | char format_char; |
| 215 | |
| 216 | /* g_return_val_if_fail (buffer != NULL, NULL); */ |
| 217 | /* g_return_val_if_fail (format[0] == '%', NULL); */ |
| 218 | /* g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL); */ |
| 219 | |
| 220 | format_char = format[strlen(format) - 1]; |
| 221 | |
| 222 | /* g_return_val_if_fail (format_char == 'e' || format_char == 'E' || */ |
| 223 | /* format_char == 'f' || format_char == 'F' || */ |
| 224 | /* format_char == 'g' || format_char == 'G', */ |
| 225 | /* NULL); */ |
| 226 | |
| 227 | if (format[0] != '%') |
| 228 | return NULL; |
| 229 | |
| 230 | if (strpbrk(format + 1, "'l%")) |
| 231 | return NULL; |
| 232 | |
| 233 | if (!(format_char == 'e' || format_char == 'E' || |
| 234 | format_char == 'f' || format_char == 'F' || |
| 235 | format_char == 'g' || format_char == 'G')) |
| 236 | return NULL; |
| 237 | |
| 238 | |
| 239 | PyOS_snprintf(buffer, buf_len, format, d); |
| 240 | |
| 241 | locale_data = localeconv(); |
| 242 | decimal_point = locale_data->decimal_point; |
| 243 | decimal_point_len = strlen(decimal_point); |
| 244 | |
| 245 | assert(decimal_point_len != 0); |
| 246 | |
| 247 | if (decimal_point[0] != '.' || |
| 248 | decimal_point[1] != 0) |
| 249 | { |
| 250 | p = buffer; |
| 251 | |
| 252 | if (*p == '+' || *p == '-') |
| 253 | p++; |
| 254 | |
| 255 | while (isdigit((unsigned char)*p)) |
| 256 | p++; |
| 257 | |
| 258 | if (strncmp(p, decimal_point, decimal_point_len) == 0) |
| 259 | { |
| 260 | *p = '.'; |
| 261 | p++; |
| 262 | if (decimal_point_len > 1) { |
| 263 | rest_len = strlen(p + (decimal_point_len - 1)); |
| 264 | memmove(p, p + (decimal_point_len - 1), |
| 265 | rest_len); |
| 266 | p[rest_len] = 0; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return buffer; |
| 272 | } |
| 273 | |
| 274 | double |
| 275 | PyOS_ascii_atof(const char *nptr) |
| 276 | { |
| 277 | return PyOS_ascii_strtod(nptr, NULL); |
| 278 | } |