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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! This crate transforms a number into its French representation

#![deny(missing_docs)]

extern crate num_integer;
extern crate num_traits;

use num_integer::*;
use num_traits::*;
use std::fmt::Display;

/// Options for French number representation
#[derive(Debug)]
pub struct Options {
    /// Set to `true` to get a feminine declination (default `false`).
    /// This only affects numbers ending in 1.
    pub feminine: bool,
    /// Set to `false` to prevent hyphens from being inserted between
    /// literals greater than 100 (default `true`). This corresponds
    /// to the way of writing predating the 1990 orthographic reform.
    pub reformed: bool,
}

impl Default for Options {
    fn default() -> Options {
        Options {
            feminine: false,
            reformed: true,
        }
    }
}

impl Options {
    fn masculine(&self) -> Self {
        Options {
            feminine: false,
            ..*self
        }
    }
}

fn literal_for(value: usize, options: &Options) -> Option<&'static str> {
    static SMALLS: [&'static str; 21] = [
        "zéro", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix",
        "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit",
        "dix-neuf", "vingt",
    ];
    if value == 1 && options.feminine {
        Some("une")
    } else if value <= 20 {
        Some(SMALLS[value])
    } else if value == 30 {
        Some("trente")
    } else if value == 40 {
        Some("quarante")
    } else if value == 50 {
        Some("cinquante")
    } else if value == 60 {
        Some("soixante")
    } else if value == 71 {
        Some(if options.reformed {
            "soixante-et-onze"
        } else {
            "soixante et onze"
        })
    } else if value == 80 {
        Some("quatre-vingts")
    } else if value == 81 {
        Some(if options.feminine {
            "quatre-vingt-une"
        } else {
            "quatre-vingt-un"
        })
    } else if value == 100 {
        Some("cent")
    } else if value == 1000 {
        Some("mille")
    } else {
        None
    }
}

fn add_unit_for(str: &mut String, prefix_count: usize, log1000: usize) -> bool {
    static PREFIXES: [&'static str; 16] = [
        "m",
        "b",
        "tr",
        "quadr",
        "quint",
        "sext",
        "sept",
        "oct",
        "non",
        "déc",
        "unodéc",
        "duodéc",
        "trédéc",
        "quattuordéc",
        "quindéc",
        "sexdéc",
    ];
    PREFIXES.get(log1000 / 2).map_or(false, |prefix| {
        str.push_str(prefix);
        if log1000 % 2 == 0 {
            str.push_str("illion");
        } else {
            str.push_str("illiard");
        }
        if prefix_count > 1 {
            str.push('s');
        }
        true
    })
}

fn unpluralize(str: &mut String) {
    if str.ends_with("ts") {
        let len = str.len();
        str.truncate(len - 1);
    }
}

fn complete(mut str: String, n: usize, prefix_under_100: bool, options: &Options) -> String {
    if n != 0 {
        unpluralize(&mut str);
    }
    if n == 1 {
        if prefix_under_100 && options.reformed {
            str.push_str("-et-un");
        } else if prefix_under_100 {
            str.push_str(" et un");
        } else if options.reformed {
            str.push_str("-un");
        } else {
            str.push_str(" un");
        }
        if options.feminine {
            str.push('e')
        }
    } else if n > 0 {
        str.push(if options.reformed || (prefix_under_100 && n < 100) {
            '-'
        } else {
            ' '
        });
        str.push_str(&basic(&n, options).unwrap());
    }
    str
}

fn basic<N: Integer + FromPrimitive + ToPrimitive>(n: &N, options: &Options) -> Option<String> {
    if let Some(n) = n.to_usize() {
        if let Some(literal) = literal_for(n, options) {
            return Some(literal.to_owned());
        } else if n < 60 {
            return Some(smaller_than_60(n, options));
        } else if n < 80 {
            return Some(base_onto(60, n, options));
        } else if n < 100 {
            return Some(base_onto(80, n, options));
        } else if n < 1000 {
            return Some(smaller_than_1000(n, options));
        } else if n < 2000 {
            return Some(smaller_than_2000(n, options));
        } else if n < 1_000_000 {
            return Some(smaller_than_1000000(n, options));
        }
    }
    over_1000000(n, options)
}

