aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Numerlor <[email protected]>2021-02-19 00:01:55 +0100
committerGravatar Numerlor <[email protected]>2021-02-23 03:33:09 +0100
commite607600cdd1084566319d4283bd747d772627121 (patch)
tree007d7ddfe1fed378adda4ae6844a842d9a9ba67a
parentFix docstring typos (diff)
Simplify the _split_parameters implementation
The main simplification was getting rid of keeping track of string depth which was unnecessary, as we can just always skip them as was being done for strings inside of brackets. The branching was also simplified to make sure less unnecessary checks were being done with a bit less confusing elifs.
-rw-r--r--bot/exts/info/doc/_parsing.py48
1 files changed, 18 insertions, 30 deletions
diff --git a/bot/exts/info/doc/_parsing.py b/bot/exts/info/doc/_parsing.py
index 3350aac0a..280a0c8f2 100644
--- a/bot/exts/info/doc/_parsing.py
+++ b/bot/exts/info/doc/_parsing.py
@@ -63,43 +63,31 @@ def _split_parameters(parameters_string: str) -> Iterator[str]:
last_split = 0
depth = 0
current_search: Optional[BracketPair] = None
- quote_character = None
enumerated_string = enumerate(parameters_string)
for index, character in enumerated_string:
- if quote_character is None and character in _BRACKET_PAIRS:
- if current_search is None:
- current_search = _BRACKET_PAIRS[character]
- depth = 1
- elif character == current_search.opening_bracket:
- depth += 1
+ if character in {"'", '"'}:
+ # Skip everything inside of strings, regardless of the depth.
+ quote_character = character
+ for index, character in enumerated_string:
+ if character == quote_character and _is_closing_quote(parameters_string, index):
+ break
- elif character in {"'", '"'}:
- if current_search is not None:
- # We're currently searching for a bracket, skip all characters that belong to the string
- # to avoid false positives of closing brackets
- quote_character = character
- for index, character in enumerated_string:
- if character == quote_character and _is_closing_quote(parameters_string, index):
- break
+ elif current_search is None:
+ if (current_search := _BRACKET_PAIRS.get(character)) is not None:
+ depth = 1
+ elif character == ",":
+ yield parameters_string[last_split:index]
+ last_split = index + 1
- elif depth == 0:
+ else:
+ if character == current_search.opening_bracket:
depth += 1
- quote_character = character
- elif character == quote_character:
- if _is_closing_quote(parameters_string, index):
- depth -= 1
- if depth == 0:
- quote_character = None
- elif current_search is not None and character == current_search.closing_bracket:
- depth -= 1
- if depth == 0:
- current_search = None
-
- elif depth == 0 and character == ",":
- yield parameters_string[last_split:index]
- last_split = index + 1
+ elif character == current_search.closing_bracket:
+ depth -= 1
+ if depth == 0:
+ current_search = None
yield parameters_string[last_split:]