Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phorward/num-parse
Generic, JavaScript-like parseInt() and parseFloat() functions for Rust.
https://github.com/phorward/num-parse
javascript-style parseint rust-lang
Last synced: about 2 months ago
JSON representation
Generic, JavaScript-like parseInt() and parseFloat() functions for Rust.
- Host: GitHub
- URL: https://github.com/phorward/num-parse
- Owner: phorward
- License: mit
- Created: 2022-06-28T13:37:07.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-02T00:08:51.000Z (3 months ago)
- Last Synced: 2024-10-06T18:36:17.413Z (3 months ago)
- Topics: javascript-style, parseint, rust-lang
- Language: Rust
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# num-parse
[![Build status](https://github.com/phorward/num-parse/actions/workflows/main.yml/badge.svg)](https://github.com/phorward/num-parse/actions/workflows/main.yml)
[![docs.rs](https://img.shields.io/docsrs/num-parse)](https://docs.rs/num-parse/latest/num_parse/)
[![crates.io](https://img.shields.io/crates/v/num-parse)](https://crates.io/crates/num-parse)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)Generic, JavaScript-like parseInt() functions for Rust.
This crate is intended to provide a fast and generic `parseInt()`-like implementation for Rust, which mostly follows the specification described in the [MDN parseInt() documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt).
Likewise in JavaScript, a `parseFloat()`-like implementation for float-types is planned as well, therefore the crate has been named `num-parse` already, althought it currently provides `parse_int()` and variative functions only.
## parse_int(), parse_uint()
`parse_int()` and `parse_uint()` are generic interfaces to parse integers from string. Whitespace in front of the parsed number is being ignored, same as anything beyond a valid number.
```rust
use num_parse::*;assert_eq!(parse_uint::("+123 as i32 "), Some(123i32));
assert_eq!(parse_int::(" -123 as i32 "), Some(-123i32));
assert_eq!(parse_uint::("+123 as i64 "), Some(123i64));
assert_eq!(parse_int::(" -123 as i64 "), Some(-123i64));assert_eq!(parse_int::(" - 1 is invalid "), None);
assert_eq!(
parse_uint::(" -123 as u64, parse_int() not available for this type "),
None
);
assert_eq!(
parse_uint::(" 0xcafebabe triggers hex-mode parsing "),
Some(3405691582usize)
);
```