Skip to content

Add std::encoding::utf8 module #53

@mjm918

Description

@mjm918

Summary

Add UTF-8 encoding/decoding support as part of the std::encoding module.

Parent issue: #48

Functions

Function Signature Description
encode (s: string) -> bytes Convert string to UTF-8 bytes
decode (data: bytes) -> string throws DecodeError Convert UTF-8 bytes to string

Usage

use std::encoding::utf8::*;

var raw: bytes = encode("Hello 世界");
var text: string = decode(raw);

// With error handling
var text: string = decode(invalid_bytes) catch e {
    println(e.message);
    return;
} ?? "";

Implementation Notes

  • naml strings are already UTF-8 internally, so encode is essentially a view/copy
  • decode validates UTF-8 sequences and throws on invalid bytes
  • Use Rust's std::str::from_utf8 for validation

Exception

exception DecodeError {
    position: int,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions