From 483fc150faccd7ae56aa8ef8906509447d695ffe Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Mon, 31 Jan 2022 07:27:11 +0530 Subject: [PATCH] Format \N character name escapes with uppercased literals --- src/black/strings.py | 9 +++++++-- tests/data/format_unicode_escape_seq.py | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/black/strings.py b/src/black/strings.py index e2b7534a3f..00c754c14e 100644 --- a/src/black/strings.py +++ b/src/black/strings.py @@ -20,7 +20,7 @@ r"^([" + STRING_PREFIX_CHARS + r"]*)(.*)$", re.DOTALL ) FIRST_NON_WHITESPACE_RE: Final = re.compile(r"\s*\t+\s*(\S)") -UNICODE_RE = re.compile(r"(\\+)(u|U|x)([a-zA-Z0-9]+)") +UNICODE_RE = re.compile(r"(\\+)(u|U|x|N)(([a-zA-Z0-9]+)|\{([a-zA-Z0-9]+)\})") def sub_twice(regex: Pattern[str], replacement: str, original: str) -> str: @@ -247,6 +247,11 @@ def normalize_unicode_escape_sequences(leaf: Leaf) -> None: def replace(m: Match[AnyStr]) -> AnyStr: groups = m.groups() - return groups[0] + groups[1] + groups[2].lower() + if m.group(4): + # \\U or \\u or \\x + return groups[0] + groups[1] + groups[2].lower() + else: + # \\N{} + return groups[0] + groups[1] + groups[2].upper() leaf.value = re.sub(UNICODE_RE, replace, text) diff --git a/tests/data/format_unicode_escape_seq.py b/tests/data/format_unicode_escape_seq.py index 288db53c42..3a8bc20f15 100644 --- a/tests/data/format_unicode_escape_seq.py +++ b/tests/data/format_unicode_escape_seq.py @@ -5,7 +5,7 @@ x = "don't format me" x = "\xhhhhh" x = "\uhhhhh" - +x = "\N{ox}\N{OX}" # Output @@ -16,3 +16,4 @@ x = "don't format me" x = "\xhhhhh" x = "\uhhhhh" +x = "\N{OX}\N{OX}"