https://github.com/trananhtung/numeric-range
Parse and format compact integer-range strings like 1,3-5,7 (page ranges, CPU sets). Bidirectional, zero-dep, no_std.
https://github.com/trananhtung/numeric-range
cpuset no-std page-range parser range rust
Last synced: 1 day ago
JSON representation
Parse and format compact integer-range strings like 1,3-5,7 (page ranges, CPU sets). Bidirectional, zero-dep, no_std.
- Host: GitHub
- URL: https://github.com/trananhtung/numeric-range
- Owner: trananhtung
- License: apache-2.0
- Created: 2026-06-21T11:14:44.000Z (8 days ago)
- Default Branch: main
- Last Pushed: 2026-06-22T14:46:39.000Z (6 days ago)
- Last Synced: 2026-06-22T16:16:52.834Z (6 days ago)
- Topics: cpuset, no-std, page-range, parser, range, rust
- Language: Rust
- Size: 11.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# numeric-range
[](#contributors-)
[](https://crates.io/crates/numeric-range)
[](https://docs.rs/numeric-range)
[](https://github.com/trananhtung/numeric-range/actions/workflows/ci.yml)
[](#license)
[](#no_std)
**Parse and format compact integer-range strings** — the familiar `"1,3-5,7"`
syntax from print dialogs (page ranges), CPU affinity lists (`taskset`, cgroups),
line selectors, and CLI flags. **Both directions**, zero dependencies, `#![no_std]`.
```rust
assert_eq!(numeric_range::parse("1,3-5,7").unwrap(), vec![1, 3, 4, 5, 7]);
assert_eq!(numeric_range::format(&[1, 3, 4, 5, 7]), "1,3-5,7");
```
## Why numeric-range?
Rust's existing crates in this space are parse-only or restrictively licensed.
`numeric-range` is a small, permissive (MIT/Apache-2.0), `no_std` crate that does
**both** parse *and* compact-format, so you can round-trip a user's `--pages`
flag, normalize a CPU set, or render a selection back to its shortest form.
```toml
[dependencies]
numeric-range = "0.1"
```
## API
| Function | Behavior |
| --- | --- |
| `parse(&str) -> Result, ParseError>` | `"1,3-5,7"` → `[1,3,4,5,7]` (input order preserved) |
| `format(&[u64]) -> String` | `[5,3,4,1,7]` → `"1,3-5,7"` (sorted, de-duplicated, runs collapsed) |
- Whitespace around numbers and separators is tolerated (`"1, 3 - 5"`).
- Empty input parses to an empty `Vec`.
- Errors: empty parts (`"1,,2"`), non-numbers (`"a"`, `"+5"`), malformed ranges
(`"1-2-3"`), descending ranges (`"5-1"`), and ranges that would expand beyond
10 million values (so a tiny untrusted string can't exhaust memory).
- Values are unsigned (`u64`), matching page/CPU/line use cases.
## no_std
`numeric-range` is `#![no_std]` and only needs `alloc`. It builds for bare-metal
targets such as `thumbv7em-none-eabi`.
## Contributors ✨
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
## License
Licensed under either of [Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) at
your option.