use crate::Result;
use crate::currency::vocab::KO 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::{ToPrimitive, Zero};
const ONES: [&str; 10] = ["", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"];
const TEN: &str = "십";
const HUNDRED: &str = "백";
const THOUSAND: &str = "천";
const ZERO: &str = "영";
const NEGATIVE: &str = "마이너스";
const DECIMAL_SEP: &str = "점";
const SCALES: &[&str] = &["만", "억", "조", "경", "해", "자", "양"];
pub const CARDINAL_MAX: Max = Max::myriad(SCALES.len() as u32);
pub const ORDINAL_MAX: Max = Max::myriad(SCALES.len() as u32);
pub const CURRENCY_MAX: Max = Max::myriad(SCALES.len() as u32);
const ORDINAL_PREFIX: &str = "제";
fn push_place(result: &mut String, digit: usize, scale: &str) {
if digit > 0 {
if digit != 1 {
result.push_str(ONES[digit]);
}
result.push_str(scale);
}
}
fn build_segment(n: u32) -> String {
if n == 0 {
return String::new();
}
let ones = (n % 10) as usize;
let tens = ((n / 10) % 10) as usize;
let hundreds = ((n / 100) % 10) as usize;
let thousands = (n / 1000) as usize;
let mut result = String::new();
push_place(&mut result, thousands, THOUSAND);
push_place(&mut result, hundreds, HUNDRED);
push_place(&mut result, tens, TEN);
if ones > 0 {
result.push_str(ONES[ones]);
}
result
}
fn integer_to_words(n: &BigUint) -> String {
if n.is_zero() {
return ZERO.to_string();
}
if let Some(small) = n.to_u64() {
if small < 10_000 {
return build_segment(small as u32);
}
}
build_large_number_words(n)
}
fn build_large_number_words(n: &BigUint) -> String {
let segments = split_groups(n, 10_000);
let mut result = String::new();
let mut prev_was_scale = false;
for (i, &segment) in segments.iter().enumerate().rev() {
if segment == 0 {
continue;
}
if i == 0 {
if prev_was_scale {
result.push(' ');
}
result.push_str(&build_segment(segment));
prev_was_scale = false;
} else {
if segment != 1 {
if prev_was_scale {
result.push(' ');
}
result.push_str(&build_segment(segment));
}
result.push_str(SCALES[i - 1]);
prev_was_scale = true;
}
}
if result.is_empty() {
ZERO.to_string()
} else {
result
}
}
fn decimal_part_to_words(decimal: &str) -> String {
let mut parts: Vec<String> = Vec::new();
let zeros = decimal.bytes().take_while(|&b| b == b'0').count();
for _ in 0..zeros {
parts.push(ZERO.to_string());
}
let remainder = &decimal[zeros..];
if !remainder.is_empty() {
parts.push(integer_to_words(&digits_to_biguint(remainder)));
}
parts.join(" ")
}
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 parts: Vec<String> = Vec::new();
if number.negative {
parts.push(NEGATIVE.to_string());
}
parts.push(integer_to_words(&number.integer));
if let Some(fraction) = &number.fraction {
parts.push(DECIMAL_SEP.to_string());
parts.push(decimal_part_to_words(fraction));
}
Ok(parts.join(" "))
}
fn integer_to_ordinal(n: &BigUint) -> 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::KRW, 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(' ');
}
result.push_str(&integer_to_words(&money.major));
result.push_str(words.major_at(0));
Ok(result)
}
pub static LANGUAGE: Language = Language {
code: "ko-KR",
name: "Korean (South Korea)",
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)),
};