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.
In this repository29 other files it gives its agents.
- algorithmic-complexity-attacksSkill
- cache-stampedeSkill
- clocks-and-timeSkill
- code-hotspotsSkill
- connascenceSkill
- connection-poolingSkill
- consistent-hashingSkill
- coordinated-omissionSkill
- cyclomatic-complexitySkill
- database-queuesSkill
- dependency-confusionSkill
- expand-contractSkill
- floating-pointSkill
- hyrums-lawSkill
- index-designSkill
- isolation-anomaliesSkill
- keyset-paginationSkill
- metamorphic-testingSkill
- module-depthSkill
- mutation-testingSkill
- mvcc-and-bloatSkill
- parse-dont-validateSkill
- query-plansSkill
- queueing-and-utilizationSkill
- retry-amplificationSkill
- start-hereSkill
- tail-latencySkill
- universal-scalability-lawSkill
- write-amplificationSkill
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
- Distinctive: line 70
Never compare unnormalized strings for equality or uniqueness.
- Distinctive: line 97
Never use locale-dependent case conversion for protocol tokens, filenames, or keys.
- Very specific: line 118
Do not validate emails with a regex or normalize the local part.
≈ “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.
Files
Unicode pitfalls
Length has three meanings
For "👨👩👧👦" (family emoji):
| Measure | Value | Use for |
| Bytes (UTF-8) | 25 | Storage limits, network buffers |
| Code points | 7 | Almost never |
| Grapheme clusters | 1 | Anything 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| Language | Grapheme-aware |
| Python | regex module, \X |
| JS | Intl.Segmenter(locale, {granularity:'grapheme'}) |
| Rust | unicode-segmentation crate |
| Swift | String.count is already grapheme-based |
| Go | rivo/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| Form | Meaning | Use |
| NFC | Composed. Shortest form. | Default for storage and comparison |
| NFD | Decomposed | Sorting, accent stripping |
| NFKC | Composed + compatibility folding ("fi"→"fi", "①"→"1") | Identifiers, search |
| NFKD | Decomposed + compatibility | Search indexing |
Rules:
- Normalize to NFC at the input boundary, once. Store normalized.
- Never compare unnormalized strings for equality or uniqueness.
- 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" correctlyThe 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, orToLowerInvariant()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. Useencoding='utf-8', and setPYTHONUTF8=1. - UTF-8 everywhere, including the DB (
utf8mb4in MySQL, notutf8, which is 3-byte and cannot store emoji), the connection charset, and the HTTPContent-Type. - Handle the BOM. Excel writes UTF-8 with a BOM. Read with
utf-8-sigto strip it. - Decide the error policy explicitly:
strictfor trusted data,replacefor 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
- Truncation and slicing on grapheme boundaries, not indexes
- NFC normalization at every input boundary
casefold()for comparison, never locale-dependentlower()- Length limits stated as bytes or graphemes, and enforced as the same one
- Explicit
utf-8on every file, socket, and DB connection utf8mb4in MySQL- Zero-width and bidi characters rejected in identifiers
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
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.

