blob: e1c84ea03e0eb735841b3493a85b6e6d4690a2b1 [file] [log] [blame]
Martin v. Löwis737ea822004-06-08 18:52:54 +00001/* -*- 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.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000034 * If memory allocation fails, %ENOMEM is stored in %errno.
Martin v. Löwis737ea822004-06-08 18:52:54 +000035 *
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 **/
41double
Neal Norwitze7214a12005-12-18 05:03:17 +000042PyOS_ascii_strtod(const char *nptr, char **endptr)
Martin v. Löwis737ea822004-06-08 18:52:54 +000043{
44 char *fail_pos;
Neal Norwitz0e7a0ed2005-12-18 05:37:36 +000045 double val = -1.0;
Martin v. Löwis737ea822004-06-08 18:52:54 +000046 struct lconv *locale_data;
47 const char *decimal_point;
Neal Norwitzd39d8612006-01-08 01:03:36 +000048 size_t decimal_point_len;
Martin v. Löwis737ea822004-06-08 18:52:54 +000049 const char *p, *decimal_point_pos;
50 const char *end = NULL; /* Silence gcc */
51
Martin v. Löwis737ea822004-06-08 18:52:54 +000052 assert(nptr != NULL);
53
54 fail_pos = NULL;
55
56 locale_data = localeconv();
57 decimal_point = locale_data->decimal_point;
58 decimal_point_len = strlen(decimal_point);
59
60 assert(decimal_point_len != 0);
61
62 decimal_point_pos = NULL;
63 if (decimal_point[0] != '.' ||
64 decimal_point[1] != 0)
65 {
66 p = nptr;
67 /* Skip leading space */
68 while (ISSPACE(*p))
69 p++;
70
71 /* Skip leading optional sign */
72 if (*p == '+' || *p == '-')
73 p++;
74
Neal Norwitze7214a12005-12-18 05:03:17 +000075 while (ISDIGIT(*p))
76 p++;
77
78 if (*p == '.')
Martin v. Löwis737ea822004-06-08 18:52:54 +000079 {
Neal Norwitze7214a12005-12-18 05:03:17 +000080 decimal_point_pos = p++;
Martin v. Löwis737ea822004-06-08 18:52:54 +000081
Martin v. Löwis737ea822004-06-08 18:52:54 +000082 while (ISDIGIT(*p))
83 p++;
84
Neal Norwitze7214a12005-12-18 05:03:17 +000085 if (*p == 'e' || *p == 'E')
86 p++;
87 if (*p == '+' || *p == '-')
88 p++;
89 while (ISDIGIT(*p))
90 p++;
91 end = p;
Martin v. Löwis737ea822004-06-08 18:52:54 +000092 }
Neal Norwitze7214a12005-12-18 05:03:17 +000093 /* For the other cases, we need not convert the decimal point */
Martin v. Löwis737ea822004-06-08 18:52:54 +000094 }
95
Neal Norwitze7214a12005-12-18 05:03:17 +000096 /* Set errno to zero, so that we can distinguish zero results
97 and underflows */
Martin v. Löwis737ea822004-06-08 18:52:54 +000098 errno = 0;
99
100 if (decimal_point_pos)
101 {
102 char *copy, *c;
103
Neal Norwitze7214a12005-12-18 05:03:17 +0000104 /* We need to convert the '.' to the locale specific decimal point */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000105 copy = (char *)PyMem_MALLOC(end - nptr + 1 + decimal_point_len);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000106 if (copy == NULL) {
107 if (endptr)
108 *endptr = (char *)nptr;
109 errno = ENOMEM;
110 return val;
111 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000112
113 c = copy;
114 memcpy(c, nptr, decimal_point_pos - nptr);
115 c += decimal_point_pos - nptr;
116 memcpy(c, decimal_point, decimal_point_len);
117 c += decimal_point_len;
118 memcpy(c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
119 c += end - (decimal_point_pos + 1);
120 *c = 0;
121
122 val = strtod(copy, &fail_pos);
123
124 if (fail_pos)
125 {
126 if (fail_pos > decimal_point_pos)
127 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
128 else
129 fail_pos = (char *)nptr + (fail_pos - copy);
130 }
131
Thomas Wouters477c8d52006-05-27 19:21:47 +0000132 PyMem_FREE(copy);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000133
134 }
Neal Norwitze7214a12005-12-18 05:03:17 +0000135 else {
136 unsigned i = 0;
137 if (nptr[i] == '-')
138 i++;
139 if (nptr[i] == '0' && (nptr[i+1] == 'x' || nptr[i+1] == 'X'))
Neal Norwitz0e7a0ed2005-12-18 05:37:36 +0000140 fail_pos = (char*)nptr;
Neal Norwitze7214a12005-12-18 05:03:17 +0000141 else
142 val = strtod(nptr, &fail_pos);
143 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000144
145 if (endptr)
146 *endptr = fail_pos;
147
148 return val;
149}
150
151
152/**
153 * PyOS_ascii_formatd:
154 * @buffer: A buffer to place the resulting string in
155 * @buf_len: The length of the buffer.
156 * @format: The printf()-style format to use for the
157 * code to use for converting.
158 * @d: The #gdouble to convert
159 *
160 * Converts a #gdouble to a string, using the '.' as
161 * decimal point. To format the number you pass in
162 * a printf()-style format string. Allowed conversion
163 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
164 *
165 * Return value: The pointer to the buffer with the converted string.
166 **/
167char *
168PyOS_ascii_formatd(char *buffer,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000169 size_t buf_len,
Martin v. Löwis737ea822004-06-08 18:52:54 +0000170 const char *format,
171 double d)
172{
173 struct lconv *locale_data;
174 const char *decimal_point;
Neal Norwitzd39d8612006-01-08 01:03:36 +0000175 size_t decimal_point_len, rest_len;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000176 char *p;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000177 char format_char;
178
179/* g_return_val_if_fail (buffer != NULL, NULL); */
180/* g_return_val_if_fail (format[0] == '%', NULL); */
181/* g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL); */
182
183 format_char = format[strlen(format) - 1];
184
185/* g_return_val_if_fail (format_char == 'e' || format_char == 'E' || */
186/* format_char == 'f' || format_char == 'F' || */
187/* format_char == 'g' || format_char == 'G', */
188/* NULL); */
189
190 if (format[0] != '%')
191 return NULL;
192
193 if (strpbrk(format + 1, "'l%"))
194 return NULL;
195
196 if (!(format_char == 'e' || format_char == 'E' ||
197 format_char == 'f' || format_char == 'F' ||
198 format_char == 'g' || format_char == 'G'))
199 return NULL;
200
201
202 PyOS_snprintf(buffer, buf_len, format, d);
203
204 locale_data = localeconv();
205 decimal_point = locale_data->decimal_point;
206 decimal_point_len = strlen(decimal_point);
207
208 assert(decimal_point_len != 0);
209
210 if (decimal_point[0] != '.' ||
211 decimal_point[1] != 0)
212 {
213 p = buffer;
214
215 if (*p == '+' || *p == '-')
216 p++;
217
218 while (isdigit((unsigned char)*p))
219 p++;
220
221 if (strncmp(p, decimal_point, decimal_point_len) == 0)
222 {
223 *p = '.';
224 p++;
225 if (decimal_point_len > 1) {
226 rest_len = strlen(p + (decimal_point_len - 1));
227 memmove(p, p + (decimal_point_len - 1),
228 rest_len);
229 p[rest_len] = 0;
230 }
231 }
232 }
233
234 return buffer;
235}
236
237double
238PyOS_ascii_atof(const char *nptr)
239{
240 return PyOS_ascii_strtod(nptr, NULL);
241}