Method: rich.text.Text.highlight_regex
Calls: 2015, Exceptions: 0, Paths: 4Back
Path 1: 1595 calls (0.79)
'(?P
None (1594) 'red' (1)
'repr.' (1268) 'iso8601.' (324) '' (3)
0 (1595)
1def highlight_regex(
2 self,
3 re_highlight: str,
4 style: Optional[Union[GetStyleCallable, StyleType]] = None,
5 *,
6 style_prefix: str = "",
7 ) -> int:
8 """Highlight text with a regular expression, where group names are
9 translated to styles.
10
11 Args:
12 re_highlight (str): A regular expression.
13 style (Union[GetStyleCallable, StyleType]): Optional style to apply to whole match, or a callable
14 which accepts the matched text and returns a style. Defaults to None.
15 style_prefix (str, optional): Optional prefix to add to style group names.
16
17 Returns:
18 int: Number of regex matches
19 """
20 count = 0
21 append_span = self._spans.append
22 _Span = Span
23 plain = self.plain
24 for match in re.finditer(re_highlight, plain):
25 get_span = match.span
26 if style:
27 start, end = get_span()
28 match_style = style(plain[start:end]) if callable(style) else style
29 if match_style is not None and end > start:
30 append_span(_Span(start, end, match_style))
31
32 count += 1
33 for name in match.groupdict().keys():
34 start, end = get_span(name)
35 if start != -1 and end > start:
36 append_span(_Span(start, end, f"{style_prefix}{name}"))
37 return count
Path 2: 400 calls (0.2)
'(?P
None (400)
'repr.' (338) 'iso8601.' (27) '' (22) 'json.' (13)
1 (189) 2 (96) 4 (24) 6 (18) 3 (16) 10 (14) 5 (13) 8 (12) 7 (9) 9 (7)
1def highlight_regex(
2 self,
3 re_highlight: str,
4 style: Optional[Union[GetStyleCallable, StyleType]] = None,
5 *,
6 style_prefix: str = "",
7 ) -> int:
8 """Highlight text with a regular expression, where group names are
9 translated to styles.
10
11 Args:
12 re_highlight (str): A regular expression.
13 style (Union[GetStyleCallable, StyleType]): Optional style to apply to whole match, or a callable
14 which accepts the matched text and returns a style. Defaults to None.
15 style_prefix (str, optional): Optional prefix to add to style group names.
16
17 Returns:
18 int: Number of regex matches
19 """
20 count = 0
21 append_span = self._spans.append
22 _Span = Span
23 plain = self.plain
24 for match in re.finditer(re_highlight, plain):
25 get_span = match.span
26 if style:
27 start, end = get_span()
28 match_style = style(plain[start:end]) if callable(style) else style
29 if match_style is not None and end > start:
30 append_span(_Span(start, end, match_style))
31
32 count += 1
33 for name in match.groupdict().keys():
34 start, end = get_span(name)
35 if start != -1 and end > start:
36 append_span(_Span(start, end, f"{style_prefix}{name}"))
37 return count
Path 3: 18 calls (0.01)
'(?P
None (18)
'repr.' (18)
1 (18)
1def highlight_regex(
2 self,
3 re_highlight: str,
4 style: Optional[Union[GetStyleCallable, StyleType]] = None,
5 *,
6 style_prefix: str = "",
7 ) -> int:
8 """Highlight text with a regular expression, where group names are
9 translated to styles.
10
11 Args:
12 re_highlight (str): A regular expression.
13 style (Union[GetStyleCallable, StyleType]): Optional style to apply to whole match, or a callable
14 which accepts the matched text and returns a style. Defaults to None.
15 style_prefix (str, optional): Optional prefix to add to style group names.
16
17 Returns:
18 int: Number of regex matches
19 """
20 count = 0
21 append_span = self._spans.append
22 _Span = Span
23 plain = self.plain
24 for match in re.finditer(re_highlight, plain):
25 get_span = match.span
26 if style:
27 start, end = get_span()
28 match_style = style(plain[start:end]) if callable(style) else style
29 if match_style is not None and end > start:
30 append_span(_Span(start, end, match_style))
31
32 count += 1
33 for name in match.groupdict().keys():
34 start, end = get_span(name)
35 if start != -1 and end > start:
36 append_span(_Span(start, end, f"{style_prefix}{name}"))
37 return count
Path 4: 2 calls (0.0)
'[a|e|o]+' (1) 'CVE-\\d{4}-\\d+' (1)
'red' (1) test_highlight_regex_callable.
'' (2)
3 (1) 1 (1)
1def highlight_regex(
2 self,
3 re_highlight: str,
4 style: Optional[Union[GetStyleCallable, StyleType]] = None,
5 *,
6 style_prefix: str = "",
7 ) -> int:
8 """Highlight text with a regular expression, where group names are
9 translated to styles.
10
11 Args:
12 re_highlight (str): A regular expression.
13 style (Union[GetStyleCallable, StyleType]): Optional style to apply to whole match, or a callable
14 which accepts the matched text and returns a style. Defaults to None.
15 style_prefix (str, optional): Optional prefix to add to style group names.
16
17 Returns:
18 int: Number of regex matches
19 """
20 count = 0
21 append_span = self._spans.append
22 _Span = Span
23 plain = self.plain
24 for match in re.finditer(re_highlight, plain):
25 get_span = match.span
26 if style:
27 start, end = get_span()
28 match_style = style(plain[start:end]) if callable(style) else style
29 if match_style is not None and end > start:
30 append_span(_Span(start, end, match_style))
31
32 count += 1
33 for name in match.groupdict().keys():
34 start, end = get_span(name)
35 if start != -1 and end > start:
36 append_span(_Span(start, end, f"{style_prefix}{name}"))
37 return count