Generate docs and fix bug

Add examples in docs
Fixed wrong words in the outputs
This commit is contained in:
Uttarayan Mondal
2021-03-08 02:12:02 +05:30
parent 2595553b2b
commit 11603829bb
7 changed files with 157 additions and 84 deletions
+53 -39
View File
@@ -1,3 +1,24 @@
//!```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.
//!
//!```rust
//!extern crate ntext;
//!use ntext::to_text;
//!fn main() {
//! println!("{}",to_text(1312));
//! assert_eq!(to_text(1312),"onethousandthreehundredtwelve");
//!}
//!```
//! This fucntion will also return an empty string on input zero
mod test;
/// Convert digit to text.
pub fn digit_to_text(digit: u8) -> Option<String> {
@@ -54,56 +75,49 @@ fn tens_place(tens: u8, ones: u8) -> Option<String> {
}
}
fn place_value(number: u8, place: u8) -> Option<String> {
let mut value = digit_to_text(number).unwrap();
match place {
p @ 3..=6 => Some({
let mut value = digit_to_text(number).unwrap();
match p {
3 => value.push_str("hundred"),
4 => value.push_str("thousand"),
// 5 => value.push_str("placeholder"),
6 => value.push_str("million"),
_ => (),
}
value
}),
_ => None,
}
1 => (),
2 => (), //This should never happen as 2 is included in the tens_place_holders
3 => value.push_str("hundred"),
4 => value.push_str("thousand"),
5 => (), // Souldn't happen
6 => value.push_str("million"),
_ => (),
};
Some(value)
}
/// Convert u32 to words in a string.
pub fn to_text(number: u32) -> 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 second_digit_array: [usize; 2] = [0, 3];
let mut last: Option<u8> = None;
if digits.len() == 1 {
numtext.push_str(digit_to_text(*digits.last().unwrap()).unwrap().as_str());
} else {
for (index, digit) in digits.iter().rev().enumerate() {
if second_digit_array.contains(&index) {
last = Some(digit.clone());
continue;
}
if let Some(last_digit) = last {
let mut buffer = String::new();
buffer.push_str(tens_place(digit.clone(), last_digit).unwrap().as_str());
if index > 2 {
buffer.push_str(place_value(0, index as u8).unwrap().as_str());
}
numtext.insert_str(0, buffer.as_str());
last = None;
} else {
numtext.insert_str(
0,
place_value(digit.clone(), index as u8 + 1)
.unwrap()
.as_str(),
)
}
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 {
println!("hello index {}", place);
numtext.push_str(tens_place(last_digit, *digit).unwrap().as_str());
if place > 2 {
numtext.push_str(place_value(0, place).unwrap().as_str());
}
last = None;
} else if *digit != 0 {
numtext.push_str(place_value(*digit, place).unwrap().as_str())
}
place -= 1;
}
numtext
}
+5 -1
View File
@@ -11,10 +11,14 @@ mod tests {
#[test]
fn numbers() {
use crate::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");
assert_eq!(to_text(2), "two");
}
#[test]
fn tens() {