Simplify Hangul Jamo decomposition

The algorithm is not copy/paste from Unicode anymore, but it's easy
enough to follow the logic.
This commit is contained in:
Behdad Esfahbod
2011-07-18 18:00:40 -04:00
parent 0584fe33de
commit 615977d337

View File

@@ -132,35 +132,22 @@ decompose_hangul (gunichar s,
gsize *result_len) gsize *result_len)
{ {
gint SIndex = s - SBase; gint SIndex = s - SBase;
gint TIndex = SIndex % TCount;
/* not a hangul syllable */ if (r)
if (SIndex < 0 || SIndex >= SCount) {
r[0] = LBase + SIndex / NCount;
r[1] = VBase + (SIndex % NCount) / TCount;
}
if (TIndex)
{ {
if (r) if (r)
r[0] = s; r[2] = TBase + TIndex;
*result_len = 1; *result_len = 3;
} }
else else
{ *result_len = 2;
gunichar L = LBase + SIndex / NCount;
gunichar V = VBase + (SIndex % NCount) / TCount;
gunichar T = TBase + SIndex % TCount;
if (r)
{
r[0] = L;
r[1] = V;
}
if (T != TBase)
{
if (r)
r[2] = T;
*result_len = 3;
}
else
*result_len = 2;
}
} }
/* returns a pointer to a null-terminated UTF-8 string */ /* returns a pointer to a null-terminated UTF-8 string */
@@ -536,36 +523,25 @@ decompose_hangul_step (gunichar ch,
gunichar *a, gunichar *a,
gunichar *b) gunichar *b)
{ {
gint SIndex; gint SIndex, TIndex;
gunichar L, V, T;
SIndex = ch - SBase; if (ch < SBase || ch >= SBase + SCount)
if (SIndex < 0 || SIndex >= SCount)
return FALSE; /* not a hangul syllable */ return FALSE; /* not a hangul syllable */
L = LBase + SIndex / NCount; SIndex = ch - SBase;
V = VBase + (SIndex % NCount) / TCount; TIndex = SIndex % TCount;
T = TBase + SIndex % TCount;
if (T != TBase) if (TIndex)
{ {
gint LIndex, VIndex;
gunichar LV;
/* split LVT -> LV,T */ /* split LVT -> LV,T */
LIndex = L - LBase; *a = ch - TIndex;
VIndex = V - VBase; *b = TBase + TIndex;
LV = SBase + (LIndex * VCount + VIndex) * TCount;
*a = LV;
*b = T;
} }
else else
{ {
/* split LV -> L,V */ /* split LV -> L,V */
*a = L; *a = LBase + SIndex / NCount;
*b = V; *b = VBase + (SIndex % NCount) / TCount;
} }
return TRUE; return TRUE;