Remove unuse parameter in split_esc

This commit is contained in:
nicolargo 2025-11-16 10:51:49 +01:00
parent c70321c3fb
commit df684c64c2
1 changed files with 19 additions and 19 deletions

View File

@ -633,13 +633,13 @@ def exit_after(seconds, default=None):
return decorator
def split_esc(string, /, sep=None, maxsplit=-1, esc='\\'):
def split_esc(input_string, sep=None, maxsplit=-1, esc='\\'):
"""
Return a list of the substrings in the string, using sep as the separator string
Return a list of the substrings in the input_string, using sep as the separator char
and esc as the escape character.
sep
The separator used to split the string.
The separator used to split the input_string.
When set to None (the default value), will split on any whitespace
character (including \n \r \t \f and spaces) unless the character is escaped
@ -653,59 +653,59 @@ def split_esc(string, /, sep=None, maxsplit=-1, esc='\\'):
When set to None, this behaves equivalently to `str.split`.
Defaults to '\\\\' i.e. backslash.
Splitting starts at the front of the string and works to the end.
Splitting starts at the front of the input_string and works to the end.
Note: escape characters in the substrings returned are removed. However, if
maxsplit is reached, escape characters in the remaining, unprocessed substring
are not removed, which allows split_esc to be called on it again.
"""
# Input validation
if not isinstance(string, str):
raise TypeError(f'must be str, not {string.__class__.__name__}')
if not isinstance(input_string, str):
raise TypeError(f'must be str, not {input_string.__class__.__name__}')
str.split('', sep=sep, maxsplit=maxsplit) # Use str.split to validate sep and maxsplit
if esc is None:
return string.split(
return input_string.split(
sep=sep, maxsplit=maxsplit
) # Short circuit to default implementation if the escape character is None
elif not isinstance(esc, str):
if not isinstance(esc, str):
raise TypeError(f'must be str or None, not {esc.__class__.__name__}')
elif len(esc) == 0:
if len(esc) == 0:
raise ValueError('empty escape character')
elif len(esc) > 1:
if len(esc) > 1:
raise ValueError('escape must be a single character')
# Set up a simple state machine keeping track of whether we have seen an escape character
ret, esc_seen, i = [''], False, 0
while i < len(string) and len(ret) - 1 != maxsplit:
while i < len(input_string) and len(ret) - 1 != maxsplit:
if not esc_seen:
if string[i] == esc:
if input_string[i] == esc:
# Consume the escape character and transition state
esc_seen = True
i += 1
elif sep is None and string[i].isspace():
elif sep is None and input_string[i].isspace():
# Consume as much whitespace as possible
n = 1
while i + n + 1 < len(string) and string[i + n : i + n + 1].isspace():
while i + n + 1 < len(input_string) and input_string[i + n : i + n + 1].isspace():
n += 1
ret.append('')
i += n
elif sep is not None and string[i : i + len(sep)] == sep:
elif sep is not None and input_string[i : i + len(sep)] == sep:
# Consume the separator
ret.append('')
i += len(sep)
else:
# Otherwise just add the current char
ret[-1] += string[i]
ret[-1] += input_string[i]
i += 1
else:
# Add the current char and transition state back
ret[-1] += string[i]
ret[-1] += input_string[i]
esc_seen = False
i += 1
# Append any remaining string if we broke early because of maxsplit
if i < len(string):
ret[-1] += string[i:]
if i < len(input_string):
ret[-1] += input_string[i:]
# If splitting on whitespace, discard empty strings from result
if sep is None: