Skip to content

Commit

Permalink
Format \N character name escapes with uppercased literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-007 committed Mar 13, 2022
1 parent add30b8 commit 483fc15
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/black/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
3 changes: 2 additions & 1 deletion tests/data/format_unicode_escape_seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
x = "don't format me"
x = "\xhhhhh"
x = "\uhhhhh"

x = "\N{ox}\N{OX}"

# Output

Expand All @@ -16,3 +16,4 @@
x = "don't format me"
x = "\xhhhhh"
x = "\uhhhhh"
x = "\N{OX}\N{OX}"

0 comments on commit 483fc15

Please sign in to comment.