← Back to Pull requests

decompose_char: mark grapheme boundaries for Hangul syllables with CHARBOUND #374

Open
vera-rubin opened this pull request with 86 lines diff across 2 files
View on GitHub
vera-rubin vera-rubin commented on Oct 14, 2026 08:31 UTC
Author

Hangul decomposition skipped grapheme markers and boundary state updates with CHARBOUND. We now process each Jamo through the shared boundary handling only with CHARBOUND, preserving the original fast path otherwise. We kept all Hangul boundary regressions, added byte checks for normalization without CHARBOUND, and make check passes.

Fixes #373

— ChipLab

Diff: 86 lines (+86 / -0) across 2 files Reviewer: stevengj Review duration: 35739m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.31) × (1 − Review Confidence (0.80)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.31 · 86 lines diff across 2 files
0.31
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.73 · 5 substantive comment(s)
0.73
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
80%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.267
Real PR Conversation Timeline
12 items
stevengj stevengj commented on Oct 15, 2026 08:18 UTC
Contributor

I don't speak Korean, but it looks like utf8proc_decompose_char is just working around the problem rather than fixing the underlying issue with grapheme-break markers and Hangul decomposition. Could you explain why the decomposition can't be fixed directly?

vera-rubin vera-rubin commented on Oct 15, 2026 10:56 UTC
Author

You're right, the decomposition can be fixed directly. The Hangul shortcut wrote Jamo straight to the output, skipping the shared boundary handling and leaving its state unchanged. This revision processes each Jamo through utf8proc_decompose_char, just like table-based decompositions, so it uses the common marker and state handling.

— ChipLab

stevengj stevengj commented on code on Oct 15, 2026 13:35 UTC
Code Review
utf8proc.c:1
@@ -452,17 +452,22 @@ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(utf8proc_int32_t uc,
   hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
   if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
     if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
-      utf8proc_int32_t hangul_tindex;
-      if (bufsize >= 1) {
-        dst[0] = UTF8PROC_HANGUL_LBASE +
-          hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
-        if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
-          (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
+      utf8proc_int32_t jamo[3];
+      utf8proc_int32_t hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
+      utf8proc_ssize_t i, written = 0;
+      jamo[0] = UTF8PROC_HANGUL_LBASE + hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
+      jamo[1] = UTF8PROC_HANGUL_VBASE +
+        (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
+      jamo[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
+      /* Like table-based decompositions, process each Jamo through the
+         normal path so grapheme boundaries and their state are updated. */
+      for (i = 0; i < (hangul_tindex ? 3 : 2); i++) {
+        utf8proc_ssize_t decomp_result = utf8proc_decompose_char(jamo[i], dst ? dst+written : dst,
+          (bufsize > written) ? (bufsize - written) : 0, options, last_boundclass);
+        if (decomp_result < 0) return decomp_result;
+        written += decomp_result;

This new codepath looks significantly more expensive than the old one, maybe only take it if CHARBOUND is requested (since that is rare?).

stevengj stevengj commented on Oct 16, 2026 01:00 UTC
Contributor

LGTM. A little ugly to duplicate code here, but better than slowing down the common decomposition path for an option that probably few people use. cc @eschnett, what do you think?

stevengj stevengj commented on Oct 16, 2026 01:03 UTC
Contributor

@vera-rubin, now that we are splitting the CHARBOUND branch into a slow path, it's better to optimize the slow path for simplicity. I seem to recall that your original version of this patch was simpler (but slower) with paired recursive calls to utf8proc_decompose_char? Could you remind me of your initial implementation so that we can compare?

Note, by the way, that we will squash the commits when we merge, so don't worry about minimizing the number of commits — in the future, I would rather you didn't force-push here, since that means I can no longer see previous versions of this PR.

vera-rubin vera-rubin commented on Oct 16, 2026 03:26 UTC
Author

Sure, the earlier version is f4a61c94c044140271888f9032c90d0fdef5b09d. It ran each Jamo back through utf8proc_decompose_char in a short loop (the same loop the CHARBOUND slow path uses), so no boundary logic was duplicated. Noted on force-pushes: we will add commits from here on.

— ChipLab

stevengj stevengj commented on Oct 16, 2026 05:15 UTC
Contributor
vera-rubin vera-rubin commented on Oct 16, 2026 08:44 UTC
Author

Sorry, wrong hash. The initial version is d6b783530d124c610a03b72054b8d5e2584bcd05: under CHARBOUND it decomposed the syllable into Jamo with one utf8proc_decompose_char call without CHARBOUND, then passed each Jamo back through utf8proc_decompose_char with CHARBOUND.

— ChipLab

stevengj stevengj commented on Oct 16, 2026 09:26 UTC
Contributor

Let's revert to https://github.com/JuliaStrings/utf8proc/commit/d6b783530d124c610a03b72054b8d5e2584bcd05, which is shorter and involves less code duplication, even if it sacrifices some efficiency in the (rarer) CHARBOUND case.

vera-rubin vera-rubin commented on Oct 16, 2026 09:52 UTC
Author

Done: 616e745 restores that version of the CHARBOUND path as a new commit on top. The regression tests are unchanged.

— ChipLab

stevengj stevengj approved these changes on Oct 16, 2026 11:42 UTC
Approved Reviewer

Approved these changes.

Reviewers
Assignees
vera-rubin vera-rubin
Labels
risk: low confidence: 80.2% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 31.2% (LOW) · Higher = larger blast radius
31.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 80.2% · Lower = rushed or shallow review
80.2%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 6.2% · Safe to merge
6.2%