Preparation of Internationalized Strings ("stringprep") as defined in RFC 3454 in MoonBit
Add the library to your project as a dependency:
moon add chawyehsu/stringprepPrepares a string using the SASLprep profile of the stringprep algorithm, as defined in RFC 4013.
@stringprep.saslprep("user") // "user"
@stringprep.saslprep("I\u{00AD}X") // "IX" (soft hyphen removed)
@stringprep.saslprep("a\u{00A0}b") // "a b" (non-ASCII space mapped to ASCII space)
@stringprep.saslprep("\u{2168}") // "IX" (roman numeral normalized via NFKC)Prepares a string using the Nameprep profile of the stringprep algorithm, as defined in RFC 3491. Used for internationalized domain name (IDN) processing.
@stringprep.nameprep("CAFE") // "cafe" (case folding)
@stringprep.nameprep("\u{00DF}") // "ss" (German sharp s)
@stringprep.nameprep("安室奈美恵-with-SUPER-MONKEYS") // "安室奈美恵-with-super-monkeys"Prepares a string using the Nodeprep profile of the stringprep algorithm, as defined in [RFC 3920, Appendix A]. Used for XMPP node identifier processing.
@stringprep.nodeprep("räksmörgås.josefßon.org") // "räksmörgås.josefsson.org"Prepares a string using the Resourceprep profile of the stringprep algorithm, as defined in [RFC 3920, Appendix B]. Used for XMPP resource identifier processing.
@stringprep.resourceprep("foo@bar") // "foo@bar"Prepares a string according to the procedures described in Section 7 of [ITU-T Recommendation X.520 (2019)]. Used for X.500 distinguished name processing.
@stringprep.x520prep("UPPERCASED", case_fold=true) // "uppercased"
@stringprep.x520prep("foo@bar", case_fold=true) // "foo@bar"All profile functions raise StringprepError with the following variants:
ProhibitedCharacter(Char)- The string contains a prohibited characterProhibitedBidirectionalText- The string violates bidirectional text rulesStartsWithCombiningCharacter- The string starts with a combining character (x520prep only)EmptyString- The input string is empty (x520prep only)
try @stringprep.saslprep("a\u{007F}b") catch {
StringprepError::ProhibitedCharacter(c) => println("Prohibited: \{c}")
_ => ()
}The codebase might not yet be updated to support the latest version of MoonBit
language. An explicit version of the MoonBit toolchain has been pinned in the
moonbit-version file, which is used by the moonup tool.
moonup pin toolchain-versionTo contribute, it is suggested to use moonup to manage the MoonBit toolchain.
The project uses code generation for Unicode tables:
# Generate NFKC normalization tables (Unicode 3.2)
python3 codegen/unicode_nfkc.py
# Generate RFC 3454 stringprep tables
python3 codegen/rfc3454_tables.pyUnicode data files are downloaded on first run and cached in codegen/data/.
- RFC 3454 - Preparation of Internationalized Strings ("stringprep")
- RFC 3491 - Nameprep: A Stringprep Profile for Internationalized Domain Names
- RFC 3920 - Extensible Messaging and Presence Protocol (XMPP): Core
- RFC 4013 - SASLprep: Stringprep Profile for User Names and Passwords
- ITU-T X.520 - Information technology – Open Systems Interconnection – The Directory: Selected attribute types
- Unicode 3.2.0 - Unicode Character Database
stringprep.mbt © Chawye Hsu. Licensed under either of the Apache License 2.0 or The Unlicense license at your option.
Blog · GitHub @chawyehsu · Twitter @chawyehsu