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
Fix String.offsetByCodePoints for unpaired surrogates #3471
Conversation
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))) { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍
) 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)
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)
fix #3470
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.