RFR: JDK-8263261 Extend String::translateEscapes to support unicode escapes
Raffaello Giulietti
rgiulietti at openjdk.org
Thu Jan 18 19:37:27 UTC 2024
On Thu, 18 Jan 2024 18:50:56 GMT, Jim Laskey <jlaskey at openjdk.org> wrote:
> Currently String::translateEscapes does not support unicode escapes, reported as a IllegalArgumentException("Invalid escape sequence: ..."). String::translateEscapes should translate unicode escape sequences to provide full coverage,
src/java.base/share/classes/java/lang/String.java line 4274:
> 4272: break;
> 4273: case 'u':
> 4274: if (from + 4 <= length) {
Avoids a potential overflow
Suggestion:
if (from <= length - 4) {
src/java.base/share/classes/java/lang/String.java line 4281:
> 4279: } catch (NumberFormatException ex) {
> 4280: throw new IllegalArgumentException("Invalid unicode sequence: " + hex);
> 4281: }
Avoids an allocation on a valid sequence, but is perhaps slower.
Suggestion:
from += 4;
try {
ch = (char) Integer.parseInt(this, from - 4, from, 16);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException("Invalid unicode sequence: " + substring(from - 4, from));
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/17491#discussion_r1457889653
PR Review Comment: https://git.openjdk.org/jdk/pull/17491#discussion_r1457897030
More information about the core-libs-dev
mailing list