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
use std::cmp;
use std::fmt;
use std::fmt::Write;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Locale {
language: [i8; 3],
country: [i8; 3],
}
impl Locale {
pub fn new(locale_name: &str) -> Self {
let (language_str, country_str) = locale_name.split_at(cmp::min(locale_name.len(), 3));
let mut locale = Locale {
language: [0; 3],
country: [0; 3],
};
for (c, s) in locale.language.iter_mut().zip(language_str.bytes().take(2)) {
*c = s as i8;
}
for (c, s) in locale.country.iter_mut().zip(country_str.bytes().take(2)) {
*c = s as i8;
}
locale
}
pub fn none() -> Self {
Locale {
language: [0; 3],
country: [0; 3],
}
}
pub(crate) fn language_ptr(&self) -> *const i8 {
&self.language as _
}
pub(crate) fn country_ptr(&self) -> *const i8 {
&self.country as _
}
pub(crate) fn language_ptr_mut(&mut self) -> *mut i8 {
&self.language as *const i8 as _
}
pub(crate) fn country_ptr_mut(&mut self) -> *mut i8 {
&self.country as *const i8 as _
}
}
impl<'a> From<&'a str> for Locale {
fn from(s: &'a str) -> Self {
Locale::new(s)
}
}
impl Default for Locale {
fn default() -> Self {
Locale::none()
}
}
impl fmt::Debug for Locale {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<Locale as fmt::Display>::fmt(self, f)
}
}
impl fmt::Display for Locale {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for &c in self.language.iter().take_while(|&&c| c != 0) {
f.write_char(c as u8 as char)?;
}
f.write_char('_')?;
for &c in self.country.iter().take_while(|&&c| c != 0) {
f.write_char(c as u8 as char)?;
}
Ok(())
}
}
#[test]
fn locale() {
let l = Locale::new("");
assert_eq!([0i8; 3], l.language);
assert_eq!([0i8; 3], l.country);
let l = Locale::none();
assert_eq!([0i8; 3], l.language);
assert_eq!([0i8; 3], l.country);
let l = Locale::new("Ab");
assert_eq!(['A' as i8, 'b' as i8, 0], l.language);
assert_eq!([0i8; 3], l.country);
let l = Locale::new("Ab-X");
assert_eq!(['A' as i8, 'b' as i8, 0], l.language);
assert_eq!(['X' as i8, 0, 0], l.country);
let l = Locale::new("overlong");
assert_eq!(['o' as i8, 'v' as i8, 0], l.language);
assert_eq!(['r' as i8, 'l' as i8, 0], l.country);
unsafe {
assert_eq!('o' as i8, *l.language_ptr());
}
unsafe {
assert_eq!('r' as i8, *l.country_ptr());
}
}