namecode 0.1.0

Encode Unicode strings as valid programming language identifiers
Documentation
  • Coverage
  • 100%
    11 out of 11 items documented1 out of 3 items with examples
  • Size
  • Source code size: 72.75 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.27 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • akesling

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