Module glob

glob
Stability: unstable — the matcher's semantics are settled and tested (the ranges and the iterative single-`*` walk landed with `issues/fixed/glob-ranges-unimplemented-and-star-exponential.md`), but the module's SHAPE is not: §4 still wants the filesystem `glob()` and a `GlobPattern.new -> Result` that reports a malformed pattern instead of accepting it and matching nothing. The second of those changes this file's only constructor, so it cannot be frozen first. — stable modules only change additively; this one may still change.

Glob pattern matching for file paths.

The vocabulary is the shell's, with / treated as a barrier:

pattern matches
* any run of bytes EXCEPT /
** any run of bytes, / included (optionally followed by /)
? exactly one byte that is not /
[abc], [a-z] one byte in the class, never /
[!…], [^…] one byte NOT in the class

A ] first in a class is a literal member and a - first or last is a literal dash, as in POSIX; an UNTERMINATED [ matches nothing. Matching is on BYTES, so a class range over non-ASCII text compares UTF-8 bytes rather than runes.

This module matches STRINGS. It does not walk the filesystem — there is no glob(pattern) returning paths yet (plans/STD_API_STABILIZATION.md §4 lists it as still open); compose fs.read_dir with glob_match for that.

Stability

unstable — the matcher's semantics are settled and tested (the ranges and the iterative single-* walk landed with issues/fixed/glob-ranges-unimplemented-and-star-exponential.md), but the module's SHAPE is not: §4 still wants the filesystem glob() and a GlobPattern.new -> Result that reports a malformed pattern instead of accepting it and matching nothing. The second of those changes this file's only constructor, so it cannot be frozen first.

Types

GlobPattern object
GlobPattern

Compiled glob pattern that can be matched against strings.

Fields

NameTypeDescription
_patternString
impl(GlobPattern, ...)
new : (GlobPattern) fn(pattern : String) -> GlobPattern

Hold a pattern for repeated matching.

It does NOT validate: a malformed pattern (an unterminated [) is accepted here and simply matches nothing later. §4 wants this to become a Result, which is why the module is unstable.

Parameters

NameTypeNotes
patternString

Returns: GlobPattern

matches : (GlobPattern) fn(self : GlobPattern, text : String) -> bool

Whether text matches this pattern — identical to glob_match(p, text) and with the same cost, since the pattern is not precompiled.

Parameters

NameTypeNotes
selfGlobPattern
textString

Returns: bool

Functions

glob_match function
fn(pattern : String, text : String) -> bool

Whether text matches pattern — the one-shot form; GlobPattern is the same matcher behind a named value.

Nothing is compiled or cached: the pattern is re-scanned on every call, so a pattern used in a loop is better held as a GlobPattern (which today costs the same, but is where a compiled representation would go). A run costs O(|pattern| · |text|) per **-free segment; see _glob_match_impl for why only ** recurses.

Parameters

NameTypeNotes
patternString
textString

Returns: bool