Transliterator

Transliterator::create(string $id, int $direction = Transliterator::FORWARD): ?Transliterator — transliterator_transliterate(Transliterator|string $transliterator, string $string, int $start = 0, int $end = -1): string|false

The ICU transliteration engine: one ID chain generates slugs ("Any-Latin; Latin-ASCII; Lower(); [^a-z0-9] Remove") — "Crème Brûlée" folds to ASCII deterministically; Han-to-pinyin (Any-Latin) varies with the ICU version.

C C23

C23
#include <stdio.h>
#include <string.h>

/* Decode one UTF-8 code point; returns its byte length (0 at end of input) */
static size_t decode_utf8(const unsigned char *s, size_t n, unsigned *cp) {
    if (n == 0) return 0;
    if (s[0] < 0x80) { *cp = s[0]; return 1; }
    if ((s[0] & 0xE0) == 0xC0 && n >= 2) { *cp = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F); return 2; }
    if ((s[0] & 0xF0) == 0xE0 && n >= 3) { *cp = ((s[0] & 0x0F) << 12) | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F); return 3; }
    if ((s[0] & 0xF8) == 0xF0 && n >= 4) { *cp = ((s[0] & 0x07) << 18) | ((s[1] & 0x3F) << 12) | ((s[2] & 0x3F) << 6) | (s[3] & 0x3F); return 4; }
    *cp = 0xFFFD; return 1;
}

/* Hand-fold the accented Latin-1 letters of the demo strings */
static char fold(unsigned cp) {
    switch (cp) {
    case 0x00E0: case 0x00E2: case 0x00E4: return 'a';
    case 0x00E9: case 0x00E8: case 0x00EA: case 0x00EB: return 'e';
    case 0x00EC: case 0x00EE: case 0x00EF: return 'i';
    case 0x00F4: case 0x00F6: return 'o';
    case 0x00F9: case 0x00FB: case 0x00FC: return 'u';
    case 0x00E7: return 'c';
    case 0x00F1: return 'n';
    default: return 0;
    }
}

/* No stdlib transliteration: fold accents, keep the rest (demo stays ASCII) */
static void fold_string(const char *s, char *out, size_t cap) {
    size_t n = strlen(s), i = 0, fn = 0;
    while (i < n && fn < cap - 1) {
        unsigned cp;
        i += decode_utf8((const unsigned char *)s + i, n - i, &cp);
        char f = fold(cp);
        out[fn++] = f != 0 ? f : (char)cp;
    }
    out[fn] = '\0';
}

int main(void) {
    char buf[256];
    fold_string("Cr\xC3\xA8" "me Br\xC3\xBB" "l\xC3\xA9" "e", buf, sizeof buf); /* Crème Brûlée */
    printf("%s\n", buf);
    fold_string("Cr\xC3\xA8" "me Br\xC3\xBB" "l\xC3\xA9" "e & More!", buf, sizeof buf);
    for (const char *p = buf; *p; p++) { /* slug chain: lowercase, keep [a-z0-9] */
        char ch = *p;
        if (ch >= 'A' && ch <= 'Z') ch = (char)(ch - 'A' + 'a');
        if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) putchar(ch);
    }
    putchar('\n');
    return 0;
}
Output
Creme Brulee
cremebruleemore

C++ C++26

C++26
#include <cstdio>
#include <string>

// Hand-fold the accented Latin-1 letters of the demo strings
static char fold(unsigned cp) {
    switch (cp) {
    case 0x00E0: case 0x00E2: case 0x00E4: return 'a';
    case 0x00E9: case 0x00E8: case 0x00EA: case 0x00EB: return 'e';
    case 0x00EC: case 0x00EE: case 0x00EF: return 'i';
    case 0x00F4: case 0x00F6: return 'o';
    case 0x00F9: case 0x00FB: case 0x00FC: return 'u';
    case 0x00E7: return 'c';
    case 0x00F1: return 'n';
    default: return 0;
    }
}

