# parse-numeric-range
[](#contributors-)
[](https://crates.io/crates/parse-numeric-range)
[](https://docs.rs/parse-numeric-range)
[](https://github.com/trananhtung/parse-numeric-range/actions/workflows/ci.yml)
[](#license)
**Parse a string of numbers and ranges into a list of integers.**
`"1,3-5,8"` → `[1, 3, 4, 5, 8]`. A faithful Rust port of the
[`parse-numeric-range`](https://www.npmjs.com/package/parse-numeric-range) npm package
(used by syntax highlighters for line-range selection).
- **Zero dependencies**, **`#![no_std]`**
- Inclusive (`-`, `..`, `‥`) and exclusive (`...`, `…`, `⋯`) ranges
- Negative numbers and descending ranges
- Differential-tested against the reference `parse-numeric-range` implementation
## Install
```toml
[dependencies]
parse-numeric-range = "0.1"
```
## Usage
```rust
use parse_numeric_range::parse;
assert_eq!(parse("1,3-5,8"), [1, 3, 4, 5, 8]);
assert_eq!(parse("1-3,7,9-11"), [1, 2, 3, 7, 9, 10, 11]);
assert_eq!(parse("5-1"), [5, 4, 3, 2, 1]); // descending
assert_eq!(parse("-5--2"), [-5, -4, -3, -2]); // negatives
assert_eq!(parse("1...5"), [1, 2, 3, 4]); // exclusive
assert_eq!(parse("nope"), [] as [i64; 0]); // junk is ignored
```
## Separators
| `-`, `..`, `‥` | inclusive of the end | `1-3` | `[1, 2, 3]` |
| `...`, `…`, `⋯` | exclusive of the end | `1...3` | `[1, 2]` |
Whitespace around each comma-separated entry is trimmed; unrecognized entries are ignored,
and duplicates are kept (no deduplication). Numbers are parsed as `i64`. Like the reference,
ranges are fully expanded, so a very large range allocates one entry per value.
## 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:
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/trananhtung"><img src="https://avatars.githubusercontent.com/u/30992229?v=4?s=100" width="100px;" alt="Tung Tran"/><br /><sub><b>Tung Tran</b></sub></a><br /><a href="https://github.com/trananhtung/./commits?author=trananhtung" title="Code">💻</a> <a href="#maintenance-trananhtung" title="Maintenance">🚧</a></td>
</tr>
</tbody>
</table>
## License
Licensed under either of [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE) at your option.