generated from kgod/ai-review-template
26 lines
918 B
Python
26 lines
918 B
Python
"""Small display-text normalizers for model and persisted proposal content."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import Any
|
|
|
|
|
|
_LITERAL_UNICODE_ESCAPE = re.compile(r"(?<!\\)\\u([0-9a-fA-F]{4})")
|
|
|
|
|
|
def decode_literal_unicode_escapes(value: Any) -> Any:
|
|
"""Decode only literal ``\\uXXXX`` sequences accidentally returned as text.
|
|
|
|
JSON parsing normally handles Unicode escapes. This is deliberately narrow so a
|
|
user-entered path or other ordinary backslash content is not reinterpreted.
|
|
"""
|
|
if isinstance(value, str):
|
|
return _LITERAL_UNICODE_ESCAPE.sub(
|
|
lambda match: chr(int(match.group(1), 16)), value
|
|
)
|
|
if isinstance(value, list):
|
|
return [decode_literal_unicode_escapes(item) for item in value]
|
|
if isinstance(value, dict):
|
|
return {key: decode_literal_unicode_escapes(item) for key, item in value.items()}
|
|
return value |