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. patmatchrange — pattern.c:3865. The Cpattern-byte-stream walker, relocated here from zle/compmatch.rs (the Rule C move this section used to say was pending). The earlier Rust-only entries patmatchrange(&[char], char, igncase) and patmatchindex(&[char], idx) were deleted as Rule B / semantic deviations; see the deletion sites.
  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
globdots
Port of file-static int globdots from the pattrystate struct at Src/pattern.c:1890 (“Glob initial dots?”), reached through #define globdots (pattrystate.globdots) at Src/pattern.c:1903. Set once per top-level match from the program’s flags — Src/pattern.c:2494 globdots = !(patflags & PAT_NOGLD); — and forced on for the duration of an exclusion operand (Src/pattern.c:3116 globdots = 1; /* OK to match . first */, restored at Src/pattern.c:3192).
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
Port of P_ISBRANCH() from Src/pattern.c:200. C macro #define P_ISBRANCH(p) ((p)->l & 0x20).
P_ISEXCLUDE
Port of P_ISEXCLUDE() from Src/pattern.c:201. C macro #define P_ISEXCLUDE(p) (((p)->l & 0x30) == 0x30).
P_NOTDOT
Port of P_NOTDOT() from Src/pattern.c:202. C macro #define P_NOTDOT(p) ((p)->l & 0x40).
charnext
Port of charnext() from Src/pattern.c:1936. C: char *charnext(char *x, char *y). 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 charref() from Src/pattern.c:1909. C: 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 charrefinc() from Src/pattern.c:1969. C: wchar_t charrefinc(char **x, char *y, int *z) — decode the character at *x and advance past it. A natively stored char is already one decoded character (the byte-at-a-time view of a native multibyte character without GF_MULTIBYTE is stepped by the callers).
charsub
Port of charsub() from Src/pattern.c:1997. C: ptrdiff_t charsub(char *x, char *y).
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 clearpatterndisables() from Src/pattern.c:4296. C: void clearpatterndisables(void). C body: memset(zpc_disables, 0, ZPC_COUNT) — zero every slot.
endpatternscope
Port of endpatternscope() from Src/pattern.c:4279. C: void endpatternscope(void). 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
!!! WARNING: RUST-ONLY HELPER !!!
freepatprog
Port of freepatprog() from Src/pattern.c:4161. C: freepatprog(Patprog prog). Frees a Patprog. Rust’s Drop on Box<patprog> handles this; the explicit fn exists for C parity (Rule A).
haswilds
Port of haswilds() from Src/pattern.c:4306. C: haswilds(char *str).
mb_patmatchindex
Port of mb_patmatchindex() from Src/pattern.c:3767. C: 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
Port of mb_patmatchrange() from Src/pattern.c:3610. C: 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() from Src/pattern.c:336. C: static wchar_t metacharinc(char **x). Advances *x past one metafied / multibyte char and returns the decoded codepoint. The C body branches on:
numeric_range_contains
!!! WARNING: RUST-ONLY HELPER !!!
numeric_ranges_to_star
!!! WARNING: RUST-ONLY HELPER !!!
pat_enables
Port of pat_enables() from Src/pattern.c:4171. C: 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 patallocstr() from Src/pattern.c:2132. C: 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() from Src/pattern.c:1760. C: patcompnot(int paren, int *flagsp).
patcomppiece
Port of patcomppiece() from Src/pattern.c:1261. C: patcomppiece(int *flagp, int paren).
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() from Src/pattern.c:1037. C: patgetglobflags(char **strp, long *assertp, int *ignore).
patmatch
Port of patmatch() from Src/pattern.c:2694. The bytecode interpreter — static int patmatch(Upat prog).
patmatchindex
Port of patmatchindex() from Src/pattern.c:4004. C: 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
Port of patmatchlen() from Src/pattern.c:2649. C: int patmatchlen(void).
patmatchrange
Port of patmatchrange(char *range, int ch, int *indptr, int *mtp) from Src/pattern.c:3865-3995 (reached through the PATMATCHRANGE macro). Walks an encoded character-range descriptor in str (Cpattern.str byte sequence) and tests whether c falls inside. Encoding written by complete.rs::parse_class (c:Src/Zle/complete.c:523/539): 0x80 + PP_RANGE (=0x95): next 2 bytes are lo,hi range 0x80 + PP_* (POSIX class id): single-byte class marker plain byte: literal char (0x00-0x7F) The port’s marker base is 0x80 where C uses Meta (0x83); every decoder in the port agrees on 0x80, so the offset is internal.
patmungestring
Port of patmungestring() from Src/pattern.c:2080. C: void patmungestring(char **string, int *stringlen, int *unmetalenin) from Src/pattern.c:2080.
pattern_range_to_string
Port of pattern_range_to_string() from Src/pattern.c:1179. C: 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() from Src/pattern.c:2223. C: pattry(Patprog prog, char *string).
pattrylen
Port of pattrylen() from Src/pattern.c:2236. C: int pattrylen(Patprog prog, char *string, int len, int unmetalen, Patstralloc patstralloc, int offset) from Src/pattern.c:2236.
pattryrefs
Port of pattryrefs() from Src/pattern.c:2294. C: 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() from Src/pattern.c:1148. C: range_type(char *start, int len). Looks up the integer code for a POSIX character class name (e.g. “alpha” → 1). Returns None for unknown names.
restorepatterndisables
Port of restorepatterndisables() from Src/pattern.c:4258. C: 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 savepatterndisables() from Src/pattern.c:4220. C: unsigned int savepatterndisables(void) from Src/pattern.c:4220.
startpatternscope
Port of startpatternscope() from Src/pattern.c:4241. C: void startpatternscope(void). 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.