Commit graph

60 commits

Author SHA1 Message Date
Pyfisch
7f4048c710 Store UNICODE_VERSION as a tuple
Remove the UnicodeVersion struct containing
major, minor and update fields and replace it with
a 3-tuple containing the version number.
As the value of each field is limited to 255
use u8 to store them.
2020-04-11 12:56:25 +02:00
Mark Rousskov
b6bc906004 Remove separate encoding for a single nonzero-mapping byte
In practice, for the two data sets that still use the bitset encoding (uppercase
and lowercase) this is not a significant win, so just drop it entirely. It costs
us about 5 bytes, and the complexity is nontrivial.
2020-03-27 19:02:23 -04:00
Mark Rousskov
9c1ceece20 Add skip list based implementation for smaller encoding
This arranges for the sparser sets (everything except lower and uppercase) to be
encoded in a significantly smaller context. However, it is also a performance
trade-off (roughly 3x slower than the bitset encoding). The 40% size reduction
is deemed to be sufficiently important to merit this performance loss,
particularly as it is unlikely that this code is hot anywhere (and if it is,
paying the memory cost for a bitset that directly represents the data seems
worthwhile).

Alphabetic     : 1599 bytes     (- 937 bytes)
Case_Ignorable : 949 bytes      (- 822 bytes)
Cased          : 359 bytes      (- 429 bytes)
Cc             : 9 bytes        (-  15 bytes)
Grapheme_Extend: 813 bytes      (- 675 bytes)
Lowercase      : 863 bytes
N              : 419 bytes      (- 619 bytes)
Uppercase      : 776 bytes
White_Space    : 37 bytes       (-  46 bytes)
Total table sizes: 5824 bytes   (-3543 bytes)
2020-03-27 19:02:23 -04:00
Mark Rousskov
af243d4d91 Avoid relying on const parameters to function
LLVM seems to at least sometimes optimize better when the length comes directly
from the `len()` of the array vs. an equivalent integer.

Also, this allows easier copy/pasting of the function into compiler explorer for
experimentation.
2020-03-21 18:01:50 -04:00
Mark Rousskov
a7ec6f8fe0 Arrange for zero to be canonical
We find that it is common for large ranges of chars to be false -- and that
means that it is plausibly common for us to ask about a word that is entirely
empty. Therefore, we should make sure that we do not need to rotate bits or
otherwise perform some operation to map to the zero word; canonicalize it first
if possible.
2020-03-21 17:53:18 -04:00
Mark Rousskov
233ab2f168 Push the byte of LAST_CHUNK_MAP into the array
This optimizes slightly better.

Alphabetic     : 2536 bytes
Case_Ignorable : 1771 bytes
Cased          : 788 bytes
Cc             : 24 bytes
Grapheme_Extend: 1488 bytes
Lowercase      : 863 bytes
N              : 1038 bytes
Uppercase      : 776 bytes
White_Space    : 83 bytes
Total table sizes: 9367 bytes  (-18 bytes; 2 bytes per set)
2020-03-21 17:51:40 -04:00
Mark Rousskov
5f71d98f90 Deduplicate test and primary range_search definitions
This ensures that what we test is what we get for final results as well.
2020-03-21 15:21:31 -04:00
Mark Rousskov
7b29b70d6e Add a right shift mapping
This saves less bytes - by far - and is likely not the best operator to choose.
But for now, it works -- a better choice may arise later.

Alphabetic     : 2538 bytes   (- 84 bytes)
Case_Ignorable : 1773 bytes   (- 30 bytes)
Cased          : 790 bytes    (- 18 bytes)
Cc             : 26 bytes     (-  6 bytes)
Grapheme_Extend: 1490 bytes   (- 18 bytes)
Lowercase      : 865 bytes    (- 36 bytes)
N              : 1040 bytes   (- 24 bytes)
Uppercase      : 778 bytes    (- 60 bytes)
White_Space    : 85 bytes     (-  6 bytes)
Total table sizes: 9385 bytes (-282 bytes)
2020-03-21 12:14:26 -04:00
Mark Rousskov
b0e121d9d5 Shrink bitset words through functional mapping
Previously, all words in the (deduplicated) bitset would be stored raw -- a full
64 bits (8 bytes). Now, those words that are equivalent to others through a
specific mapping are stored separately and "mapped" to the original when
loading; this shrinks the table sizes significantly, as each mapped word is
stored in 2 bytes (a 4x decrease from the previous).

