Skip to content

Commit

Permalink
Character.isWhitespace(-1) returns false (#3284)
Browse files Browse the repository at this point in the history
Fix #3154

Previously, `Character.isWhitespace(-1)` and
`Character.isLowerCase(-1)` throw
`java.lang.ArrayIndexOutOfBoundsException: -1`.
However, they should return `false` as other java std libraries do.

We may want to fix `getTypeLT256`, but this commit guaranteed that
the codepoint is gte 0 on call-site of `getTypeLT256` for
not introducing the duplicated check of `codePoint < 0`.
  • Loading branch information
tanishiking committed May 15, 2023
1 parent 89f8ed5 commit f71420c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions javalib/src/main/scala/java/lang/Character.scala
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ object Character {
def isWhitespace(codePoint: scala.Int): scala.Boolean = {
def isSeparator(tpe: Int): scala.Boolean =
tpe == SPACE_SEPARATOR || tpe == LINE_SEPARATOR || tpe == PARAGRAPH_SEPARATOR
if (codePoint < 256) {
if (codePoint < 0) false
else if (codePoint < 256) {
codePoint == '\t' || codePoint == '\n' || codePoint == '\u000B' ||
codePoint == '\f' || codePoint == '\r' ||
('\u001C' <= codePoint && codePoint <= '\u001F') ||
Expand Down Expand Up @@ -592,7 +593,8 @@ object Character {
isLowerCase(c.toInt)

def isLowerCase(c: Int): scala.Boolean = {
if (c < 256)
if (c < 0) false
else if (c < 256)
c == '\u00AA' || c == '\u00BA' || getTypeLT256(c) == LOWERCASE_LETTER
else
isLowerCaseGE256(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,13 @@ class CharacterTest {

}

@Test def isLowerCase(): Unit = {
assertTrue(Character.isLowerCase('a'))
assertTrue(Character.isLowerCase('z'))
assertFalse(Character.isLowerCase('A'))
assertFalse(Character.isLowerCase(-1))
}

@Test def toLowerCaseLow(): Unit = {
// low chars
assertTrue(toLowerCase('\n') equals '\n')
Expand Down Expand Up @@ -606,4 +613,20 @@ class CharacterTest {
// unspecified for non-supplementary code points
}

@Test def isWhitespace(): Unit = {
assertTrue(Character.isWhitespace(' '))
assertTrue(Character.isWhitespace('\t'))
assertTrue(Character.isWhitespace('\n'))
assertTrue(Character.isWhitespace('\f'))
assertTrue(Character.isWhitespace('\r'))
assertTrue(Character.isWhitespace('\u001C')) // file separator
assertTrue(Character.isWhitespace('\u001D')) // group separator
assertTrue(Character.isWhitespace('\u001E')) // record separator
assertTrue(Character.isWhitespace('\u001F')) // unit separator

assertFalse(Character.isWhitespace('\b'))
assertFalse(Character.isWhitespace('a'))
// https://github.com/scala-native/scala-native/issues/3154
assertFalse(Character.isWhitespace(-1))
}
}

0 comments on commit f71420c

Please sign in to comment.