From cc82c4762849ea590bf91a81b3a503f740af09ff Mon Sep 17 00:00:00 2001 From: Uttarayan Mondal Date: Mon, 8 Mar 2021 23:40:19 +0530 Subject: [PATCH] Update 2.0 Changes to formatting system Create a formatting struct and then pass it to the to_text macro or the to_text_fmt function to format the strings accordingly Updated docs for the new version --- .gitignore | 1 + Cargo.toml | 4 +- README.md | 2 +- docs/implementors/core/clone/trait.Clone.js | 3 + docs/implementors/core/marker/trait.Copy.js | 3 + docs/implementors/core/marker/trait.Freeze.js | 3 + docs/implementors/core/marker/trait.Send.js | 3 + docs/implementors/core/marker/trait.Sync.js | 3 + docs/implementors/core/marker/trait.Unpin.js | 3 + .../std/panic/trait.RefUnwindSafe.js | 3 + .../std/panic/trait.UnwindSafe.js | 3 + docs/ntext/all.html | 4 +- docs/ntext/fn.digit_to_text.html | 2 +- docs/ntext/fn.to_text_fmt.html | 6 + docs/ntext/formatting/struct.Formatting.html | 10 + docs/ntext/index.html | 38 +- docs/ntext/macro.to_text.html | 4 +- docs/ntext/numtext/fn.digit_to_text.html | 10 + docs/ntext/numtext/fn.to_text_fmt.html | 10 + docs/ntext/sidebar-items.js | 2 +- docs/ntext/struct.Formatting.html | 36 ++ docs/search-index.js | 2 +- docs/source-files.js | 2 +- docs/src/ntext/formatting.rs.html | 131 +++++++ docs/src/ntext/lib.rs.html | 112 +----- docs/src/ntext/numtext.rs.html | 361 ++++++++++++++++++ docs/src/ntext/seperator.rs.html | 2 +- docs/src/ntext/test.rs.html | 48 +-- src/formatting.rs | 62 +++ src/lib.rs | 77 +--- src/no_seperator.rs | 71 ---- src/numtext.rs | 177 +++++++++ src/seperator.rs | 86 ----- src/test.rs | 40 +- 34 files changed, 932 insertions(+), 392 deletions(-) create mode 100644 docs/implementors/core/clone/trait.Clone.js create mode 100644 docs/implementors/core/marker/trait.Copy.js create mode 100644 docs/implementors/core/marker/trait.Freeze.js create mode 100644 docs/implementors/core/marker/trait.Send.js create mode 100644 docs/implementors/core/marker/trait.Sync.js create mode 100644 docs/implementors/core/marker/trait.Unpin.js create mode 100644 docs/implementors/std/panic/trait.RefUnwindSafe.js create mode 100644 docs/implementors/std/panic/trait.UnwindSafe.js create mode 100644 docs/ntext/fn.to_text_fmt.html create mode 100644 docs/ntext/formatting/struct.Formatting.html create mode 100644 docs/ntext/numtext/fn.digit_to_text.html create mode 100644 docs/ntext/numtext/fn.to_text_fmt.html create mode 100644 docs/ntext/struct.Formatting.html create mode 100644 docs/src/ntext/formatting.rs.html create mode 100644 docs/src/ntext/numtext.rs.html create mode 100644 src/formatting.rs delete mode 100644 src/no_seperator.rs create mode 100644 src/numtext.rs delete mode 100644 src/seperator.rs diff --git a/.gitignore b/.gitignore index 96ef6c0..2782907 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target Cargo.lock +index.html diff --git a/Cargo.toml b/Cargo.toml index 809d7cf..f9096b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,7 @@ [package] name = "ntext" -version = "0.1.2" +version = "0.2.0" authors = ["Uttarayan Mondal "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] diff --git a/README.md b/README.md index 7bea74b..8b9228b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Documentation of [ntext-rs](https://uttarayan21.github.io/ntext-rs) generated by cargo doc. -A rust library to get numbers (u32) as words +A rust library to get numbers (usize) as words `12345 ` -> `twelvethousandthreehundredfortyfive` diff --git a/docs/implementors/core/clone/trait.Clone.js b/docs/implementors/core/clone/trait.Clone.js new file mode 100644 index 0000000..92fc98c --- /dev/null +++ b/docs/implementors/core/clone/trait.Clone.js @@ -0,0 +1,3 @@ +(function() {var implementors = {}; +implementors["ntext"] = [{"text":"impl<'format> Clone for Formatting<'format>","synthetic":false,"types":["ntext::formatting::Formatting"]}]; +if (window.register_implementors) {window.register_implementors(implementors);} else {window.pending_implementors = implementors;}})() \ No newline at end of file diff --git a/docs/implementors/core/marker/trait.Copy.js b/docs/implementors/core/marker/trait.Copy.js new file mode 100644 index 0000000..f9da8c0 --- /dev/null +++ b/docs/implementors/core/marker/trait.Copy.js @@ -0,0 +1,3 @@ +(function() {var implementors = {}; +implementors["ntext"] = [{"text":"impl<'format> Copy for Formatting<'format>","synthetic":false,"types":["ntext::formatting::Formatting"]}]; +if (window.register_implementors) {window.register_implementors(implementors);} else {window.pending_implementors = implementors;}})() \ No newline at end of file diff --git a/docs/implementors/core/marker/trait.Freeze.js b/docs/implementors/core/marker/trait.Freeze.js new file mode 100644 index 0000000..45745cf --- /dev/null +++ b/docs/implementors/core/marker/trait.Freeze.js @@ -0,0 +1,3 @@ +(function() {var implementors = {}; +implementors["ntext"] = [{"text":"impl<'format> Freeze for Formatting<'format>","synthetic":true,"types":["ntext::formatting::Formatting"]}]; +if (window.register_implementors) {window.register_implementors(implementors);} else {window.pending_implementors = implementors;}})() \ No newline at end of file diff --git a/docs/implementors/core/marker/trait.Send.js b/docs/implementors/core/marker/trait.Send.js new file mode 100644 index 0000000..0e38083 --- /dev/null +++ b/docs/implementors/core/marker/trait.Send.js @@ -0,0 +1,3 @@ +(function() {var implementors = {}; +implementors["ntext"] = [{"text":"impl<'format> Send for Formatting<'format>","synthetic":true,"types":["ntext::formatting::Formatting"]}]; +if (window.register_implementors) {window.register_implementors(implementors);} else {window.pending_implementors = implementors;}})() \ No newline at end of file diff --git a/docs/implementors/core/marker/trait.Sync.js b/docs/implementors/core/marker/trait.Sync.js new file mode 100644 index 0000000..d382f67 --- /dev/null +++ b/docs/implementors/core/marker/trait.Sync.js @@ -0,0 +1,3 @@ +(function() {var implementors = {}; +implementors["ntext"] = [{"text":"impl<'format> Sync for Formatting<'format>","synthetic":true,"types":["ntext::formatting::Formatting"]}]; +if (window.register_implementors) {window.register_implementors(implementors);} else {window.pending_implementors = implementors;}})() \ No newline at end of file diff --git a/docs/implementors/core/marker/trait.Unpin.js b/docs/implementors/core/marker/trait.Unpin.js new file mode 100644 index 0000000..6b51210 --- /dev/null +++ b/docs/implementors/core/marker/trait.Unpin.js @@ -0,0 +1,3 @@ +(function() {var implementors = {}; +implementors["ntext"] = [{"text":"impl<'format> Unpin for Formatting<'format>","synthetic":true,"types":["ntext::formatting::Formatting"]}]; +if (window.register_implementors) {window.register_implementors(implementors);} else {window.pending_implementors = implementors;}})() \ No newline at end of file diff --git a/docs/implementors/std/panic/trait.RefUnwindSafe.js b/docs/implementors/std/panic/trait.RefUnwindSafe.js new file mode 100644 index 0000000..35c2569 --- /dev/null +++ b/docs/implementors/std/panic/trait.RefUnwindSafe.js @@ -0,0 +1,3 @@ +(function() {var implementors = {}; +implementors["ntext"] = [{"text":"impl<'format> RefUnwindSafe for Formatting<'format>","synthetic":true,"types":["ntext::formatting::Formatting"]}]; +if (window.register_implementors) {window.register_implementors(implementors);} else {window.pending_implementors = implementors;}})() \ No newline at end of file diff --git a/docs/implementors/std/panic/trait.UnwindSafe.js b/docs/implementors/std/panic/trait.UnwindSafe.js new file mode 100644 index 0000000..5e55d22 --- /dev/null +++ b/docs/implementors/std/panic/trait.UnwindSafe.js @@ -0,0 +1,3 @@ +(function() {var implementors = {}; +implementors["ntext"] = [{"text":"impl<'format> UnwindSafe for Formatting<'format>","synthetic":true,"types":["ntext::formatting::Formatting"]}]; +if (window.register_implementors) {window.register_implementors(implementors);} else {window.pending_implementors = implementors;}})() \ No newline at end of file diff --git a/docs/ntext/all.html b/docs/ntext/all.html index 0167cf5..b850f6b 100644 --- a/docs/ntext/all.html +++ b/docs/ntext/all.html @@ -1,7 +1,7 @@ List of all items in this crate -

Function ntext::digit_to_text[][src]

pub fn digit_to_text(digit: u8, capitalize: bool) -> Option<String>

Convert digit to words in a string.

\ No newline at end of file diff --git a/docs/ntext/fn.to_text_fmt.html b/docs/ntext/fn.to_text_fmt.html new file mode 100644 index 0000000..406c3fd --- /dev/null +++ b/docs/ntext/fn.to_text_fmt.html @@ -0,0 +1,6 @@ +ntext::to_text_fmt - Rust + +

Function ntext::to_text_fmt[][src]

pub fn to_text_fmt(number: usize, fmt: &Formatting<'_>) -> String

Convert usize to words in a string seperated by a seperator.

+
+ \ No newline at end of file diff --git a/docs/ntext/formatting/struct.Formatting.html b/docs/ntext/formatting/struct.Formatting.html new file mode 100644 index 0000000..188282a --- /dev/null +++ b/docs/ntext/formatting/struct.Formatting.html @@ -0,0 +1,10 @@ + + + + + + +

Redirecting to ../../ntext/struct.Formatting.html...

+ + + \ No newline at end of file diff --git a/docs/ntext/index.html b/docs/ntext/index.html index 634bf40..3966c83 100644 --- a/docs/ntext/index.html +++ b/docs/ntext/index.html @@ -1,32 +1,32 @@ -ntext - Rust +ntext - Rust -

Crate ntext[][src]

-
-extern crate ntext;
-use ntext::digit_to_text;
-fn main() {
-    println!("{}",digit_to_text(1).unwrap());
-    assert_eq!("two",digit_to_text(2).unwrap());
-    assert_ne!("five",digit_to_text(8).unwrap());
-}
-

However giving the program a zero will return an empty string.

+

Crate ntext[][src]

Example program with default formatting

 extern crate ntext;
 use ntext::to_text;
 fn main() {
-     println!("{}",to_text!(1312));
-     assert_eq!(to_text!(1312),"onethousandthreehundredtwelve");
-     println!("{}",to_text!(7123));
-     assert_eq!(to_text!(7123," "),"seven thousand one hundred twenty three");
+     assert_eq!(to_text!(7123),"Seven Thousand,One Hundred,Twenty-Three");
+     assert_eq!(to_text!(1000),"One Thousand");
 }
-

This macro will also return an empty string on input zero

+

Example program with custom formatting

+ +
+extern crate ntext;
+use ntext::{Formatting,to_text};
+fn main() {
+     assert_eq!(to_text!(1000, &Formatting::none().capitalize()),"OneThousand");
+     assert_eq!(to_text!(34123, &Formatting::with_seperator("#").capitalize()),"Thirty#Four#Thousand#One#Hundred#Twenty#Three");
+}
+

This macro will also return an empty string on input zero +You can also create the Formatting struct manually

Macros

to_text

Macro which supports both seperator and without it

+

Structs

+
Formatting

Set the formatting of the output

Functions

digit_to_text

Convert digit to words in a string.

-
to_text_no_seperator

Convert u32 to words in a string.

-
to_text_with_seperator

Convert u32 to words in a string seperated by a seperator.

+
to_text_fmt

Convert usize to words in a string seperated by a seperator.

\ No newline at end of file diff --git a/docs/ntext/macro.to_text.html b/docs/ntext/macro.to_text.html index 221f32d..863ff00 100644 --- a/docs/ntext/macro.to_text.html +++ b/docs/ntext/macro.to_text.html @@ -1,10 +1,10 @@ ntext::to_text - Rust

Macro ntext::to_text[][src]

+                Change settings

Macro ntext::to_text[][src]

 macro_rules! to_text {
     ($number:expr) => { ... };
-    ($number:expr, $seperator:expr) => { ... };
+    ($number:expr, $formatting:expr) => { ... };
 }

Macro which supports both seperator and without it

diff --git a/docs/ntext/numtext/fn.digit_to_text.html b/docs/ntext/numtext/fn.digit_to_text.html new file mode 100644 index 0000000..606711e --- /dev/null +++ b/docs/ntext/numtext/fn.digit_to_text.html @@ -0,0 +1,10 @@ + + + + + + +

Redirecting to ../../ntext/fn.digit_to_text.html...

+ + + \ No newline at end of file diff --git a/docs/ntext/numtext/fn.to_text_fmt.html b/docs/ntext/numtext/fn.to_text_fmt.html new file mode 100644 index 0000000..893f492 --- /dev/null +++ b/docs/ntext/numtext/fn.to_text_fmt.html @@ -0,0 +1,10 @@ + + + + + + +

Redirecting to ../../ntext/fn.to_text_fmt.html...

+ + + \ No newline at end of file diff --git a/docs/ntext/sidebar-items.js b/docs/ntext/sidebar-items.js index 73ddd95..5b424aa 100644 --- a/docs/ntext/sidebar-items.js +++ b/docs/ntext/sidebar-items.js @@ -1 +1 @@ -initSidebarItems({"fn":[["digit_to_text","Convert digit to words in a string."],["to_text_no_seperator","Convert u32 to words in a string."],["to_text_with_seperator","Convert u32 to words in a string seperated by a seperator."]],"macro":[["to_text","Macro which supports both seperator and without it"]]}); \ No newline at end of file +initSidebarItems({"fn":[["digit_to_text","Convert digit to words in a string."],["to_text_fmt","Convert usize to words in a string seperated by a seperator."]],"macro":[["to_text","Macro which supports both seperator and without it"]],"struct":[["Formatting","Set the formatting of the output "]]}); \ No newline at end of file diff --git a/docs/ntext/struct.Formatting.html b/docs/ntext/struct.Formatting.html new file mode 100644 index 0000000..52f53c3 --- /dev/null +++ b/docs/ntext/struct.Formatting.html @@ -0,0 +1,36 @@ +ntext::Formatting - Rust + +

Struct ntext::Formatting[][src]

pub struct Formatting<'format> {
+    pub capitalize: bool,
+    pub digit_seperator: Option<&'format str>,
+    pub place_seperator: Option<&'format str>,
+    pub tens_seperator: Option<&'format str>,
+}

Set the formatting of the output

+

+ Fields

capitalize: bool

capitalize the start of the word.

+
digit_seperator: Option<&'format str>

Set the seperator in between digits like “threehundred,twentytwo”.

+
place_seperator: Option<&'format str>

Set the seperator in between words like “three/hundred”.

+
tens_seperator: Option<&'format str>

Set the seperator between tens place digits like “twenty-two”.

+

Implementations

impl<'format> Formatting<'format>[src]

pub fn default() -> Self[src]

Get the default formatting.

+

pub fn none() -> Self[src]

No formatting at all

+

pub fn with_seperator(seperator: &'format str) -> Self[src]

With same formatting for all

+

pub fn capitalize(&mut self) -> Self[src]

Capitalize the formatting

+

pub fn decapitalize(&mut self) -> Self[src]

Lower case the formatting struct

+

Trait Implementations

impl<'format> Clone for Formatting<'format>[src]

impl<'format> Copy for Formatting<'format>[src]

Auto Trait Implementations

impl<'format> RefUnwindSafe for Formatting<'format>

impl<'format> Send for Formatting<'format>

impl<'format> Sync for Formatting<'format>

impl<'format> Unpin for Formatting<'format>

impl<'format> UnwindSafe for Formatting<'format>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

+

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

+

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

+
+ \ No newline at end of file diff --git a/docs/search-index.js b/docs/search-index.js index 739a307..595a23f 100644 --- a/docs/search-index.js +++ b/docs/search-index.js @@ -1,4 +1,4 @@ var searchIndex = JSON.parse('{\ -"ntext":{"doc":"","i":[[5,"to_text_no_seperator","ntext","Convert u32 to words in a string.",null,[[["u32",15]],["string",3]]],[5,"to_text_with_seperator","","Convert u32 to words in a string seperated by a seperator.",null,[[["u32",15],["str",15]],["string",3]]],[5,"digit_to_text","","Convert digit to words in a string.",null,[[["u8",15]],[["option",4],["string",3]]]],[14,"to_text","","Macro which supports both seperator and without it",null,null]],"p":[]}\ +"ntext":{"doc":"Example program with default formatting","i":[[3,"Formatting","ntext","Set the formatting of the output ",null,null],[12,"capitalize","","capitalize the start of the word.",0,null],[12,"digit_seperator","","Set the seperator in between digits like “…",0,null],[12,"place_seperator","","Set the seperator in between words like “three/hundred…",0,null],[12,"tens_seperator","","Set the seperator between tens place digits like “…",0,null],[5,"digit_to_text","","Convert digit to words in a string.",null,[[["bool",15],["u8",15]],[["string",3],["option",4]]]],[5,"to_text_fmt","","Convert usize to words in a string seperated by a …",null,[[["usize",15],["formatting",3]],["string",3]]],[14,"to_text","","Macro which supports both seperator and without it",null,null],[11,"from","","",0,[[]]],[11,"into","","",0,[[]]],[11,"to_owned","","",0,[[]]],[11,"clone_into","","",0,[[]]],[11,"borrow","","",0,[[]]],[11,"borrow_mut","","",0,[[]]],[11,"try_from","","",0,[[],["result",4]]],[11,"try_into","","",0,[[],["result",4]]],[11,"type_id","","",0,[[],["typeid",3]]],[11,"clone","","",0,[[],["formatting",3]]],[11,"default","","Get the default formatting.",0,[[]]],[11,"none","","No formatting at all",0,[[]]],[11,"with_seperator","","With same formatting for all",0,[[["str",15]]]],[11,"capitalize","","Capitalize the formatting",0,[[]]],[11,"decapitalize","","Lower case the formatting struct",0,[[]]]],"p":[[3,"Formatting"]]}\ }'); addSearchOptions(searchIndex);initSearch(searchIndex); \ No newline at end of file diff --git a/docs/source-files.js b/docs/source-files.js index 365c4e1..4e486bf 100644 --- a/docs/source-files.js +++ b/docs/source-files.js @@ -1,3 +1,3 @@ var N = null;var sourcesIndex = {}; -sourcesIndex["ntext"] = {"name":"","files":["lib.rs","no_seperator.rs","seperator.rs","test.rs"]}; +sourcesIndex["ntext"] = {"name":"","files":["formatting.rs","lib.rs","numtext.rs","test.rs"]}; createSourceSidebar(); diff --git a/docs/src/ntext/formatting.rs.html b/docs/src/ntext/formatting.rs.html new file mode 100644 index 0000000..3acb6a2 --- /dev/null +++ b/docs/src/ntext/formatting.rs.html @@ -0,0 +1,131 @@ +formatting.rs - source + +
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+
+/// Set the formatting of the output  
+#[derive(Clone, Copy)]
+pub struct Formatting<'format> {
+    /// capitalize the start of the word.
+    pub capitalize: bool,
+    /// Set the seperator in between digits like "threehundred,twentytwo".
+    pub digit_seperator: Option<&'format str>,
+    /// Set the seperator in between words like "three/hundred".
+    pub place_seperator: Option<&'format str>,
+    /// Set the seperator between tens place digits like "twenty-two".
+    pub tens_seperator: Option<&'format str>,
+}
+
+impl<'format> Formatting<'format> {
+    /// Get the default formatting.
+    pub fn default() -> Self {
+        Self {
+            capitalize: true,
+            digit_seperator: Some(","),
+            place_seperator: Some(" "),
+            tens_seperator: Some("-"),
+        }
+    }
+    /// No formatting at all
+    pub fn none() -> Self {
+        Self {
+            capitalize: false,
+            digit_seperator: None,
+            place_seperator: None,
+            tens_seperator: None,
+        }
+    }
+    /// With same formatting for all
+    pub fn with_seperator(seperator: &'format str) -> Self {
+        Self {
+            capitalize: false,
+            digit_seperator: Some(seperator),
+            place_seperator: Some(seperator),
+            tens_seperator: Some(seperator),
+        }
+    }
+    /// Capitalize the formatting
+    pub fn capitalize(&mut self) -> Self {
+        self.capitalize = true;
+        Self {
+            capitalize: self.capitalize,
+            digit_seperator: self.digit_seperator,
+            place_seperator: self.place_seperator,
+            tens_seperator: self.tens_seperator,
+        }
+    }
+    /// Lower case the formatting struct
+    pub fn decapitalize(&mut self) -> Self {
+        self.capitalize = false;
+        Self {
+            capitalize: self.capitalize,
+            digit_seperator: self.digit_seperator,
+            place_seperator: self.place_seperator,
+            tens_seperator: self.tens_seperator,
+        }
+    }
+}
+
+
+ \ No newline at end of file diff --git a/docs/src/ntext/lib.rs.html b/docs/src/ntext/lib.rs.html index 518e84e..3ee53c5 100644 --- a/docs/src/ntext/lib.rs.html +++ b/docs/src/ntext/lib.rs.html @@ -40,114 +40,44 @@ 37 38 39 -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -72 -73 -74
-//!```rust
-//!extern crate ntext;
-//!use ntext::digit_to_text;
-//!fn main() {
-//!    println!("{}",digit_to_text(1).unwrap());
-//!    assert_eq!("two",digit_to_text(2).unwrap());
-//!    assert_ne!("five",digit_to_text(8).unwrap());
-//!}
-//!```
-//! However giving the program a zero will return an empty string.
-//!
+//! Example program with default formatting
 //!```rust
 //!extern crate ntext;
 //!use ntext::to_text;
 //!fn main() {
-//!     println!("{}",to_text!(1312));
-//!     assert_eq!(to_text!(1312),"onethousandthreehundredtwelve");
-//!     println!("{}",to_text!(7123));
-//!     assert_eq!(to_text!(7123," "),"seven thousand one hundred twenty three");
+//!     assert_eq!(to_text!(7123),"Seven Thousand,One Hundred,Twenty-Three");
+//!     assert_eq!(to_text!(1000),"One Thousand");
+//!}
+//!```
+//! Example program with custom formatting
+//!
+//!```rust
+//!extern crate ntext;
+//!use ntext::{Formatting,to_text};
+//!fn main() {
+//!     assert_eq!(to_text!(1000, &Formatting::none().capitalize()),"OneThousand");
+//!     assert_eq!(to_text!(34123, &Formatting::with_seperator("#").capitalize()),"Thirty#Four#Thousand#One#Hundred#Twenty#Three");
 //!}
 //!```
 //! This macro will also return an empty string on input zero
+//! You can also create the Formatting struct manually
 
-mod no_seperator;
-mod seperator;
+mod formatting;
+mod numtext;
 mod test;
-pub use no_seperator::to_text_no_seperator;
-pub use seperator::to_text_with_seperator;
 
-/// Convert digit to words in a string.
-pub fn digit_to_text(digit: u8) -> Option<String> {
-    match digit {
-        0 => Some("".to_string()),
-        1 => Some("one".to_string()),
-        2 => Some("two".to_string()),
-        3 => Some("three".to_string()),
-        4 => Some("four".to_string()),
-        5 => Some("five".to_string()),
-        6 => Some("six".to_string()),
-        7 => Some("seven".to_string()),
-        8 => Some("eight".to_string()),
-        9 => Some("nine".to_string()),
-        _ => None,
-    }
-}
-fn place_value(number: u8, place: u8, seperator: Option<&str>) -> Option<String> {
-    let mut buffer = digit_to_text(number).unwrap();
-    if let Some(sep) = seperator {
-        if number != 0 {
-            buffer.push_str(sep)
-        }
-    }
-    match place {
-        1 => (),
-        2 => (), //This should never happen as 2 is included in the tens_place_holders
-        3 => buffer.push_str("hundred"),
-        4 => buffer.push_str("thousand"),
-        5 => (), // Souldn't happen
-        6 => buffer.push_str("million"),
-        _ => (),
-    };
-    Some(buffer)
-}
+pub use formatting::Formatting;
+pub use numtext::{digit_to_text, to_text_fmt};
 
 /// Macro which supports both seperator and without it
 #[macro_export]
 macro_rules! to_text {
     ($number:expr) => {
-        ntext::to_text_no_seperator($number);
+        ntext::to_text_fmt($number, &ntext::Formatting::default());
     };
-    ($number:expr, $seperator:expr) => {
-        ntext::to_text_with_seperator($number, $seperator);
+    ($number:expr, $formatting:expr) => {
+        ntext::to_text_fmt($number, $formatting);
     };
 }
 
diff --git a/docs/src/ntext/numtext.rs.html b/docs/src/ntext/numtext.rs.html new file mode 100644 index 0000000..7463c20 --- /dev/null +++ b/docs/src/ntext/numtext.rs.html @@ -0,0 +1,361 @@ +numtext.rs - source + +
  1
+  2
+  3
+  4
+  5
+  6
+  7
+  8
+  9
+ 10
+ 11
+ 12
+ 13
+ 14
+ 15
+ 16
+ 17
+ 18
+ 19
+ 20
+ 21
+ 22
+ 23
+ 24
+ 25
+ 26
+ 27
+ 28
+ 29
+ 30
+ 31
+ 32
+ 33
+ 34
+ 35
+ 36
+ 37
+ 38
+ 39
+ 40
+ 41
+ 42
+ 43
+ 44
+ 45
+ 46
+ 47
+ 48
+ 49
+ 50
+ 51
+ 52
+ 53
+ 54
+ 55
+ 56
+ 57
+ 58
+ 59
+ 60
+ 61
+ 62
+ 63
+ 64
+ 65
+ 66
+ 67
+ 68
+ 69
+ 70
+ 71
+ 72
+ 73
+ 74
+ 75
+ 76
+ 77
+ 78
+ 79
+ 80
+ 81
+ 82
+ 83
+ 84
+ 85
+ 86
+ 87
+ 88
+ 89
+ 90
+ 91
+ 92
+ 93
+ 94
+ 95
+ 96
+ 97
+ 98
+ 99
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+119
+120
+121
+122
+123
+124
+125
+126
+127
+128
+129
+130
+131
+132
+133
+134
+135
+136
+137
+138
+139
+140
+141
+142
+143
+144
+145
+146
+147
+148
+149
+150
+151
+152
+153
+154
+155
+156
+157
+158
+159
+160
+161
+162
+163
+164
+165
+166
+167
+168
+169
+170
+171
+172
+173
+174
+175
+176
+177
+
+use crate::Formatting;
+
+/// Convert digit to words in a string.
+pub fn digit_to_text(digit: u8, capitalize: bool) -> Option<String> {
+    if capitalize {
+        match digit {
+            0 => Some("".to_string()),
+            1 => Some("One".to_string()),
+            2 => Some("Two".to_string()),
+            3 => Some("Three".to_string()),
+            4 => Some("Four".to_string()),
+            5 => Some("Five".to_string()),
+            6 => Some("Six".to_string()),
+            7 => Some("Seven".to_string()),
+            8 => Some("Eight".to_string()),
+            9 => Some("Nine".to_string()),
+            _ => None,
+        }
+    } else {
+        match digit {
+            0 => Some("".to_string()),
+            1 => Some("one".to_string()),
+            2 => Some("two".to_string()),
+            3 => Some("three".to_string()),
+            4 => Some("four".to_string()),
+            5 => Some("five".to_string()),
+            6 => Some("six".to_string()),
+            7 => Some("seven".to_string()),
+            8 => Some("eight".to_string()),
+            9 => Some("nine".to_string()),
+            _ => None,
+        }
+    }
+}
+fn place_value(number: u8, place: u8, fmt: &Formatting) -> Option<String> {
+    let mut buffer = digit_to_text(number, fmt.capitalize).unwrap();
+    if let Some(sep) = fmt.place_seperator {
+        if number != 0 && place != 1 {
+            buffer.push_str(sep)
+        }
+    }
+    if fmt.capitalize {
+        match place {
+            1 => (), //Can happen but should return the same digit
+            2 => (), //This should never happen as 2 is included in the tens_place_holders
+            3 => buffer.push_str("Hundred"),
+            4 => buffer.push_str("Thousand"),
+            5 => (), // Souldn't happen
+            6 => buffer.push_str("Million"),
+            _ => (),
+        };
+    } else {
+        match place {
+            1 => (), //Can happen but should return the same digit
+            2 => (), //This should never happen as 2 is included in the tens_place_holders
+            3 => buffer.push_str("hundred"),
+            4 => buffer.push_str("thousand"),
+            5 => (), // Souldn't happen
+            6 => buffer.push_str("million"),
+            _ => (),
+        }
+    }
+    Some(buffer)
+}
+fn tens_place(tens: u8, ones: u8, fmt: &Formatting) -> Option<String> {
+    match tens {
+        0 => digit_to_text(ones, fmt.capitalize),
+        1 => {
+            if fmt.capitalize {
+                match ones {
+                    0 => Some("Ten".to_string()),
+                    1 => Some("Eleven".to_string()),
+                    2 => Some("Twelve".to_string()),
+                    3 => Some("Thirteen".to_string()),
+                    4 => Some("Fourteen".to_string()),
+                    5 => Some("Fifteen".to_string()),
+                    6 => Some("Sixteen".to_string()),
+                    7 => Some("Seventeen".to_string()),
+                    8 => Some("Eighteen".to_string()),
+                    9 => Some("Nineteen".to_string()),
+                    _ => None,
+                }
+            } else {
+                match ones {
+                    0 => Some("ten".to_string()),
+                    1 => Some("eleven".to_string()),
+                    2 => Some("twelve".to_string()),
+                    3 => Some("thirteen".to_string()),
+                    4 => Some("fourteen".to_string()),
+                    5 => Some("fifteen".to_string()),
+                    6 => Some("sixteen".to_string()),
+                    7 => Some("seventeen".to_string()),
+                    8 => Some("eighteen".to_string()),
+                    9 => Some("nineteen".to_string()),
+                    _ => None,
+                }
+            }
+        }
+        d @ 2..=5 | d @ 8 => Some({
+            let mut buffer = String::new();
+            if fmt.capitalize {
+                match d {
+                    2 => buffer.push_str("Twenty"),
+                    3 => buffer.push_str("Thirty"),
+                    4 => buffer.push_str("Forty"),
+                    5 => buffer.push_str("Fifty"),
+                    8 => buffer.push_str("Eighty"),
+                    _ => (),
+                }
+            } else {
+                match d {
+                    2 => buffer.push_str("twenty"),
+                    3 => buffer.push_str("thirty"),
+                    4 => buffer.push_str("forty"),
+                    5 => buffer.push_str("fifty"),
+                    8 => buffer.push_str("eighty"),
+                    _ => (),
+                }
+            }
+            if let Some(sep) = fmt.tens_seperator {
+                buffer.push_str(sep);
+            }
+            buffer.push_str(digit_to_text(ones, fmt.capitalize).unwrap().as_str());
+            buffer
+        }),
+        d @ 6..=9 => Some({
+            let mut buffer = digit_to_text(d, fmt.capitalize).unwrap() + "ty";
+            if let Some(sep) = fmt.tens_seperator {
+                buffer.push_str(sep);
+            }
+            buffer.push_str(digit_to_text(ones, fmt.capitalize).unwrap().as_str());
+            buffer
+        }),
+        _ => None,
+    }
+}
+
+/// Convert usize to words in a string seperated by a seperator.
+pub fn to_text_fmt(number: usize, fmt: &Formatting) -> String {
+    let mut numtext: String = String::new();
+    let mut last: Option<u8> = None;
+    let tens_place_holders: [u8; 2] = [2, 5];
+    let digits: Vec<u8> = number
+        .to_string()
+        .chars()
+        .map(|d| d.to_digit(10).unwrap() as u8)
+        .collect();
+
+    let mut place: u8 = digits.len() as u8;
+
+    for (_index, digit) in digits.iter().enumerate() {
+        if tens_place_holders.contains(&place) {
+            last = Some(*digit);
+            place -= 1;
+            continue;
+        }
+        if *digit != 0 && place != digits.len() as u8 && numtext != "" {
+            if let Some(sep) = fmt.digit_seperator {
+                numtext.push_str(sep);
+            }
+        }
+        if let Some(last_digit) = last {
+            numtext.push_str(tens_place(last_digit, *digit, fmt).unwrap().as_str());
+            if place > 2 {
+                if let Some(sep) = fmt.digit_seperator {
+                    numtext.push_str(sep);
+                }
+                numtext.push_str(place_value(0, place, fmt).unwrap().as_str());
+            }
+            last = None;
+        } else if *digit != 0 {
+            numtext.push_str(place_value(*digit, place, fmt).unwrap().as_str());
+        }
+        place -= 1;
+    }
+    numtext
+}
+
+
+ \ No newline at end of file diff --git a/docs/src/ntext/seperator.rs.html b/docs/src/ntext/seperator.rs.html index 067213f..98c0b7b 100644 --- a/docs/src/ntext/seperator.rs.html +++ b/docs/src/ntext/seperator.rs.html @@ -90,7 +90,7 @@
 use crate::{digit_to_text, place_value};
 
-fn tens_place_seperator(tens: u8, ones: u8, seperator: &str) -> Option<String> {
+fn tens_place(tens: u8, ones: u8, seperator: &str) -> Option<String> {
     match tens {
         0 => digit_to_text(ones),
         1 => match ones {
diff --git a/docs/src/ntext/test.rs.html b/docs/src/ntext/test.rs.html
index 8fe5232..bfe02eb 100644
--- a/docs/src/ntext/test.rs.html
+++ b/docs/src/ntext/test.rs.html
@@ -29,49 +29,33 @@
 26
 27
 28
-29
-30
-31
-32
-33
-34
-35
-36
 
 #[cfg(test)]
 mod tests {
     #[test]
     fn digits() {
         use crate::digit_to_text;
-        assert_eq!(digit_to_text(9).unwrap(), "nine");
-        assert_eq!(digit_to_text(3).unwrap(), "three");
-        assert_eq!(digit_to_text(7).unwrap(), "seven");
-        assert_eq!(digit_to_text(5).unwrap(), "five");
+        assert_eq!(digit_to_text(9, false).unwrap(), "nine");
+        assert_eq!(digit_to_text(3, false).unwrap(), "three");
+        assert_eq!(digit_to_text(7, false).unwrap(), "seven");
+        assert_eq!(digit_to_text(5, false).unwrap(), "five");
+        assert_eq!(digit_to_text(5, true).unwrap(), "Five");
     }
     #[test]
     fn numbers() {
-        use crate::to_text_no_seperator as to_text;
-        assert_eq!(to_text(1), "one");
-        assert_eq!(to_text(10), "ten");
-        assert_eq!(to_text(100), "onehundred");
-        assert_eq!(to_text(1000), "onethousand");
-        assert_eq!(to_text(12345), "twelvethousandthreehundredfortyfive");
-        assert_eq!(to_text(81123), "eightyonethousandonehundredtwentythree");
-        assert_eq!(to_text(12), "twelve");
-    }
-    #[test]
-    fn numbers_seperator() {
-        use crate::to_text_with_seperator as to_text;
-        assert_eq!(to_text(103, "/"), "one/hundred/three");
-        assert_eq!(to_text(1000, "/"), "one/thousand");
+        use crate::to_text_fmt as to_text;
+        use crate::Formatting;
+        let fmt = &Formatting::none();
+        assert_eq!(to_text(1, fmt), "one");
+        assert_eq!(to_text(10, fmt), "ten");
+        assert_eq!(to_text(100, fmt), "onehundred");
+        assert_eq!(to_text(1000, fmt), "onethousand");
+        assert_eq!(to_text(12345, fmt), "twelvethousandthreehundredfortyfive");
         assert_eq!(
-            to_text(12345, "/"),
-            "twelve/thousand/three/hundred/forty/five"
-        );
-        assert_eq!(
-            to_text(651243, "/"),
-            "six/million/fifty/one/thousand/two/hundred/forty/three"
+            to_text(81123, fmt),
+            "eightyonethousandonehundredtwentythree"
         );
+        assert_eq!(to_text(12, fmt), "twelve");
     }
 }
 
diff --git a/src/formatting.rs b/src/formatting.rs new file mode 100644 index 0000000..a41cdaf --- /dev/null +++ b/src/formatting.rs @@ -0,0 +1,62 @@ +/// Set the formatting of the output +#[derive(Clone, Copy)] +pub struct Formatting<'format> { + /// capitalize the start of the word. + pub capitalize: bool, + /// Set the seperator in between digits like "threehundred,twentytwo". + pub digit_seperator: Option<&'format str>, + /// Set the seperator in between words like "three/hundred". + pub place_seperator: Option<&'format str>, + /// Set the seperator between tens place digits like "twenty-two". + pub tens_seperator: Option<&'format str>, +} + +impl<'format> Formatting<'format> { + /// Get the default formatting. + pub fn default() -> Self { + Self { + capitalize: true, + digit_seperator: Some(","), + place_seperator: Some(" "), + tens_seperator: Some("-"), + } + } + /// No formatting at all + pub fn none() -> Self { + Self { + capitalize: false, + digit_seperator: None, + place_seperator: None, + tens_seperator: None, + } + } + /// With same formatting for all + pub fn with_seperator(seperator: &'format str) -> Self { + Self { + capitalize: false, + digit_seperator: Some(seperator), + place_seperator: Some(seperator), + tens_seperator: Some(seperator), + } + } + /// Capitalize the formatting + pub fn capitalize(&mut self) -> Self { + self.capitalize = true; + Self { + capitalize: self.capitalize, + digit_seperator: self.digit_seperator, + place_seperator: self.place_seperator, + tens_seperator: self.tens_seperator, + } + } + /// Lower case the formatting struct + pub fn decapitalize(&mut self) -> Self { + self.capitalize = false; + Self { + capitalize: self.capitalize, + digit_seperator: self.digit_seperator, + place_seperator: self.place_seperator, + tens_seperator: self.tens_seperator, + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 65da738..6fa8ea4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,74 +1,39 @@ -//!```rust -//!extern crate ntext; -//!use ntext::digit_to_text; -//!fn main() { -//! println!("{}",digit_to_text(1).unwrap()); -//! assert_eq!("two",digit_to_text(2).unwrap()); -//! assert_ne!("five",digit_to_text(8).unwrap()); -//!} -//!``` -//! However giving the program a zero will return an empty string. -//! +//! Example program with default formatting //!```rust //!extern crate ntext; //!use ntext::to_text; //!fn main() { -//! println!("{}",to_text!(1312)); -//! assert_eq!(to_text!(1312),"onethousandthreehundredtwelve"); -//! println!("{}",to_text!(7123)); -//! assert_eq!(to_text!(7123," "),"seven thousand one hundred twenty three"); +//! assert_eq!(to_text!(7123),"Seven Thousand,One Hundred,Twenty-Three"); +//! assert_eq!(to_text!(1000),"One Thousand"); +//!} +//!``` +//! Example program with custom formatting +//! +//!```rust +//!extern crate ntext; +//!use ntext::{Formatting,to_text}; +//!fn main() { +//! assert_eq!(to_text!(1000, &Formatting::none().capitalize()),"OneThousand"); +//! assert_eq!(to_text!(34123, &Formatting::with_seperator("#").capitalize()),"Thirty#Four#Thousand#One#Hundred#Twenty#Three"); //!} //!``` //! This macro will also return an empty string on input zero +//! You can also create the Formatting struct manually -mod no_seperator; -mod seperator; +mod formatting; +mod numtext; mod test; -pub use no_seperator::to_text_no_seperator; -pub use seperator::to_text_with_seperator; -/// Convert digit to words in a string. -pub fn digit_to_text(digit: u8) -> Option { - match digit { - 0 => Some("".to_string()), - 1 => Some("one".to_string()), - 2 => Some("two".to_string()), - 3 => Some("three".to_string()), - 4 => Some("four".to_string()), - 5 => Some("five".to_string()), - 6 => Some("six".to_string()), - 7 => Some("seven".to_string()), - 8 => Some("eight".to_string()), - 9 => Some("nine".to_string()), - _ => None, - } -} -fn place_value(number: u8, place: u8, seperator: Option<&str>) -> Option { - let mut buffer = digit_to_text(number).unwrap(); - if let Some(sep) = seperator { - if number != 0 { - buffer.push_str(sep) - } - } - match place { - 1 => (), - 2 => (), //This should never happen as 2 is included in the tens_place_holders - 3 => buffer.push_str("hundred"), - 4 => buffer.push_str("thousand"), - 5 => (), // Souldn't happen - 6 => buffer.push_str("million"), - _ => (), - }; - Some(buffer) -} +pub use formatting::Formatting; +pub use numtext::{digit_to_text, to_text_fmt}; /// Macro which supports both seperator and without it #[macro_export] macro_rules! to_text { ($number:expr) => { - ntext::to_text_no_seperator($number); + ntext::to_text_fmt($number, &ntext::Formatting::default()); }; - ($number:expr, $seperator:expr) => { - ntext::to_text_with_seperator($number, $seperator); + ($number:expr, $formatting:expr) => { + ntext::to_text_fmt($number, $formatting); }; } diff --git a/src/no_seperator.rs b/src/no_seperator.rs deleted file mode 100644 index bc93d2e..0000000 --- a/src/no_seperator.rs +++ /dev/null @@ -1,71 +0,0 @@ -use crate::{digit_to_text, place_value}; -fn tens_place(tens: u8, ones: u8) -> Option { - match tens { - 0 => digit_to_text(ones), - 1 => match ones { - 0 => Some("ten".to_string()), - 1 => Some("eleven".to_string()), - 2 => Some("twelve".to_string()), - 3 => Some("thirteen".to_string()), - 4 => Some("fourteen".to_string()), - 5 => Some("fifteen".to_string()), - 6 => Some("sixteen".to_string()), - 7 => Some("seventeen".to_string()), - 8 => Some("eighteen".to_string()), - 9 => Some("nineteen".to_string()), - _ => None, - }, - d @ 2..=5 | d @ 8 => Some({ - let mut buffer = String::new(); - match d { - 2 => buffer.push_str("twenty"), - 3 => buffer.push_str("thirty"), - 4 => buffer.push_str("forty"), - 5 => buffer.push_str("fifty"), - 8 => buffer.push_str("eighty"), - _ => (), - } - buffer.push_str(digit_to_text(ones).unwrap().as_str()); - buffer - }), - d @ 6..=9 => Some({ - let mut string = digit_to_text(d).unwrap() + "ty"; - string.push_str(digit_to_text(ones).unwrap().as_str()); - string - }), - _ => None, - } -} - -/// Convert u32 to words in a string. -pub fn to_text_no_seperator(number: u32) -> String { - let mut numtext: String = String::new(); - let mut last: Option = None; - let tens_place_holders: [u8; 2] = [2, 5]; - let digits: Vec = number - .to_string() - .chars() - .map(|d| d.to_digit(10).unwrap() as u8) - .collect(); - - let mut place: u8 = digits.len() as u8; - - for (_index, digit) in digits.iter().enumerate() { - if tens_place_holders.contains(&place) { - last = Some(*digit); - place -= 1; - continue; - } - if let Some(last_digit) = last { - numtext.push_str(tens_place(last_digit, *digit).unwrap().as_str()); - if place > 2 { - numtext.push_str(place_value(0, place, None).unwrap().as_str()); - } - last = None; - } else if *digit != 0 { - numtext.push_str(place_value(*digit, place, None).unwrap().as_str()) - } - place -= 1; - } - numtext -} diff --git a/src/numtext.rs b/src/numtext.rs new file mode 100644 index 0000000..59860a8 --- /dev/null +++ b/src/numtext.rs @@ -0,0 +1,177 @@ +use crate::Formatting; + +/// Convert digit to words in a string. +pub fn digit_to_text(digit: u8, capitalize: bool) -> Option { + if capitalize { + match digit { + 0 => Some("".to_string()), + 1 => Some("One".to_string()), + 2 => Some("Two".to_string()), + 3 => Some("Three".to_string()), + 4 => Some("Four".to_string()), + 5 => Some("Five".to_string()), + 6 => Some("Six".to_string()), + 7 => Some("Seven".to_string()), + 8 => Some("Eight".to_string()), + 9 => Some("Nine".to_string()), + _ => None, + } + } else { + match digit { + 0 => Some("".to_string()), + 1 => Some("one".to_string()), + 2 => Some("two".to_string()), + 3 => Some("three".to_string()), + 4 => Some("four".to_string()), + 5 => Some("five".to_string()), + 6 => Some("six".to_string()), + 7 => Some("seven".to_string()), + 8 => Some("eight".to_string()), + 9 => Some("nine".to_string()), + _ => None, + } + } +} +fn place_value(number: u8, place: u8, fmt: &Formatting) -> Option { + let mut buffer = digit_to_text(number, fmt.capitalize).unwrap(); + if let Some(sep) = fmt.place_seperator { + if number != 0 && place != 1 { + buffer.push_str(sep) + } + } + if fmt.capitalize { + match place { + 1 => (), //Can happen but should return the same digit + 2 => (), //This should never happen as 2 is included in the tens_place_holders + 3 => buffer.push_str("Hundred"), + 4 => buffer.push_str("Thousand"), + 5 => (), // Souldn't happen + 6 => buffer.push_str("Million"), + _ => (), + }; + } else { + match place { + 1 => (), //Can happen but should return the same digit + 2 => (), //This should never happen as 2 is included in the tens_place_holders + 3 => buffer.push_str("hundred"), + 4 => buffer.push_str("thousand"), + 5 => (), // Souldn't happen + 6 => buffer.push_str("million"), + _ => (), + } + } + Some(buffer) +} +fn tens_place(tens: u8, ones: u8, fmt: &Formatting) -> Option { + match tens { + 0 => digit_to_text(ones, fmt.capitalize), + 1 => { + if fmt.capitalize { + match ones { + 0 => Some("Ten".to_string()), + 1 => Some("Eleven".to_string()), + 2 => Some("Twelve".to_string()), + 3 => Some("Thirteen".to_string()), + 4 => Some("Fourteen".to_string()), + 5 => Some("Fifteen".to_string()), + 6 => Some("Sixteen".to_string()), + 7 => Some("Seventeen".to_string()), + 8 => Some("Eighteen".to_string()), + 9 => Some("Nineteen".to_string()), + _ => None, + } + } else { + match ones { + 0 => Some("ten".to_string()), + 1 => Some("eleven".to_string()), + 2 => Some("twelve".to_string()), + 3 => Some("thirteen".to_string()), + 4 => Some("fourteen".to_string()), + 5 => Some("fifteen".to_string()), + 6 => Some("sixteen".to_string()), + 7 => Some("seventeen".to_string()), + 8 => Some("eighteen".to_string()), + 9 => Some("nineteen".to_string()), + _ => None, + } + } + } + d @ 2..=5 | d @ 8 => Some({ + let mut buffer = String::new(); + if fmt.capitalize { + match d { + 2 => buffer.push_str("Twenty"), + 3 => buffer.push_str("Thirty"), + 4 => buffer.push_str("Forty"), + 5 => buffer.push_str("Fifty"), + 8 => buffer.push_str("Eighty"), + _ => (), + } + } else { + match d { + 2 => buffer.push_str("twenty"), + 3 => buffer.push_str("thirty"), + 4 => buffer.push_str("forty"), + 5 => buffer.push_str("fifty"), + 8 => buffer.push_str("eighty"), + _ => (), + } + } + if let Some(sep) = fmt.tens_seperator { + buffer.push_str(sep); + } + buffer.push_str(digit_to_text(ones, fmt.capitalize).unwrap().as_str()); + buffer + }), + d @ 6..=9 => Some({ + let mut buffer = digit_to_text(d, fmt.capitalize).unwrap() + "ty"; + if let Some(sep) = fmt.tens_seperator { + buffer.push_str(sep); + } + buffer.push_str(digit_to_text(ones, fmt.capitalize).unwrap().as_str()); + buffer + }), + _ => None, + } +} + +/// Convert usize to words in a string seperated by a seperator. +pub fn to_text_fmt(number: usize, fmt: &Formatting) -> String { + let mut numtext: String = String::new(); + let mut last: Option = None; + let tens_place_holders: [u8; 2] = [2, 5]; + let digits: Vec = number + .to_string() + .chars() + .map(|d| d.to_digit(10).unwrap() as u8) + .collect(); + + let mut place: u8 = digits.len() as u8; + + for (_index, digit) in digits.iter().enumerate() { + if tens_place_holders.contains(&place) { + last = Some(*digit); + place -= 1; + continue; + } + if *digit != 0 && place != digits.len() as u8 && numtext != "" { + if let Some(sep) = fmt.digit_seperator { + numtext.push_str(sep); + } + } + if let Some(last_digit) = last { + numtext.push_str(tens_place(last_digit, *digit, fmt).unwrap().as_str()); + if place > 2 { + if let Some(sep) = fmt.digit_seperator { + numtext.push_str(sep); + } + numtext.push_str(place_value(0, place, fmt).unwrap().as_str()); + } + last = None; + } else if *digit != 0 { + numtext.push_str(place_value(*digit, place, fmt).unwrap().as_str()); + } + place -= 1; + } + numtext +} diff --git a/src/seperator.rs b/src/seperator.rs deleted file mode 100644 index c35ae14..0000000 --- a/src/seperator.rs +++ /dev/null @@ -1,86 +0,0 @@ -use crate::{digit_to_text, place_value}; - -fn tens_place_seperator(tens: u8, ones: u8, seperator: &str) -> Option { - match tens { - 0 => digit_to_text(ones), - 1 => match ones { - 0 => Some("ten".to_string()), - 1 => Some("eleven".to_string()), - 2 => Some("twelve".to_string()), - 3 => Some("thirteen".to_string()), - 4 => Some("fourteen".to_string()), - 5 => Some("fifteen".to_string()), - 6 => Some("sixteen".to_string()), - 7 => Some("seventeen".to_string()), - 8 => Some("eighteen".to_string()), - 9 => Some("nineteen".to_string()), - _ => None, - }, - d @ 2..=5 | d @ 8 => Some({ - let mut buffer = String::new(); - match d { - 2 => buffer.push_str("twenty"), - 3 => buffer.push_str("thirty"), - 4 => buffer.push_str("forty"), - 5 => buffer.push_str("fifty"), - 8 => buffer.push_str("eighty"), - _ => (), - } - buffer.push_str(seperator); - buffer.push_str(digit_to_text(ones).unwrap().as_str()); - buffer - }), - d @ 6..=9 => Some({ - let mut buffer = digit_to_text(d).unwrap() + "ty"; - buffer.push_str(seperator); - buffer.push_str(digit_to_text(ones).unwrap().as_str()); - buffer - }), - _ => None, - } -} - -/// Convert u32 to words in a string seperated by a seperator. -pub fn to_text_with_seperator(number: u32, seperator: &str) -> String { - let mut numtext: String = String::new(); - let mut last: Option = None; - let tens_place_holders: [u8; 2] = [2, 5]; - let digits: Vec = number - .to_string() - .chars() - .map(|d| d.to_digit(10).unwrap() as u8) - .collect(); - - let mut place: u8 = digits.len() as u8; - - for (_index, digit) in digits.iter().enumerate() { - if tens_place_holders.contains(&place) { - last = Some(*digit); - place -= 1; - continue; - } - if *digit != 0 && place != digits.len() as u8 && numtext != "" { - numtext.push_str(seperator); - } - if let Some(last_digit) = last { - numtext.push_str( - tens_place_seperator(last_digit, *digit, seperator) - .unwrap() - .as_str(), - ); - if place > 2 { - numtext.push_str(seperator); - numtext.push_str(place_value(0, place, Some(seperator)).unwrap().as_str()); - } - last = None; - } else if *digit != 0 { - numtext.push_str( - place_value(*digit, place, Some(seperator)) - .unwrap() - .as_str(), - ); - } - place -= 1; - } - numtext -} diff --git a/src/test.rs b/src/test.rs index 545631a..b7c27f7 100644 --- a/src/test.rs +++ b/src/test.rs @@ -3,34 +3,26 @@ mod tests { #[test] fn digits() { use crate::digit_to_text; - assert_eq!(digit_to_text(9).unwrap(), "nine"); - assert_eq!(digit_to_text(3).unwrap(), "three"); - assert_eq!(digit_to_text(7).unwrap(), "seven"); - assert_eq!(digit_to_text(5).unwrap(), "five"); + assert_eq!(digit_to_text(9, false).unwrap(), "nine"); + assert_eq!(digit_to_text(3, false).unwrap(), "three"); + assert_eq!(digit_to_text(7, false).unwrap(), "seven"); + assert_eq!(digit_to_text(5, false).unwrap(), "five"); + assert_eq!(digit_to_text(5, true).unwrap(), "Five"); } #[test] fn numbers() { - use crate::to_text_no_seperator as to_text; - assert_eq!(to_text(1), "one"); - assert_eq!(to_text(10), "ten"); - assert_eq!(to_text(100), "onehundred"); - assert_eq!(to_text(1000), "onethousand"); - assert_eq!(to_text(12345), "twelvethousandthreehundredfortyfive"); - assert_eq!(to_text(81123), "eightyonethousandonehundredtwentythree"); - assert_eq!(to_text(12), "twelve"); - } - #[test] - fn numbers_seperator() { - use crate::to_text_with_seperator as to_text; - assert_eq!(to_text(103, "/"), "one/hundred/three"); - assert_eq!(to_text(1000, "/"), "one/thousand"); + use crate::to_text_fmt as to_text; + use crate::Formatting; + let fmt = &Formatting::none(); + assert_eq!(to_text(1, fmt), "one"); + assert_eq!(to_text(10, fmt), "ten"); + assert_eq!(to_text(100, fmt), "onehundred"); + assert_eq!(to_text(1000, fmt), "onethousand"); + assert_eq!(to_text(12345, fmt), "twelvethousandthreehundredfortyfive"); assert_eq!( - to_text(12345, "/"), - "twelve/thousand/three/hundred/forty/five" - ); - assert_eq!( - to_text(651243, "/"), - "six/million/fifty/one/thousand/two/hundred/forty/three" + to_text(81123, fmt), + "eightyonethousandonehundredtwentythree" ); + assert_eq!(to_text(12, fmt), "twelve"); } }