alhudz opened a new pull request, #1858:
URL: https://github.com/apache/commons-lang/pull/1858

   `UnicodeEscaper.toUtf16Escape(int)` is the protected hook documented since 
3.2 as returning the `\uXXXX` form. Its BMP branch is `"\\u" + hex(codePoint)`, 
and `hex()` is `Integer.toHexString`, so any code point below U+1000 comes back 
short:
   
   ```java
   new UnicodeEscaper().toUtf16Escape('A');   // \u41, expected A
   new UnicodeEscaper().toUtf16Escape('\n');  // \uA,  expected \u000A
   new UnicodeEscaper().toUtf16Escape(0);     // \u0,  expected \u0000
   ```
   
   No `\u` reader decodes those (`UnicodeUnescaper` passes `\u41` through 
unchanged), while `translate()` writes the same code point padded through 
`HEX_DIGITS`, so the hook and the translator disagree on BMP input. 
`JavaUnicodeEscaper` overrides the hook with the surrogate-pair form only and 
indexes `surrogatePair[1]` unconditionally, so `new JavaUnicodeEscaper(0, 
Integer.MAX_VALUE, true).toUtf16Escape('A')` throws 
`ArrayIndexOutOfBoundsException`.
   
   The fix left-pads the BMP hex to four digits inside `toUtf16Escape`, next to 
the surrogate-pair branch from #1726, and drops the `JavaUnicodeEscaper` 
override, which that PR made redundant: the base method now emits the same 
surrogate-pair form, so subclasses and direct callers get a well-formed escape 
for every code point. Output of `translate()`, and therefore of 
`StringEscapeUtils.escapeJava`, `escapeEcmaScript` and `escapeJson`, is byte 
for byte unchanged; `translate()` only reaches the hook for supplementary code 
points, where both forms were already identical. 
`UnicodeEscaperTest.testToUtf16EscapeBmp` and 
`JavaUnicodeEscaperTest.testToUtf16EscapeBmp` fail before the change (`\u0` and 
the `ArrayIndexOutOfBoundsException`) and pass after.
   
   Following up on the question on #1726: the Commons Text 
`UnicodeEscaper.toUtf16Escape` still has the pre-#1726 body (`"\\u" + 
hex(codePoint)` for every code point), so it has both the 
supplementary-code-point defect and this padding one; `JavaUnicodeEscaper` 
there has the same BMP `ArrayIndexOutOfBoundsException`. I will raise the same 
change on Commons Text separately.
   
   Before you push a pull request, review this list:
   
   - [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
   - [x] Read the [ASF Generative Tooling 
Guidance](https://www.apache.org/legal/generative-tooling.html) if you use 
Artificial Intelligence (AI).
   - [x] I used AI to create any part of, or all of, this pull request. Which 
AI tool was used to create this pull request, and to what extent did it 
contribute?
   - [x] Run a successful build using the default 
[Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command 
line by itself.
   - [x] Write unit tests that match behavioral changes, where the tests fail 
if the changes to the runtime are not applied. This may not always be possible, 
but it is a best practice.
   - [x] Write a pull request description that is detailed enough to understand 
what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line 
and body. Note that a maintainer may squash commits during the merge process.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to