// No stdlib transliteration: walk UTF-8, fold accents (demo stays ASCII)
static std::string deascii(const std::string &s) {
    std::string out;
    for (size_t i = 0; i < s.size(); ) {
        unsigned char lead = s[i];
        size_t len = lead < 0x80 ? 1 : (lead & 0xE0) == 0xC0 ? 2 : (lead & 0xF0) == 0xE0 ? 3 : 4;
        unsigned cp = lead;
        if (len == 2) cp = ((lead & 0x1F) << 6) | (unsigned char)s[i + 1];
        if (len == 3) cp = ((lead & 0x0F) << 12) | ((unsigned char)s[i + 1] << 6) | (unsigned char)s[i + 2];
        char f = fold(cp);
        out += f != 0 ? std::string(1, f) : s.substr(i, len);
        i += len;
    }
    return out;
}

int main() {
    std::string folded = deascii("Cr\xC3\xA8" "me Br\xC3\xBB" "l\xC3\xA9" "e"); // Crème Brûlée
    std::printf("%s\n", folded.c_str());
    for (char ch : deascii("Cr\xC3\xA8" "me Br\xC3\xBB" "l\xC3\xA9" "e & More!")) { // slug chain
        if (ch >= 'A' && ch <= 'Z') ch = (char)(ch - 'A' + 'a');
        if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) std::putchar(ch);
    }
    std::printf("\n");
    return 0;
}
Output
Creme Brulee
cremebruleemore

C# v11.0.100

v11.0.100
using System.Globalization;
using System.Text;

// hand-rolled Latin-ASCII: decompose (FormD), drop nonspacing marks, recompose (FormC)
static string Deascii(string s)
{
    var sb = new StringBuilder();
    foreach (var c in s.Normalize(NormalizationForm.FormD))
    {
        if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
        {
            sb.Append(c);
        }
    }
    return sb.ToString().Normalize(NormalizationForm.FormC);
}

Console.WriteLine(Deascii("Crème Brûlée"));
var slug = new string([.. Deascii("Crème Brûlée & More!").ToLowerInvariant()
    .Where(c => c is >= 'a' and <= 'z' or >= '0' and <= '9')]);
Console.WriteLine(slug);
Output
Creme Brulee
cremebruleemore

Go v1.27.4

v1.27.4
package main

import "fmt"

// the stdlib has no transliteration — hand-fold the accented Latin-1 letters we need
var fold = map[rune]rune{
	'à': 'a', 'â': 'a', 'ä': 'a', 'é': 'e', 'è': 'e', 'ê': 'e', 'ë': 'e',
	'î': 'i', 'ï': 'i', 'ô': 'o', 'ö': 'o', 'ù': 'u', 'û': 'u', 'ü': 'u',
	'ç': 'c', 'ñ': 'n',
}

func deascii(s string) string {
	out := make([]rune, 0, len(s))
	for _, r := range s {
		if f, ok := fold[r]; ok {
			out = append(out, f)
		} else {
			out = append(out, r)
		}
	}
	return string(out)
}

func main() {
	fmt.Println(deascii("Crème Brûlée"))
	slug := make([]rune, 0)
	for _, r := range deascii("Crème Brûlée & More!") {
		if r >= 'A' && r <= 'Z' {
			r += 'a' - 'A'
		}
		if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
			slug = append(slug, r)
		}
	}
	fmt.Println(string(slug))
}
Output
Creme Brulee
cremebruleemore

Java v27.0.1

v27.0.1
import java.text.Normalizer;
import java.util.Locale;

