agentleFS
auralshinauralshin/coding-skills/skills/unicode-pitfalls/SKILL.md

unicode-pitfalls

Skill0 starsChanged 3 months agoMITView on GitHub

This skill should be used when the user asks "why is this string length wrong", "how do I truncate text safely", "why do these identical strings not match", "how do I compare text case-insensitively", "why does emoji break my column limit", or is handling normalization, encodings, grapheme clusters, or homoglyph attacks.

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 2 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Never compare unnormalized strings for equality or uniqueness.

    line 70
  2. Distinctive:

    Never use locale-dependent case conversion for protocol tokens, filenames, or keys.

    line 97
  3. Very specific:

    Do not validate emails with a regex or normalize the local part.

    line 118

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Unicode pitfalls

Length has three meanings

For "👨‍👩‍👧‍👦" (family emoji):

MeasureValueUse for
Bytes (UTF-8)25Storage limits, network buffers
Code points7Almost never
Grapheme clusters1Anything a user sees or counts

len() in Python and .length in JS give code points (JS gives UTF-16 units, so emoji count as 2). Neither matches what a user calls a character.

import regex
len(regex.findall(r'\X', s))     # grapheme clusters
LanguageGrapheme-aware
Pythonregex module, \X
JSIntl.Segmenter(locale, {granularity:'grapheme'})
Rustunicode-segmentation crate
SwiftString.count is already grapheme-based
Gorivo/uniseg

Never slice by index

s[:20] can cut a multi-byte character in half, split an emoji from its modifier, or separate a base character from its combining accent. Result is mojibake or a different character.

Truncate on grapheme boundaries:

import regex
def truncate(s, n):
    g = regex.findall(r'\X', s)
    return ''.join(g[:n]) + ('…' if len(g) > n else '')

For a byte limit (DB column, protocol field), cut on a character boundary at or below the limit, not at the exact byte.

Normalization

The same visible text has multiple valid encodings. "é" is either U+00E9 (composed) or U+0065 U+0301 (e + combining accent). They render identically and compare unequal.

import unicodedata
a = "café"                    # composed
b = "café"              # decomposed
a == b                        # False
unicodedata.normalize('NFC', a) == unicodedata.normalize('NFC', b)   # True
FormMeaningUse
NFCComposed. Shortest form.Default for storage and comparison
NFDDecomposedSorting, accent stripping
NFKCComposed + compatibility folding ("fi"→"fi", "①"→"1")Identifiers, search
NFKDDecomposed + compatibilitySearch indexing

Rules:

  1. Normalize to NFC at the input boundary, once. Store normalized.
  2. Never compare unnormalized strings for equality or uniqueness.
  3. macOS filesystems produce NFD while most other sources produce NFC. A file synced between systems then has two names that look identical.

NFKC loses information ("²" becomes "2"), so use it for matching, not for storage of display text.

Case folding, not uppercasing

upper() is for display. casefold() is for comparison.

"ß".upper()      # 'SS'
"ß".lower()      # 'ß'   round trip is lossy
"ß".casefold()   # 'ss'  matches "SS" correctly

The Turkish dotless i is the classic production bug:

"I".lower()                        # 'i'   in most locales
# in tr_TR locale, "I".lower() is 'ı' (dotless)

Any code doing s.lower() == "identifier" under a Turkish locale compares wrong. This has broken installers, config parsers, and auth checks.

Rules:

  • Compare with casefold() (Python), toLowerCase() with an explicit 'en-US' locale in JS, or ToLowerInvariant() in .NET.
  • Never use locale-dependent case conversion for protocol tokens, filenames, or keys.
  • Normalize before folding: unicodedata.normalize('NFC', s).casefold().

Homoglyphs and identifiers

"раypal.com" with Cyrillic "р" and "а" looks identical to "paypal.com". For anything that identifies a user or resource:

  • Apply NFKC.
  • Restrict to a script allowlist, or reject mixed-script strings.
  • Reject zero-width characters (U+200B-200D, U+FEFF) and bidi controls (U+202A-202E, U+2066-2069). Bidi controls can make displayed source code differ from what compiles (the "Trojan Source" attack).
  • Compare against existing entries after normalization plus confusable-skeleton mapping (UTS 39).

Encoding hygiene

  • Always specify the encoding. open(path) uses a platform default that differs between machines. Use encoding='utf-8', and set PYTHONUTF8=1.
  • UTF-8 everywhere, including the DB (utf8mb4 in MySQL, not utf8, which is 3-byte and cannot store emoji), the connection charset, and the HTTP Content-Type.
  • Handle the BOM. Excel writes UTF-8 with a BOM. Read with utf-8-sig to strip it.
  • Decide the error policy explicitly: strict for trusted data, replace for display, never silent.

Emails and identifiers

Do not validate emails with a regex or normalize the local part. User@example.com and user@example.com may be different mailboxes, since only the domain is case-insensitive. Lowercase the domain, leave the local part alone, and verify by sending mail.

Checklist

  1. Truncation and slicing on grapheme boundaries, not indexes
  2. NFC normalization at every input boundary
  3. casefold() for comparison, never locale-dependent lower()
  4. Length limits stated as bytes or graphemes, and enforced as the same one
  5. Explicit utf-8 on every file, socket, and DB connection
  6. utf8mb4 in MySQL
  7. Zero-width and bidi characters rejected in identifiers

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.