From 7dd967768e90abfbcced81148cd3bb9d17bc8b23 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Mon, 18 Sep 2023 17:10:02 +0200 Subject: Handle the exception that will come when codepoints < 'from' < len. Already done and tested for 'to'. --- vespajlib/src/main/java/com/yahoo/text/Text.java | 7 ++++++- vespajlib/src/test/java/com/yahoo/text/TextTestCase.java | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) 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}, ""), -- cgit v1.2.3