aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-09-18 23:06:36 +0200
committerGitHub <noreply@github.com>2023-09-18 23:06:36 +0200
commitea761a84ccacf2ffa85cc85e8cdc6d9212330a84 (patch)
tree9d6c13062018078c06c1e9fd00aa6f9f0e847ea2
parenteede788d1c20d2f246f44287308dad69487369ea (diff)
parent7dd967768e90abfbcced81148cd3bb9d17bc8b23 (diff)
Merge pull request #28567 from vespa-engine/balder/catch-when-from-is-within-codepoints-and-lengthv8.229.1
Handle the exception that will come when codepoints < 'from' < len. MERGEOK
-rw-r--r--vespajlib/src/main/java/com/yahoo/text/Text.java7
-rw-r--r--vespajlib/src/test/java/com/yahoo/text/TextTestCase.java1
2 files changed, 7 insertions, 1 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/text/Text.java b/vespajlib/src/main/java/com/yahoo/text/Text.java
index 2f0051d4795..a2e7a696857 100644
--- a/vespajlib/src/main/java/com/yahoo/text/Text.java
+++ b/vespajlib/src/main/java/com/yahoo/text/Text.java
@@ -186,7 +186,12 @@ public final class Text {
int len = s.length();
if ((fromCP >= len) || (fromCP >= toCP)) return "";
- int from = s.offsetByCodePoints(0, fromCP);
+ int from;
+ try {
+ from = s.offsetByCodePoints(0, fromCP);
+ } catch (IndexOutOfBoundsException e) {
+ return "";
+ }
if (from >= len) return "";
int lenCP = toCP - fromCP;
if (from + lenCP >= len) return s.substring(from);
diff --git a/vespajlib/src/test/java/com/yahoo/text/TextTestCase.java b/vespajlib/src/test/java/com/yahoo/text/TextTestCase.java
index 2639882230f..c82eba0246f 100644
--- a/vespajlib/src/test/java/com/yahoo/text/TextTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/text/TextTestCase.java
@@ -73,6 +73,7 @@ public class TextTestCase {
String withSurrogates = fromCP("abc", new int[]{0x10F000, 0x10F001, 0x10F002}, "def");
assertEquals(withSurrogates, Text.substringByCodepoints(withSurrogates, 0, 11));
assertEquals(withSurrogates, Text.substringByCodepoints(withSurrogates, 0, 20));
+ assertEquals("", Text.substringByCodepoints(withSurrogates, 10, 11));
assertEquals(fromCP("bc", new int[]{0x10F000, 0x10F001}, ""),
Text.substringByCodepoints(withSurrogates, 1, 5));
assertEquals(fromCP("", new int[]{0x10F001}, ""),