Skip to content

Commit

Permalink
feat:unicode removed debug prints, added docs to lib.rs, some polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
feefladder committed Feb 7, 2024
1 parent 242545b commit feefadd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/fraction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 6 additions & 15 deletions src/fraction/unicode_str_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl<T: Clone + Integer + fmt::Display> GenericFraction<T> {
}
}

impl<T: Clone + Integer + fmt::Display + From<u8>> GenericFraction<T> {
impl<T: Clone + Integer + From<u8>> GenericFraction<T> {
/// Parse a unicode string
/// The string can be:
/// - A normal fraction e.g. "1/2"
Expand All @@ -215,8 +215,8 @@ impl<T: Clone + Integer + fmt::Display + From<u8>> GenericFraction<T> {
/// ("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)),
Expand Down Expand Up @@ -312,7 +312,6 @@ impl<T: Clone + Integer + fmt::Display + From<u8>> GenericFraction<T> {
if let Some(idx) =
first.find(&['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹', '⁰'][..])
{
println!("Supsub! {}", first);
let trunc = if idx.is_zero() {
T::zero()
} else {
Expand Down Expand Up @@ -343,8 +342,6 @@ impl<T: Clone + Integer + fmt::Display + From<u8>> GenericFraction<T> {
) else {
return Err(ParseError::ParseIntError);
};
// numer = n;
println!("numer: {}", &numer);
let Ok(denom) = T::from_str_radix(
// let n =
&denom_str
Expand All @@ -367,9 +364,6 @@ impl<T: Clone + Integer + fmt::Display + From<u8>> GenericFraction<T> {
) 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),
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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)),
Expand All @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit feefadd

Please sign in to comment.