public class Main {
    // stdlib stand-in for Latin-ASCII: decompose (NFD), drop nonspacing marks, recompose (NFC)
    static String deascii(String s) {
        String nfd = Normalizer.normalize(s, Normalizer.Form.NFD);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nfd.length(); ) {
            int cp = nfd.codePointAt(i);
            if (Character.getType(cp) != Character.NON_SPACING_MARK) {
                sb.appendCodePoint(cp);
            }
            i += Character.charCount(cp);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(deascii("Crème Brûlée"));
        String folded = deascii("Crème Brûlée & More!").toLowerCase(Locale.ROOT);
        StringBuilder slug = new StringBuilder();
        for (int i = 0; i < folded.length(); i++) {
            char c = folded.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
                slug.append(c);
            }
        }
        System.out.println(slug);
    }
}
Output
Creme Brulee
cremebruleemore

JavaScript v26.13.1

v26.13.1
// No built-in transliteration — hand-fold the accented Latin-1 letters we need
const FOLD = { "à": "a", "â": "a", "ä": "a", "é": "e", "è": "e", "ê": "e", "ë": "e",
  "î": "i", "ï": "i", "ô": "o", "ö": "o", "ù": "u", "û": "u", "ü": "u", "ç": "c", "ñ": "n" };
const deascii = (s) => [...s].map((ch) => FOLD[ch] ?? ch).join("");
console.log(deascii("Crème Brûlée"));
const slug = [...deascii("Crème Brûlée & More!").toLowerCase()]
  .filter((c) => /[a-z0-9]/.test(c)).join("");
console.log(slug);
Output
Creme Brulee
cremebruleemore

PHP v8.6.0

v8.6.0
// The slug-generation workhorse: fold to Latin-ASCII, lowercase, keep [a-z0-9]
echo transliterator_transliterate("Any-Latin; Latin-ASCII; Lower(); [^a-z0-9] Remove", "Crème Brûlée & More!"), "\n";
// One link of that chain: Latin-ASCII strips accents deterministically
echo transliterator_transliterate("Latin-ASCII", "Crème Brûlée"), "\n";
// Same effect via decomposition: split accents (NFD), drop them, recompose (NFC)
echo transliterator_transliterate("NFD; [:Nonspacing Mark:] Remove; NFC", "Crème Brûlée"), "\n";
// Han -> Latin pinyin via Any-Latin (ICU-version sensitive, see notes)
echo transliterator_transliterate("Any-Latin; Latin-ASCII", "中国"), "\n";
Output
cremebruleemore
Creme Brulee
Creme Brulee
zhong guo

Python v3.15.2

v3.15.2
import unicodedata

def deascii(s):
    # hand-rolled Latin-ASCII: decompose (NFD), drop combining marks (Mn), recompose (NFC)
    return "".join(c for c in unicodedata.normalize("NFD", s)
                   if unicodedata.category(c) != "Mn")

print(deascii("Crème Brûlée"))
slug = "".join(c for c in deascii("Crème Brûlée & More!").lower()
               if c.isascii() and c.isalnum())
print(slug)
Output
Creme Brulee
cremebruleemore

Rust v1.99.1

v1.99.1
use std::collections::HashMap;

// the stdlib has no transliteration — hand-fold the accented Latin-1 letters we need
fn deascii(s: &str, fold: &HashMap<char, char>) -> String {
    s.chars().map(|c| fold.get(&c).copied().unwrap_or(c)).collect()
}

fn main() {
    let mut fold = HashMap::new();
    for (from, to) in [
        ('à', 'a'), ('â', 'a'), ('ä', 'a'), ('é', 'e'), ('è', 'e'), ('ê', 'e'), ('ë', 'e'),
        ('î', 'i'), ('ï', 'i'), ('ô', 'o'), ('ö', 'o'), ('ù', 'u'), ('û', 'u'), ('ü', 'u'),
        ('ç', 'c'), ('ñ', 'n'),
    ] {
        fold.insert(from, to);
    }
    println!("{}", deascii("Crème Brûlée", &fold));
    let slug: String = deascii("Crème Brûlée & More!", &fold)
        .to_lowercase()
        .chars()
        .filter(|c| c.is_ascii_lowercase() || c.is_ascii_digit())
        .collect();
    println!("{}", slug);
}
Output
Creme Brulee
cremebruleemore

