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
fn is_whitespace(c: char) -> bool {
c == ' ' || c == ' ' || c == ' '
}
pub trait Cleaner {
fn clean(&self, _: &mut String, _: bool) {}
}
pub struct Off;
impl Cleaner for Off {}
pub struct Default;
impl Cleaner for Default {
fn clean(&self, s: &mut String, _: bool) {
if s.contains(is_whitespace) {
let mut new_s = String::with_capacity(s.len());
let mut previous_space = false;
for c in s.chars() {
if is_whitespace(c) {
if previous_space {
} else {
new_s.push(c);
previous_space = true;
}
} else {
previous_space = false;
new_s.push(c);
}
}
*s = new_s
}
}
}
pub struct French;
impl Cleaner for French {
fn clean(&self, s: &mut String, latex: bool) {
fn is_trouble(c: char) -> bool {
match c {
'?'|'!'|';'|':'|'»'|'«'|'—' => true,
_ => false
}
}
let nb_char = if latex {
'~'
} else {
' '
};
let nb_char_narrow = if latex {
'~'
} else {
'\u{202F}'
};
let nb_char_em = if latex {
'~'
} else {
'\u{2002}'
};
if !s.contains(is_trouble) {
return;
}
Default.clean(s, latex);
let mut new_s = String::with_capacity(s.len());
{
let mut chars = s.chars();
if let Some(mut current) = chars.next() {
while let Some(next) = chars.next() {
if is_whitespace(current) {
match next {
'?' | '!' | ';' => new_s.push(nb_char_narrow),
':' | '»' => new_s.push(nb_char),
_ => new_s.push(current)
}
} else {
new_s.push(current);
match current {
'—' | '«' => {
if is_whitespace(next) {
let replacing_char = match current {
'—' => nb_char_em,
'«' => nb_char,
_ => unreachable!(),
};
if let Some(next) = chars.next() {
new_s.push(replacing_char);
current = next;
continue;
} else {
current = replacing_char;
break;
}
}
},
_ => (),
}
}
current = next;
}
new_s.push(current);
}
}
*s = new_s
}
}