Coder Social home page Coder Social logo

atreyagaurav / number_range Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 0.0 50 KB

Parse human readable numbers list into an iterable and vice versa

Home Page: https://crates.io/crates/number_range

License: GNU General Public License v3.0

Rust 100.00%
integers iterator parser rust

number_range's Introduction

Introduction

This is a simple library that converts the numbers range in human readable string to numeric type and vice versa. For example: convert between "1-2" & [1,2] or "1,3:5" & [1,3,4,5].

There are mainly two separators (for more types refer docs), list_sep (default ,) and range_sep (default :), the string is first separated by the list separators, and then the individual part is considered a range, there are 3 types of ranges:

  • number ⇒ Single number (e.g. 3)
  • start:end ⇒ Inclusive Range with step 1 (e.g. 1:10)
  • start:step:end ⇒ Inclusive Range with variable step (e.g. 1:2:10)

Disclaimers

This is unstable library, I'll be changing a few things that might break the compatibility with older versions till I can figure things out to make the parsing optimum.

Even though the name is called Number Range, this is made to be used with Integers in mind, although the generics does work for float numbers, the results might not be as expected (which are the limitations of using float operations in rust).

Uses

NumberRange

The simple use case is:

NumberRange::<i64>::default()
	.parse_str("-10,3:10,14:2:20")?;

It'll return you an iterator that you can use to iterate through those numbers. You can collect it in a vector with .collect::Vec<T>(). If you run out of the Iterator and want to iterate again, you can use .parse().

All the numbers in the string must be of the same type that you want to parse into, due to that restriction even the step needs to be unsigned for unsigned integer (meaning "4:-1:1" would fail even if the final output should be unsigned).

NumberRangeOptions

The separators can be customized using the NumberRangeOptions. For example, if you're dealing with unsigned numbers then you can use - as a range separator to parse ranges from many sources.

NumberRangeOptions::new()
             .with_list_sep(',')
             .with_range_sep('-')
             .parse::<usize>("1,3-10,14")?;

Parsing numbers with localization.

let rng: Vec<usize> = NumberRangeOptions::new()
             .with_list_sep('/')
             .with_range_sep('-')
             .with_group_sep(',')
             .with_whitespace(true)
             .parse("1,200/1, 400, 230")?.collect();
assert_eq!(rng, vec![1200, 1400230]);

From Rust List or Vec:

assert_eq!(
    format!("{}", NumberRange::default()
           .from_vec([1,3,4,5,6,7,8,9,10,14], None)),
                     "1,3:10,14");

Links

number_range's People

Contributors

atreyagaurav avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.