Expand description
Light-weight unit parsing of dimension strings into iota.
joto_parse
is intended to offer parsing functions that cover all common dimension parsing needs
for typesetting, for architectural and mechanical specification, or for general engineering use.
The functions herein can be evaluated in const contexts.
They do not panic, they do not allocate, and generally finish parsing in under 50 ns on a laptop.
Intended for dimensioning, not for expressing ‘distances’ like an interplanetary voyage.
That having been said, in i64
, the most restrictive supported integer unit, lengths
just over 1 Gm (1,000,000 km) can be represented in iota.
For reference, 1 Gm is more than twice the distance from the center of Earth to the farthest
the far side of the moon gets in orbit.
In f64
, with 53 mantissa bits, lengths up to 1,000 km can be represented in exact iota.
Both of the 128-bit integer types support truly absurd linear distances; in practice I expect
i128
and u128
to be used for area, up to around 2.1/4.2 × 10¹² km²,
and volume up to around 233/466 × 10⁶ m³ respectively.
§Examples
§Basic usage
use joto_constants::length::u64::{FOOT, INCH, MILLIMETER, SIXTY_FOURTH};
use joto_parse::u64::parse_dim;
assert_eq!(parse_dim("2.5cm").unwrap(), 25 * MILLIMETER);
assert_eq!(parse_dim("46'11 37⁄64\"").unwrap(), 46 * FOOT + 11 * INCH + 37 * SIXTY_FOURTH);
§Invertibility
Invertibility is the primary attraction of iota as a base unit. You can add and subtract mixed units with iota, and by extension joto, without loss.
use joto_parse::u128::parse_dim;
fn p(s: impl AsRef<str>) -> u128 {
parse_dim(s.as_ref()).unwrap()
}
assert_eq!(0, p("2.5cm") + p("1⁄64in") + p("0.500 in") - p("37,700μm") - p("1/64″"));
§Constant evaluation
The parsing functions are all const
, so can be used for compile-time constants and statics.
use joto_parse::u128::parse_dim;
use joto_constants::length::u128::{FOOT, INCH, MILLIMETER, SIXTY_FOURTH};
const DIAMETER: u128 = parse_dim("21ft1117⁄32in").unwrap();
assert_eq!(DIAMETER / 2, 10 * FOOT + 11 * INCH + 49 * SIXTY_FOURTH);
Since parsing can be done at compile time, it can also fail at compile time. This example fails to compile because 0.1 nm is too precise to represent:
use joto_parse::f64::parse_dim;
const FAIL: f64 = parse_dim("0.1 nm").unwrap();
Modules§
- f64
- Parsing functions for dimensions.
- i64
- Parsing functions for dimensions.
- i128
- Parsing functions for dimensions.
- u64
- Parsing functions for dimensions.
- u128
- Parsing functions for dimensions.
Enums§
- Parse
Error - Unit
- Unit type for parsing.
Functions§
- strip_
unit - Detect a unit at the end of a dimension string, returning the rest of the
str
and theUnit
.