├── .github
├── dependabot.yml
└── workflows
│ └── main.yml
├── .gitignore
├── Cargo.lock
├── Cargo.toml
├── LICENSE
├── README.md
├── deny.toml
└── src
├── bin
└── tf.rs
├── chunks.rs
├── counter.rs
├── key_finder.rs
└── lib.rs
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: cargo
4 | directory: "/"
5 | schedule:
6 | interval: weekly
7 | groups:
8 | crates-io:
9 | patterns:
10 | - ".*"
11 | - package-ecosystem: github-actions
12 | directory: "/"
13 | schedule:
14 | interval: weekly
15 |
--------------------------------------------------------------------------------
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | push:
5 | branches: ['main']
6 | pull_request:
7 |
8 | jobs:
9 | Test:
10 | strategy:
11 | matrix:
12 | os: [ubuntu-latest, macos-latest, windows-latest]
13 | rust: [stable, beta]
14 | exclude:
15 | - os: macos-latest
16 | rust: beta
17 | - os: windows-latest
18 | rust: beta
19 |
20 | runs-on: ${{ matrix.os }}
21 |
22 | steps:
23 | - uses: actions/checkout@v5
24 | - uses: dtolnay/rust-toolchain@master
25 | with:
26 | toolchain: ${{ matrix.rust }}
27 | - run: cargo test
28 |
29 | Lint:
30 | runs-on: ubuntu-latest
31 | steps:
32 | - uses: actions/checkout@v5
33 | - uses: dtolnay/rust-toolchain@stable
34 | with:
35 | toolchain: stable
36 | components: rustfmt, clippy
37 | - run: cargo fmt --all -- --check
38 | - run: cargo clippy --workspace --all-targets -- -D warnings
39 | if: always()
40 |
41 | Audit:
42 | runs-on: ubuntu-latest
43 | steps:
44 | - uses: actions/checkout@v5
45 | - uses: EmbarkStudios/cargo-deny-action@v2
46 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | access_log*
3 | .DS_Store
4 | *.svg
5 |
--------------------------------------------------------------------------------
/Cargo.lock:
--------------------------------------------------------------------------------
1 | # This file is automatically @generated by Cargo.
2 | # It is not intended for manual editing.
3 | version = 4
4 |
5 | [[package]]
6 | name = "aho-corasick"
7 | version = "1.1.3"
8 | source = "registry+https://github.com/rust-lang/crates.io-index"
9 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
10 | dependencies = [
11 | "memchr",
12 | ]
13 |
14 | [[package]]
15 | name = "anstream"
16 | version = "0.6.15"
17 | source = "registry+https://github.com/rust-lang/crates.io-index"
18 | checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
19 | dependencies = [
20 | "anstyle",
21 | "anstyle-parse",
22 | "anstyle-query",
23 | "anstyle-wincon",
24 | "colorchoice",
25 | "is_terminal_polyfill",
26 | "utf8parse",
27 | ]
28 |
29 | [[package]]
30 | name = "anstyle"
31 | version = "1.0.8"
32 | source = "registry+https://github.com/rust-lang/crates.io-index"
33 | checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
34 |
35 | [[package]]
36 | name = "anstyle-parse"
37 | version = "0.2.5"
38 | source = "registry+https://github.com/rust-lang/crates.io-index"
39 | checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
40 | dependencies = [
41 | "utf8parse",
42 | ]
43 |
44 | [[package]]
45 | name = "anstyle-query"
46 | version = "1.1.1"
47 | source = "registry+https://github.com/rust-lang/crates.io-index"
48 | checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
49 | dependencies = [
50 | "windows-sys",
51 | ]
52 |
53 | [[package]]
54 | name = "anstyle-wincon"
55 | version = "3.0.4"
56 | source = "registry+https://github.com/rust-lang/crates.io-index"
57 | checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
58 | dependencies = [
59 | "anstyle",
60 | "windows-sys",
61 | ]
62 |
63 | [[package]]
64 | name = "anyhow"
65 | version = "1.0.100"
66 | source = "registry+https://github.com/rust-lang/crates.io-index"
67 | checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
68 |
69 | [[package]]
70 | name = "autocfg"
71 | version = "1.3.0"
72 | source = "registry+https://github.com/rust-lang/crates.io-index"
73 | checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
74 |
75 | [[package]]
76 | name = "cc"
77 | version = "1.1.7"
78 | source = "registry+https://github.com/rust-lang/crates.io-index"
79 | checksum = "26a5c3fd7bfa1ce3897a3a3501d362b2d87b7f2583ebcb4a949ec25911025cbc"
80 |
81 | [[package]]
82 | name = "cfg-if"
83 | version = "1.0.0"
84 | source = "registry+https://github.com/rust-lang/crates.io-index"
85 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
86 |
87 | [[package]]
88 | name = "clap"
89 | version = "4.5.13"
90 | source = "registry+https://github.com/rust-lang/crates.io-index"
91 | checksum = "0fbb260a053428790f3de475e304ff84cdbc4face759ea7a3e64c1edd938a7fc"
92 | dependencies = [
93 | "clap_builder",
94 | "clap_derive",
95 | ]
96 |
97 | [[package]]
98 | name = "clap_builder"
99 | version = "4.5.13"
100 | source = "registry+https://github.com/rust-lang/crates.io-index"
101 | checksum = "64b17d7ea74e9f833c7dbf2cbe4fb12ff26783eda4782a8975b72f895c9b4d99"
102 | dependencies = [
103 | "anstream",
104 | "anstyle",
105 | "clap_lex",
106 | "strsim",
107 | ]
108 |
109 | [[package]]
110 | name = "clap_derive"
111 | version = "4.5.13"
112 | source = "registry+https://github.com/rust-lang/crates.io-index"
113 | checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0"
114 | dependencies = [
115 | "heck",
116 | "proc-macro2",
117 | "quote",
118 | "syn",
119 | ]
120 |
121 | [[package]]
122 | name = "clap_lex"
123 | version = "0.7.2"
124 | source = "registry+https://github.com/rust-lang/crates.io-index"
125 | checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
126 |
127 | [[package]]
128 | name = "colorchoice"
129 | version = "1.0.2"
130 | source = "registry+https://github.com/rust-lang/crates.io-index"
131 | checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
132 |
133 | [[package]]
134 | name = "crossbeam-deque"
135 | version = "0.8.5"
136 | source = "registry+https://github.com/rust-lang/crates.io-index"
137 | checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
138 | dependencies = [
139 | "crossbeam-epoch",
140 | "crossbeam-utils",
141 | ]
142 |
143 | [[package]]
144 | name = "crossbeam-epoch"
145 | version = "0.9.18"
146 | source = "registry+https://github.com/rust-lang/crates.io-index"
147 | checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
148 | dependencies = [
149 | "crossbeam-utils",
150 | ]
151 |
152 | [[package]]
153 | name = "crossbeam-utils"
154 | version = "0.8.20"
155 | source = "registry+https://github.com/rust-lang/crates.io-index"
156 | checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
157 |
158 | [[package]]
159 | name = "either"
160 | version = "1.13.0"
161 | source = "registry+https://github.com/rust-lang/crates.io-index"
162 | checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
163 |
164 | [[package]]
165 | name = "env_logger"
166 | version = "0.8.4"
167 | source = "registry+https://github.com/rust-lang/crates.io-index"
168 | checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3"
169 | dependencies = [
170 | "log",
171 | "regex",
172 | ]
173 |
174 | [[package]]
175 | name = "getrandom"
176 | version = "0.2.15"
177 | source = "registry+https://github.com/rust-lang/crates.io-index"
178 | checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
179 | dependencies = [
180 | "cfg-if",
181 | "libc",
182 | "wasi",
183 | ]
184 |
185 | [[package]]
186 | name = "heck"
187 | version = "0.5.0"
188 | source = "registry+https://github.com/rust-lang/crates.io-index"
189 | checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
190 |
191 | [[package]]
192 | name = "is_terminal_polyfill"
193 | version = "1.70.1"
194 | source = "registry+https://github.com/rust-lang/crates.io-index"
195 | checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
196 |
197 | [[package]]
198 | name = "libc"
199 | version = "0.2.155"
200 | source = "registry+https://github.com/rust-lang/crates.io-index"
201 | checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
202 |
203 | [[package]]
204 | name = "log"
205 | version = "0.4.22"
206 | source = "registry+https://github.com/rust-lang/crates.io-index"
207 | checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
208 |
209 | [[package]]
210 | name = "memchr"
211 | version = "2.7.4"
212 | source = "registry+https://github.com/rust-lang/crates.io-index"
213 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
214 |
215 | [[package]]
216 | name = "proc-macro2"
217 | version = "1.0.86"
218 | source = "registry+https://github.com/rust-lang/crates.io-index"
219 | checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
220 | dependencies = [
221 | "unicode-ident",
222 | ]
223 |
224 | [[package]]
225 | name = "quickcheck"
226 | version = "1.0.3"
227 | source = "registry+https://github.com/rust-lang/crates.io-index"
228 | checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6"
229 | dependencies = [
230 | "env_logger",
231 | "log",
232 | "rand",
233 | ]
234 |
235 | [[package]]
236 | name = "quote"
237 | version = "1.0.36"
238 | source = "registry+https://github.com/rust-lang/crates.io-index"
239 | checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
240 | dependencies = [
241 | "proc-macro2",
242 | ]
243 |
244 | [[package]]
245 | name = "rand"
246 | version = "0.8.5"
247 | source = "registry+https://github.com/rust-lang/crates.io-index"
248 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
249 | dependencies = [
250 | "rand_core",
251 | ]
252 |
253 | [[package]]
254 | name = "rand_core"
255 | version = "0.6.4"
256 | source = "registry+https://github.com/rust-lang/crates.io-index"
257 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
258 | dependencies = [
259 | "getrandom",
260 | ]
261 |
262 | [[package]]
263 | name = "rayon"
264 | version = "1.10.0"
265 | source = "registry+https://github.com/rust-lang/crates.io-index"
266 | checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
267 | dependencies = [
268 | "either",
269 | "rayon-core",
270 | ]
271 |
272 | [[package]]
273 | name = "rayon-core"
274 | version = "1.12.1"
275 | source = "registry+https://github.com/rust-lang/crates.io-index"
276 | checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
277 | dependencies = [
278 | "crossbeam-deque",
279 | "crossbeam-utils",
280 | ]
281 |
282 | [[package]]
283 | name = "regex"
284 | version = "1.12.2"
285 | source = "registry+https://github.com/rust-lang/crates.io-index"
286 | checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
287 | dependencies = [
288 | "aho-corasick",
289 | "memchr",
290 | "regex-automata",
291 | "regex-syntax",
292 | ]
293 |
294 | [[package]]
295 | name = "regex-automata"
296 | version = "0.4.12"
297 | source = "registry+https://github.com/rust-lang/crates.io-index"
298 | checksum = "722166aa0d7438abbaa4d5cc2c649dac844e8c56d82fb3d33e9c34b5cd268fc6"
299 | dependencies = [
300 | "aho-corasick",
301 | "memchr",
302 | "regex-syntax",
303 | ]
304 |
305 | [[package]]
306 | name = "regex-syntax"
307 | version = "0.8.5"
308 | source = "registry+https://github.com/rust-lang/crates.io-index"
309 | checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
310 |
311 | [[package]]
312 | name = "rustc-hash"
313 | version = "2.0.0"
314 | source = "registry+https://github.com/rust-lang/crates.io-index"
315 | checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152"
316 |
317 | [[package]]
318 | name = "smartstring"
319 | version = "1.0.1"
320 | source = "registry+https://github.com/rust-lang/crates.io-index"
321 | checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
322 | dependencies = [
323 | "autocfg",
324 | "static_assertions",
325 | "version_check",
326 | ]
327 |
328 | [[package]]
329 | name = "static_assertions"
330 | version = "1.1.0"
331 | source = "registry+https://github.com/rust-lang/crates.io-index"
332 | checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
333 |
334 | [[package]]
335 | name = "strsim"
336 | version = "0.11.1"
337 | source = "registry+https://github.com/rust-lang/crates.io-index"
338 | checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
339 |
340 | [[package]]
341 | name = "syn"
342 | version = "2.0.72"
343 | source = "registry+https://github.com/rust-lang/crates.io-index"
344 | checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af"
345 | dependencies = [
346 | "proc-macro2",
347 | "quote",
348 | "unicode-ident",
349 | ]
350 |
351 | [[package]]
352 | name = "tikv-jemalloc-sys"
353 | version = "0.6.1+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
354 | source = "registry+https://github.com/rust-lang/crates.io-index"
355 | checksum = "cd8aa5b2ab86a2cefa406d889139c162cbb230092f7d1d7cbc1716405d852a3b"
356 | dependencies = [
357 | "cc",
358 | "libc",
359 | ]
360 |
361 | [[package]]
362 | name = "tikv-jemallocator"
363 | version = "0.6.1"
364 | source = "registry+https://github.com/rust-lang/crates.io-index"
365 | checksum = "0359b4327f954e0567e69fb191cf1436617748813819c94b8cd4a431422d053a"
366 | dependencies = [
367 | "libc",
368 | "tikv-jemalloc-sys",
369 | ]
370 |
371 | [[package]]
372 | name = "topfew"
373 | version = "0.2.4"
374 | dependencies = [
375 | "anyhow",
376 | "clap",
377 | "quickcheck",
378 | "rayon",
379 | "regex",
380 | "rustc-hash",
381 | "smartstring",
382 | "tikv-jemallocator",
383 | ]
384 |
385 | [[package]]
386 | name = "unicode-ident"
387 | version = "1.0.12"
388 | source = "registry+https://github.com/rust-lang/crates.io-index"
389 | checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
390 |
391 | [[package]]
392 | name = "utf8parse"
393 | version = "0.2.2"
394 | source = "registry+https://github.com/rust-lang/crates.io-index"
395 | checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
396 |
397 | [[package]]
398 | name = "version_check"
399 | version = "0.9.5"
400 | source = "registry+https://github.com/rust-lang/crates.io-index"
401 | checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
402 |
403 | [[package]]
404 | name = "wasi"
405 | version = "0.11.0+wasi-snapshot-preview1"
406 | source = "registry+https://github.com/rust-lang/crates.io-index"
407 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
408 |
409 | [[package]]
410 | name = "windows-sys"
411 | version = "0.52.0"
412 | source = "registry+https://github.com/rust-lang/crates.io-index"
413 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
414 | dependencies = [
415 | "windows-targets",
416 | ]
417 |
418 | [[package]]
419 | name = "windows-targets"
420 | version = "0.52.6"
421 | source = "registry+https://github.com/rust-lang/crates.io-index"
422 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
423 | dependencies = [
424 | "windows_aarch64_gnullvm",
425 | "windows_aarch64_msvc",
426 | "windows_i686_gnu",
427 | "windows_i686_gnullvm",
428 | "windows_i686_msvc",
429 | "windows_x86_64_gnu",
430 | "windows_x86_64_gnullvm",
431 | "windows_x86_64_msvc",
432 | ]
433 |
434 | [[package]]
435 | name = "windows_aarch64_gnullvm"
436 | version = "0.52.6"
437 | source = "registry+https://github.com/rust-lang/crates.io-index"
438 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
439 |
440 | [[package]]
441 | name = "windows_aarch64_msvc"
442 | version = "0.52.6"
443 | source = "registry+https://github.com/rust-lang/crates.io-index"
444 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
445 |
446 | [[package]]
447 | name = "windows_i686_gnu"
448 | version = "0.52.6"
449 | source = "registry+https://github.com/rust-lang/crates.io-index"
450 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
451 |
452 | [[package]]
453 | name = "windows_i686_gnullvm"
454 | version = "0.52.6"
455 | source = "registry+https://github.com/rust-lang/crates.io-index"
456 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
457 |
458 | [[package]]
459 | name = "windows_i686_msvc"
460 | version = "0.52.6"
461 | source = "registry+https://github.com/rust-lang/crates.io-index"
462 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
463 |
464 | [[package]]
465 | name = "windows_x86_64_gnu"
466 | version = "0.52.6"
467 | source = "registry+https://github.com/rust-lang/crates.io-index"
468 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
469 |
470 | [[package]]
471 | name = "windows_x86_64_gnullvm"
472 | version = "0.52.6"
473 | source = "registry+https://github.com/rust-lang/crates.io-index"
474 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
475 |
476 | [[package]]
477 | name = "windows_x86_64_msvc"
478 | version = "0.52.6"
479 | source = "registry+https://github.com/rust-lang/crates.io-index"
480 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
481 |
--------------------------------------------------------------------------------
/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "topfew"
3 | version = "0.2.4"
4 | license = "GPL-3.0"
5 | repository = "https://github.com/djc/topfew-rs"
6 | description = "CLI to find high frequency occurrences in structured text files"
7 | keywords = ["cli"]
8 | categories = ["command-line-utilities", "text-processing"]
9 | edition = "2021"
10 |
11 | [dependencies]
12 | anyhow = "1"
13 | clap = { version = "4.0.2", features = ["derive"] }
14 | rayon = "1.3"
15 | regex = "1"
16 | rustc-hash = "2.0"
17 | smartstring = "1"
18 |
19 | [target.'cfg(not(target_env = "msvc"))'.dependencies]
20 | tikv-jemallocator = "0.6"
21 |
22 | [dev-dependencies]
23 | quickcheck = "1.0"
24 |
25 | [profile.release]
26 | codegen-units = 1
27 | opt-level = 3
28 | debug = true
29 | lto = "fat"
30 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # topfew-rs: Rust implementation of Tim Bray's topfew tool
2 |
3 | Tim Bray [blogged about topfew](https://www.tbray.org/ongoing/When/202x/2020/05/18/TopFew),
4 | his Go [implementation](https://github.com/timbray/topfew/) of a tool to replace the
5 | `awk '{print $1}' access_log | sort | uniq -c | sort -rn | head -12` pipeline.
6 | This is a reimplementation of that tool in idiomatic stable Rust.
7 |
8 | The initial Rust version was 2.9x faster than Tim's Go implementation;
9 | current main appears to be around 6.7x faster thanks to some additional optimizations.
10 | The Rust version currently has slightly fewer non-test SLOC than the Go version.
11 | Thanks to [Thomas Andreas Jung](https://github.com/blob79) for contributing substantial
12 | performance improvements.
13 |
14 | ```
15 | Usage: tf [OPTIONS] --fields
16 |
17 | Arguments:
18 | File to search
19 |
20 | Options:
21 | -f, --fields Fields to use as part of the line's key
22 | -n, --num Top number of matches to show [default: 10]
23 | -e, --regex Regular expression used to split lines into fields [default: "[ \\t]"]
24 | -h, --help Print help
25 | ```
26 |
27 | If you have the Rust toolchain installed, you can install it with `cargo install topfew`.
28 |
--------------------------------------------------------------------------------
/deny.toml:
--------------------------------------------------------------------------------
1 | [licenses]
2 | allow = ["Apache-2.0", "MIT", "MPL-2.0", "Unicode-DFS-2016"]
3 | exceptions = [
4 | { allow = ["GPL-3.0"], name = "topfew" },
5 | ]
6 |
--------------------------------------------------------------------------------
/src/bin/tf.rs:
--------------------------------------------------------------------------------
1 | use std::str::FromStr;
2 |
3 | use anyhow::{anyhow, Error};
4 | use clap::Parser;
5 | use regex::Regex;
6 |
7 | use topfew::{top_few_from_stream, KeyFinder};
8 |
9 | fn main() -> Result<(), Error> {
10 | let options = Options::parse();
11 | if options.num < 1 {
12 | return Err(anyhow!(
13 | "--num needs to be 1 or larger, got {}",
14 | options.num
15 | ));
16 | }
17 | let sep = Regex::new(&options.regex)?;
18 | let kf = KeyFinder::new(Some(options.fields.indices), sep)?;
19 | let top_list = top_few_from_stream(options.file.into(), &kf, options.num)?;
20 | for kc in top_list {
21 | println!("{} {}", kc.count, kc.key);
22 | }
23 | Ok(())
24 | }
25 |
26 | #[derive(Debug, Parser)]
27 | #[clap(name = "topfew")]
28 | struct Options {
29 | /// Fields to use as part of the line's key
30 | #[clap(long, short)]
31 | fields: FieldSpec,
32 | /// Top number of matches to show
33 | #[clap(long, short, default_value = "10")]
34 | num: usize,
35 | /// Regular expression used to split lines into fields
36 | #[clap(long, short = 'e', default_value = "[ \\t]")]
37 | regex: String,
38 | /// File to search
39 | file: String,
40 | }
41 |
42 | #[derive(Debug, Clone)]
43 | struct FieldSpec {
44 | indices: Vec,
45 | }
46 |
47 | impl FromStr for FieldSpec {
48 | type Err = Error;
49 | fn from_str(s: &str) -> Result {
50 | let mut indices = Vec::new();
51 | for f in s.split(',') {
52 | indices.push(usize::from_str(f)?);
53 | }
54 | Ok(FieldSpec { indices })
55 | }
56 | }
57 |
58 | #[cfg(not(target_env = "msvc"))]
59 | use tikv_jemallocator::Jemalloc;
60 |
61 | #[cfg(not(target_env = "msvc"))]
62 | #[global_allocator]
63 | static GLOBAL: Jemalloc = Jemalloc;
64 |
--------------------------------------------------------------------------------
/src/chunks.rs:
--------------------------------------------------------------------------------
1 | use std::fs::File;
2 | use std::io::{BufRead, BufReader, Seek, SeekFrom};
3 | use std::path::PathBuf;
4 | use std::thread::available_parallelism;
5 |
6 | use anyhow::Context;
7 |
8 | /// Create an Iterator over chunks of lines of a file.
9 | ///
10 | /// The last line in a chunk potentially reads over the chunk byte boundary to find the line end.
11 | /// In the same way the first line searches the line end.
12 | pub fn chunks(path: PathBuf) -> anyhow::Result> {
13 | let size = File::open(&path)?.metadata()?.len();
14 | Ok(Chunker::new(
15 | FileSource { path },
16 | size,
17 | size / available_parallelism()?.get() as u64,
18 | ))
19 | }
20 |
21 | pub struct Chunker {
22 | source: S,
23 | position: u64,
24 | count: usize,
25 | chunks: usize,
26 | chunk_size: u64,
27 | size: u64,
28 | }
29 |
30 | impl Chunker {
31 | fn new(source: S, size: u64, chunk_size: u64) -> Self {
32 | let chunks = if chunk_size == 0 {
33 | 0
34 | } else {
35 | size / chunk_size + 1.min(size % chunk_size)
36 | } as usize;
37 |
38 | Chunker {
39 | source,
40 | position: 0,
41 | count: 0,
42 | chunks,
43 | chunk_size,
44 | size,
45 | }
46 | }
47 | }
48 |
49 | impl Iterator for Chunker {
50 | type Item = Chunk;
51 |
52 | fn next(&mut self) -> Option {
53 | if self.count == self.chunks {
54 | return None;
55 | }
56 |
57 | let start = (self.count as u64) * self.chunk_size;
58 | self.count += 1;
59 | let input = self.source.call().ok()?;
60 | let (chunk, position) =
61 | Chunk::new(input, self.chunk_size, self.position, start, self.size).ok()?;
62 | self.position = position;
63 | Some(chunk)
64 | }
65 | }
66 |
67 | #[derive(Debug)]
68 | pub struct Chunk {
69 | lines: std::io::Lines,
70 | position: u64,
71 | end: u64,
72 | }
73 |
74 | impl Chunk {
75 | pub fn new(
76 | mut input: C,
77 | chunk_size: u64,
78 | mut position: u64,
79 | start: u64,
80 | size: u64,
81 | ) -> anyhow::Result<(Self, u64)>
82 | where
83 | C: Seek + BufRead,
84 | {
85 | let skip = if position > start {
86 | true
87 | } else if start != position {
88 | input.seek(SeekFrom::Start(start - 1))?;
89 | let mut buf = [0; 1];
90 | if let Ok(1) = input.read(&mut buf) {
91 | buf[0] != b'\n'
92 | } else {
93 | false
94 | }
95 | } else {
96 | false
97 | };
98 |
99 | input.seek(SeekFrom::Start(start))?;
100 | position = if skip {
101 | let mut skip_leader = String::new();
102 | let _ = input.read_line(&mut skip_leader)?;
103 | start + skip_leader.len() as u64
104 | } else {
105 | start
106 | };
107 | let lines = input.lines();
108 | let c = Self {
109 | lines,
110 | position,
111 | end: size.min(start + chunk_size),
112 | };
113 | Ok((c, position))
114 | }
115 | }
116 |
117 | impl Iterator for Chunk
118 | where
119 | T: Seek + BufRead,
120 | {
121 | type Item = String;
122 | fn next(&mut self) -> Option {
123 | if self.position >= self.end {
124 | return None;
125 | }
126 | let l = self.lines.next()?.ok()?;
127 | self.position += l.len() as u64 + 1;
128 | Some(l)
129 | }
130 | }
131 |
132 | pub trait Source: Sized {
133 | type Item: Seek + BufRead;
134 |
135 | fn call(&self) -> anyhow::Result;
136 | }
137 |
138 | pub struct FileSource {
139 | path: PathBuf,
140 | }
141 |
142 | impl Source for FileSource {
143 | type Item = BufReader;
144 |
145 | fn call(&self) -> anyhow::Result {
146 | File::open(&self.path)
147 | .map(BufReader::new)
148 | .with_context(|| "Failed")
149 | }
150 | }
151 |
152 | #[cfg(test)]
153 | mod tests {
154 | use super::*;
155 | use quickcheck::TestResult;
156 | use std::io::Cursor;
157 |
158 | #[test]
159 | fn test_chunks() {
160 | fn test_split_buf(i: Vec, chunk_size: u64) -> TestResult {
161 | fn t(b: String, chunk_size: u64) -> anyhow::Result<()> {
162 | let bytes = b.as_bytes().to_owned();
163 | let size = bytes.len() as u64;
164 | let source = MemorySource { bytes };
165 |
166 | let chunks: Vec<_> = Chunker::new(source, size, chunk_size)
167 | .map(|i| i.collect::>().join("\n"))
168 | .collect();
169 |
170 | let r = regex::Regex::new(r"\s+")?;
171 | let e = r.replace_all(&b, " ");
172 | let cs = chunks.join(" ");
173 | let a = r.replace_all(&cs, " ");
174 | if e.trim() == a.trim() {
175 | Ok(())
176 | } else {
177 | Err(anyhow::anyhow!(
178 | "Expected >{}< Actual >{}<",
179 | e.trim(),
180 | a.trim()
181 | ))
182 | }
183 | }
184 |
185 | let filter = regex::Regex::new(r"\W").unwrap();
186 | if chunk_size < 1 || i.iter().any(|s| filter.is_match(s)) {
187 | TestResult::discard()
188 | } else if let Err(e) = t(i.join("\n"), chunk_size) {
189 | TestResult::error(format!("{:?}", e))
190 | } else {
191 | TestResult::from_bool(true)
192 | }
193 | }
194 | quickcheck::QuickCheck::new()
195 | .max_tests(300)
196 | .quickcheck(test_split_buf as fn(_, _) -> TestResult);
197 | }
198 |
199 | struct MemorySource {
200 | bytes: Vec,
201 | }
202 |
203 | impl Source for MemorySource {
204 | type Item = Cursor>;
205 |
206 | fn call(&self) -> anyhow::Result {
207 | Ok(Cursor::new(self.bytes.clone()))
208 | }
209 | }
210 | }
211 |
--------------------------------------------------------------------------------
/src/counter.rs:
--------------------------------------------------------------------------------
1 | use rustc_hash::FxHashMap;
2 | use smartstring::alias::String;
3 |
4 | #[derive(Default)]
5 | pub struct Counter {
6 | counts: FxHashMap,
7 | top: FxHashMap,
8 | threshold: u64,
9 | num: usize,
10 | }
11 |
12 | impl Counter {
13 | pub fn new(num: Option) -> Self {
14 | Self {
15 | num: num.unwrap_or(0),
16 | threshold: num.map(|_| 0).unwrap_or(u64::MAX),
17 | ..Default::default()
18 | }
19 | }
20 |
21 | pub fn add(&mut self, key: &str, added: u64) {
22 | let count = match self.counts.get_mut(key) {
23 | Some(count) => {
24 | *count += added;
25 | *count
26 | }
27 | None => {
28 | self.counts.insert(key.into(), added);
29 | added
30 | }
31 | };
32 |
33 | if count < self.threshold {
34 | return;
35 | }
36 |
37 | if let Some(t) = self.top.get_mut(key) {
38 | *t = count;
39 | return;
40 | }
41 |
42 | self.top.insert(key.into(), count);
43 | if self.top.len() < self.num * 2 {
44 | return;
45 | }
46 |
47 | let mut top_values = self.top.values().collect::>();
48 | top_values.sort_unstable();
49 | let threshold = *top_values[self.num - 1];
50 | self.threshold = threshold;
51 | self.top.retain(|_, v| *v > threshold);
52 | }
53 |
54 | pub fn top(&self) -> Vec {
55 | let mut top = Vec::with_capacity(self.num);
56 | for (key, &count) in &self.top {
57 | top.push(KeyCount {
58 | count,
59 | key: key.clone(),
60 | });
61 | }
62 |
63 | top.sort_unstable();
64 | top.reverse();
65 | if self.num > 0 {
66 | top.truncate(self.num);
67 | }
68 | top
69 | }
70 |
71 | pub fn merge(mut self, r: Counter) -> Self {
72 | for (key, count) in r.counts.iter() {
73 | self.add(key, *count);
74 | }
75 | self
76 | }
77 | }
78 |
79 | #[derive(PartialEq, PartialOrd, Eq, Ord)]
80 | pub struct KeyCount {
81 | pub count: u64,
82 | pub key: String,
83 | }
84 |
--------------------------------------------------------------------------------
/src/key_finder.rs:
--------------------------------------------------------------------------------
1 | use anyhow::{anyhow, Error};
2 | use regex::Regex;
3 |
4 | pub struct KeyFinder {
5 | keys: Option<(usize, usize, Vec)>,
6 | sep: Regex,
7 | }
8 |
9 | impl KeyFinder {
10 | pub fn new(keys: Option>, sep: Regex) -> Result {
11 | let keys = keys.map(|mut keys| {
12 | keys.sort_unstable();
13 |
14 | let last = *keys.last().unwrap();
15 | (
16 | keys.len(),
17 | last,
18 | (0..=last)
19 | .map(|i| keys.contains(&(i + 1)))
20 | .collect::>(),
21 | )
22 | });
23 | Ok(KeyFinder { keys, sep })
24 | }
25 |
26 | pub fn key<'a>(&self, record: &'a str, s: &'a mut String) -> Result<&'a str, Error> {
27 | let (num, last, keep) = match &self.keys {
28 | None => return Ok(record),
29 | Some((num, _, _)) if *num == 0 => return Ok(record),
30 | Some((num, last, keep)) => (num, last, keep),
31 | };
32 |
33 | let mut fields = keep
34 | .iter()
35 | .zip(self.sep.splitn(record, last + 2))
36 | .filter_map(|(keep, field)| if *keep { Some(field) } else { None });
37 |
38 | if *num == 1 {
39 | return match fields.next() {
40 | Some(f) => Ok(f),
41 | None => Err(anyhow!("not enough fields to make key")),
42 | };
43 | }
44 |
45 | let mut found = 0;
46 | for f in fields {
47 | s.push(' ');
48 | s.push_str(f);
49 | found += 1;
50 | }
51 |
52 | if found == *num {
53 | Ok(s)
54 | } else {
55 | Err(anyhow!("not enough fields to make key"))
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/lib.rs:
--------------------------------------------------------------------------------
1 | use std::path::PathBuf;
2 |
3 | use rayon::iter::{IntoParallelIterator, ParallelIterator};
4 |
5 | mod chunks;
6 | mod counter;
7 | mod key_finder;
8 |
9 | pub use chunks::chunks;
10 | pub use counter::{Counter, KeyCount};
11 | pub use key_finder::KeyFinder;
12 |
13 | pub fn top_few_from_stream(
14 | path: PathBuf,
15 | kf: &KeyFinder,
16 | num: usize,
17 | ) -> anyhow::Result> {
18 | let total = chunks(path)?
19 | .collect::>()
20 | .into_par_iter()
21 | .map(|reader| {
22 | let mut counter = Counter::new(None);
23 | let mut s = String::new();
24 | for ln in reader {
25 | s.clear();
26 | if let Ok(key) = kf.key(&ln, &mut s) {
27 | counter.add(key, 1)
28 | }
29 | }
30 | counter
31 | })
32 | .reduce(|| Counter::new(Some(num)), |l, r| l.merge(r));
33 |
34 | Ok(total.top())
35 | }
36 |
--------------------------------------------------------------------------------