use crate::Result;
use crate::currency::vocab::MS as CURRENCY_VOCAB;
use crate::currency::{Currency, assert_minor_unit};
use crate::language::{Kind, Language, form};
use crate::max::Max;
use crate::number::{
Number, digits_to_biguint, parse_cardinal, parse_currency, parse_ordinal, split_groups,
};
use crate::options::options;
use num_bigint::BigUint;
use num_traits::{One, ToPrimitive, Zero};
const ONES: [&str; 10] = [
"", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "lapan", "sembilan",
];
const TEENS: [&str; 10] = [
"sepuluh",
"sebelas",
"dua belas",
"tiga belas",
"empat belas",
"lima belas",
"enam belas",
"tujuh belas",
"lapan belas",
"sembilan belas",
];
const TENS: [&str; 10] = [
"",
"",
"dua puluh",
"tiga puluh",
"empat puluh",
"lima puluh",
"enam puluh",
"tujuh puluh",
"lapan puluh",
"sembilan puluh",
];
const HUNDRED_WORD: &str = "ratus";
const THOUSAND_WORD: &str = "ribu";
const SCALE_WORDS: &[&str] = &["juta", "bilion", "trilion"];
pub const CARDINAL_MAX: Max = Max::western(SCALE_WORDS.len() as u32 + 1);
pub const ORDINAL_MAX: Max = Max::western(SCALE_WORDS.len() as u32 + 1);
pub const CURRENCY_MAX: Max = Max::western(SCALE_WORDS.len() as u32 + 1);
const ZERO: &str = "sifar";
const NEGATIVE: &str = "minus";
const DECIMAL_SEP: &str = "perpuluhan";
const ORDINAL_PREFIX: &str = "ke";
const ORDINAL_FIRST: &str = "pertama";
fn build_segment(n: u32) -> String {
if n == 0 {
return String::new();
}
let ones_digit = (n % 10) as usize;
let tens_digit = ((n / 10) % 10) as usize;
let hundreds_digit = (n / 100) as usize;
let mut parts: Vec<String> = Vec::new();
if hundreds_digit > 0 {
if hundreds_digit == 1 {
parts.push(format!("se{HUNDRED_WORD}"));
} else {
parts.push(format!("{} {HUNDRED_WORD}", ONES[hundreds_digit]));
}
}
let tens_ones = (n % 100) as usize;
if tens_ones == 0 {
} else if tens_ones < 10 {
parts.push(ONES[tens_ones].to_string());
} else if tens_ones < 20 {
parts.push(TEENS[tens_ones - 10].to_string());
} else if ones_digit == 0 {
parts.push(TENS[tens_digit].to_string());
} else {
parts.push(format!("{} {}", TENS[tens_digit], ONES[ones_digit]));
}
parts.join(" ")
}
fn integer_to_words(n: &BigUint) -> String {
if n.is_zero() {
return ZERO.to_string();
}
if let Some(small) = n.to_u64() {
if small < 1000 {
return build_segment(small as u32);
}
if small < 1_000_000 {
let thousands = (small / 1000) as u32;
let remainder = (small % 1000) as u32;
let mut result = if thousands == 1 {
format!("se{THOUSAND_WORD}")
} else {
format!("{} {THOUSAND_WORD}", build_segment(thousands))
};
if remainder > 0 {
result.push(' ');
result.push_str(&build_segment(remainder));
}
return result;
}
}
build_large_number_words(n)
}
fn build_large_number_words(n: &BigUint) -> String {
let segments = split_groups(n, 1000);
let mut parts: Vec<String> = Vec::new();
for (scale_index, &segment) in segments.iter().enumerate().rev() {
if segment == 0 {
continue;
}
if scale_index == 0 {
parts.push(build_segment(segment));
} else if scale_index == 1 {
if segment == 1 {
parts.push(format!("se{THOUSAND_WORD}"));
} else {
parts.push(format!("{} {THOUSAND_WORD}", build_segment(segment)));
}
} else {
let scale_word = SCALE_WORDS[scale_index - 2];
if segment == 1 {
parts.push(format!("se{scale_word}"));
} else {
parts.push(format!("{} {scale_word}", build_segment(segment)));
}
}
}
parts.join(" ")
}
fn decimal_part_to_words(decimal: &str) -> String {
let mut result = String::new();
let zeros = decimal.bytes().take_while(|&b| b == b'0').count();
for _ in 0..zeros {
if !result.is_empty() {
result.push(' ');
}
result.push_str(ZERO);
}
let remainder = &decimal[zeros..];
if !remainder.is_empty() {
if !result.is_empty() {
result.push(' ');
}
result.push_str(&integer_to_words(&digits_to_biguint(remainder)));
}
result
}
pub fn to_cardinal<'a>(value: impl Into<Number<'a>>) -> Result<String> {
let number = parse_cardinal(&value.into())?;
CARDINAL_MAX.check(&number.integer, number.fraction.as_deref())?;
let mut result = String::new();
if number.negative {
result.push_str(NEGATIVE);
result.push(' ');
}
result.push_str(&integer_to_words(&number.integer));
if let Some(fraction) = &number.fraction {
result.push(' ');
result.push_str(DECIMAL_SEP);
result.push(' ');
result.push_str(&decimal_part_to_words(fraction));
}
Ok(result)
}
fn integer_to_ordinal(n: &BigUint) -> String {
if n.is_one() {
return ORDINAL_FIRST.to_string();
}
format!("{ORDINAL_PREFIX}{}", integer_to_words(n))
}
pub fn to_ordinal<'a>(value: impl Into<Number<'a>>) -> Result<String> {
let n = parse_ordinal(&value.into())?;
ORDINAL_MAX.check(&n, None)?;
Ok(integer_to_ordinal(&n))
}
options! {
pub struct CurrencyOptions {
currency: Currency = Currency::MYR, table = CURRENCY_VOCAB;
}
}
pub fn to_currency<'a>(value: impl Into<Number<'a>>) -> Result<String> {
to_currency_with(value, &CurrencyOptions::default())
}
pub fn to_currency_with<'a>(
value: impl Into<Number<'a>>,
options: &CurrencyOptions,
) -> Result<String> {
let words = CURRENCY_VOCAB.words(options.currency)?;
let money = parse_currency(&value.into(), options.currency.minor_unit_digits())?;
CURRENCY_MAX.check(&money.major, None)?;
assert_minor_unit(money.minor, options.currency)?;
let mut result = String::new();
if money.negative {
result.push_str(NEGATIVE);
result.push(' ');
}
if !money.major.is_zero() || money.minor == 0 {
result.push_str(&integer_to_words(&money.major));
result.push(' ');
result.push_str(words.major_at(0));
}
if money.minor > 0 {
if !money.major.is_zero() {
result.push(' ');
}
result.push_str(&integer_to_words(&BigUint::from(money.minor)));
result.push(' ');
result.push_str(words.minor_at(0));
}
Ok(result)
}
pub static LANGUAGE: Language = Language {
code: "ms-MY",
name: "Malay (Malaysia)",
kind: Kind::Full,
cardinal: Some(form!(plain to_cardinal, CARDINAL_MAX)),
ordinal: Some(form!(plain to_ordinal, ORDINAL_MAX)),
currency: Some(form!(with_options to_currency_with, CurrencyOptions, CURRENCY_MAX)),
};