build-a-glob-matcher / lesson-10.md
Lesson 10 · Character classes and escaping

Escaping a metacharacter

Sometimes you need to match a literal star or bracket, not use its special meaning. A backslash escapes the next character, stripping its power. Today matchOne learns to honour the escape before it interprets anything.

The goal

Make a backslash turn the following metacharacter into a literal.

Start here - the target
TO DO
Scenario: A backslash forces the next character to be literal
Givenpatterns that escape a metacharacter
WhenMatch is called against various names
Thena backslash escapes the next character: Match("\*", "*") is true and Match("\*", "a") is false
AndMatch("\?", "?") is true, Match("a\[b", "a[b") is true, and a doubled backslash Match("\\", "\") matches one literal backslash
Background

Every pattern language needs an escape hatch: a way to say “I mean a literal *, not the wildcard.” The convention is a backslash - \* matches a single literal star, \? a literal question mark, \[ a literal open bracket, and \\ a single literal backslash. The escape strips the special meaning from exactly one following character.

The check goes at the top of matchOne, before it looks for ? or [: if the token is a backslash and something follows, match the name character against that next character literally and advance by two. Putting it first is what makes \[ match a bracket instead of starting a class. The star needs no special handling here - the main scan only treats a bare * as a wildcard, and a \* reaches matchOne as an escape - so the wildcard branch never even sees it. With escaping in place, the single-segment matcher is complete; next it learns about paths.

Make it work
// the very first thing matchOne checks: a backslash escapes what follows
if pat[p] == '\\' && p+1 < len(pat) {
return pat[p+1] == c, p + 2 // match the escaped char literally, span two
}
// ... then the '?', '[', and literal cases ...
CheckpointDONE
A backslash escapes the following metacharacter to a literal. Commit and stop here.