The new encoding is also potentially non-optimal: the "mapped" byte is
frequently repeated, as in practice many mapped words use the same base word.

Currently we only support two forms of mapping: rotation and inversion. Note
that these are both guaranteed to map transitively if at all, and supporting
mappings for which this is not true may require a more interesting algorithm for
choosing the optimal pairing.

Updated sizes:

Alphabetic     : 2622 bytes     (-  414 bytes)
Case_Ignorable : 1803 bytes     (-  330 bytes)
Cased          : 808 bytes      (-  126 bytes)
Cc             : 32 bytes
Grapheme_Extend: 1508 bytes     (-  252 bytes)
Lowercase      : 901 bytes      (-   84 bytes)
N              : 1064 bytes     (-  156 bytes)
Uppercase      : 838 bytes      (-   96 bytes)
White_Space    : 91 bytes       (-    6 bytes)
Total table sizes: 9667 bytes   (-1,464 bytes)
2020-03-21 11:22:00 -04:00
Mark Rousskov
6c7691a37b Pre-pop zero chunks before mapping LAST_CHUNK_MAP
This avoids wasting a small amount of space for some of the data sets.

The chunk resizing is caused by but not directly related to changes in this
commit.

Alphabetic     : 3036 bytes
Case_Ignorable : 2133 bytes    (- 3 bytes)
Cased          : 934 bytes
Cc             : 32 bytes
Grapheme_Extend: 1760 bytes    (-14 bytes)
Lowercase      : 985 bytes
N              : 1220 bytes    (- 5 bytes)
Uppercase      : 934 bytes
White_Space    : 97 bytes
Total table sizes: 11131 bytes (-22 bytes)
2020-03-20 18:38:08 -04:00
Mark Rousskov
7c4baedb3a Dynamically choose best chunk size
Try chunk sizes between 1 and 64, selecting the one which minimizes the number
of bytes used. 16, the previous constant, turned out to be a rather good choice,
with 5/9 of the datasets still using it.

Alphabetic     : 3036 bytes    (- 19 bytes)
Case_Ignorable : 2136 bytes
Cased          : 934 bytes
Cc             : 32 bytes      (- 11 bytes)
Grapheme_Extend: 1774 bytes
Lowercase      : 985 bytes
N              : 1225 bytes    (- 41 bytes)
Uppercase      : 934 bytes
White_Space    : 97 bytes      (- 43 bytes)
Total table sizes: 11153 bytes (-114 bytes)
2020-03-20 12:11:13 -04:00
Josh Stone
543832b06c Regenerate tables for Unicode 13.0.0 2020-03-11 09:24:08 -07:00
Mark Rousskov
efcda04739 Replace old tables with new unicode data 2020-01-14 19:11:28 -05:00
Mark Rousskov
40ad877851 Add support code for new unicode_data module 2020-01-14 19:11:15 -05:00
Ross MacArthur
f7256d28d1
Require issue = "none" over issue = "0" in unstable attributes 2019-12-21 13:16:18 +02:00
David Tolnay
28eb31f8dc
Make libcore/unicode/tables.rs compatible with rustfmt 2019-11-29 20:17:11 -08:00
David Tolnay
f4cff27792
Make libcore/unicode/printable.rs compatible with rustfmt 2019-11-29 20:17:10 -08:00
David Tolnay
95e00bfed8
Format libcore with rustfmt
This commit applies rustfmt with default settings to files in
src/libcore *that are not involved in any currently open PR* to minimize
merge conflicts. The list of files involved in open PRs was determined
by querying GitHub's GraphQL API with this script:
https://gist.github.com/dtolnay/aa9c34993dc051a4f344d1b10e4487e8

With the list of files from the script in `outstanding_files`, the
relevant commands were:

    $ find src/libcore -name '*.rs' | xargs rustfmt --edition=2018
    $ rg libcore outstanding_files | xargs git checkout --

Repeating this process several months apart should get us coverage of
most of the rest of libcore.
2019-11-26 23:02:11 -08:00
Guanqun Lu
ba7d1b80d0 it's more pythonic to use 'is not None' in python files 2019-09-06 15:14:25 +08:00
Aleksey Kladov
a0c186c34f remove XID and Pattern_White_Space unicode tables from libcore
They are only used by rustc_lexer, and are not needed elsewhere.

