number_based/
lib.rs

1//! # Overview
2//!
3//! Converting a uBase/iBase instance
4//! ```rust
5//! // converting down
6//!
7//! // create uBase instance
8//! let mut number = uBase::from_string(7863, "NUMBERBASED");
9//!
10//! // convert number to base 10
11//! number.convert(10);
12//!
13//! assert_eq!(number.display(), String::from("20781774882369576414324066149192513674771"));
14//! // Do aslso note that number.conver(10).display() is equivalent to number.as_decimal() with the
15//! // important distinction that as_decimal() returns a u128 and not a string
16//!
17//!
18//!
19//!
20//!
21//! // converting up
22//!
23//! // create uBase instance
24//! let mut other_number = uBase::from_string(10, "120387517860123746975");
25//!
26//! // convert number to base 10000
27//! other_number.convert(10000);
28//!
29//! assert_eq!(other_number.display(), String::from("1ࡒᶹ⇵ঢᮛ"));
30//! ```
31//!
32//! Performing operations with uBase/iBase instances
33//!
34//! ```rust
35//! // create the iBase instances
36//! let number1 = iBase::from_string(30000, "-ð䧈炙㞈榻");
37//! assert_eq!(number1.as_decimal(), -120387517860123746975);
38//!
39//! let number2 = iBase::from_string(30000, "20");
40//! assert_eq!(number2.as_decimal(), 60000);
41//!
42//! // divide the numbers
43//! let quotient = number1 / number2;
44//! // other operations such as addition, subtraction, and multiplication are also available with
45//! // the operators "+", "-", and "*" respectively
46//!
47//! assert_eq!(quotient.display(), String::from("-¦┒㡺嚊"));
48//! assert_eq!(quotient.as_decimal(), -120387517860123746975 / 60000);
49//! ```
50
51use std::{
52    fmt::Display,
53    ops::{Add, Div, Mul, Sub},
54};
55
56mod unsigned_integer;
57use crate::unsigned_integer::ubase;
58
59mod signed_integer;
60use crate::signed_integer::ibase;
61
62mod utils;
63
64mod graphemes;
65
66#[allow(dead_code, non_camel_case_types)]
67pub type uBase = ubase::uBase;
68
69#[allow(dead_code, non_camel_case_types)]
70pub type iBase = ibase::iBase;
71
72pub trait NumberBase: Add + Sub + Div + Mul + Display + Sized + Clone {
73    /// The type being returned when a decimal is to be returned
74    /// For instance, the as_octal() function returns either an integer or float depending on
75    /// whether the struct implementing NumberBase only accepts integers
76    type DecimalType;
77
78    /// returns the base of the number
79    fn base(&self) -> u16;
80
81    /// returns true if the number is greater than the input for this funciton
82    fn greater_than(&self, number: Self) -> bool;
83
84    /// returns the vector containg the digits in base 10
85    fn digits(&self) -> &Vec<u16>;
86
87    /// creates an instance of Self from a string
88    fn from_string(base: u16, number: &str) -> Self;
89
90    /// creates and instance of Self from a vector
91    fn from_vec(base: u16, number: Vec<u16>) -> Self;
92
93    /// converts the number to the inputted number base
94    fn convert(&mut self, base: u16) -> Self;
95
96    /// returns the value of the number as a string
97    fn display(&self) -> String;
98
99    /// returns an instance of self but in binary
100    fn as_binary(&self) -> Self::DecimalType;
101
102    /// returns an instance of self but in octal
103    fn as_octal(&self) -> Self::DecimalType;
104
105    /// returns an instance of self but in decimal
106    fn as_decimal(&self) -> Self::DecimalType;
107
108    /// returns an instance of self but in hex
109    fn as_hex(&self) -> String;
110}