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