Sign in

    u8str

    UTF-8 String and its functionalities.

    utf-8
    string
    Download zip
    Author
    Version
    0.1.3
    License
    GPL-3.0-or-later
    Last updated
    last year
    Downloads
    1K

    #AdUhTkJm/u8str

    As its name suggests, this is an UTF-8 string library.

    You can use the [:] and [] to manipulate bytes directly. These operations do not check whether the bytes are valid character sequences.

    For example:

    let u8 = U8Str::from("你"); // U+4F60 => 0xE461A0
    u8[2] = 0x93; // 0xE46193 => U+4F53
    inspect(u8.char(0), content="体");
    inspect(u8[0], content="\{0xe4}");

    To obtain the characters, as shown above, you can use the char(pos) method, where pos is the byte position.

    If you want to use indices according to character position, you can directly convert it into an Array[Char] and index on that:

    let chars = (u8.out(): Array[Char])
    chars[0]

    You can also iter() over it:

    for v in U8Str::from("这是?") {
    println(v) // 这\n是\n?\n
    }

    The out() and from() methods mentioned above, along with the function to() function, allow conversion to character arrays and builtin UTF-16 strings. They are used in a way similar to that of bitviewer:

    // The return-type annotation can be omitted,
    // when the compiler can infer it from context.
    let str = (u8.out(): String);

    // Supply a value of some type to convert it to that type.
    let str = u8.to("");

    let u8 = U8Str::from("abc");