Skip to main content

Module pattern

Module pattern 

Source
Expand description

pattern submodule. Pattern matching — port of Src/pattern.c.

This is the bytecode-based port. The C source compiles patterns into a flat char *patout buffer of packed opcodes; the matcher is an interpreter that walks the buffer using pointer arithmetic.

Rust port preserves the bytecode architecture using Vec<u8>:

  • Each opcode is 1 byte (matches C Upat::c).
  • next_off is a 4-byte little-endian u32 offset to the next opcode in sequence (0 = end). C uses native-endian long; we pin to LE for portable on-disk bytecode caches.
  • Payloads (strings, ranges, branch operands) are encoded inline after the next_off slot.

Top-level declaration order mirrors Src/pattern.c:

  1. Opcode constants (P_*) — pattern.c:97-127
  2. Macro-style accessors (P_OP / P_NEXT / etc.) — pattern.c:175-210
  3. Flag-bit constants (P_SIMPLE / P_HSTART / P_PURESTR) — pattern.c:216
  4. struct patprog — zsh.h:1601
  5. PAT_* flag constants — zsh.h:1623
  6. ZPC_* indexes — zsh.h:1644
  7. File-static globals — pattern.c file-scope
  8. Bytecode write helpers (patadd, patnode, patinsert, pattail, patoptail, patcompcharsset, patcompstart)
  9. Compiler entry points (patcompile, patcompswitch, patcompbranch, patcomppiece, patcompnot)
  10. Glob-flag parser (patgetglobflags)
  11. Range helpers (range_type, pattern_range_to_string)
  12. Char-decode helpers (charref, charnext, charrefinc, charsub, metacharinc, clear_shiftstate)
  13. Matcher entry points (pattry, pattrylen, pattryrefs)
  14. patmatch interpreter — pattern.c:2694
  15. (Section removed — formerly held Rust-only entries patmatchrange(&[char], char, igncase), patmatchindex(&[char], idx), mb_patmatchrange, mb_patmatchindex. All deleted as Rule B / semantic deviations; see comments at the deletion sites for the faithful-port plan. The Cpattern-byte-stream walker in zle/compmatch.rs::patmatchrange is the production matcher pending a Rule C relocation to pattern.rs.)
  16. String pre-processing (patmungestring, patallocstr, pattrystart)
  17. Module-loader / disable mgmt (startpatternscope, endpatternscope, savepatterndisables, restorepatterndisables, clearpatterndisables, freepatprog, pat_enables)
  18. (Section removed — formerly held Rust-only convenience entries patmatch(pat, text), patmatchlen(prog, string), patrepeat( prog, s, max), mb_patmatchrange, mb_patmatchindex. All deleted: patmatch got renamed to the C-faithful bytecode- walker name; the others had zero Rust callers + Rule S1 signature deviations. haswilds is a real C name — port lives in section 4 helpers.)

See docs/PORT.md Rules A/B/C/D/E.

Re-exports§

