#range #string #indices #parse #selection

choice-string

Provides utilities to parse selections (indices and ranges) from a string

1 unstable release

0.0.1 Apr 15, 2023

#33 in #selection

Download history 9775/week @ 2024-06-10 11843/week @ 2024-06-17 14238/week @ 2024-06-24 11290/week @ 2024-07-01 6616/week @ 2024-07-08 5819/week @ 2024-07-15 6944/week @ 2024-07-22 5694/week @ 2024-07-29 4912/week @ 2024-08-05 4742/week @ 2024-08-12 6353/week @ 2024-08-19 5374/week @ 2024-08-26 6750/week @ 2024-09-02 5342/week @ 2024-09-09 3490/week @ 2024-09-16 6677/week @ 2024-09-23

22,372 downloads per month
Used in sonnerie

MIT/Apache

14KB
296 lines

choice-string

Provides utilities to parse selections (indices and ranges) from a string


Features

Parsing selection strings

For this crate, the format of the selection string can be understood through the following example:

The following packages are available:
(1) good-package    (2) bad-package     (3) decent-package
(4) awful-package   (5) amazing-package (6) package6
(7) package7        (8) package8    
Select which packages to install:
>> 1 3 5 6-8

The following packages are being installed:
good-package, decent-package, amazing-package, package6,
package7, package8
...

In this case, the user has selected indices 1, 3, 5, and all between (inclusive) 6 through 8.

Simple API

let selection =  choice_string::parse("1, 2, 3, 4-8").expect("parse error");
assert!(selection.contains_item(5));

Flexible syntax

The delimiters are flexible. Any of the following work:

  • - A space
  • , - A comma
  • ; - A semicolon

You can also use any combination of the above in a single string, and even mix as single separators.

For example:

  • 1, 2, 3,5 6-9
  • 1-8 11, 12
  • 1,2,3,4
  • 1;2;3;4

Condenses to minimal representation

After parsing, the provided ranges are condensed by a union operation.

For example:

  • 1, 2, 3, 4-5, 11 -> 1-5, 11

Dependencies

~1.3–2MB
~40K SLoC