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_offis a 4-byte little-endianu32offset to the next opcode in sequence (0 = end). C uses native-endianlong; we pin to LE for portable on-disk bytecode caches.- Payloads (strings, ranges, branch operands) are encoded inline
after the
next_offslot.
Top-level declaration order mirrors Src/pattern.c:
- Opcode constants (P_*) — pattern.c:97-127
- Macro-style accessors (P_OP / P_NEXT / etc.) — pattern.c:175-210
- Flag-bit constants (P_SIMPLE / P_HSTART / P_PURESTR) — pattern.c:216
struct patprog— zsh.h:1601- PAT_* flag constants — zsh.h:1623
- ZPC_* indexes — zsh.h:1644
- File-static globals — pattern.c file-scope
- Bytecode write helpers (
patadd,patnode,patinsert,pattail,patoptail,patcompcharsset,patcompstart) - Compiler entry points (
patcompile,patcompswitch,patcompbranch,patcomppiece,patcompnot) - Glob-flag parser (
patgetglobflags) - Range helpers (
range_type,pattern_range_to_string) - Char-decode helpers (
charref,charnext,charrefinc,charsub,metacharinc,clear_shiftstate) - Matcher entry points (
pattry,pattrylen,pattryrefs) patmatchinterpreter — pattern.c:2694patmatchrange— pattern.c:3865. The Cpattern-byte-stream walker, relocated here fromzle/compmatch.rs(the Rule C move this section used to say was pending). The earlier Rust-only entriespatmatchrange(&[char], char, igncase)andpatmatchindex(&[char], idx)were deleted as Rule B / semantic deviations; see the deletion sites.- String pre-processing (
patmungestring,patallocstr,pattrystart) - Module-loader / disable mgmt (
startpatternscope,endpatternscope,savepatterndisables,restorepatterndisables,clearpatterndisables,freepatprog,pat_enables) - (Section removed — formerly held Rust-only convenience entries
patmatch(pat, text),patmatchlen(prog, string),patrepeat( prog, s, max),mb_patmatchrange,mb_patmatchindex. All deleted:patmatchgot renamed to the C-faithful bytecode- walker name; the others had zero Rust callers + Rule S1 signature deviations.haswildsis 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
patmatchwalk. 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 tostruct rpat pattrystateat 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 };fromSrc/pattern.c:405-408. Flags passed as thepaflagsarg topatadd. - PA_
UNMETA PA_UNMETAconstant.- P_ANY
P_ANYconstant.- P_
ANYBUT P_ANYBUTconstant.- P_ANYOF
P_ANYOFconstant.- P_BACK
P_BACKconstant.- P_
BRANCH P_BRANCHconstant.- P_CLOSE
P_CLOSEconstant.- P_COUNT
P_COUNTconstant.- P_
COUNTSTART P_COUNTSTARTconstant.- P_END
P_ENDconstant.- P_
EXACTLY P_EXACTLYconstant.- P_
EXCEND P_EXCENDconstant.- P_
EXCLUDE P_EXCLUDEconstant.- P_
EXCLUDP P_EXCLUDPconstant.- P_
EXCSYNC P_EXCSYNCconstant.- P_
GFLAGS P_GFLAGSconstant.- P_
HSTART P_HSTARTconstant.- P_ISEND
P_ISENDconstant.- P_
ISSTART P_ISSTARTconstant.- P_
NOTHING P_NOTHINGconstant.- P_
NUMANY P_NUMANYconstant.- P_
NUMFROM P_NUMFROMconstant.- P_
NUMRNG P_NUMRNGconstant.- P_NUMTO
P_NUMTOconstant.- P_
ONEHASH P_ONEHASHconstant.- P_OPEN
P_OPENconstant.- P_
PURESTR P_PURESTRconstant.- P_
SIMPLE P_SIMPLEconstant.- P_STAR
P_STARconstant.- P_
TWOHASH P_TWOHASHconstant.- P_
WBRANCH P_WBRANCHconstant.- ZPC_
STRINGS - Port of
mod_export const char *zpc_strings[ZPC_COUNT]fromSrc/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 globdotsfrom thepattrystatestruct atSrc/pattern.c:1890(“Glob initial dots?”), reached through#define globdots (pattrystate.globdots)atSrc/pattern.c:1903. Set once per top-level match from the program’s flags —Src/pattern.c:2494globdots = !(patflags & PAT_NOGLD);— and forced on for the duration of an exclusion operand (Src/pattern.c:3116globdots = 1; /* OK to match . first */, restored atSrc/pattern.c:3192). - patflags
- patglobflags
- patglobflags_
orig - patinlen
- Port of file-static
int patinlenfrompattrystatestruct atSrc/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 ofpattry/pattrylen/pattryrefs(patinput - patinstart, pattern.c:2508). Read bypatmatchlen()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
patparsewe’re currently looking at. C source uses achar *cursor; Rust uses byte offset into the String. - patstart
- patstrcache
- zpc_
disables - Port of
char zpc_disables[ZPC_COUNT]fromSrc/pattern.c:268. Per-token disable byte — whenzpc_disables[i]is non-zero, the pattern token at indexi(ZPC_*) is treated as a literal, not its meta-meaning. - zpc_
special
Functions§
- P_
ISBRANCH - Port of
P_ISBRANCH()fromSrc/pattern.c:200. C macro#define P_ISBRANCH(p) ((p)->l & 0x20). - P_
ISEXCLUDE - Port of
P_ISEXCLUDE()fromSrc/pattern.c:201. C macro#define P_ISEXCLUDE(p) (((p)->l & 0x30) == 0x30). - P_
NOTDOT - Port of
P_NOTDOT()fromSrc/pattern.c:202. C macro#define P_NOTDOT(p) ((p)->l & 0x40). - charnext
- Port of
charnext()fromSrc/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 tometacharinc— same advance-by-one-codepoint logic. - charref
- Port of
charref()fromSrc/pattern.c:1909. C:wchar_t charref(char *x, char *y, int *zmb_ind)fromSrc/pattern.c:1909. Decode the char atposwithout 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&stris already UTF-8. - charrefinc
- Port of
charrefinc()fromSrc/pattern.c:1969. C:wchar_t charrefinc(char **x, char *y, int *z)— decode the character at*xand advance past it. A natively storedcharis 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()fromSrc/pattern.c:1997. C:ptrdiff_t charsub(char *x, char *y). - clear_
shiftstate - Port of
clear_shiftstate()fromSrc/pattern.c:327. C usesmbstate_t; Rustcharis already a code point, so no shift state to clear. - clearpatterndisables
- Port of
clearpatterndisables()fromSrc/pattern.c:4296. C:void clearpatterndisables(void). C body:memset(zpc_disables, 0, ZPC_COUNT)— zero every slot. - endpatternscope
- Port of
endpatternscope()fromSrc/pattern.c:4279. C:void endpatternscope(void). Pops the saved bitmap fromPATSCOPE_STACK(zpc_disables_stackin C); restoreszpc_disables[]from the bitmap ONLY whenisset(LOCALPATTERNS)per C c:4286. Called at function exit. - extract_
numeric_ ranges - !!! WARNING: RUST-ONLY HELPER !!!
- freepatprog
- Port of
freepatprog()fromSrc/pattern.c:4161. C:freepatprog(Patprog prog). Frees a Patprog. Rust’sDroponBox<patprog>handles this; the explicit fn exists for C parity (Rule A). - haswilds
- Port of
haswilds()fromSrc/pattern.c:4306. C:haswilds(char *str). - mb_
patmatchindex - Port of
mb_patmatchindex()fromSrc/pattern.c:3767. C:int mb_patmatchindex(char *range, wint_t ind, wint_t *chr, int *mtp)fromSrc/pattern.c:3767. The reverse ofmb_patmatchrange: given a metafied byte range and an indexindinto it, return the character (or PP_* class) at that position. - mb_
patmatchrange - Port of
mb_patmatchrange()fromSrc/pattern.c:3610. C:int mb_patmatchrange(char *range, wchar_t ch, int zmb_ind, wint_t *indptr, int *mtp)fromSrc/pattern.c:3610. Multibyte variant ofpatmatchrange: walks the metafied, PP_-encoded byte streamrangeand tests whether wide charchis in it.indptr(when Some) accumulates the running index formb_patmatchindex-side lookup;mtp(when Some) records which PP_ class fired. - metacharinc
- Port of
metacharinc()fromSrc/pattern.c:336. C:static wchar_t metacharinc(char **x). Advances*xpast 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()fromSrc/pattern.c:4171. C:int pat_enables(const char *cmd, char **patp, int enable)fromSrc/pattern.c:4171. Implementsenable -p/disable -p: with an emptypatp, prints the currently enabled (or disabled, if!enable) tokens by walkingzpc_strings[]/zpc_disables[]in lockstep. Otherwise toggles each named token’szpc_disables[i]slot, emittinginvalid pattern: NAMEfor misses. - patallocstr
- Port of
patallocstr()fromSrc/pattern.c:2132. C:char *patallocstr(Patprog prog, char *string, int stringlen, int unmetalen, int force, Patstralloc patstralloc)fromSrc/pattern.c:2132. - patcompbranch
- Port of
patcompbranch(int *flagp, int paren)fromSrc/pattern.c:942. - patcompcharsset
- Port of
patcompcharsset()fromSrc/pattern.c:464. - patcompile
- Port of
patcompile(char *exp, int inflags, char **endexp)fromSrc/pattern.c:540. - patcompnot
- Port of
patcompnot()fromSrc/pattern.c:1760. C:patcompnot(int paren, int *flagsp). - patcomppiece
- Port of
patcomppiece()fromSrc/pattern.c:1261. C:patcomppiece(int *flagp, int paren). - patcompstart
- Port of
patcompstart()fromSrc/pattern.c:517. - patcompswitch
- Port of
patcompswitch(int paren, int *flagp)fromSrc/pattern.c:765. - patgetglobflags
- Port of
patgetglobflags()fromSrc/pattern.c:1037. C:patgetglobflags(char **strp, long *assertp, int *ignore). - patmatch
- Port of
patmatch()fromSrc/pattern.c:2694. The bytecode interpreter —static int patmatch(Upat prog). - patmatchindex
- Port of
patmatchindex()fromSrc/pattern.c:4004. C:int patmatchindex(char *range, int ind, int *chr, int *mtp)fromSrc/pattern.c:4004(MULTIBYTE_SUPPORT-disabled single-byte variant). Walks a NULL-terminated, METAFIED, PP_*-encoded byte streamrangeand returns the character (or POSIX-class id) at indexind. Output: - patmatchlen
- Port of
patmatchlen()fromSrc/pattern.c:2649. C:int patmatchlen(void). - patmatchrange
- Port of
patmatchrange(char *range, int ch, int *indptr, int *mtp)fromSrc/pattern.c:3865-3995(reached through thePATMATCHRANGEmacro). Walks an encoded character-range descriptor instr(Cpattern.str byte sequence) and tests whethercfalls inside. Encoding written bycomplete.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 usesMeta(0x83); every decoder in the port agrees on 0x80, so the offset is internal. - patmungestring
- Port of
patmungestring()fromSrc/pattern.c:2080. C:void patmungestring(char **string, int *stringlen, int *unmetalenin)fromSrc/pattern.c:2080. - pattern_
range_ to_ string - Port of
pattern_range_to_string()fromSrc/pattern.c:1179. C:int pattern_range_to_string(char *rangestr, char *outstr)fromSrc/pattern.c:1179. Walks a Meta-encoded range bytestring, re-emitting the human-readable form: literal chars as-is, PP_RANGE pairs asc1-c2, POSIX classes as[:name:]. Returns the output length;outstris 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 atrange_type’s reverse lookup. - pattry
- Port of
pattry()fromSrc/pattern.c:2223. C:pattry(Patprog prog, char *string). - pattrylen
- Port of
pattrylen()fromSrc/pattern.c:2236. C:int pattrylen(Patprog prog, char *string, int len, int unmetalen, Patstralloc patstralloc, int offset)fromSrc/pattern.c:2236. - pattryrefs
- Port of
pattryrefs()fromSrc/pattern.c:2294. C:int pattryrefs(Patprog prog, char *string, int stringlen, int unmetalenin, Patstralloc patstralloc, int patoffset, int *nump, int *begp, int *endp)fromSrc/pattern.c:2294. Runsprogagainststring[0..stringlen](or whole string when stringlen=-1) atpatoffset, returning capture-group ranges. - pattrystart
- Port of
pattrystart()fromSrc/pattern.c:2063. C resets per- match state globals; Rust state is per-call so no-op. - range_
type - Port of
range_type()fromSrc/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()fromSrc/pattern.c:4258. C:void restorepatterndisables(unsigned int disables)fromSrc/pattern.c:4258. Walks the 12-slotzpc_disables[]array, setting each slot’s byte from the bitmask:disables & (1<<i)→ slotigets 1, else 0. - savepatterndisables
- Port of
savepatterndisables()fromSrc/pattern.c:4220. C:unsigned int savepatterndisables(void)fromSrc/pattern.c:4220. - startpatternscope
- Port of
startpatternscope()fromSrc/pattern.c:4241. C:void startpatternscope(void). Pushes a frame ontoPATSCOPE_STACK(zpc_disables_stackin C) carrying the currentzpc_disables[]state as asavepatterndisables()u32 bitmap. Called at function entry;endpatternscopepops it.