mirror of
https://github.com/ytdl-org/youtube-dl.git
synced 2024-12-22 22:35:21 +00:00
Refactor JSInterpreter._separate
yt-dlp/yt-dlp/@06dfe0a, improve _MATCHING_PARENS
This commit is contained in:
parent
9d142109f4
commit
6ca7b77696
@ -36,6 +36,8 @@ _ASSIGN_OPERATORS.append(('=', (lambda cur, right: right)))
|
|||||||
|
|
||||||
_NAME_RE = r'[a-zA-Z_$][a-zA-Z_$0-9]*'
|
_NAME_RE = r'[a-zA-Z_$][a-zA-Z_$0-9]*'
|
||||||
|
|
||||||
|
_MATCHING_PARENS = dict(zip(*zip('()', '{}', '[]')))
|
||||||
|
|
||||||
|
|
||||||
class JS_Break(ExtractorError):
|
class JS_Break(ExtractorError):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -100,26 +102,24 @@ class JSInterpreter(object):
|
|||||||
def _separate(expr, delim=',', max_split=None):
|
def _separate(expr, delim=',', max_split=None):
|
||||||
if not expr:
|
if not expr:
|
||||||
return
|
return
|
||||||
parens = {'(': 0, '{': 0, '[': 0, ']': 0, '}': 0, ')': 0}
|
counters = {k: 0 for k in _MATCHING_PARENS.values()}
|
||||||
start, splits, pos, max_pos = 0, 0, 0, len(delim) - 1
|
start, splits, pos, delim_len = 0, 0, 0, len(delim) - 1
|
||||||
for idx, char in enumerate(expr):
|
for idx, char in enumerate(expr):
|
||||||
if char in parens:
|
if char in _MATCHING_PARENS:
|
||||||
parens[char] += 1
|
counters[_MATCHING_PARENS[char]] += 1
|
||||||
is_in_parens = (parens['['] - parens[']']
|
elif char in counters:
|
||||||
or parens['('] - parens[')']
|
counters[char] -= 1
|
||||||
or parens['{'] - parens['}'])
|
if char != delim[pos] or any(counters.values()):
|
||||||
if char == delim[pos] and not is_in_parens:
|
|
||||||
if pos == max_pos:
|
|
||||||
pos = 0
|
pos = 0
|
||||||
yield expr[start: idx - max_pos]
|
continue
|
||||||
start = idx + 1
|
elif pos != delim_len:
|
||||||
|
pos += 1
|
||||||
|
continue
|
||||||
|
yield expr[start: idx - delim_len]
|
||||||
|
start, pos = idx + 1, 0
|
||||||
splits += 1
|
splits += 1
|
||||||
if max_split and splits >= max_split:
|
if max_split and splits >= max_split:
|
||||||
break
|
break
|
||||||
else:
|
|
||||||
pos += 1
|
|
||||||
else:
|
|
||||||
pos = 0
|
|
||||||
yield expr[start:]
|
yield expr[start:]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
Loading…
Reference in New Issue
Block a user