Skip to content

Commit 19c36d6

Browse files
committed
Compiling regular expression at the class level.
1 parent a05d071 commit 19c36d6

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

cmd2/completion.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
class CompletionItem:
5252
"""A single completion result."""
5353

54+
_SANITIZE_RE = re.compile(r'\r\n|[\n\r\t\f\v]')
55+
5456
# The underlying object this completion represents (e.g., str, int, Path).
5557
# This is used to support argparse choices validation.
5658
value: Any = field(kw_only=False)
@@ -76,14 +78,14 @@ class CompletionItem:
7678
display_plain: str = field(init=False)
7779
display_meta_plain: str = field(init=False)
7880

79-
@staticmethod
80-
def _sanitize_display_string(val: str) -> str:
81+
@classmethod
82+
def _sanitize_display_string(cls, val: str) -> str:
8183
"""Sanitize a string for display in the completion menu.
8284
8385
This replaces whitespace characters that are rendered as
8486
control sequences (like ^J or ^I) with spaces.
8587
"""
86-
return re.sub(r'\r\n|[\n\r\t\f\v]', ' ', val)
88+
return cls._SANITIZE_RE.sub(' ', val)
8789

8890
def __post_init__(self) -> None:
8991
"""Finalize the object after initialization."""

0 commit comments

Comments
 (0)