Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix String.offsetByCodePoints for unpaired surrogates #3471

Merged

Conversation

tanishiking
Copy link
Member

fix #3470

Previously, String.offsetByCodePoints had an error when the offsetByCodePoints ends with the unpaired higher surrogate.

❯ cat test.scala
@main def main = {
    println("\uD800".offsetByCodePoints(0, 1))
}

❯ scala-cli test.scala
Compiling project (Scala 3.3.0, JVM)
Compiled project (Scala 3.3.0, JVM)
1

❯ scala-cli test.scala --native
Compiling project (Scala 3.3.0, Scala Native)
Compiled project (Scala 3.3.0, Scala Native)
[info] Linking (756 ms)
[info] Discovered 680 classes and 3771 methods
[info] Optimizing (debug mode) (889 ms)
[info] Generating intermediate code (856 ms)
[info] Produced 8 files
[info] Compiling to native code (1649 ms)
[info] Total (4257 ms)
java.lang.ArrayIndexOutOfBoundsException: 1
        at java.lang.StackTrace$.currentStackTrace$$anonfun$1(Unknown Source)
        at java.lang.StackTrace$$$Lambda$2.applyVoid(Unknown Source)
        at scala.runtime.function.JProcedure1.apply(Unknown Source)
        at scala.scalanative.unsafe.Zone$.apply(Unknown Source)
        at java.lang.StackTrace$.currentStackTrace(Unknown Source)
        at java.lang.Throwable.fillInStackTrace(Unknown Source)
        at scala.scalanative.runtime.package$.throwOutOfBounds(Unknown Source)
        at java.lang.Character$.offsetByCodePoints(Unknown Source)
        at java.lang.String.offsetByCodePoints(Unknown Source)
        at test$package$.main(Unknown Source)
        at main.main(Unknown Source)
        at <none>.main(Unknown Source)

This commit fix the bound check in the offsetByCodePoints not to check the next index is the lower surrogate after the higher surrogate.

fix scala-native#3470

Previously, String.offsetByCodePoints had an error
when the offsetByCodePoints ends with the unpaired higher surrogate.

```
❯ cat test.scala
@main def main = {
    println("\uD800".offsetByCodePoints(0, 1))
}

❯ scala-cli test.scala
Compiling project (Scala 3.3.0, JVM)
Compiled project (Scala 3.3.0, JVM)
1

❯ scala-cli test.scala --native
Compiling project (Scala 3.3.0, Scala Native)
Compiled project (Scala 3.3.0, Scala Native)
[info] Linking (756 ms)
[info] Discovered 680 classes and 3771 methods
[info] Optimizing (debug mode) (889 ms)
[info] Generating intermediate code (856 ms)
[info] Produced 8 files
[info] Compiling to native code (1649 ms)
[info] Total (4257 ms)
java.lang.ArrayIndexOutOfBoundsException: 1
        at java.lang.StackTrace$.currentStackTrace$$anonfun$1(Unknown Source)
        at java.lang.StackTrace$$$Lambda$2.applyVoid(Unknown Source)
        at scala.runtime.function.JProcedure1.apply(Unknown Source)
        at scala.scalanative.unsafe.Zone$.apply(Unknown Source)
        at java.lang.StackTrace$.currentStackTrace(Unknown Source)
        at java.lang.Throwable.fillInStackTrace(Unknown Source)
        at scala.scalanative.runtime.package$.throwOutOfBounds(Unknown Source)
        at java.lang.Character$.offsetByCodePoints(Unknown Source)
        at java.lang.String.offsetByCodePoints(Unknown Source)
        at test$package$.main(Unknown Source)
        at main.main(Unknown Source)
        at <none>.main(Unknown Source)
```

This commit fix the bound check in the offsetByCodePoints not to
check the next index is the lower surrogate after the higher surrogate.
@@ -378,7 +378,7 @@ object Character {
}
if (isHighSurrogate(seq(i))) {
val next = i + 1
if (next <= end && isLowSurrogate(seq(next))) {
if (next < end && isLowSurrogate(seq(next))) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though there shouldn't be a difference, it might be easier to understand by

if (next <= end && next < seq.length && isLowerSurrogate(seq(next))) {

?


There shouldn't be a difference because we already have a check prior to this line that end <= seq.length which entails next < end <= seq.length

if (start < 0 || count < 0 || end > seq.length || index < start

Copy link
Member

@ekrich ekrich Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to check Scala.js code too as most of this was ported from Scala.js. Edit: I checked and it works as expected. It is a good idea to keep that code in sync with Scala.js if able.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. I also checked the Scala.js's implementation and it seems they don't have an implementation Character#offsetByCodePoints and there's only a String.offsetByCodePoints.

It might be good to sync after we implement Character.offsetByCodePoints on scala.js side 👍

@WojciechMazur WojciechMazur merged commit e51e4e7 into scala-native:main Sep 6, 2023
80 checks passed
@tanishiking tanishiking deleted the fix-offset-by-codepoints branch September 7, 2023 04:45
WojciechMazur pushed a commit to WojciechMazur/scala-native that referenced this pull request Oct 13, 2023
)

Previously, String.offsetByCodePoints had an error
when the offsetByCodePoints ends with the unpaired higher surrogate.

This commit fix the bound check in the offsetByCodePoints not to
check the next index is the lower surrogate after the higher surrogate.

(cherry picked from commit e51e4e7)
WojciechMazur pushed a commit that referenced this pull request Oct 13, 2023
Previously, String.offsetByCodePoints had an error
when the offsetByCodePoints ends with the unpaired higher surrogate.

This commit fix the bound check in the offsetByCodePoints not to
check the next index is the lower surrogate after the higher surrogate.

(cherry picked from commit e51e4e7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

javalib String.offsetByCodePoints throws if the final character in the string is an unpaired high surrogate
3 participants