Refactored implementation of `iterparentnodeids` in `nodes`.

This commit is contained in:
Charles Quinn 2023-04-21 12:52:42 -05:00
parent 41f57ef95d
commit 315dad5ab0
1 changed files with 21 additions and 22 deletions

View File

@ -66,31 +66,30 @@ def iterparentnodeids(nodeid: str) -> Iterator[str]:
Note that / components are only considered until the first ::. Note that / components are only considered until the first ::.
""" """
pos = 0
first_colons: Optional[int] = nodeid.find("::")
if first_colons == -1:
first_colons = None
# The root Session node - always present. # The root Session node - always present.
yield "" yield ""
# Eagerly consume SEP parts until first colons.
while True: if nodeid == '':
at = nodeid.find(SEP, pos, first_colons) return
if at == -1:
break if nodeid.startswith('::'):
if at > 0: # Weird case of '::x' (no path)
yield nodeid[:at]
pos = at + len(SEP)
# Eagerly consume :: parts.
while True:
at = nodeid.find("::", pos)
if at == -1:
break
if at > 0:
yield nodeid[:at]
pos = at + len("::")
# The node ID itself.
if nodeid:
yield nodeid yield nodeid
return
path, sep, node = nodeid.partition('::')
sections = path.split(SEP)
for i, _ in enumerate(sections, start=1):
yield SEP.join(sections[:i])
if sep == '':
# Only a path
return
nodes = node.split('::')
for i, _ in enumerate(nodes, start=1):
yield '::'.join([path] + nodes[:i])
def _check_path(path: Path, fspath: LEGACY_PATH) -> None: def _check_path(path: Path, fspath: LEGACY_PATH) -> None: