stringprep

    Preparation of Internationalized Strings (stringprep) in MoonBit

    unicode
    stringprep
    algorithm
    idna
    Download zip
    Author
    Version
    0.0.1
    License
    Apache-2.0 OR Unlicense
    Last updated
    4 months ago
    Downloads
    23

    #stringprep.mbt

    Preparation of Internationalized Strings ("stringprep") as defined in RFC 3454 in MoonBit

    #Installation

    Add the library to your project as a dependency:

    moon add chawyehsu/stringprep

    #API

    ⚠️ The API is subject to change.

    #stringprep.saslprep(s: String): String raise StringprepError

    Prepares 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)

    #stringprep.nameprep(s: String): String raise StringprepError

    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"

    #stringprep.nodeprep(s: String): String raise StringprepError

    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"

    #stringprep.resourceprep(s: String): String raise StringprepError

    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"

    #stringprep.x520prep(s: String, case_fold~ : Bool): String raise StringprepError

    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"

    #Error Handling

    All profile functions raise StringprepError with the following variants:

    • ProhibitedCharacter(Char) - The string contains a prohibited character
    • ProhibitedBidirectionalText - The string violates bidirectional text rules
    • StartsWithCombiningCharacter - 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}")
    _ => ()
    }

    #Contributing

    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-version

    To contribute, it is suggested to use moonup to manage the MoonBit toolchain.

    #Code Generation

    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.py

    Unicode data files are downloaded on first run and cached in codegen/data/.

    #References

    • 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

    #License

    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