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:2694- (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 inzle/compmatch.rs::patmatchrangeis the production matcher pending a Rule C relocation to pattern.rs.) - 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
- 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 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)fromSrc/pattern.c:1935. 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
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
wchar_t charrefinc(char **x, char *y, int *z)fromSrc/pattern.c:1964. Decode + advance: delegates to thecharref-then-len_utf8-step pattern. The C body’s Meta / mbrtowc / zshtoken triple collapses to onechars().next()call followed by a byte-count step. - charsub
- Port of
ptrdiff_t charsub(char *x, char *y)fromSrc/pattern.c:1997. - 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
void clearpatterndisables(void)fromSrc/pattern.c:4296. C body:memset(zpc_disables, 0, ZPC_COUNT)— zero every slot. - endpatternscope
- Port of
void endpatternscope(void)fromSrc/pattern.c:4279. 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 - Extract all
<N-M>/<N->/<-M>/<->ranges from a glob pattern. Returns(start, end, lo, hi)tuples —start/endare byte offsets of</ past>,lo/hiare bounds (None= unbounded on that side). - freepatprog
- Port of
freepatprog(Patprog prog)fromSrc/pattern.c:4161. Frees a Patprog. Rust’sDroponBox<patprog>handles this; the explicit fn exists for C parity (Rule A). - haswilds
- Port of
haswilds(char *str)fromSrc/pattern.c:4306. - mb_
patmatchindex - Direct port of
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 - Direct port of
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(char **x)fromSrc/pattern.c:336. Advances past Port ofwchar_t metacharinc(char **x)fromSrc/pattern.c:336. Advances*xpast one metafied / multibyte char and returns the decoded codepoint. The C body branches on: - numeric_
range_ contains - Test whether
nfalls within numeric range(lo, hi). Unbounded sides always pass. - numeric_
ranges_ to_ star - Replace every
<N-M>inswith*for fallback glob expansion. - pat_
enables - Port of
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
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(int paren, int *flagsp)fromSrc/pattern.c:1760. - patcomppiece
- Port of
patcomppiece(int *flagp, int paren)fromSrc/pattern.c:1261. - patcompstart
- Port of
patcompstart()fromSrc/pattern.c:517. - patcompswitch
- Port of
patcompswitch(int paren, int *flagp)fromSrc/pattern.c:765. - patgetglobflags
- Port of
patgetglobflags(char **strp, long *assertp, int *ignore)fromSrc/pattern.c:1037. - patmatch
- patmatchindex
- Direct port of
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
- Direct port of
int patmatchlen(void)fromSrc/pattern.c:2649. - patmungestring
- Port of
void patmungestring(char **string, int *stringlen, int *unmetalenin)fromSrc/pattern.c:2080. - pattern_
range_ to_ string - Port of
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(Patprog prog, char *string)fromSrc/pattern.c:2223. - pattrylen
- Port of
int pattrylen(Patprog prog, char *string, int len, int unmetalen, Patstralloc patstralloc, int offset)fromSrc/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)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(char *start, int len)fromSrc/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)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
unsigned int savepatterndisables(void)fromSrc/pattern.c:4220. - startpatternscope
- Port of
void startpatternscope(void)fromSrc/pattern.c:4241. Pushes a frame ontoPATSCOPE_STACK(zpc_disables_stackin C) carrying the currentzpc_disables[]state as asavepatterndisables()u32 bitmap. Called at function entry;endpatternscopepops it.