Skip to content
Snippets Groups Projects
Commit e8a65c0a authored by Julian Rother's avatar Julian Rother
Browse files

DN parsing rewrite with REs

parent 040be539
No related branches found
No related tags found
No related merge requests found
Pipeline #8856 passed
......@@ -55,22 +55,16 @@ class DN(tuple):
:raises ValueError: if expr is invalid
:returns: Parsed DN
:rtype: DN'''
escaped = False
rdns = []
token = ''
for char in expr:
if escaped:
escaped = False
token += char
elif char == ',':
rdns.append(RDN.from_str(schema, token))
token = ''
else:
if char == '\\':
escaped = True
token += char
if token:
rdns.append(RDN.from_str(schema, token))
while expr:
# relativeDistinguishedName may contain escape sequences including "\,".
# Split off first token expr at "," while ignoring "\,".
match = re.match(r'^(([^,\\]|\\.)+)(,|$)', expr)
if not match:
raise ValueError(f'Unrecognized token {expr!r}')
expr = expr[match.end():]
rdn_expr, _, _ = match.groups()
rdns.append(RDN.from_str(schema, rdn_expr))
return cls(schema, *rdns)
def __str__(self):
......@@ -269,22 +263,16 @@ class RDN(tuple):
:raises ValueError: if expr is invalid
:returns: Parsed RDN
:rtype: RDN'''
escaped = False
assertions = []
token = ''
for char in expr:
if escaped:
escaped = False
token += char
elif char == '+':
assertions.append(RDNAssertion.from_str(schema, token))
token = ''
else:
if char == '\\':
escaped = True
token += char
if token:
assertions.append(RDNAssertion.from_str(schema, token))
while expr:
# attributeTypeAndValue may contain escape sequences including "\+".
# Split off first token expr at "+" while ignoring "\+".
match = re.match(r'^(([^+\\]|\\.)+)(\+|$)', expr)
if not match:
raise ValueError(f'Unrecognized token {expr!r}')
expr = expr[match.end():]
assertion_expr, _, _ = match.groups()
assertions.append(RDNAssertion.from_str(schema, assertion_expr))
return cls(schema, *assertions)
def __str__(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment