Skip to main content

Crate namecode

Crate namecode 

Source
Expand description

Namecode: Encode Unicode strings as valid programming language identifiers.

Namecode encodes arbitrary Unicode strings into valid programming language identifiers that work across Rust, Go, JavaScript, and Python. Think “Punycode for variable names”.

§Key Properties

  • Encode/decode in O(n) time
  • Idempotent: encode(encode(x)) == encode(x)
  • Strict roundtrip: encode(decode(s)) == s for valid encodings

§Examples

use namecode::{encode, decode};

// Valid XID identifiers pass through unchanged
assert_eq!(encode("foo"), "foo");
assert_eq!(encode("café"), "café");
assert_eq!(encode("名前"), "名前");

// Non-XID characters get encoded
let encoded = encode("hello world");
assert!(encoded.starts_with("_N_"));
assert_eq!(decode(&encoded).unwrap(), "hello world");

// Roundtrip property
let original = "foo-bar";
let encoded = encode(original);
assert_eq!(decode(&encoded).unwrap(), original);

Enums§

DecodeError
Errors that can occur during Namecode decoding.

Functions§

decode
Decode a Namecode string back to Unicode.
encode
Encode a Unicode string into a valid UAX 31 identifier.
is_xid_identifier
Check if a string is a valid XID identifier per UAX 31.