Debian Codegraph @

debian/rust-vendor/fancy-regex/docs/subroutines/2_flags.md

raw
1 ## Flags and the common misconception
2
3 When subroutines are first introduced, a very common assumption is:
4
5 > *"If I apply flags at the call site, they should affect the subroutine."*
6
7 This assumption is reasonable — many regex engines either behave this way or do not specify the behavior clearly.
8 **fancy-regex does not do this**, and the reason is central to its design.
9
10 Let's look at a concrete example.
11
12 ---
13
14 ### The pattern
15
16 ```regex
17 \A(?<word>[a-z]+)\s+(?i:\g<word>)
18 ```
19
20 ### The input
21
22 ```text
23 hello Mr
24 ```
25
26 At first glance, this pattern appears to say:
27
28 1. At the beginning of the input string
29 2. Match a word of lowercase letters and capture it as `word`
30 3. Match some whitespace
31 4. Call the subroutine `word`, but case-insensitively
32
33 Many users therefore expect this pattern to match the input above.
34
35 **It does not.**
36
37 ---
38
39 ### What actually happens
40
41 The key to understanding this behavior is that **flags belong to the subroutine definition, not the call site**.
42
43 Let's walk through the execution step by step.
44
45 ---
46
47 ### Execution trace
48
49 #### Step 1: Assert position at the beginning of the input string
50
51 * Pattern: `\A`
52 * Active flags: none
53 * Input position: 0 (start of `"hello Mr"`)
54
55 #### Step 2: Enter subroutine definition `word`
56
57 * Pattern: `[a-z]+`
58 * Active flags: none
59 * Input position: 0 (start of `"hello Mr"`)
60
61 The engine greedily matches `[a-z]+` against `"hello"`.
62 The range 0 - 5 is stored in capture group 1, whose name is `word`.
63
64 #### Step 3: Exit subroutine definition `word` and continue matching
65
66 * Pattern: `\s+`
67 * Active flags: none
68 * Input position: 5 (at the space after `hello`)
69
70 The engine matches `\s+` against `" "`.
71
72 #### Step 4: Call subroutine `word`
73
74 * Pattern: `\g<word>` -> `(?<word>[a-z]+)`
75 * Active flags: none - because the capture group definition had no flags active
76 * Input position: 6 (after `" "` at the `'M'` of `"Mr"`)
77
78 This fails immediately, because:
79
80 * The pattern is case-sensitive
81 * The first character is `'M'`, which does not fall in the range a-z.
82
83 #### Step 5: No alternatives available
84
85 * There are no alternations inside `word`
86 * There are no backtracking points before the failure
87 * The anchor prevents us from trying other starting positions in the input string
88
89 The match fails.
90
91 ---
92
93 ### Why the `i` flag did not apply
94
95 The `i` flag appears **only at the call site**:
96
97 ```regex
98 (?i:\g<word>)
99 ```
100
101 However, calling a subroutine does **not** re-evaluate or modify its definition.
102
103 The subroutine `word` was compiled once, with these properties:
104
105 * Pattern: `[a-z]+`
106 * Flags: none
107 * Capture group number: 1
108
109 When the subroutine is called, the engine:
110
111 * Enters the already-compiled definition
112 * Executes it exactly as defined
113 * Ignores any flags applied at the call site
114
115 ---
116
117 ### This is a feature, not a limitation
118
119 fancy-regex deliberately enforces this rule to guarantee that:
120
121 * A subroutine behaves the same everywhere it is used
122 * Flags cannot silently change the meaning of a reused pattern
123 * There is no "action at a distance" from call sites
124
125 If call-site flags were allowed to affect subroutines, the same subroutine could behave differently depending on where it was called - making patterns harder to reason about and easier to misuse.
126
127 ---
128
129 ### Expressing the intended behavior
130
131 If the intent is for `word` to be matched case-insensitively, the flag must be applied **at the definition**:
132
133 ```regex
134 (?i:(?<word>[a-z]+))\s+\g<word>
135 ```
136
137 or
138 ```regex
139 (?<word>(?i:[a-z]+))\s+\g<word>
140 ```
141
142 Now the subroutine is compiled with the `i` flag, and every call to it behaves consistently.
143
144 ---
145
146 ### Key takeaway
147
148 > **Subroutines in fancy-regex are compiled once, with fixed flags.**
149 > **Call sites cannot change their behavior.**
150
151 This rule enables safe reuse, predictable execution, and clear reasoning - especially in larger and more complex patterns. It also matches Oniguruma behavior, so if you plan to use fancy-regex as a memory-safe alternative, you can!