Crates.io | uwc |
lib.rs | uwc |
version | 1.0.8 |
source | src |
created_at | 2018-02-05 17:05:40.898054 |
updated_at | 2024-09-30 23:59:29.072036 |
description | Counts things in unicode text files |
homepage | |
repository | https://github.com/dead10ck/uwc |
max_upload_size | |
id | 49752 |
size | 104,238 |
Like wc
, but unicode-aware, and with line mode.
uwc
can count:
Additionally, it can operate in line mode, which will count things within lines.
By default, uwc
will count lines, words, and bytes. You can specify the counters
you'd like, or ask for all counters with the -a
flag.
$ uwc tests/fixtures/**/input
lines words bytes filename
8 5 29 tests/fixtures/all_newlines/input
0 0 0 tests/fixtures/empty/input
0 0 0 tests/fixtures/empty_line_mode/input
1 9 97 tests/fixtures/flags_bp/input
1 9 97 tests/fixtures/flags_cl/input
1 9 97 tests/fixtures/flags_w/input
0 1 5 tests/fixtures/hello/input
1 9 97 tests/fixtures/i_can_eat_glass/input
8 8 29 tests/fixtures/line_mode/input
7 8 28 tests/fixtures/line_mode_no_trailing_newline/input
7 8 28 tests/fixtures/line_mode_no_trailing_newline_count_newlines/input
34 66 507 total
$ uwc -a tests/fixtures/**/input
lines words bytes graphemes codepoints filename
8 5 29 23 24 tests/fixtures/all_newlines/input
0 0 0 0 0 tests/fixtures/empty/input
0 0 0 0 0 tests/fixtures/empty_line_mode/input
1 9 97 51 51 tests/fixtures/flags_bp/input
1 9 97 51 51 tests/fixtures/flags_cl/input
1 9 97 51 51 tests/fixtures/flags_w/input
0 1 5 5 5 tests/fixtures/hello/input
1 9 97 51 51 tests/fixtures/i_can_eat_glass/input
8 8 29 28 28 tests/fixtures/line_mode/input
7 8 28 27 27 tests/fixtures/line_mode_no_trailing_newline/input
7 8 28 27 27 tests/fixtures/line_mode_no_trailing_newline_count_newlines/input
34 66 507 314 315 total
You can also switch into line mode with the --mode
flag:
$ uwc -a --mode line tests/fixtures/line_mode/input
lines words bytes graphemes codepoints filename
0 1 1 1 1 tests/fixtures/line_mode/input:1
0 1 2 2 2 tests/fixtures/line_mode/input:2
0 1 3 3 3 tests/fixtures/line_mode/input:3
0 1 5 4 4 tests/fixtures/line_mode/input:4
0 1 1 1 1 tests/fixtures/line_mode/input:5
0 1 4 4 4 tests/fixtures/line_mode/input:6
0 1 2 2 2 tests/fixtures/line_mode/input:7
0 1 3 3 3 tests/fixtures/line_mode/input:8
0 8 21 20 20 tests/fixtures/line_mode/input:total
The goal of this project is to consider unicode rules correctly when counting things. Specifically, it should:
It does not aim to implement these unicode algorithms, however, so it makes use of
the unicode-segmentation
library
for most of the heavy lifting. And since Unicode support in the Rust ecosystem is
not quite mature yet, that has some consequences for this project. See the
caveats below.
It is published on crates.io, so simply:
$ cargo install uwc
It only supports UTF-8 files. UTF-16 can go on my to-do list if there is demand.
For now, you can use iconv
to convert non-UTF-8 files first.
The current implementation will always read complete lines before proceeding to do its counts; without hand-rolling my own streaming implementation of the Unicode line splitting algorithm, this is necessary for correctness with line mode. The consequence of this is that if you give it files with very large lines, it will use memory proportional to the size of the lines. If you give it a file with no newline sequences, it will soak up the whole file into memory. Beware.
It is slower than wc
. My analysis hasn't been extensive, but as far as I can
tell, the reasons are:
With that said, it is parallelized, which helps. With testing on my local
laptop with larger data sets, the speed is within an order of magnitude of
wc
. I measured uwc
being 1.5x slower than wc
on a collection of 18 MiB of
text files.
Rust, as yet, has no localization libraries, so this has some consequences. Some counts will just be wrong, such as hyphenated words, which is locale-specific and requires language dictionary lookups to be correct. Also, there are some languages that have no syntactic word separators, such as Japanese, so e.g.
私はガラスを食べられます。
should be 5 words, but without localization, we cannot determine that.