Skip to content

Commit

Permalink
Change: Performance improvement for parsing CPEs
Browse files Browse the repository at this point in the history
Use a list to collect string parts instead of concat them directly.
  • Loading branch information
bjoernricks committed Dec 12, 2023
1 parent 4f62b35 commit 2e3fe01
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pontos/cpe/_cpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,22 @@ def _add_quoting(value: str) -> str:
Add quoting for parsing attributes from formatted string format to
Well-Formed CPE Name Data Model (WFN)
"""
result = ""
result: list[str] = []
index = 0
embedded = False

while index < len(value):
c = value[index]
if c.isalnum() or c in ["_"]:
# just add character
result += c
result.append(c)
index += 1
embedded = True
continue

if c == "\\":
# keep escaped character
result += value[index : index + 2]
result.append(value[index : index + 2])
index += 2
embedded = True
continue
Expand All @@ -163,7 +163,7 @@ def _add_quoting(value: str) -> str:
# An unquoted asterisk must appear at the beginning or
# end of the string.
if index == 0 or index == (len(value) - 1):
result += c
result.append(c)
index += 1
embedded = True
continue
Expand All @@ -186,7 +186,7 @@ def _add_quoting(value: str) -> str:
embedded and (value[index + 1] == "?")
)
):
result += c
result.append(c)
index += 1
embedded = False
continue
Expand All @@ -197,11 +197,11 @@ def _add_quoting(value: str) -> str:
)

# all other characters must be quoted
result += f"\\{c}"
result.append(f"\\{c}")
index += 1
embedded = True

return result
return "".join(result)


def unbind_value_from_formatted_string(value: Optional[str]) -> Optional[str]:
Expand Down

0 comments on commit 2e3fe01

Please sign in to comment.