Removed the struct
This commit is contained in:
+13
-63
@@ -1,20 +1,7 @@
|
|||||||
#[allow(dead_code)]
|
mod test;
|
||||||
pub struct NumText {
|
/// Convert digit to text.
|
||||||
number: u32,
|
pub fn digit_to_text(digit: u8) -> Option<String> {
|
||||||
text: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code, unused_variables)]
|
|
||||||
impl NumText {
|
|
||||||
fn new(number: u32) -> Self {
|
|
||||||
NumText {
|
|
||||||
number,
|
|
||||||
text: NumText::to_text(number),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn digit_to_text(digit: u8) -> Option<String> {
|
|
||||||
match digit {
|
match digit {
|
||||||
// 0 => Some("zero".to_string()),
|
|
||||||
0 => Some("".to_string()),
|
0 => Some("".to_string()),
|
||||||
1 => Some("one".to_string()),
|
1 => Some("one".to_string()),
|
||||||
2 => Some("two".to_string()),
|
2 => Some("two".to_string()),
|
||||||
@@ -31,7 +18,7 @@ impl NumText {
|
|||||||
|
|
||||||
fn tens_place(tens: u8, ones: u8) -> Option<String> {
|
fn tens_place(tens: u8, ones: u8) -> Option<String> {
|
||||||
match tens {
|
match tens {
|
||||||
0 => NumText::digit_to_text(ones),
|
0 => digit_to_text(ones),
|
||||||
1 => match ones {
|
1 => match ones {
|
||||||
0 => Some("ten".to_string()),
|
0 => Some("ten".to_string()),
|
||||||
1 => Some("eleven".to_string()),
|
1 => Some("eleven".to_string()),
|
||||||
@@ -55,12 +42,12 @@ impl NumText {
|
|||||||
8 => buffer.push_str("eighty"),
|
8 => buffer.push_str("eighty"),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
buffer.push_str(NumText::digit_to_text(ones).unwrap().as_str());
|
buffer.push_str(digit_to_text(ones).unwrap().as_str());
|
||||||
buffer
|
buffer
|
||||||
}),
|
}),
|
||||||
d @ 6..=9 => Some({
|
d @ 6..=9 => Some({
|
||||||
let mut string = NumText::digit_to_text(d).unwrap() + "ty";
|
let mut string = digit_to_text(d).unwrap() + "ty";
|
||||||
string.push_str(NumText::digit_to_text(ones).unwrap().as_str());
|
string.push_str(digit_to_text(ones).unwrap().as_str());
|
||||||
string
|
string
|
||||||
}),
|
}),
|
||||||
_ => None,
|
_ => None,
|
||||||
@@ -69,7 +56,7 @@ impl NumText {
|
|||||||
fn place_value(number: u8, place: u8) -> Option<String> {
|
fn place_value(number: u8, place: u8) -> Option<String> {
|
||||||
match place {
|
match place {
|
||||||
p @ 3..=6 => Some({
|
p @ 3..=6 => Some({
|
||||||
let mut value = Self::digit_to_text(number).unwrap();
|
let mut value = digit_to_text(number).unwrap();
|
||||||
match p {
|
match p {
|
||||||
3 => value.push_str("hundred"),
|
3 => value.push_str("hundred"),
|
||||||
4 => value.push_str("thousand"),
|
4 => value.push_str("thousand"),
|
||||||
@@ -82,8 +69,8 @@ impl NumText {
|
|||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Convert u32 to words in a string.
|
||||||
fn to_text(number: u32) -> String {
|
pub fn to_text(number: u32) -> String {
|
||||||
let mut numtext: String = String::new();
|
let mut numtext: String = String::new();
|
||||||
let digits: Vec<u8> = number
|
let digits: Vec<u8> = number
|
||||||
.to_string()
|
.to_string()
|
||||||
@@ -99,20 +86,16 @@ impl NumText {
|
|||||||
}
|
}
|
||||||
if let Some(last_digit) = last {
|
if let Some(last_digit) = last {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
buffer.push_str(
|
buffer.push_str(tens_place(digit.clone(), last_digit).unwrap().as_str());
|
||||||
Self::tens_place(digit.clone(), last_digit)
|
|
||||||
.unwrap()
|
|
||||||
.as_str(),
|
|
||||||
);
|
|
||||||
if index > 2 {
|
if index > 2 {
|
||||||
buffer.push_str(Self::place_value(0, index as u8).unwrap().as_str());
|
buffer.push_str(place_value(0, index as u8).unwrap().as_str());
|
||||||
}
|
}
|
||||||
numtext.insert_str(0, buffer.as_str());
|
numtext.insert_str(0, buffer.as_str());
|
||||||
last = None;
|
last = None;
|
||||||
} else {
|
} else {
|
||||||
numtext.insert_str(
|
numtext.insert_str(
|
||||||
0,
|
0,
|
||||||
Self::place_value(digit.clone(), index as u8 + 1)
|
place_value(digit.clone(), index as u8 + 1)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_str(),
|
.as_str(),
|
||||||
)
|
)
|
||||||
@@ -120,36 +103,3 @@ impl NumText {
|
|||||||
}
|
}
|
||||||
numtext
|
numtext
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
#[test]
|
|
||||||
fn digits() {
|
|
||||||
use crate::NumText;
|
|
||||||
assert_eq!(NumText::digit_to_text(9).unwrap(), "nine");
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn numbers() {
|
|
||||||
use crate::NumText;
|
|
||||||
assert_eq!(
|
|
||||||
NumText::to_text(12345),
|
|
||||||
"twelvethousandthreehundredfortyfive"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
NumText::to_text(81123),
|
|
||||||
"eightyonethousandonehundredtwentythree"
|
|
||||||
);
|
|
||||||
assert_eq!(NumText::to_text(12), "twelve");
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn tens() {
|
|
||||||
use crate::NumText;
|
|
||||||
assert_eq!(NumText::tens_place(1, 2).unwrap(), "twelve");
|
|
||||||
assert_eq!(NumText::tens_place(7, 6).unwrap(), "seventysix");
|
|
||||||
assert_eq!(NumText::tens_place(5, 7).unwrap(), "fiftyseven");
|
|
||||||
assert_eq!(NumText::tens_place(2, 3).unwrap(), "twentythree");
|
|
||||||
assert_eq!(NumText::tens_place(6, 9).unwrap(), "sixtynine");
|
|
||||||
assert_eq!(NumText::tens_place(0, 9).unwrap(), "nine");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn digits() {
|
||||||
|
use crate::digit_to_text;
|
||||||
|
assert_eq!(digit_to_text(9).unwrap(), "nine");
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn numbers() {
|
||||||
|
use crate::to_text;
|
||||||
|
assert_eq!(to_text(12345), "twelvethousandthreehundredfortyfive");
|
||||||
|
assert_eq!(to_text(81123), "eightyonethousandonehundredtwentythree");
|
||||||
|
assert_eq!(to_text(12), "twelve");
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn tens() {
|
||||||
|
use crate::tens_place;
|
||||||
|
assert_eq!(tens_place(1, 2).unwrap(), "twelve");
|
||||||
|
assert_eq!(tens_place(7, 6).unwrap(), "seventysix");
|
||||||
|
assert_eq!(tens_place(5, 7).unwrap(), "fiftyseven");
|
||||||
|
assert_eq!(tens_place(2, 3).unwrap(), "twentythree");
|
||||||
|
assert_eq!(tens_place(6, 9).unwrap(), "sixtynine");
|
||||||
|
assert_eq!(tens_place(0, 9).unwrap(), "nine");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user