diff options
author | Tom Tromey <tromey@redhat.com> | 2001-11-10 20:15:13 +0000 |
---|---|---|
committer | Tom Tromey <tromey@redhat.com> | 2001-11-10 20:15:13 +0000 |
commit | 8058906d624f6eed06403ecc80814020e8a2c920 (patch) | |
tree | 944385b5209108cc2d5593de63e826c5ffc06052 /native | |
parent | 4ea159c8260cc6a89990ed79eb76676429a69c62 (diff) | |
download | classpath-8058906d624f6eed06403ecc80814020e8a2c920.tar.gz |
* java/lang/Double.java (parseDouble): Now native.
(parseDouble0): Removed.
* native/jni/java-lang/java_lang_Double.c
(Java_java_lang_Double_parseDouble): Renamed. Rewrote to trim
String internally. Free the UTF buffer at end.
Diffstat (limited to 'native')
-rw-r--r-- | native/jni/java-lang/java_lang_Double.c | 88 |
1 files changed, 68 insertions, 20 deletions
diff --git a/native/jni/java-lang/java_lang_Double.c b/native/jni/java-lang/java_lang_Double.c index cc1a702db..b592ebf7c 100644 --- a/native/jni/java-lang/java_lang_Double.c +++ b/native/jni/java-lang/java_lang_Double.c @@ -1,5 +1,5 @@ /* Double.c - java.lang.Double native functions - Copyright (C) 1998, 1999 Free Software Foundation, Inc. + Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -222,39 +222,87 @@ JNIEXPORT jstring JNICALL Java_java_lang_Double_toString return (*env)->NewStringUTF(env, result); } - + /* * Class: java_lang_Double * Method: parseDouble * Signature: (Ljava/lang/String)D */ -JNIEXPORT jdouble JNICALL Java_java_lang_Double_parseDouble0 +JNIEXPORT jdouble JNICALL Java_java_lang_Double_parseDouble (JNIEnv * env, jclass cls, jstring str) { jboolean isCopy; int length; char *buf, *endptr; - jdouble val; + jdouble val = 0.0; - buf = (*env)->GetStringUTFChars(env, str, &isCopy); - if (buf == NULL) + buf = (char *) (*env)->GetStringUTFChars (env, str, &isCopy); + if (buf != NULL) { - return 0.0; /* OutOfMemoryError already thrown */ - } + unsigned char *p = buf, *end, *last_non_ws; + unsigned char *copy; + int ok = 1; + + /* Trim the buffer, similar to String.trim(). */ + while (*p <= ' ') + ++p; + + /* Find the last non-whitespace character and terminate the + buffer at that position. This method is safe even with + multi-byte UTF-8 characters. */ + end = p; + last_non_ws = NULL; + while (*end) + { + if (*end > ' ') + last_non_ws = end; + ++end; + } - if (strlen(buf) > 0) - { - struct _Jv_reent reent; - memset (&reent, 0, sizeof reent); + /* If the VM didn't copy the result for us, we make our own copy + so we can edit it a bit. */ + if (isCopy) + copy = p; + else + { + copy = (char *) malloc (last_non_ws + 1 - p); + memcpy (copy, p, last_non_ws - p); + last_non_ws = copy + (last_non_ws - p); + } + + if (last_non_ws != NULL) + *(last_non_ws + 1) = '\0'; + + length = strlen (p); + if (length > 0) + { + struct _Jv_reent reent; + memset (&reent, 0, sizeof reent); #ifdef KISSME_LINUX_USER - val = strtod ( buf, &endptr); - #else - val = _strtod_r (&reent, buf, &endptr); - #endif - if (endptr == buf + strlen(buf)) - return val; + val = strtod ( p, &endptr); +#else + val = _strtod_r (&reent, p, &endptr); +#endif + if ((unsigned char *) endptr != p + length) + ok = 0; + } + else + ok = 0; + + if (copy != p) + free (copy); + + if (! ok) + { + val = 0.0; + JCL_ThrowException (env, + "java/lang/NumberFormatException", + "unable to parse double"); + } + + (*env)->ReleaseStringUTFChars (env, str, buf); } - JCL_ThrowException(env, "java/lang/NumberFormatException", "unable to parse double"); - return 0.0; /* NumberFormatException already thrown */ + + return val; } |