Unicode in MoonBit
Dependencies
moon add tonyfettes/ucd
moon add tonyfettes/normalization
moon add tonyfettes/punycode
moon add tonyfettes/bidi
moon add tonyfettes/idnaimport {
"tonyfettes/ucd"
"tonyfettes/normalization"
"tonyfettes/punycode"
"tonyfettes/idna"
"tonyfettes/bidi"
}let composed = @normalization.nfc("e\u{0301}") // "é"
let decomposed = @normalization.nfd("é") // "e" + combining acute
let compatible = @normalization.nfkc("fi") // "fi"
let already_nfc = @normalization.is_normalized(
composed,
@normalization.NFC,
)
let normalized = @normalization.normalize(
"text",
@normalization.NFKC,
)let encoded = try! @punycode.encode("münchen") // "mnchen-3ya"
let decoded = try! @punycode.decode("mnchen-3ya") // "münchen"
let chinese = try! @punycode.encode("中文") // "fiq228c"let ascii = try! @idna.to_ascii("münchen.de")
// "xn--mnchen-3ya.de"
let unicode = try! @idna.to_unicode("xn--mnchen-3ya.de")
// "münchen.de"
let checked = try! @idna.to_ascii(
"example.com",
use_std3_ascii_rules=true,
check_hyphens=true,
check_bidi=true,
check_joiners=true,
verify_dns_length=true,
)try @idna.to_ascii("example..com") catch {
err => println("invalid domain: \{err}")
} noraise {
ascii => println(ascii)
}let direction = @bidi.detect_direction("Hello World") // LTR
let needs_bidi = @bidi.requires_bidi("Hello \u{05E9}\u{05DC}\u{05D5}\u{05DD}")
let paragraph = @bidi.process("abc\u{05D0}\u{05D1}")
let visual = @bidi.reorder_string(paragraph)
let order = @bidi.reorder(paragraph)
let forced = @bidi.process(
"\u{05D0}\u{05D1}\u{05D2}",
direction=@bidi.Direction::LTR,
)let category = @ucd.general_category('A') // Lu
let group = category.group() // L
let can_start = @ucd.is_xid_start('A') // true
let can_continue = @ucd.is_xid_continue('0') // true
let simple = @ucd.to_simple_uppercase('a') // 'A'
let full = @ucd.to_uppercase('\u{00DF}') // "SS"
let lower = @ucd.to_lowercase('\u{0130}') // "i" + combining dot above| API | Description |
|---|---|
| general_category(Char) -> GeneralCategory | Return the two-letter Unicode General_Category value. |
| GeneralCategory::group() -> GeneralCategoryGroup | Return the one-letter category group. |
| is_xid_start(Char) -> Bool | Test the Unicode XID_Start identifier property. |
| is_xid_continue(Char) -> Bool | Test the Unicode XID_Continue identifier property. |
| to_simple_uppercase(Char) -> Char | Simple uppercase mapping. |
| to_simple_lowercase(Char) -> Char | Simple lowercase mapping. |
| to_simple_titlecase(Char) -> Char | Simple titlecase mapping. |
| to_uppercase(Char) -> String | Full uppercase mapping. |
| to_lowercase(Char) -> String | Full lowercase mapping. |
| to_titlecase(Char) -> String | Full titlecase mapping. |
| API | Description |
|---|---|
| nfd(String) -> String | Canonical decomposition. |
| nfc(String) -> String | Canonical decomposition followed by canonical composition. |
| nfkd(String) -> String | Compatibility decomposition. |
| nfkc(String) -> String | Compatibility decomposition followed by canonical composition. |
| normalize(String, NormalizationForm) -> String | Normalize with a selected form. |
| is_normalized(String, NormalizationForm) -> Bool | Check whether text is already in a selected form. |
| API | Description |
|---|---|
| encode(String) -> String raise PunycodeError | Encode Unicode text as Punycode. |
| decode(String) -> String raise PunycodeError | Decode Punycode text back to Unicode. |
| API | Description |
|---|---|
| to_ascii(String, ...) -> String raise IdnaError | Convert a domain name to ASCII form for DNS use. |
| to_unicode(String, ...) -> String raise IdnaError | Convert an ASCII or ACE domain name to Unicode form for display. |
| API | Description |
|---|---|
| detect_direction(String) -> Direction | Detect the base direction from the first strong character. |
| requires_bidi(String) -> Bool | Check whether text contains right-to-left characters. |
| process(String, direction? : Direction) -> BidiParagraph | Resolve classes and levels, optionally forcing the base direction. |
| reorder(BidiParagraph) -> Array[Int] | Return visual-order indexes. |
| reorder_string(BidiParagraph) -> String | Return visually reordered text. |
| bidi_class(Char) -> BidiClass | Return the Unicode Bidi_Class value. |
| get_mirrored(Char, Int) -> Char | Return the mirrored character at an RTL level when one exists. |
| direction_from_level(Int) -> Direction | Convert an embedding level to LTR or RTL. |
moon check
moon test
moon test -p tonyfettes/normalization
moon fmt
moon info
moon buildmoon run --target native tools/gen data
moon run --target native tools/gen tests
# Or regenerate everything in one pass:
moon run --target native tools/gen allmoon -C bidi package --listmoon run --target native tools/release -- prepare --version 0.4.0moon run --target native tools/release -- publish \
--version 0.4.0 \
--executemoon run --target native tools/release -- publish \
--version 0.4.0 \
--from normalization \
--executepub(all) enum GeneralCategory {
Lu
Ll
Lt
Lm
Lo
Mn
Mc
Me
Nd
Nl
No
Zs
Zl
Zp
Pc
Pd
Ps
Pe
Pi
Pf
Po
Sm
Sc
Sk
So
Cc
Cf
Cs
Co
Cn
} derive(Eq)impl Show for GeneralCategoryGroupfn is_xid_continue(c : Char) -> Boolfn is_xid_start(c : Char) -> Boolfn to_lowercase(c : Char) -> Stringfn to_simple_lowercase(c : Char) -> Charfn to_simple_titlecase(c : Char) -> Charfn to_simple_uppercase(c : Char) -> Charfn to_titlecase(c : Char) -> Stringfn to_uppercase(c : Char) -> StringUnicode in MoonBit
Dependencies