Add UTF-8 decoding utility and corresponding unit tests
- Introduced a new utility function `decode_utf` in `utils.py` to decode escaped UTF-8 descriptions. - Updated unit tests in `test_units.py` to include tests for the `decode_utf` function, covering plain text and escaped UTF-8 scenarios. - Refactored existing tests to streamline the usage of the `expand_vlan_range` function.
This commit is contained in:
@@ -23,3 +23,17 @@ def expand_vlan_range(value: str | list[str]) -> list[str]:
|
||||
else:
|
||||
result.append(part)
|
||||
return result
|
||||
|
||||
|
||||
def decode_utf(text: str):
|
||||
"""Decode escaped UTF-8 descriptions."""
|
||||
if "\\x" in text:
|
||||
desc = text.strip('"')
|
||||
decoded = (
|
||||
desc.encode("utf-8")
|
||||
.decode("unicode_escape")
|
||||
.encode("latin1")
|
||||
.decode("utf-8")
|
||||
)
|
||||
return decoded
|
||||
return text
|
||||
|
||||
Reference in New Issue
Block a user