Bidirectional text — usually shortened to BiDi — is text that mixes two reading directions on the same line. This page explains what BiDi is, the invisible characters that steer it, and how those characters affect everything from display and search to code review and security.
What is BiDi / bidirectional text?
Most scripts run in a single direction. English and most other Latin-based languages are written left to right (LTR). Arabic, Hebrew, Persian, and several others are written right to left (RTL). BiDi is what happens when both directions appear in the same piece of text — an English sentence that quotes an Arabic phrase, or an Arabic paragraph that quotes a URL or a code identifier.
Computers handle this with the Unicode Bidirectional Algorithm, which assigns each character an implicit direction and then works out the order in which those characters are displayed. The text itself is stored in one logical order — the order in which it was typed — while the visual order on screen is rearranged so that each run stays readable. In short, logical order and display order are two different things, and BiDi is the bridge between them.
The characters that steer direction
Alongside the letters themselves, Unicode defines a small set of invisible characters that let text set or isolate direction explicitly:
- U+200E Left-to-Right Mark (LRM) and U+200F Right-to-Left Mark (RLM) — invisible markers that fix the direction of an ambiguous character, such as punctuation sitting between two runs.
- U+061C Arabic Letter Mark — a directional marker used in Arabic context.
- U+202A–U+202E — the embedding and override controls (LRE, RLE, PDF, LRO, RLO) that force a span of text to a chosen directional level.
- U+2066–U+2069 — the newer isolate controls (LRI, RLI, FSI, PDI) that contain a directional run without letting it leak into the surrounding text.
None of these characters takes up visible space. That is exactly what makes them useful for correct rendering — and equally useful for hiding things.
How direction affects display
Because the renderer is allowed to reorder characters for readability, the same sequence of code points can be shown in more than one way. A line can read one thing on screen while carrying its characters in a different order underneath. In ordinary bilingual prose this is desirable: it keeps the text natural for both scripts. But it also means that what you see is not always what the bytes say, and that gap is the property that attacks such as Trojan Source exploit.
Where BiDi text shows up in practice
- Reading and editing: mixed-direction lines need isolation marks to look right; without them, punctuation appears to jump to the wrong end of a sentence.
- Search and comparison: an invisible mark inside a string can make two apparently identical values differ, so exact-match search and deduplication can quietly miss.
- Copy-paste and data entry: directional controls travel with the text when it is copied, and can end up sitting silently inside a form field, an email address, or a filename.
- Interfaces and localization: an RTL interface that also shows LTR strings needs correct BiDi handling, or labels and numbers display in the wrong order.
- Code and security: an override inside a comment, string, or identifier can hide logic from a reviewer — the mechanism behind Trojan Source.
Why a bidirectional text detector is useful
Because the directional controls are invisible, neither an editor nor a preview will point them out. A bidirectional text detector shows where they are: it reports each directional or hidden character with its position and code point, so you can tell deliberate, correct BiDi markup apart from a stray control that has no business being there. The output is not a judgement — it is a clear map of what is actually inside the text.
Related reading
Directional controls are the raw material for Trojan Source attacks. To see how they are used to hide code, read Trojan Source Scanner and the companion page Trojan Source: When Text Order Lies. For the encoding angle, see Unicode Normalization in Plain English.