|
19 | 19 |
|
20 | 20 | from synapse.events import EventBase |
21 | 21 | from synapse.types import UserID |
| 22 | +from synapse.util import glob_to_regex, re_word_boundary |
22 | 23 | from synapse.util.caches.lrucache import LruCache |
23 | 24 |
|
24 | 25 | logger = logging.getLogger(__name__) |
@@ -183,7 +184,7 @@ def _contains_display_name(self, display_name: str) -> bool: |
183 | 184 | r = regex_cache.get((display_name, False, True), None) |
184 | 185 | if not r: |
185 | 186 | r1 = re.escape(display_name) |
186 | | - r1 = _re_word_boundary(r1) |
| 187 | + r1 = re_word_boundary(r1) |
187 | 188 | r = re.compile(r1, flags=re.IGNORECASE) |
188 | 189 | regex_cache[(display_name, False, True)] = r |
189 | 190 |
|
@@ -212,64 +213,14 @@ def _glob_matches(glob: str, value: str, word_boundary: bool = False) -> bool: |
212 | 213 | try: |
213 | 214 | r = regex_cache.get((glob, True, word_boundary), None) |
214 | 215 | if not r: |
215 | | - r = _glob_to_re(glob, word_boundary) |
| 216 | + r = glob_to_regex(glob, word_boundary) |
216 | 217 | regex_cache[(glob, True, word_boundary)] = r |
217 | 218 | return bool(r.search(value)) |
218 | 219 | except re.error: |
219 | 220 | logger.warning("Failed to parse glob to regex: %r", glob) |
220 | 221 | return False |
221 | 222 |
|
222 | 223 |
|
223 | | -def _glob_to_re(glob: str, word_boundary: bool) -> Pattern: |
224 | | - """Generates regex for a given glob. |
225 | | -
|
226 | | - Args: |
227 | | - glob |
228 | | - word_boundary: Whether to match against word boundaries or entire string. |
229 | | - """ |
230 | | - if IS_GLOB.search(glob): |
231 | | - r = re.escape(glob) |
232 | | - |
233 | | - r = r.replace(r"\*", ".*?") |
234 | | - r = r.replace(r"\?", ".") |
235 | | - |
236 | | - # handle [abc], [a-z] and [!a-z] style ranges. |
237 | | - r = GLOB_REGEX.sub( |
238 | | - lambda x: ( |
239 | | - "[%s%s]" % (x.group(1) and "^" or "", x.group(2).replace(r"\\\-", "-")) |
240 | | - ), |
241 | | - r, |
242 | | - ) |
243 | | - if word_boundary: |
244 | | - r = _re_word_boundary(r) |
245 | | - |
246 | | - return re.compile(r, flags=re.IGNORECASE) |
247 | | - else: |
248 | | - r = "^" + r + "$" |
249 | | - |
250 | | - return re.compile(r, flags=re.IGNORECASE) |
251 | | - elif word_boundary: |
252 | | - r = re.escape(glob) |
253 | | - r = _re_word_boundary(r) |
254 | | - |
255 | | - return re.compile(r, flags=re.IGNORECASE) |
256 | | - else: |
257 | | - r = "^" + re.escape(glob) + "$" |
258 | | - return re.compile(r, flags=re.IGNORECASE) |
259 | | - |
260 | | - |
261 | | -def _re_word_boundary(r: str) -> str: |
262 | | - """ |
263 | | - Adds word boundary characters to the start and end of an |
264 | | - expression to require that the match occur as a whole word, |
265 | | - but do so respecting the fact that strings starting or ending |
266 | | - with non-word characters will change word boundaries. |
267 | | - """ |
268 | | - # we can't use \b as it chokes on unicode. however \W seems to be okay |
269 | | - # as shorthand for [^0-9A-Za-z_]. |
270 | | - return r"(^|\W)%s(\W|$)" % (r,) |
271 | | - |
272 | | - |
273 | 224 | def _flatten_dict( |
274 | 225 | d: Union[EventBase, dict], |
275 | 226 | prefix: Optional[List[str]] = None, |
|
0 commit comments