pub use crate::ported::zsh_h::patstralloc;
pub use crate::ported::zsh_h::Patstralloc;
pub use crate::ported::zsh_h::GF_BACKREF;
pub use crate::ported::zsh_h::GF_IGNCASE;
pub use crate::ported::zsh_h::GF_LCMATCHUC;
pub use crate::ported::zsh_h::GF_MATCHREF;
pub use crate::ported::zsh_h::GF_MULTIBYTE;
pub use crate::ported::zsh_h::PAT_ANY;
pub use crate::ported::zsh_h::PAT_FILE;
pub use crate::ported::zsh_h::PAT_FILET;
pub use crate::ported::zsh_h::PAT_HAS_EXCLUDP;
pub use crate::ported::zsh_h::PAT_HEAPDUP;
pub use crate::ported::zsh_h::PAT_LCMATCHUC;
pub use crate::ported::zsh_h::PAT_NOANCH;
pub use crate::ported::zsh_h::PAT_NOGLD;
pub use crate::ported::zsh_h::PAT_NOTEND;
pub use crate::ported::zsh_h::PAT_NOTSTART;
pub use crate::ported::zsh_h::PAT_PURES;
pub use crate::ported::zsh_h::PAT_SCAN;
pub use crate::ported::zsh_h::PAT_STATIC;
pub use crate::ported::zsh_h::PAT_ZDUP;
pub use crate::ported::zsh_h::PP_ALNUM;
pub use crate::ported::zsh_h::PP_ALPHA;
pub use crate::ported::zsh_h::PP_ASCII;
pub use crate::ported::zsh_h::PP_BLANK;
pub use crate::ported::zsh_h::PP_CNTRL;
pub use crate::ported::zsh_h::PP_DIGIT;
pub use crate::ported::zsh_h::PP_GRAPH;
pub use crate::ported::zsh_h::PP_IDENT;
pub use crate::ported::zsh_h::PP_IFS;
pub use crate::ported::zsh_h::PP_IFSSPACE;
pub use crate::ported::zsh_h::PP_INCOMPLETE;
pub use crate::ported::zsh_h::PP_INVALID;
pub use crate::ported::zsh_h::PP_LOWER;
pub use crate::ported::zsh_h::PP_PRINT;
pub use crate::ported::zsh_h::PP_PUNCT;
pub use crate::ported::zsh_h::PP_RANGE;
pub use crate::ported::zsh_h::PP_SPACE;
pub use crate::ported::zsh_h::PP_UPPER;
pub use crate::ported::zsh_h::PP_WORD;
pub use crate::ported::zsh_h::PP_XDIGIT;
pub use crate::ported::zsh_h::ZMB_INCOMPLETE;
pub use crate::ported::zsh_h::ZMB_INVALID;
pub use crate::ported::zsh_h::ZPC_SEG_COUNT;

Structs§

rpat
State accumulated during a single patmatch walk. C uses per-thread globals (patbeginp[] / patendp[] for captures); the Rust port encapsulates them in this struct passed by &mut. Rule D: this struct represents matcher-internal scratch state (analogous to struct rpat pattrystate at pattern.c:248), not a bag-of-globals from unrelated subsystems.

Constants§

NSUBEXP
Maximum captures, from pattern.c:94 NSUBEXP.
PA_NOALIGN
Port of the anonymous enum { PA_NOALIGN = 1, PA_UNMETA = 2 }; from Src/pattern.c:405-408. Flags passed as the paflags arg to patadd.
PA_UNMETA
PA_UNMETA constant.
P_ANY
P_ANY constant.
P_ANYBUT
P_ANYBUT constant.
P_ANYOF
P_ANYOF constant.
P_BACK
P_BACK constant.
P_BRANCH
P_BRANCH constant.
P_CLOSE
P_CLOSE constant.
P_COUNT
P_COUNT constant.
P_COUNTSTART
P_COUNTSTART constant.
P_END
P_END constant.
P_EXACTLY
P_EXACTLY constant.
P_EXCEND
P_EXCEND constant.
P_EXCLUDE
P_EXCLUDE constant.
P_EXCLUDP
P_EXCLUDP constant.
P_EXCSYNC
P_EXCSYNC constant.
P_GFLAGS
P_GFLAGS constant.
P_HSTART
P_HSTART constant.
P_ISEND
P_ISEND constant.
P_ISSTART
P_ISSTART constant.
P_NOTHING
P_NOTHING constant.
P_NUMANY
P_NUMANY constant.
P_NUMFROM
P_NUMFROM constant.
P_NUMRNG
P_NUMRNG constant.
P_NUMTO
P_NUMTO constant.
P_ONEHASH
P_ONEHASH constant.
P_OPEN
P_OPEN constant.
P_PURESTR
P_PURESTR constant.
P_SIMPLE
P_SIMPLE constant.
P_STAR
P_STAR constant.
P_TWOHASH
P_TWOHASH constant.
P_WBRANCH
P_WBRANCH constant.
ZPC_STRINGS
Port of mod_export const char *zpc_strings[ZPC_COUNT] from Src/pattern.c:258. Static token-name table indexed by ZPC_*; NULL entries (ZPC_NULL, ZPC_BNULLKEEP, ZPC_INPAR_PIPE, ZPC_KSHCHAR) have no user-visible name.

Statics§

