1 /* -*- Mode: C; c-file-style: "python" -*- */
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'))
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.
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
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.
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.
35 * This function resets %errno before calling strtod() so that
36 * you can reliably detect overflow and underflow.
38 * Return value: the #gdouble value.
41 PyOS_ascii_strtod(const char *nptr
, char **endptr
)
45 struct lconv
*locale_data
;
46 const char *decimal_point
;
47 size_t decimal_point_len
;
48 const char *p
, *decimal_point_pos
;
49 const char *end
= NULL
; /* Silence gcc */
55 locale_data
= localeconv();
56 decimal_point
= locale_data
->decimal_point
;
57 decimal_point_len
= strlen(decimal_point
);
59 assert(decimal_point_len
!= 0);
61 decimal_point_pos
= NULL
;
62 if (decimal_point
[0] != '.' ||
63 decimal_point
[1] != 0)
66 /* Skip leading space */
70 /* Skip leading optional sign */
71 if (*p
== '+' || *p
== '-')
79 decimal_point_pos
= p
++;
84 if (*p
== 'e' || *p
== 'E')
86 if (*p
== '+' || *p
== '-')
92 /* For the other cases, we need not convert the decimal point */
95 /* Set errno to zero, so that we can distinguish zero results
99 if (decimal_point_pos
)
103 /* We need to convert the '.' to the locale specific decimal point */
104 copy
= malloc(end
- nptr
+ 1 + decimal_point_len
);
107 memcpy(c
, nptr
, decimal_point_pos
- nptr
);
108 c
+= decimal_point_pos
- nptr
;
109 memcpy(c
, decimal_point
, decimal_point_len
);
110 c
+= decimal_point_len
;
111 memcpy(c
, decimal_point_pos
+ 1, end
- (decimal_point_pos
+ 1));
112 c
+= end
- (decimal_point_pos
+ 1);
115 val
= strtod(copy
, &fail_pos
);
119 if (fail_pos
> decimal_point_pos
)
120 fail_pos
= (char *)nptr
+ (fail_pos
- copy
) - (decimal_point_len
- 1);
122 fail_pos
= (char *)nptr
+ (fail_pos
- copy
);
132 if (nptr
[i
] == '0' && (nptr
[i
+1] == 'x' || nptr
[i
+1] == 'X'))
133 fail_pos
= (char*)nptr
;
135 val
= strtod(nptr
, &fail_pos
);
146 * PyOS_ascii_formatd:
147 * @buffer: A buffer to place the resulting string in
148 * @buf_len: The length of the buffer.
149 * @format: The printf()-style format to use for the
150 * code to use for converting.
151 * @d: The #gdouble to convert
153 * Converts a #gdouble to a string, using the '.' as
154 * decimal point. To format the number you pass in
155 * a printf()-style format string. Allowed conversion
156 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
158 * Return value: The pointer to the buffer with the converted string.
161 PyOS_ascii_formatd(char *buffer
,
166 struct lconv
*locale_data
;
167 const char *decimal_point
;
168 size_t decimal_point_len
, rest_len
;
172 /* g_return_val_if_fail (buffer != NULL, NULL); */
173 /* g_return_val_if_fail (format[0] == '%', NULL); */
174 /* g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL); */
176 format_char
= format
[strlen(format
) - 1];
178 /* g_return_val_if_fail (format_char == 'e' || format_char == 'E' || */
179 /* format_char == 'f' || format_char == 'F' || */
180 /* format_char == 'g' || format_char == 'G', */
183 if (format
[0] != '%')
186 if (strpbrk(format
+ 1, "'l%"))
189 if (!(format_char
== 'e' || format_char
== 'E' ||
190 format_char
== 'f' || format_char
== 'F' ||
191 format_char
== 'g' || format_char
== 'G'))
195 PyOS_snprintf(buffer
, buf_len
, format
, d
);
197 locale_data
= localeconv();
198 decimal_point
= locale_data
->decimal_point
;
199 decimal_point_len
= strlen(decimal_point
);
201 assert(decimal_point_len
!= 0);
203 if (decimal_point
[0] != '.' ||
204 decimal_point
[1] != 0)
208 if (*p
== '+' || *p
== '-')
211 while (isdigit((unsigned char)*p
))
214 if (strncmp(p
, decimal_point
, decimal_point_len
) == 0)
218 if (decimal_point_len
> 1) {
219 rest_len
= strlen(p
+ (decimal_point_len
- 1));
220 memmove(p
, p
+ (decimal_point_len
- 1),
231 PyOS_ascii_atof(const char *nptr
)
233 return PyOS_ascii_strtod(nptr
, NULL
);