MCPcopy Index your code
hub / github.com/python/cpython / _translate

Function _translate

Lib/fnmatch.py:109–183  ·  view source on GitHub ↗
(pat, star, question_mark)

Source from the content-addressed store, hash-verified

107
108
109def _translate(pat, star, question_mark):
110 res = []
111 add = res.append
112 star_indices = []
113
114 i, n = 0, len(pat)
115 while i < n:
116 c = pat[i]
117 i = i+1
118 if c == '*':
119 # store the position of the wildcard
120 star_indices.append(len(res))
121 add(star)
122 # compress consecutive `*` into one
123 while i < n and pat[i] == '*':
124 i += 1
125 elif c == '?':
126 add(question_mark)
127 elif c == '[':
128 j = i
129 if j < n and pat[j] == '!':
130 j = j+1
131 if j < n and pat[j] == ']':
132 j = j+1
133 while j < n and pat[j] != ']':
134 j = j+1
135 if j >= n:
136 add('\\[')
137 else:
138 stuff = pat[i:j]
139 if '-' not in stuff:
140 stuff = stuff.replace('\\', r'\\')
141 else:
142 chunks = []
143 k = i+2 if pat[i] == '!' else i+1
144 while True:
145 k = pat.find('-', k, j)
146 if k < 0:
147 break
148 chunks.append(pat[i:k])
149 i = k+1
150 k = k+3
151 chunk = pat[i:j]
152 if chunk:
153 chunks.append(chunk)
154 else:
155 chunks[-1] += '-'
156 # Remove empty ranges -- invalid in RE.
157 for k in range(len(chunks)-1, 0, -1):
158 if chunks[k-1][-1] > chunks[k][0]:
159 chunks[k-1] = chunks[k-1][:-1] + chunks[k][1:]
160 del chunks[k]
161 # Escape backslashes and hyphens for set difference (--).
162 # Hyphens that create ranges shouldn't be escaped.
163 stuff = '-'.join(s.replace('\\', r'\\').replace('-', r'\-')
164 for s in chunks)
165 i = j+1
166 if not stuff:

Callers 2

translateFunction · 0.85

Calls 5

addFunction · 0.70
appendMethod · 0.45
replaceMethod · 0.45
findMethod · 0.45
joinMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…