1 |
/* Double.c - java.lang.Double native functions |
/* Double.c - java.lang.Double native functions |
2 |
Copyright (C) 1998, 1999 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
231 |
|
|
232 |
return (*env)->NewStringUTF(env, result); |
return (*env)->NewStringUTF(env, result); |
233 |
} |
} |
234 |
|
|
235 |
/* |
/* |
236 |
* Class: java_lang_Double |
* Class: java_lang_Double |
237 |
* Method: parseDouble |
* Method: parseDouble |
238 |
* Signature: (Ljava/lang/String)D |
* Signature: (Ljava/lang/String)D |
239 |
*/ |
*/ |
240 |
JNIEXPORT jdouble JNICALL Java_java_lang_Double_parseDouble0 |
JNIEXPORT jdouble JNICALL Java_java_lang_Double_parseDouble |
241 |
(JNIEnv * env, jclass cls, jstring str) |
(JNIEnv * env, jclass cls, jstring str) |
242 |
{ |
{ |
243 |
jboolean isCopy; |
jboolean isCopy; |
|
int length; |
|
244 |
char *buf, *endptr; |
char *buf, *endptr; |
245 |
jdouble val; |
jdouble val = 0.0; |
246 |
|
|
247 |
buf = (*env)->GetStringUTFChars(env, str, &isCopy); |
buf = (char *) (*env)->GetStringUTFChars(env, str, &isCopy); |
248 |
if (buf == NULL) |
if (buf == NULL) |
249 |
{ |
{ |
250 |
return 0.0; /* OutOfMemoryError already thrown */ |
/* OutOfMemoryError already thrown */ |
251 |
} |
} |
252 |
|
else |
253 |
|
{ |
254 |
|
unsigned char *p = buf, *end, *last_non_ws; |
255 |
|
int ok = 1; |
256 |
|
|
257 |
#ifdef DEBUG |
#ifdef DEBUG |
258 |
fprintf (stderr, "java.lang.Double.parseDouble0 (%s)\n", buf); |
fprintf (stderr, "java.lang.Double.parseDouble (%s)\n", buf); |
259 |
#endif |
#endif |
260 |
|
|
261 |
if (strlen(buf) > 0) |
/* Trim the buffer, similar to String.trim(). First the leading |
262 |
{ |
characters. */ |
263 |
struct _Jv_reent reent; |
while (*p && *p <= ' ') |
264 |
memset (&reent, 0, sizeof reent); |
++p; |
265 |
|
|
266 |
|
/* Find the last non-whitespace character. This method is safe |
267 |
|
even with multi-byte UTF-8 characters. */ |
268 |
|
end = p; |
269 |
|
last_non_ws = NULL; |
270 |
|
while (*end) |
271 |
|
{ |
272 |
|
if (*end > ' ') |
273 |
|
last_non_ws = end; |
274 |
|
++end; |
275 |
|
} |
276 |
|
|
277 |
|
if (last_non_ws == NULL) |
278 |
|
last_non_ws = p + strlen (p); |
279 |
|
else |
280 |
|
{ |
281 |
|
/* Skip past the last non-whitespace character. */ |
282 |
|
++last_non_ws; |
283 |
|
} |
284 |
|
|
285 |
|
/* Skip a trailing `f' or `d'. */ |
286 |
|
if (last_non_ws > p |
287 |
|
&& (last_non_ws[-1] == 'f' |
288 |
|
|| last_non_ws[-1] == 'F' |
289 |
|
|| last_non_ws[-1] == 'd' |
290 |
|
|| last_non_ws[-1] == 'D')) |
291 |
|
--last_non_ws; |
292 |
|
|
293 |
|
if (last_non_ws > p) |
294 |
|
{ |
295 |
|
struct _Jv_reent reent; |
296 |
|
memset (&reent, 0, sizeof reent); |
297 |
|
|
298 |
#ifdef KISSME_LINUX_USER |
#ifdef KISSME_LINUX_USER |
299 |
val = strtod ( buf, &endptr); |
val = strtod (p, &endptr); |
300 |
#else |
#else |
301 |
val = _strtod_r (&reent, buf, &endptr); |
val = _strtod_r (&reent, p, &endptr); |
302 |
#endif |
#endif |
303 |
|
|
|
length = strlen(buf); |
|
|
if ((buf[length-1] == 'f') |
|
|
|| (buf[length-1] == 'F') |
|
|
|| (buf[length-1] == 'd') |
|
|
|| (buf[length-1] == 'D')) |
|
|
length = length - 1; |
|
|
|
|
304 |
#ifdef DEBUG |
#ifdef DEBUG |
305 |
fprintf (stderr, "java.lang.Double.parseDouble0 val = %g\n", val); |
fprintf (stderr, "java.lang.Double.parseDouble val = %g\n", val); |
306 |
fprintf (stderr, "java.lang.Double.parseDouble0 %i = %i + %i\n", endptr, buf, length); |
fprintf (stderr, "java.lang.Double.parseDouble %i != %i ???\n", |
307 |
|
endptr, last_non_ws); |
308 |
#endif |
#endif |
309 |
|
if ((unsigned char *) endptr != last_non_ws) |
310 |
|
ok = 0; |
311 |
|
} |
312 |
|
else |
313 |
|
ok = 0; |
314 |
|
|
315 |
|
if (! ok) |
316 |
|
{ |
317 |
|
val = 0.0; |
318 |
|
JCL_ThrowException (env, |
319 |
|
"java/lang/NumberFormatException", |
320 |
|
"unable to parse double"); |
321 |
|
} |
322 |
|
|
323 |
if (endptr == buf + length) |
(*env)->ReleaseStringUTFChars (env, str, buf); |
|
return val; |
|
324 |
} |
} |
325 |
JCL_ThrowException(env, "java/lang/NumberFormatException", "unable to parse double"); |
|
326 |
return 0.0; /* NumberFormatException already thrown */ |
return val; |
327 |
} |
} |