fn smaller_than_60(n: usize, options: &Options) -> String {
    let unit = n % 10;
    complete(
        basic(&(n - unit), &Default::default()).unwrap(),
        unit,
        true,
        options,
    )
}

fn base_onto(b: usize, n: usize, options: &Options) -> String {
    complete(
        literal_for(b, options).unwrap().to_owned(),
        n - b,
        true,
        options,
    )
}

fn smaller_than_1000(n: usize, options: &Options) -> String {
    let (hundredths, rest) = n.div_rem(&100);
    let result = if hundredths > 1 {
        let mut prefix = literal_for(hundredths, options).unwrap().to_owned();
        push_space_or_dash(&mut prefix, options);
        prefix.push_str("cents");
        prefix
    } else {
        "cent".to_owned()
    };
    complete(result, rest, false, options)
}

fn smaller_than_2000(n: usize, options: &Options) -> String {
    complete("mille".to_owned(), n - 1000, false, options)
}

fn push_space_or_dash(str: &mut String, options: &Options) {
    str.push(if options.reformed { '-' } else { ' ' });
}

fn smaller_than_1000000(n: usize, options: &Options) -> String {
    let (thousands, rest) = n.div_rem(&1000);
    let prefix = if thousands > 1 {
        let mut thousands = basic(&thousands, &options.masculine()).unwrap();
        unpluralize(&mut thousands);
        push_space_or_dash(&mut thousands, options);
        thousands.push_str("mille");
        thousands
    } else {
        "mille".to_owned()
    };
    complete(prefix, rest, false, options)
}

fn over_1000000<N: Integer + FromPrimitive + ToPrimitive>(
    n: &N,
    options: &Options,
) -> Option<String> {
    let thousand = N::from_u32(1000).unwrap();
    let (mut n, small) = n.div_rem(&N::from_u32(1_000_000).unwrap());
    let mut base = if small != N::zero() {
        basic(&small, options).unwrap()
    } else {
        String::new()
    };
    let mut log1000 = 0;
    while n != N::zero() {
        let (rest, prefix) = n.div_rem(&thousand);
        let prefix = prefix.to_usize().unwrap();
        if prefix > 0 {
            let mut str = basic(&prefix, &options.masculine()).unwrap();
            push_space_or_dash(&mut str, options);
            if !add_unit_for(&mut str, prefix, log1000) {
                return None;
            }
            if !base.is_empty() {
                push_space_or_dash(&mut str, options);
                str.push_str(&base);
            }
            base = str;
        }
        log1000 += 1;
        n = rest;
    }
    Some(base)
}

/// Compute the French language representation of the given number.
///
/// If the number is too large (greater than 10^103), then its numerical
/// representation is returned with a leading minus sign if needed.
///
/// By default, the masculine declination is used, as well as the preferred
/// orthographic form introduced in the 1990 reform (use hyphens everywhere).
/// See `french_number_options` if you wish to change either of those options.
///
/// # Example
///
/// ```
/// use french_numbers::french_number;
///
/// assert_eq!(french_number(&71), "soixante-et-onze");
/// assert_eq!(french_number(&1001), "mille-un");
/// assert_eq!(french_number(&-200001), "moins deux-cent-mille-un");
/// assert_eq!(french_number(&-200000001), "moins deux-cents-millions-un");
/// assert_eq!(french_number(&-204000001), "moins deux-cent-quatre-millions-un");
/// ```
pub fn french_number<N: Integer + FromPrimitive + ToPrimitive + Display>(n: &N) -> String {
    french_number_options(n, &Default::default())
}

/// Compute the French language representation of the given number.
///
/// If the number is too large (greater than 10^103), then its numerical
/// representation is returned with a leading minus sign if needed.
///
/// # Example
///
/// ```
/// use french_numbers::*;
///
/// assert_eq!(french_number_options(&37251061,
///                                  &Options { feminine: false, reformed: true}),
///            "trente-sept-millions-deux-cent-cinquante-et-un-mille-soixante-et-un");
/// assert_eq!(french_number_options(&37251061,
///                                  &Options { feminine: true, reformed: true}),
///            "trente-sept-millions-deux-cent-cinquante-et-un-mille-soixante-et-une");
/// assert_eq!(french_number_options(&37251061,
///                                  &Options { feminine: true, reformed: false }),
///            "trente-sept millions deux cent cinquante et un mille soixante et une");
/// assert_eq!(french_number_options(&37251061,
///                                  &Options { feminine: false, reformed: false }),
///            "trente-sept millions deux cent cinquante et un mille soixante et un")
/// ```
pub fn french_number_options<N: Integer + FromPrimitive + ToPrimitive + Display>(
    n: &N,
    options: &Options,
) -> String {
    if n < &N::zero() {
        // Take the absolute value of n without consuming it. Since n is
        // negative, we know that we can build the -1 constant.
        let n = n.div_floor(&N::from_isize(-1).unwrap());
        if let Some(str) = basic(&n, options) {
            let mut result = "moins ".to_owned();
            result.push_str(&str);
            return result;
        }
    } else if let Some(result) = basic(n, options) {
        return result;
    }
    n.to_string()
}

