feat(libSceIcu): implement the string and UTF-8 character iterators #11136

Merged
mantavisual merged 36 commits from upstream-pr/11136 into main 2026-11-04 04:49:13 +00:00
mantavisual commented 2026-10-25 19:23:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mantavisual on 2026-10-25T19:23:07Z.


What

Implements uiter_setString and uiter_setUTF8, two exports that firmware 13.42's libSceIcu has (BunnyFl's export list). They fill a caller-owned UCharIterator, ICU's C character iterator, which callers then drive through its function pointers: getIndex, move, hasNext, hasPrevious, current, next, previous, getState and setState. 13.42 also exports ucol_strcollIter, which consumes such iterators.

The code is a port of ICU 78.1's uiter.cpp: the no-op, char16_t string and UTF-8 iterators. They are installed into the public 112-byte UCharIterator layout from uiter.h. The callbacks use the guest ABI, so guest code can call them, and they carry the _nid_no_patch suffix because they are helpers, not exports.

  • Setup. A non-null text with a length of at least −1 installs the string or UTF-8 iterator. −1 means NUL-terminated, measured with u_strlen's or strlen's 32-bit result. Anything else installs the no-op iterator: index 0, U_SENTINEL for code units, UITER_NO_STATE, and setState reports U_UNSUPPORTED_ERROR. A null iterator is ignored.
  • String iterator. Index arithmetic wraps as in ICU's UPRV_NO_SANITIZE_UNDEFINED functions. move clamps to start/limit, an unknown origin returns −1, the state is the index, and setState checks the error, the iterator and the bounds in ICU's order.
  • UTF-8 iterator.
    • Its UTF-16 index and length are computed lazily from the bytes. A supplementary character is returned as its two surrogates, with the code point held in reservedField between them.
    • The state is byte offset << 1 | inside-pair, and setState validates it as ICU does.
    • move follows ICU's shortcuts and its UITER_UNKNOWN_INDEX results. That includes one quirk, which is kept: when an unknown-index forward move reaches the end, the index ICU has just computed is overwritten by the running position.
  • Ill-formed UTF-8 decodes to U+FFFD per maximal subpart. This is ported from U8_NEXT_OR_FFFD and from U8_PREV_OR_FFFD's utf8_prevCharSafeBody(..., -3) in utf8.h and utf_impl.cpp, with their validity bit tables.

A callback given a null iterator, and the no-op setState given a null error pointer, throw APS5_INVALID_ARG_EX, since ICU dereferences them. The string and UTF-8 setState accept a null error pointer as ICU does. The new debt entry records the port and that nothing is compared with a console.

Depends on #11132 (stacked on it). The agreed ICU order is #11013 → #10961 → … → #10967 → #11088 → #11115 → #11118 → #11122 → #11132, and this PR comes after #11132. Until those merge, the diff also shows their commits. This PR's own change is the last commit, 428dab7efa.

Tested