TypeScript v26.13.1

v26.13.1
// No built-in transliteration — hand-fold the accented Latin-1 letters we need
const FOLD: Record<string, string> = { "à": "a", "â": "a", "ä": "a", "é": "e", "è": "e", "ê": "e", "ë": "e",
  "î": "i", "ï": "i", "ô": "o", "ö": "o", "ù": "u", "û": "u", "ü": "u", "ç": "c", "ñ": "n" };
const deascii = (s: string): string => [...s].map((ch) => FOLD[ch] ?? ch).join("");
console.log(deascii("Crème Brûlée"));
const slug: string = [...deascii("Crème Brûlée & More!").toLowerCase()]
  .filter((c) => /[a-z0-9]/.test(c)).join("");
console.log(slug);
Output
Creme Brulee
cremebruleemore

Language differences

PHP: Transliterator wraps the ICU general transform engine — rules chain into one ID: "Any-Latin; Latin-ASCII; Lower(); [^a-z0-9] Remove" generates a slug in a single pass; Latin-ASCII strips accents from Latin letters into deterministic ASCII; "NFD; [:Nonspacing Mark:] Remove; NFC" is the equivalent decomposition route (contrast of lines 2 and 3); the procedural transliterator_transliterate() is exactly equivalent to Transliterator::create()->transliterate(); an invalid rule returns false (getErrorMessage for the cause). Requires the intl extension.

ICU sensitivity analysis (per the A26 precedent): the main demo "Crème Brûlée" → "Creme Brulee" takes Latin-1 accented input to pure-ASCII output — NFD decomposition and the Latin-ASCII mapping for the Latin block have been frozen for years and are byte-identical across ICU versions, a closed determinism loop; the slug chain output "cremebruleemore" is likewise pure deterministic ASCII. Line 4 (Han -> pinyin, "中国" -> "zhong guo") is the ICU-sensitive demo: the pinyin word list is maintained per ICU release, measured here on ICU 79.1, included only as a secondary demo and isolated in the output.

C: no transliteration in the stdlib — the example hand-decodes UTF-8 with a fixed Latin-1 accent folding table (fixed mappings, deterministic); new scripts need table expansion or ICU.

C++: same idea as C (a std::string folding-table version).

C#: string.Normalize(FormD/FormC) provides native NFC/NFD — dropping CharUnicodeInfo NonSpacingMark characters yields the Latin-ASCII equivalent (a half counterpart in the .NET stdlib).

Go: no transliteration or normalization in the stdlib — the example uses a fixed folding table; use golang.org/x/text/runes + norm (NFD, drop Mn, NFC) in production.

Java: java.text.Normalizer provides native NFD/NFC — the example removes Character.NON_SPACING_MARK after NFD and recomposes, same idiom as Python; Han transliteration needs ICU4J.

JavaScript: no built-in transliteration or normalization exposure — Intl does not surface ICU transforms; the example uses a fixed folding table; in production use string.normalize("NFD") plus a combining-mark regex (an ES standard) or a fold-to-ascii style library.

Python: unicodedata.normalize("NFD") + category(c) != "Mn" filtering + NFC recomposition — a stdlib hand-roll that equals Latin-ASCII (same idiom as the Java example); Han-to-pinyin uses the third-party unidecode (a bundled word list, stable across environments).

Rust: no transliteration in the stdlib — the example uses a fixed folding table; use the deunicode crate in production (it bundles a Han word list).

TypeScript: same as JavaScript (fixed folding table).

Determinism: the main demo input "Crème Brûlée" is Latin-1 and the output is pure ASCII, byte-identical across languages and versions; the Han line is a deliberate ICU-sensitive secondary demo — one line, flagged, carrying no assertion.