#[cfg(test)]
mod tests {

    use ::*;

    #[test]
    fn test_literal_for() {
        assert_eq!(literal_for(30, &Default::default()), Some("trente"));
        assert_eq!(literal_for(31, &Default::default()), None);
    }

    #[test]
    fn test_add_unit_for() {
        let mut str = String::new();
        assert_eq!(add_unit_for(&mut str, 1, 0), true);
        assert_eq!(str, "million");
        str.clear();
        assert_eq!(add_unit_for(&mut str, 2, 0), true);
        assert_eq!(str, "millions");
        str.clear();
        assert_eq!(add_unit_for(&mut str, 1, 3), true);
        assert_eq!(str, "billiard");
        assert_eq!(add_unit_for(&mut str, 1, 97), false);
    }

    #[test]
    fn test_unpluralize() {
        let mut s = "quatre-cents".to_owned();
        unpluralize(&mut s);
        assert_eq!(s, "quatre-cent");
        let mut s = "cent".to_owned();
        unpluralize(&mut s);
        assert_eq!(s, "cent");
    }

    #[test]
    fn test_basic() {
        assert_eq!(basic(&0, &Default::default()).unwrap(), "zéro");
        assert_eq!(basic(&21, &Default::default()).unwrap(), "vingt-et-un");
        assert_eq!(basic(&54, &Default::default()).unwrap(), "cinquante-quatre");
        assert_eq!(basic(&64, &Default::default()).unwrap(), "soixante-quatre");
        assert_eq!(basic(&71, &Default::default()).unwrap(), "soixante-et-onze");
        assert_eq!(basic(&72, &Default::default()).unwrap(), "soixante-douze");
        assert_eq!(basic(&80, &Default::default()).unwrap(), "quatre-vingts");
        assert_eq!(basic(&81, &Default::default()).unwrap(), "quatre-vingt-un");
        assert_eq!(
            basic(&91, &Default::default()).unwrap(),
            "quatre-vingt-onze"
        );
        assert_eq!(basic(&101, &Default::default()).unwrap(), "cent-un");
        assert_eq!(basic(&800, &Default::default()).unwrap(), "huit-cents");
        assert_eq!(basic(&803, &Default::default()).unwrap(), "huit-cent-trois");
        assert_eq!(
            basic(&872, &Default::default()).unwrap(),
            "huit-cent-soixante-douze"
        );
        assert_eq!(
            basic(&880, &Default::default()).unwrap(),
            "huit-cent-quatre-vingts"
        );
        assert_eq!(
            basic(&882, &Default::default()).unwrap(),
            "huit-cent-quatre-vingt-deux"
        );
        assert_eq!(basic(&1001, &Default::default()).unwrap(), "mille-un");
        assert_eq!(
            basic(&1882, &Default::default()).unwrap(),
            "mille-huit-cent-quatre-vingt-deux"
        );
        assert_eq!(basic(&2001, &Default::default()).unwrap(), "deux-mille-un");
        assert_eq!(
            basic(&300001, &Default::default()).unwrap(),
            "trois-cent-mille-un"
        );
        assert_eq!(
            basic(&180203, &Default::default()).unwrap(),
            "cent-quatre-vingt-mille-deux-cent-trois"
        );
        assert_eq!(
            basic(&180203, &Default::default()).unwrap(),
            "cent-quatre-vingt-mille-deux-cent-trois"
        );
        assert_eq!(
            basic(&17180203, &Default::default()).unwrap(),
            "dix-sept-millions-cent-quatre-vingt-mille-deux-cent-trois"
        );
    }
}