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
static ONES: [&str; 10] = [
    "zéro", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf",
];

static TEENS: [&str; 10] = [
    "dix", "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit",
    "dix-neuf",
];

static TENS: [&str; 8] = [
    "vingt",
    "trente",
    "quarante",
    "cinquante",
    "soixante",
    "soixante-dix",
    "quatre-vingt",
    "quatre-vingt-dix",
];

static BASES: [&str; 6] = [
    "mille", "million", "milliard", "billion", "billiard", "trillion",
];

static BASES_PLURAL: [&str; 6] = [
    "mille",
    "millions",
    "milliards",
    "billions",
    "billiards",
    "trillions",
];

static AND: &str = "et";
static DASH: &str = "-";
static HUNDRED: [&str; 2] = ["cent", "cents"];
static QUATRE_VINGTS: &str = "quatre-vingts";

/// Says a number in French.
///
/// The rules for saying numbers are from
/// [Wikipedia](https://fr.wikipedia.org/wiki/Nombres_en_français) and follow the 1990 orthography.
/// If you find any deviations from those rules, please file
/// [a bug report](https://github.com/minhnhdo/say-number/issues/new).
///
/// # Examples
///
/// ```rust
/// assert_eq!(say_number::french::say(416), "quatre-cent-seize".to_string());
/// assert_eq!(say_number::french::say(514), "cinq-cent-quatorze".to_string());
/// ```
pub fn say(mut n: u64) -> String {
    if n == 0 {
        return ONES[0].to_string();
    }
    let mut ret = Vec::new();
    let mut base_index = 0;
    while n > 0 {
        let hundreds_tens_and_ones = n % 1000;
        n /= 1000;
        if hundreds_tens_and_ones > 0 && base_index != 0 {
            if base_index == 1 && ret.len() > 0 {
                ret.push(DASH);
            }
            if hundreds_tens_and_ones == 1 {
                // single base (mille, million, milliard, ...)
                ret.push(BASES[base_index - 1]);
            } else {
                // plural base (mille, millions, milliards, ...)
                ret.push(BASES_PLURAL[base_index - 1]);
            }
        }
        let tens_and_ones = hundreds_tens_and_ones % 100;
        let hundreds = hundreds_tens_and_ones / 100;
        let ones = tens_and_ones % 10;
        let mut tens = tens_and_ones / 10;
        if hundreds_tens_and_ones > 1 && base_index == 1 {
            ret.push(DASH);
        }
        if ones > 0 {
            if tens == 1 || tens == 7 || tens == 9 {
                // special case for onze, douze,...;
                //                  soixante-et-onze, soixante-douze,..,;
                //                  quatre-vingt-onze, quatre-vingt-douze,...
                ret.push(TEENS[ones as usize]);
                tens -= 1;
            } else {
                // except for mille (not "UN mille" but just "mille")
                if base_index != 1 || hundreds_tens_and_ones != 1 {
                    ret.push(ONES[ones as usize]);
                }
            }
        }
        if tens > 0 {
            if tens != 8 && tens != 9 && ones == 1 {
                // cases for 21, 31, ..., 71 with "et-"
                ret.push(DASH);
                ret.push(AND);
            }
            if tens_and_ones == 80 && base_index != 1 {
                ret.push(QUATRE_VINGTS);
            } else {
                if ones > 0 {
                    ret.push(DASH);
                }
                ret.push(TENS[tens as usize - 2]);
            }
        }
        if hundreds > 0 {
            if tens_and_ones > 0 {
                ret.push(DASH);
            }
            if hundreds > 1 && tens_and_ones == 0 && base_index != 1 {
                // deux-centS, trois-centS, ...
                ret.push(HUNDRED[1]);
            } else {
                ret.push(HUNDRED[0]);
            }
            if hundreds > 1 {
                // number and dash before "cent", eg. DEUX-cent...
                ret.push(DASH);
                ret.push(ONES[hundreds as usize]);
            }
        }
        base_index += 1;
    }
    let mut result = String::new();
    let mut first = true;
    let mut dash = false;
    for s in ret.iter().rev() {
        if first {
            first = false;
        } else if !dash && s != &DASH {
            result.push_str(" ");
        }
        result.push_str(s);
        dash = s == ‐
    }
    result
}