diff --git a/src/fraction/mod.rs b/src/fraction/mod.rs index 599e8100..0ab97a15 100644 --- a/src/fraction/mod.rs +++ b/src/fraction/mod.rs @@ -7,7 +7,7 @@ pub mod approx; pub mod display; #[cfg(feature = "with-unicode")] -mod unicode_str_io; +pub mod unicode_str_io; #[cfg(feature = "with-juniper-support")] pub mod juniper_support; diff --git a/src/fraction/unicode_str_io.rs b/src/fraction/unicode_str_io.rs index d9e6904d..c090d52d 100644 --- a/src/fraction/unicode_str_io.rs +++ b/src/fraction/unicode_str_io.rs @@ -190,7 +190,7 @@ impl GenericFraction { } } -impl> GenericFraction { +impl> GenericFraction { /// Parse a unicode string /// The string can be: /// - A normal fraction e.g. "1/2" @@ -215,8 +215,8 @@ impl> GenericFraction { /// ("1/2", Fraction::new(1u8,2u8)), /// ("-1/2", Fraction::new_neg(1u8,2u8)), /// ("½", Fraction::new(1u8,2u8)), - /// // ("1½", Fraction::new(1u8,2u8)), - /// // ("-1½", Fraction::new_neg(1u8,2u8)), + /// // ("1½", Fraction::new(3u8,2u8)), // mixed vulgar fractions + /// // ("-1½", Fraction::new_neg(3u8,2u8)), // currently not supported /// ("1⁄2", Fraction::new(1u8,2u8)), /// ("-1⁄2", Fraction::new_neg(1u8,2u8)), /// ("1⁤1⁄2", Fraction::new(3u8,2u8)), @@ -312,7 +312,6 @@ impl> GenericFraction { if let Some(idx) = first.find(&['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹', '⁰'][..]) { - println!("Supsub! {}", first); let trunc = if idx.is_zero() { T::zero() } else { @@ -343,8 +342,6 @@ impl> GenericFraction { ) else { return Err(ParseError::ParseIntError); }; - // numer = n; - println!("numer: {}", &numer); let Ok(denom) = T::from_str_radix( // let n = &denom_str @@ -367,9 +364,6 @@ impl> GenericFraction { ) else { return Err(ParseError::ParseIntError); }; - println!("denom: {}", denom); - // denom = d; - // numer = numer + trunc * denom.clone(); Ok(GenericFraction::Rational( sign, Ratio::new(numer + trunc * denom.clone(), denom), @@ -494,8 +488,6 @@ mod tests { for (string, frac) in test_vec { println!("{} ?= {}", string, frac); assert_eq!(Fraction::from_unicode_str(string), Ok(frac)); - // println!("{} ?= {}", string, frac); - // assert_eq!(format!("{}", frac.get_unicode_display()), string); } } @@ -544,7 +536,6 @@ mod tests { ("-1", -Fraction::one()), ("5", Fraction::from(5)), ("1\u{2064}1⁄2", Fraction::new(3u8, 2u8)), - // ("1⁣1⁄2", Fraction::new(3u8, 2u8)), ("-1\u{2064}1⁄2", Fraction::new_neg(3u8, 2u8)), ("1⁄2", Fraction::new(1u8, 2u8)), ("-1⁄2", Fraction::new_neg(1u8, 2u8)), @@ -559,11 +550,11 @@ mod tests { #[test] fn test_from_fail() { + // TODO: "nanBOGUS" and "∞BOGUS" will parse. + // Either make that everything with BOGUS + // after will parse, or make ^those fail. let test_vec = vec![ "asdf", - // "NanBOGUS", - // "nanBOGUS", - // "+∞BOGUS", "+1BOGUS", "+5BOGUS", "1⁤1⁄2BOGUS", diff --git a/src/lib.rs b/src/lib.rs index 45a4d3e9..5a70aa67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,7 @@ //! - `with-bigint` (default) integration with [num::BigInt] and [num::BigUint] data types //! - `with-decimal` (default) [Decimal] type implemented upon [GenericFraction] //! - `with-dynaint` (default) dynamically growing integer avoiding stack overflows +//! - `with-unicode` Unicode formatting and parsing options //! - `with-approx` adds methods for approximate computations (currently `sqrt`) //! - `with-juniper-support` [Juniper](https://crates.io/crates/juniper) integration //! - `with-postgres-support` [PostgreSQL](https://crates.io/crates/postgres) integration; Numeric/Decimal type @@ -154,6 +155,19 @@ //! assert_eq!(format!("{:#.3}", result), "1.750"); // to print leading zeroes, pass hash to the format //! ``` //! +//! Additionally, there are [methods](GenericFraction::get_unicode_display) available for various unicode display options: +//! See [this SO answer](https://stackoverflow.com/a/77861320/14681457) for a discussion. +//! +//! ``` +//! type F = fraction::Fraction; +//! +//! let res = F::from(0.7) / F::from(0.4); +//! assert_eq!("7⁄4",format!("{}", res.get_unicode_display())); // needs font support. Unicode way +//! assert_eq!("1⁤3⁄4",format!("{}", res.get_unicode_display().mixed())); // interpreted wrongly without font support +//! assert_eq!("⁷/₄",format!("{}", res.get_unicode_display().supsub())); // no need for font support +//! assert_eq!("1³/₄",format!("{}", res.get_unicode_display().supsub().mixed())); +//! ``` +//! //! ## Convert into/from other types //! //! Both `fraction` and `decimal` types implement