stdlib: Use iswspace_Konan in parseDouble/parseFloat
This commit is contained in:
@@ -47,46 +47,6 @@ OBJ_GETTER(utf8ToUtf16, const char* rawString, size_t rawStringLength) {
|
||||
RETURN_OBJ(result->obj());
|
||||
}
|
||||
|
||||
void checkParsingErrors(const char* c_str, char* end, std::string::size_type c_str_size) {
|
||||
if (end == c_str) {
|
||||
ThrowNumberFormatException();
|
||||
}
|
||||
// According to http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-
|
||||
// trailing whitespace characters must be ignored so we need to do an additional check.
|
||||
for (char* p = end; p < c_str + c_str_size; p++) {
|
||||
if (!isspace(*p)) {
|
||||
ThrowNumberFormatException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Java Double.valueOf specification requires mandatory binary exponent character (p) in the string parsed if the string is a hex one.
|
||||
// See: http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-
|
||||
// E.g.
|
||||
// "0x77p0".toDouble() // OK for both Kotlin/JVM and Kotlin/Native.
|
||||
// "0x77".toDouble() // throws NumberFormatException in Kotlin/JVM and OK in Kotlin/Native.
|
||||
// Do we need to handle such case? Or it is OK to consume such strings?
|
||||
KFloat parseFloat(KString value) {
|
||||
const KChar* utf16 = CharArrayAddressOfElementAt(value, 0);
|
||||
std::string utf8;
|
||||
utf8::utf16to8(utf16, utf16 + value->count_, back_inserter(utf8));
|
||||
char* end = nullptr;
|
||||
KFloat result = strtof(utf8.c_str(), &end);
|
||||
checkParsingErrors(utf8.c_str(), end, utf8.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
KDouble parseDouble(KString value) {
|
||||
const KChar* utf16 =
|
||||
CharArrayAddressOfElementAt(value, 0);
|
||||
std::string utf8;
|
||||
utf8::utf16to8(utf16, utf16 + value->count_, back_inserter(utf8));
|
||||
char* end = nullptr;
|
||||
KDouble result = strtod(utf8.c_str(), &end);
|
||||
checkParsingErrors(utf8.c_str(), end, utf8.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
// Case conversion is derived work from Apache Harmony.
|
||||
// Unicode 3.0.1 (same as Unicode 3.0.0)
|
||||
enum CharacterClass {
|
||||
@@ -731,6 +691,46 @@ int iswlower_Konan(KChar ch) {
|
||||
return getType(ch) == LOWERCASE_LETTER;
|
||||
}
|
||||
|
||||
void checkParsingErrors(const char* c_str, char* end, std::string::size_type c_str_size) {
|
||||
if (end == c_str) {
|
||||
ThrowNumberFormatException();
|
||||
}
|
||||
// According to http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-
|
||||
// trailing whitespace characters must be ignored so we need to do an additional check.
|
||||
for (char* p = end; p < c_str + c_str_size; p++) {
|
||||
if (!iswspace_Konan(*p)) {
|
||||
ThrowNumberFormatException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Java Double.valueOf specification requires mandatory binary exponent character (p) in the string parsed if the string is a hex one.
|
||||
// See: http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-
|
||||
// E.g.
|
||||
// "0x77p0".toDouble() // OK for both Kotlin/JVM and Kotlin/Native.
|
||||
// "0x77".toDouble() // throws NumberFormatException in Kotlin/JVM and OK in Kotlin/Native.
|
||||
// Do we need to handle such case? Or it is OK to consume such strings?
|
||||
KFloat parseFloat(KString value) {
|
||||
const KChar* utf16 = CharArrayAddressOfElementAt(value, 0);
|
||||
std::string utf8;
|
||||
utf8::utf16to8(utf16, utf16 + value->count_, back_inserter(utf8));
|
||||
char* end = nullptr;
|
||||
KFloat result = strtof(utf8.c_str(), &end);
|
||||
checkParsingErrors(utf8.c_str(), end, utf8.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
KDouble parseDouble(KString value) {
|
||||
const KChar* utf16 =
|
||||
CharArrayAddressOfElementAt(value, 0);
|
||||
std::string utf8;
|
||||
utf8::utf16to8(utf16, utf16 + value->count_, back_inserter(utf8));
|
||||
char* end = nullptr;
|
||||
KDouble result = strtod(utf8.c_str(), &end);
|
||||
checkParsingErrors(utf8.c_str(), end, utf8.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
Reference in New Issue
Block a user