parse-numeric-range 0.1.0

Parse a string of numbers and ranges (e.g. 1,3-5,8) into a list of integers. A faithful port of the parse-numeric-range npm package. Zero dependencies, no_std.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented2 out of 2 items with examples
  • Size
  • Source code size: 27.14 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 126.9 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/parse-numeric-range
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

parse-numeric-range

All Contributors

crates.io docs.rs CI 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 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

[dependencies]
parse-numeric-range = "0.1"

Usage

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

Separator Meaning Example Result
-, .., 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 specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the 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 MIT or Apache-2.0 at your option.