-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate our own unicode block constants
Add a script to generate a custom unicode_block module with constants representing unicode blocks. This replaces the blocks from unic-ucd-blocks. Also refactor CharacterType::Range to use a RangeInclusive so we can also remove the dependency on unic-char-range.
- Loading branch information
1 parent
3897101
commit b5942d7
Showing
7 changed files
with
707 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python | ||
import io | ||
import urllib.request | ||
import re | ||
|
||
BLOCKDEF = re.compile( | ||
r"^(?P<low>[0-9A-Fa-f]+)\.\.(?P<high>[0-9A-Fa-f]+); (?P<name>.*)$" | ||
) | ||
|
||
|
||
resp = urllib.request.urlopen("https://www.unicode.org/Public/UNIDATA/Blocks.txt") | ||
blocks = [] | ||
for line in io.TextIOWrapper(resp): | ||
if match := BLOCKDEF.match(line.strip()): | ||
name = match.group("name") | ||
if name in {"Low Surrogates", "High Surrogates", "High Private Use Surrogates"}: | ||
continue | ||
low = match.group("low") | ||
high = match.group("high") | ||
blocks.append((name, low, high)) | ||
|
||
|
||
def constname(blockname): | ||
return blockname.replace(" ", "_").replace("-", "_").upper() | ||
|
||
|
||
print("#![cfg_attr(rustfmt, rustfmt_skip)]") | ||
print("// Code generated by hack/genblocks. DO NOT EDIT.") | ||
print() | ||
for name, low, high in blocks: | ||
rustrange = f"'\\u{{{low}}}'..='\\u{{{high}}}'" | ||
print(f"pub const {constname(name)}: std::ops::RangeInclusive<char> = {rustrange};") | ||
|
||
print( | ||
"""pub static UNICODE_BLOCKS: phf::Map<&'static str, std::ops::RangeInclusive<char>> = phf::phf_map! {""" | ||
) | ||
for name, _, _ in blocks: | ||
print(f' "{name}" => {constname(name)},') | ||
print("};") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.