errsfound
forceerrs
patflags
patglobflags
patglobflags_orig
patinlen
Port of file-static int patinlen from pattrystate struct at Src/pattern.c:1877 (accessed via #define patinlen (pattrystate.patinlen) at line 1894). Length in metafied bytes of the last successful pattry match; computed at the end of pattry/pattrylen/pattryrefs (patinput - patinstart, pattern.c:2508). Read by patmatchlen() and by the ${var//pat/repl} paramsubst pipeline that needs to know how much of the input was consumed.
patnpar
patout
patparse
patparse_off
Position within patparse we’re currently looking at. C source uses a char * cursor; Rust uses byte offset into the String.
patstart
patstrcache
zpc_disables
Port of char zpc_disables[ZPC_COUNT] from Src/pattern.c:268. Per-token disable byte — when zpc_disables[i] is non-zero, the pattern token at index i (ZPC_*) is treated as a literal, not its meta-meaning.
zpc_special

Functions§

P_ISBRANCH
P_ISBRANCH(p) macro from pattern.c:200 — (p->l & 0x20).
P_ISEXCLUDE
P_ISEXCLUDE(p) macro from pattern.c:201 — ((p->l & 0x30) == 0x30).
P_NOTDOT
P_NOTDOT(p) macro from pattern.c:202 — (p->l & 0x40).
charnext
Port of char *charnext(char *x, char *y) from Src/pattern.c:1935. C returns the pointer past the current character; the Rust port returns that position as a byte offset. Delegates to metacharinc — same advance-by-one-codepoint logic.
charref
Port of wchar_t charref(char *x, char *y, int *zmb_ind) from Src/pattern.c:1909. Decode the char at pos without advancing. UTF-32-native delegation: s.chars().next() returns the decoded codepoint; delegates to Rust’s native UTF-8 string iterator instead of the C Meta-decode + zshtoken-translate + mbrtowc state machine, which all collapse because Rust’s &str is already UTF-8.
charrefinc
Port of wchar_t charrefinc(char **x, char *y, int *z) from Src/pattern.c:1964. Decode + advance: delegates to the charref-then-len_utf8-step pattern. The C body’s Meta / mbrtowc / zshtoken triple collapses to one chars().next() call followed by a byte-count step.
charsub
Port of ptrdiff_t charsub(char *x, char *y) from Src/pattern.c:1997.
clear_shiftstate
Port of clear_shiftstate() from Src/pattern.c:327. C uses mbstate_t; Rust char is already a code point, so no shift state to clear.
clearpatterndisables
Port of void clearpatterndisables(void) from Src/pattern.c:4296. C body: memset(zpc_disables, 0, ZPC_COUNT) — zero every slot.
endpatternscope
Port of void endpatternscope(void) from Src/pattern.c:4279. Pops the saved bitmap from PATSCOPE_STACK (zpc_disables_stack in C); restores zpc_disables[] from the bitmap ONLY when isset(LOCALPATTERNS) per C c:4286. Called at function exit.
extract_numeric_ranges
Extract all <N-M> / <N-> / <-M> / <-> ranges from a glob pattern. Returns (start, end, lo, hi) tuples — start/end are byte offsets of < / past >, lo/hi are bounds (None = unbounded on that side).
freepatprog
Port of freepatprog(Patprog prog) from Src/pattern.c:4161. Frees a Patprog. Rust’s Drop on Box<patprog> handles this; the explicit fn exists for C parity (Rule A).
haswilds
Port of haswilds(char *str) from Src/pattern.c:4306.
mb_patmatchindex
Direct port of int mb_patmatchindex(char *range, wint_t ind, wint_t *chr, int *mtp) from Src/pattern.c:3767. The reverse of mb_patmatchrange: given a metafied byte range and an index ind into it, return the character (or PP_* class) at that position.
mb_patmatchrange
Direct port of int mb_patmatchrange(char *range, wchar_t ch, int zmb_ind, wint_t *indptr, int *mtp) from Src/pattern.c:3610. Multibyte variant of patmatchrange: walks the metafied, PP_-encoded byte stream range and tests whether wide char ch is in it. indptr (when Some) accumulates the running index for mb_patmatchindex-side lookup; mtp (when Some) records which PP_ class fired.
metacharinc
Port of metacharinc(char **x) from Src/pattern.c:336. Advances past Port of wchar_t metacharinc(char **x) from Src/pattern.c:336. Advances *x past one metafied / multibyte char and returns the decoded codepoint. The C body branches on:
numeric_range_contains
Test whether n falls within numeric range (lo, hi). Unbounded sides always pass.
numeric_ranges_to_star
Replace every <N-M> in s with * for fallback glob expansion.
pat_enables
Port of int pat_enables(const char *cmd, char **patp, int enable) from Src/pattern.c:4171. Implements enable -p/disable -p: with an empty patp, prints the currently enabled (or disabled, if !enable) tokens by walking zpc_strings[]/zpc_disables[] in lockstep. Otherwise toggles each named token’s zpc_disables[i] slot, emitting invalid pattern: NAME for misses.
patallocstr
Port of char *patallocstr(Patprog prog, char *string, int stringlen, int unmetalen, int force, Patstralloc patstralloc) from Src/pattern.c:2132.
patcompbranch
Port of patcompbranch(int *flagp, int paren) from Src/pattern.c:942.
patcompcharsset
Port of patcompcharsset() from Src/pattern.c:464.
patcompile
Port of patcompile(char *exp, int inflags, char **endexp) from Src/pattern.c:540.
patcompnot
Port of patcompnot(int paren, int *flagsp) from Src/pattern.c:1760.
patcomppiece
Port of patcomppiece(int *flagp, int paren) from Src/pattern.c:1261.
patcompstart
Port of patcompstart() from Src/pattern.c:517.
patcompswitch
Port of patcompswitch(int paren, int *flagp) from Src/pattern.c:765.
patgetglobflags
Port of patgetglobflags(char **strp, long *assertp, int *ignore) from Src/pattern.c:1037.
patmatch
patmatchindex
Direct port of int patmatchindex(char *range, int ind, int *chr, int *mtp) from Src/pattern.c:4004 (MULTIBYTE_SUPPORT-disabled single-byte variant). Walks a NULL-terminated, METAFIED, PP_*-encoded byte stream range and returns the character (or POSIX-class id) at index ind. Output:
patmatchlen
Direct port of int patmatchlen(void) from Src/pattern.c:2649.
patmungestring
Port of void patmungestring(char **string, int *stringlen, int *unmetalenin) from Src/pattern.c:2080.
pattern_range_to_string
Port of int pattern_range_to_string(char *rangestr, char *outstr) from Src/pattern.c:1179. Walks a Meta-encoded range bytestring, re-emitting the human-readable form: literal chars as-is, PP_RANGE pairs as c1-c2, POSIX classes as [:name:]. Returns the output length; outstr is the destination buffer (NULL = measure-only). The Rust port operates on &str (UTF-8 native) — Meta-byte decode collapses since zshrs’s pattern compiler stores raw chars, so the function effectively walks the chars and emits POSIX class names from the [i] table at range_type’s reverse lookup.
pattry
Port of pattry(Patprog prog, char *string) from Src/pattern.c:2223.
pattrylen
Port of int pattrylen(Patprog prog, char *string, int len, int unmetalen, Patstralloc patstralloc, int offset) from Src/pattern.c:2236.
pattryrefs
Port of int pattryrefs(Patprog prog, char *string, int stringlen, int unmetalenin, Patstralloc patstralloc, int patoffset, int *nump, int *begp, int *endp) from Src/pattern.c:2294. Runs prog against string[0..stringlen] (or whole string when stringlen=-1) at patoffset, returning capture-group ranges.
pattrystart
Port of pattrystart() from Src/pattern.c:2063. C resets per- match state globals; Rust state is per-call so no-op.
range_type
Port of range_type(char *start, int len) from Src/pattern.c:1148. Looks up the integer code for a POSIX character class name (e.g. “alpha” → 1). Returns None for unknown names.
restorepatterndisables
Port of void restorepatterndisables(unsigned int disables) from Src/pattern.c:4258. Walks the 12-slot zpc_disables[] array, setting each slot’s byte from the bitmask: disables & (1<<i) → slot i gets 1, else 0.
savepatterndisables
Port of unsigned int savepatterndisables(void) from Src/pattern.c:4220.
startpatternscope
Port of void startpatternscope(void) from Src/pattern.c:4241. Pushes a frame onto PATSCOPE_STACK (zpc_disables_stack in C) carrying the current zpc_disables[] state as a savepatterndisables() u32 bitmap. Called at function entry; endpatternscope pops it.

Type Aliases§

PatProgDeprecated
PatProg type alias.
Patprog
typedef struct patprog *Patprog; from zsh.h:542. typedef struct patprog *Patprog; from zsh.h:542.