Windows 11 Pro 23H2, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, on #11132 (c1d9fa2f64).

  • New guest_icu_iterator, which drives every iterator only through its function pointers:
    • String iterator: fields, every origin, forward and backward walks, clamped and wrapping moves, the state round trip, and setState's error, iterator and bound checks. Also explicit lengths with embedded NULs and the empty string.
    • No-op iterator: all four ways to get one, and its callbacks.
    • UTF-8 iterator: a known string with a supplementary character, including the state inside the surrogate pair and the setState rejections.
    • UTF-8 cross-check: 8 handpicked and 3000 seeded random byte strings, biased towards lead, trail and boundary bytes. For each string, forward walks are compared with a reference decoder written independently from the Unicode Standard's well-formed byte sequence table, with U+FFFD per maximal subpart. Backward walks must give the same units in reverse. For every UTF-16 index, moving from zero and from the limit (with the index unknown) must land on the right unit, the computed index must be right, and a getState/setState round trip must restore it.
    • I checked that the test detects errors: making the E0 lead accept 80..9F trail bytes fails the forward comparison, and dropping the truncated four-byte case in the backward decoder fails the backward comparison.
  • ctest -R "guest_icu_|patched_libraries": all 8 pass. The NID-patched libSceIcu.prx builds.
  • tools/progress.py --module libSceIcu: 52 implemented, 0 stubs (+2; the callbacks are not counted). tools/check_test_registration.py build: 1347 test executables, none built and not run.
  • python3 tools/check_conventions.py --base origin/main passes. Overlap: no open PR implements or names any uiter_ function (checked 2026-10-25).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #11132
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5007893665 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11136). Originally posted by [mantavisual](https://github.com/mantavisual) on 2026-10-25T19:23:07Z. --- ### What Implements `uiter_setString` and `uiter_setUTF8`, two exports that firmware 13.42's `libSceIcu` has ([BunnyFl's export list](https://github.com/boykopovar/AnyPS5/pull/11013#issuecomment-6401310351)). They fill a caller-owned `UCharIterator`, ICU's C character iterator, which callers then drive through its function pointers: `getIndex`, `move`, `hasNext`, `hasPrevious`, `current`, `next`, `previous`, `getState` and `setState`. 13.42 also exports `ucol_strcollIter`, which consumes such iterators. The code is a port of ICU 78.1's [uiter.cpp](https://github.com/unicode-org/icu/blob/release-78.1/icu4c/source/common/uiter.cpp): the no-op, `char16_t` string and UTF-8 iterators. They are installed into the public 112-byte `UCharIterator` layout from [uiter.h](https://github.com/unicode-org/icu/blob/release-78.1/icu4c/source/common/unicode/uiter.h). The callbacks use the guest ABI, so guest code can call them, and they carry the `_nid_no_patch` suffix because they are helpers, not exports. - **Setup.** A non-null text with a length of at least −1 installs the string or UTF-8 iterator. −1 means NUL-terminated, measured with `u_strlen`'s or `strlen`'s 32-bit result. Anything else installs the no-op iterator: index 0, `U_SENTINEL` for code units, `UITER_NO_STATE`, and `setState` reports `U_UNSUPPORTED_ERROR`. A null iterator is ignored. - **String iterator.** Index arithmetic wraps as in ICU's `UPRV_NO_SANITIZE_UNDEFINED` functions. `move` clamps to start/limit, an unknown origin returns −1, the state is the index, and `setState` checks the error, the iterator and the bounds in ICU's order. - **UTF-8 iterator.** - Its UTF-16 index and length are computed lazily from the bytes. A supplementary character is returned as its two surrogates, with the code point held in `reservedField` between them. - The state is `byte offset << 1 | inside-pair`, and `setState` validates it as ICU does. - `move` follows ICU's shortcuts and its `UITER_UNKNOWN_INDEX` results. That includes one quirk, which is kept: when an unknown-index forward move reaches the end, the index ICU has just computed is overwritten by the running position. - **Ill-formed UTF-8** decodes to U+FFFD per maximal subpart. This is ported from `U8_NEXT_OR_FFFD` and from `U8_PREV_OR_FFFD`'s `utf8_prevCharSafeBody(..., -3)` in [utf8.h](https://github.com/unicode-org/icu/blob/release-78.1/icu4c/source/common/unicode/utf8.h) and [utf_impl.cpp](https://github.com/unicode-org/icu/blob/release-78.1/icu4c/source/common/utf_impl.cpp), with their validity bit tables. A callback given a null iterator, and the no-op `setState` given a null error pointer, throw `APS5_INVALID_ARG_EX`, since ICU dereferences them. The string and UTF-8 `setState` accept a null error pointer as ICU does. The new debt entry records the port and that nothing is compared with a console. **Depends on #11132** (stacked on it). The [agreed ICU order](https://github.com/boykopovar/AnyPS5/pull/11088#issuecomment-6401396662) is #11013 → #10961 → … → #10967 → #11088 → #11115 → #11118 → #11122 → #11132, and this PR comes after #11132. Until those merge, the diff also shows their commits. This PR's own change is the last commit, `428dab7efa`. ### Tested Windows 11 Pro 23H2, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, on #11132 (`c1d9fa2f64`). - New `guest_icu_iterator`, which drives every iterator only through its function pointers: - **String iterator:** fields, every origin, forward and backward walks, clamped and wrapping moves, the state round trip, and `setState`'s error, iterator and bound checks. Also explicit lengths with embedded NULs and the empty string. - **No-op iterator:** all four ways to get one, and its callbacks. - **UTF-8 iterator:** a known string with a supplementary character, including the state inside the surrogate pair and the `setState` rejections. - **UTF-8 cross-check:** 8 handpicked and 3000 seeded random byte strings, biased towards lead, trail and boundary bytes. For each string, forward walks are compared with a reference decoder written independently from the Unicode Standard's well-formed byte sequence table, with U+FFFD per maximal subpart. Backward walks must give the same units in reverse. For every UTF-16 index, moving from zero and from the limit (with the index unknown) must land on the right unit, the computed index must be right, and a `getState`/`setState` round trip must restore it. - I checked that the test detects errors: making the `E0` lead accept `80..9F` trail bytes fails the forward comparison, and dropping the truncated four-byte case in the backward decoder fails the backward comparison. - `ctest -R "guest_icu_|patched_libraries"`: all 8 pass. The NID-patched `libSceIcu.prx` builds. - `tools/progress.py --module libSceIcu`: 52 implemented, 0 stubs (+2; the callbacks are not counted). `tools/check_test_registration.py build`: 1347 test executables, none built and not run. - `python3 tools/check_conventions.py --base origin/main` passes. Overlap: no open PR implements or names any `uiter_` function (checked 2026-10-25). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #11132 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 19:27:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T19:27:25Z.

Review: APPROVED


Reviewed the PR-specific iterator commit 428dab7 on its declared stack after #11132. Compared the string and UTF-8 iterator setup, callbacks, lazy UTF-16 indexing, surrogate-pair state, and malformed UTF-8 handling with ICU 78.1 uiter.cpp and uiter.h. The 112-byte guest layout, callback signatures, state/error ordering, and iterator behavior match the referenced implementation.

Local checks on this commit:

  • g++ -std=c++20 -Icore/libs -Icore/libs/prx/libc/include -fsyntax-only for Iterator.cpp and GuestIcuIterator.cpp: passed.
  • Built and ran GuestIcuIterator.cpp with Iterator.cpp directly: passed, including the seeded malformed UTF-8 and state/movement cases.
  • Repeated that executable with AddressSanitizer and UndefinedBehaviorSanitizer: passed.
  • python3 tools/check_conventions.py --base upstream/main --head HEAD: passed.

The PR reports the full ICU suite and NID-patched library build on Windows; this host run covered the new iterator test only. Hosted eligibility, conventions, intake, labels and report checks pass; product build/test jobs are skipped. The declared dependency on #11132 remains part of the agreed ICU merge order.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644873997 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11136#pullrequestreview-5644873997). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T19:27:25Z. **Review:** APPROVED --- Reviewed the PR-specific iterator commit `428dab7` on its declared stack after #11132. Compared the string and UTF-8 iterator setup, callbacks, lazy UTF-16 indexing, surrogate-pair state, and malformed UTF-8 handling with ICU 78.1 `uiter.cpp` and `uiter.h`. The 112-byte guest layout, callback signatures, state/error ordering, and iterator behavior match the referenced implementation. Local checks on this commit: - `g++ -std=c++20 -Icore/libs -Icore/libs/prx/libc/include -fsyntax-only` for `Iterator.cpp` and `GuestIcuIterator.cpp`: passed. - Built and ran `GuestIcuIterator.cpp` with `Iterator.cpp` directly: passed, including the seeded malformed UTF-8 and state/movement cases. - Repeated that executable with AddressSanitizer and UndefinedBehaviorSanitizer: passed. - `python3 tools/check_conventions.py --base upstream/main --head HEAD`: passed. The PR reports the full ICU suite and NID-patched library build on Windows; this host run covered the new iterator test only. Hosted eligibility, conventions, intake, labels and report checks pass; product build/test jobs are skipped. The declared dependency on #11132 remains part of the agreed ICU merge order.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!11136
No description provided.