Check all whitespace characters according to Unicode standard
This commit is contained in:
committed by
Pavel Punegov
parent
8d4bedfef6
commit
902519a26b
@@ -1512,6 +1512,11 @@ task to_string0(type: RunKonanTest) {
|
||||
source = "runtime/text/to_string0.kt"
|
||||
}
|
||||
|
||||
task trim(type: RunKonanTest) {
|
||||
goldValue = "OK\n"
|
||||
source = "runtime/text/trim.kt"
|
||||
}
|
||||
|
||||
task chars0(type: RunKonanTest) {
|
||||
expectedFail = (project.testTarget == 'wasm32') // Uses exceptions.
|
||||
source = "runtime/text/chars0.kt"
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package runtime.text.trim
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
/**
|
||||
* Tests correct conversions of floats/doubles along with trimming of leading/trailing whitespaces.
|
||||
* String.trim() trims all whitespaces (see kotlin/text/Strings.kt) while String.toFloat() uses
|
||||
* the same approach as java.lang.Float.parseFloat()
|
||||
*/
|
||||
@Test fun runTest() {
|
||||
convertToFloatingPoint()
|
||||
convertWithWhitespaces()
|
||||
trimWhitespaces()
|
||||
|
||||
println("OK")
|
||||
}
|
||||
|
||||
private fun convertToFloatingPoint() {
|
||||
assertEquals(expected = 3.14F, actual = " 3.14 ".toFloat(), message = "String float should be trimmed")
|
||||
assertEquals(expected = 3.14, actual = " 3.14 ".toDouble(), message = "String double should be trimmed")
|
||||
assertEquals(expected = 7.15F, actual = "\u0019 7.15 ".toFloat(), message = "String float should be trimmed")
|
||||
assertEquals(expected = 42.3, actual = "\n 42.3 ".toDouble(), message = "String double should be trimmed")
|
||||
}
|
||||
|
||||
private fun convertWithWhitespaces() {
|
||||
val s = "\u0009 \u000A 2.71 \u000D"
|
||||
assertEquals(expected = 2.71F, actual = s.toFloat(),
|
||||
message = "String should be cleared of LF, CR, TAB and converted to Float")
|
||||
assertEquals(expected = 2.71, actual = s.toDouble(),
|
||||
message = "String should be cleared of LF, CR, TAB and converted to Double")
|
||||
|
||||
// Special symbols should not be trimmed during String to Float/Double conversion
|
||||
assertFailsWith<NumberFormatException> { "\u202F3.14".toFloat() }
|
||||
assertFailsWith<NumberFormatException> { "\u20293.14".toDouble() }
|
||||
assertFailsWith<NumberFormatException> { "3.14\u200B".toDouble() }
|
||||
assertFailsWith<NumberFormatException> { "3.14\u200B ABC".toDouble() }
|
||||
}
|
||||
|
||||
private fun trimWhitespaces() {
|
||||
assertEquals(expected = "String", actual = " String".trim(), message = "Trim leading spaces")
|
||||
assertEquals(expected = "String ", actual = " String ".trimStart(), message = "Trim start")
|
||||
assertEquals(expected = " String", actual = " String \t ".trimEnd(), message = "Trim end")
|
||||
|
||||
assertEquals(expected = "String", actual = "\u0020 \u202FString\u2028\u2029".trim(),
|
||||
message = "Trim special whitespaces")
|
||||
assertEquals(expected = "\u1FFFString", actual = "\u0085 \u1FFFString".trim(),
|
||||
message = "Trim special whitespace but should left a unicode symbol")
|
||||
assertEquals(expected = "String\tSTR", actual = " \nString\tSTR ".trim(), message = "Trim newline")
|
||||
}
|
||||
@@ -671,17 +671,17 @@ int iswalnum_Konan(KChar ch) {
|
||||
}
|
||||
|
||||
int iswspace_Konan(KChar ch) {
|
||||
// Optimized case for ASCII.
|
||||
// FILE, GROUP, RECORD, UNIT separators, # Zs SPACE or # Cc CONTROLs
|
||||
if ((ch >= 0x1c && ch <= 0x20) || (ch >= 0x9 && ch <= 0xd)) {
|
||||
return true;
|
||||
}
|
||||
if (ch == 0x1680) {
|
||||
return true;
|
||||
}
|
||||
if (ch < 0x2000 || ch == 0x2007) {
|
||||
// not (# Zs OGHAM SPACE MARK or # Cc or # Zs NO-BREAK SPACE)
|
||||
if (ch < 0x2000 && !(ch == 0x1680 || ch == 0x85 || ch == 0xA0)) {
|
||||
return false;
|
||||
}
|
||||
return ch <= 0x200b || ch == 0x2028 || ch == 0x2029 || ch == 0x3000;
|
||||
// if # Zl LINE SEPARATOR or # Zp PARAGRAPH SEPARATOR or # Zs NARROW NO-BREAK SPACE
|
||||
// or # Zs MEDIUM MATHEMATICAL SPACE or # Zs IDEOGRAPHIC SPACE
|
||||
return ch < 0x200b || ch == 0x2028 || ch == 0x2029 || ch == 0x202F || ch == 0x205F || ch == 0x3000;
|
||||
}
|
||||
|
||||
int iswupper_Konan(KChar ch) {
|
||||
@@ -708,19 +708,6 @@ int iswlower_Konan(KChar ch) {
|
||||
return getType(ch) == LOWERCASE_LETTER;
|
||||
}
|
||||
|
||||
void checkParsingErrors(const char* c_str, const char* end, KStdString::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 (const char* p = end; p < c_str + c_str_size; p++) {
|
||||
if (!iswspace_Konan(*p)) {
|
||||
ThrowNumberFormatException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
Reference in New Issue
Block a user