// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What Johab and Wansung both need to know about Hangul: how a syllable is //! its three letters, and which Compatibility Jamo each letter is. //! //! Unicode lays out the Hangul Syllables block by arithmetic -- 19 initial //! consonants, 21 vowels, and 27 final consonants or none -- so a syllable //! and its letters convert both ways without a table. The Compatibility Jamo //! are another matter: they have one code point per letter in the order KS X //! 1001 row 4 has them, which interleaves the consonants that can begin a //! syllable with the clusters that can only end one, so going between the two //! takes the two short tables here. const std = @import("std"); /// The Hangul Compatibility Jamo for each initial consonant, in the order the /// Hangul Syllables block uses, as an offset from U+3130. pub const initial_jamo = [19]u8{ 0x31, 0x32, 0x34, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, }; /// The same for each final consonant. Index 0 here is final 1: final 0 is /// "none", which has no letter. pub const final_jamo = [27]u8{ 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x44, 0x45, 0x46, 0x47, 0x48, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, }; /// The first vowel, ㅏ. The other twenty follow it in order. pub const first_vowel = 0x314f; pub const first_syllable = 0xac00; pub const last_syllable = 0xd7a3; /// A syllable as its letters. `final` is 0 for a syllable without one. pub const Letters = struct { initial: u8, vowel: u8, final: u8, }; pub fn isSyllable(code_point: u21) bool { return code_point >= first_syllable and code_point <= last_syllable; } pub fn decompose(syllable: u21) Letters { std.debug.assert(isSyllable(syllable)); const s = syllable - first_syllable; return .{ .initial = @intCast(s / 588), .vowel = @intCast(s / 28 % 21), .final = @intCast(s % 28), }; } pub fn compose(letters: Letters) u21 { std.debug.assert(letters.initial < 19 and letters.vowel < 21 and letters.final < 28); return first_syllable + @as(u21, letters.initial) * 588 + @as(u21, letters.vowel) * 28 + letters.final; } /// Which initial a Compatibility Jamo is, or null if it cannot begin a /// syllable. pub fn initialOf(jamo: u21) ?u8 { return find(&initial_jamo, jamo); } /// Which final a Compatibility Jamo is, from 1, or null if it cannot end one. pub fn finalOf(jamo: u21) ?u8 { const index = find(&final_jamo, jamo) orelse return null; return index + 1; } /// Which vowel a Compatibility Jamo is, or null if it is not one. pub fn vowelOf(jamo: u21) ?u8 { if (jamo < first_vowel or jamo > first_vowel + 20) return null; return @intCast(jamo - first_vowel); } fn find(table: []const u8, jamo: u21) ?u8 { if (jamo < 0x3131 or jamo > 0x314e) return null; const index = std.mem.findScalar(u8, table, @intCast(jamo - 0x3100)) orelse return null; return @intCast(index); } test "a syllable is its letters and back" { // 똠: ㄸ, ㅗ, ㅁ. const letters = decompose('똠'); try std.testing.expectEqual(@as(?u8, letters.initial), initialOf('ㄸ')); try std.testing.expectEqual(@as(?u8, letters.vowel), vowelOf('ㅗ')); try std.testing.expectEqual(@as(?u8, letters.final), finalOf('ㅁ')); try std.testing.expectEqual(@as(u21, '똠'), compose(letters)); } test "some consonants only begin a syllable and some only end one" { // ㄸ cannot end a syllable; ㄳ cannot begin one. try std.testing.expectEqual(@as(?u8, null), finalOf('ㄸ')); try std.testing.expectEqual(@as(?u8, null), initialOf('ㄳ')); try std.testing.expectEqual(@as(?u8, null), initialOf('ㅏ')); }