So we move the relevant definitions into rustc_lexer (while the actual
unicode data comes from the unicode-xid crate) and make the rest of
the compiler use it.
2019-09-04 13:11:11 +03:00
Matthew Jasper
7b41fd2158 Make some items in core::unicode private
They were reachable through opaque macros defined in `core`
2019-08-05 23:50:47 +01:00
Mazdak Farrokhzad
68d94bd741
Rollup merge of #62084 - euclio:unicode-table-tweak, r=kennytm
allow clippy::unreadable_literal in unicode tables

Also modifies the generation script to emit 2018 edition paths.
2019-07-26 18:56:33 +02:00
Andy Russell
dee3d27d9d
allow clippy::unreadable_literal in unicode tables
Also modifies the generation script to emit 2018 edition paths.
2019-07-12 22:51:37 -04:00
Josh Stone
de1e489115 Regenerate character tables for Unicode 12.1 2019-07-12 16:29:40 -07:00
Josh Stone
76128c304d Update unicode scripts for the current coding style 2019-07-12 16:28:58 -07:00
Mazdak Farrokhzad
327c54ed02
Rollup merge of #60081 - pawroman:cleanup_unicode_script, r=varkor
Refactor unicode.py script

Hi, I noticed that the `unicode.py` script used some deprecated escapes in regular expressions. E.g. `\d`, `\w`, `\.` will be illegal in the future without "raw strings". This is now fixed. I have also cleaned up the script quite a bit.

## Escape deprecation

OK (note the `r`):
`re.compile(r"\d")`

Deprecated (from Python 3.6 onwards, see [here][link1] and [here][link2]):
`re.compile("\d")`.

[link1]: https://docs.python.org/3.6/whatsnew/3.6.html#deprecated-python-behavior
[link2]: https://bugs.python.org/issue27364

This was evident running the script using Python 3.7 like so:

```
$ python3 -Wall unicode.py
unicode.py:227: DeprecationWarning: invalid escape sequence \w
  re1 = re.compile("^ *([0-9A-F]+) *; *(\w+)")
unicode.py:228: DeprecationWarning: invalid escape sequence \.
  re2 = re.compile("^ *([0-9A-F]+)\.\.([0-9A-F]+) *; *(\w+)")
unicode.py:453: DeprecationWarning: invalid escape sequence \d
  pattern = "for Version (\d+)\.(\d+)\.(\d+) of the Unicode"
```

The documentation states that
> A backslash-character pair that is not a valid escape sequence now generates a DeprecationWarning. Although this will eventually become a SyntaxError, that will not be for several Python releases.

## Testing

To test my changes, I had to add support for choosing the Unicode version to use. The script will default to latest release (which is 12.0.0 at the moment, repo has 11.0.0 checked in).

The script generates the exact same output for version 11.0.0 with Python 2.7 and 3.7 and no longer generates any deprecation warnings:

```
$ python3 -Wall unicode.py -v 11.0.0
Using Unicode version: 11.0.0
Regenerated tables.rs.
$ git diff tables.rs
$ python2 -Wall unicode.py -v 11.0.0
Using Unicode version: 11.0.0
Regenerated tables.rs.
$ git diff tables.rs
$ python2 --version
Python 2.7.16
$ python3 --version
Python 3.7.3
```

## Extra functionality

Furthermore, the script will check and download the latest Unicode version by default (without the `-v` argument). The `--help` is below:

```
$ ./unicode.py --help
usage: unicode.py [-h] [-v VERSION]

Regenerate Unicode tables (tables.rs).

optional arguments:
  -h, --help            show this help message and exit
  -v VERSION, --version VERSION
                        Unicode version to use (if not specified, defaults to
                        latest available final release).
```

## Cleanups

I have cleaned up the code quite a bit, with Python best practices and code style in mind. I'm happy to provide more details and rationale for all my changes if the reviewers so desire.

One externally visible change is that the Unicode data will now be downloaded into `src/libcore/unicode/downloaded` directory suffixed by Unicode version:

```
$ pwd
.../rust/src/libcore/unicode
$ exa -T downloaded/
downloaded
├── 11.0.0
│  ├── DerivedCoreProperties.txt
│  ├── DerivedNormalizationProps.txt
│  ├── PropList.txt
│  ├── ReadMe.txt
│  ├── Scripts.txt
│  ├── SpecialCasing.txt
│  └── UnicodeData.txt
└── 12.0.0
   ├── DerivedCoreProperties.txt
   ├── DerivedNormalizationProps.txt
   ├── PropList.txt
   ├── ReadMe.txt
   ├── Scripts.txt
   ├── SpecialCasing.txt
   └── UnicodeData.txt
```
2019-07-06 22:14:33 +02:00
Paweł Romanowski
2b47a085dd Address review remarks in unicode.py 2019-07-01 19:43:48 +02:00
Paweł Romanowski
60ccf89693
Apply suggestions from code review
Co-Authored-By: varkor <github@varkor.com>
2019-06-10 20:45:58 +02:00
Paweł Romanowski
2c9c978e1d Refactor and document unicode.py script 2019-04-19 11:42:08 +02:00
Paweł Romanowski
edbc27da2d Fix tidy errors 2019-04-18 17:14:31 +02:00
Paweł Romanowski
a580421afb More cleanups for unicode.py 2019-04-18 16:16:34 +02:00
Paweł Romanowski
89feb6d5fd Clean up unicode.py script 2019-04-18 15:30:50 +02:00
Taiki Endo
360432f1e8 libcore => 2018 2019-04-18 14:47:35 +09:00
Mark Rousskov
2a663555dd Remove licenses 2018-12-25 21:08:33 -07:00
ljedrz
d0c64bb296 cleanup: remove static lifetimes from consts 2018-12-04 12:46:10 +01:00
Mazdak Farrokhzad
e15c62d61f revert making internal APIs const fn. 2018-11-10 01:10:07 +01:00
Mazdak Farrokhzad
5b89877dda constify parts of libcore. 2018-11-10 01:07:32 +01:00
bors
c4156768aa Auto merge of #51609 - dscorbett:is_numeric, r=alexcrichton
Treat gc=No characters as numeric

[`char::is_numeric`](https://doc.rust-lang.org/std/primitive.char.html#method.is_numeric) and [`char::is_alphanumeric`](https://doc.rust-lang.org/std/primitive.char.html#method.is_alphanumeric) are documented to be defined “in terms of the Unicode General Categories 'Nd', 'Nl', 'No'”, but unicode.py does not group 'No' with the other 'N' categories. These functions therefore currently return `false` for characters like ⟨¾⟩ and ⟨①⟩.
2018-08-01 17:44:25 +00:00
Pazzaz
ad7621d42e Handle array manually in string case conversion methods 2018-07-06 17:20:39 +02:00
David Corbett
5150ff0c72 Treat gc=No characters as numeric 2018-06-17 13:47:47 -04:00
Josh Stone
f81e34b825 Regenerate character tables for Unicode 11 2018-06-11 10:54:30 -07:00
varkor
b6539372e9 Fix tables.rs 2018-05-21 19:12:36 +01:00
varkor
2fa22effb6 Avoid counting characters and add explanatory comment to test 2018-05-21 18:57:54 +01:00
varkor
d7aa35eb1b Use Grapheme_Extend instead of Mn 2018-05-21 18:57:54 +01:00
varkor
d3c257b0ae Use the correct output directory for downloading Unicode files 2018-05-21 18:57:54 +01:00
varkor
4694d20170 Escape combining characters in escape_debug 2018-05-21 18:57:54 +01:00
varkor
b72faf5795 Keep tables.rs copyright notice up to date 2018-05-21 18:57:54 +01:00
varkor
a0b5d3813e Download unicode data files in directory of unicode.py 2018-05-21 18:57:54 +01:00
varkor
f53022f88d Update unicode/tables.rs with Mn 2018-05-21 18:57:54 +01:00
Vadzim Dambrouski
f29e62aadf Fix a warning in libcore on 16bit targets.
This code is assuming that usize >= 32bits, but it is not the case on
16bit targets. It is producing a warning that will fail the compilation
on MSP430 if deny(warnings) is enabled.
It is very unlikely that someone would actually use this code on
a microcontroller, but since unicode was merged into libcore we
have compile it on 16bit targets.
2018-05-01 17:48:31 +03:00