Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/src/csl/case.rs

10.6 KiB, 1 run

created by r1870400018:73171, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Ported from hayagriva 0.10.1 `src/lang/mod.rs`, Copyright (c) Martin Haug and the hayagriva
2//! contributors, MIT OR Apache-2.0.
3//!
4//! hayagriva's `TitleCase` and `SentenceCase` options are fixed here at the defaults its CSL
5//! renderer uses (capitalise after punctuation, hyphens separate words, keep all-capital words, use
6//! the word lists, trim both ends; never force the last word), so only the branches those reach
7//! are kept.
8use crate::csl::{
9 case_words::{
10 ALWAYS_CAPITALIZE,
11 NEVER_CAPITALIZE,
12 },
13 style::TextCase,
14};
15
16
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
18pub enum Case {
19 Title,
20 Sentence,
21 Uppercase,
22 Lowercase,
23 #[default]
24 NoTransform,
25 FirstUpper, // capitalize-first
26 AllUpper, // capitalize-all
27}
28
29impl From<TextCase> for Case {
30 fn from(c: TextCase) -> Self {
31 match c {
32 TextCase::Uppercase => Self::Uppercase,
33 TextCase::Lowercase => Self::Lowercase,
34 TextCase::Title => Self::Title,
35 TextCase::Sentence => Self::Sentence,
36 TextCase::CapitaliseFirst => Self::FirstUpper,
37 TextCase::CapitaliseAll => Self::AllUpper,
38 }
39 }
40}
41
42impl Case {
43 pub fn transform(self, s: &str) -> String {
44 let mut f = CaseFolder::with_case(self);
45 f.push_str(s);
46 f.finish()
47 }
48}
49
50/// Does the case depend on the language, so that hayagriva applies it only to English?
51pub fn is_language_dependent(c: TextCase) -> bool {
52 matches!(c, TextCase::Title | TextCase::Sentence)
53}
54
55#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
56enum CharClass {
57 #[default]
58 NewSentence,
59 MaybeNewSentence,
60 NewSubclause,
61 MaybeSubclause,
62 NewWord,
63 InWord,
64}
65
66impl CharClass {
67
68 fn step(self, c: char, hyphen_separates: bool) -> Self {
69 match c {
70 '.' | '?' | '!' | '…' | '‽' | '。' | ':' => if self.is_in_word() { Self::MaybeNewSentence } else { self },
71 '(' | '[' | '{' => Self::NewSubclause,
72 ')' | ']' | '}' => self,
73 '"' | '”' | '“' | '»' | '›' | '«' | '‹' | '‘' | '\'' => self,
74 ',' | ';' => if self.is_in_word() { Self::MaybeSubclause } else { self },
75 _ if c.is_whitespace() || (hyphen_separates && matches!(c, '-' | '–' | '—')) => match self {
76 Self::MaybeNewSentence => Self::NewSentence,
77 Self::MaybeSubclause => Self::NewSubclause,
78 Self::InWord => Self::NewWord,
79 _ => self,
80 },
81 _ => Self::InWord,
82 }
83 }
84
85 fn is_new_sentence(self) -> bool { matches!(self, Self::NewSentence) }
86 fn is_new_subclause(self) -> bool { matches!(self, Self::NewSubclause | Self::NewSentence) }
87 fn is_new_word(self) -> bool { matches!(self, Self::NewWord | Self::NewSubclause | Self::NewSentence) }
88 fn is_in_word(self) -> bool { matches!(self, Self::InWord | Self::MaybeSubclause | Self::MaybeNewSentence) }
89}
90
91#[derive(Copy, Clone, Debug, PartialEq, Eq)]
92enum WordCase { Lowercase, HasNonStartUpper, AllUpper }
93
94impl WordCase {
95 fn feed(self, is_upper: bool, second: bool) -> Self {
96 match (self, is_upper, second) {
97 (Self::Lowercase, true, _) => Self::HasNonStartUpper,
98 (Self::AllUpper, false, false) => Self::HasNonStartUpper,
99 (Self::AllUpper, false, true) => Self::Lowercase,
100 _ => self,
101 }
102 }
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106struct WordData {
107 start: usize,
108 end: usize,
109 case: WordCase,
110 follows_punctuation: bool,
111}
112
113impl WordData {
114
115 fn new(c: char, before: usize, now: usize, prev: CharClass, class: CharClass) -> Option<Self> {
116 match class.is_in_word() {
117 true => Some(Self {
118 start: before,
119 end: now,
120 case: if c.is_uppercase() { WordCase::AllUpper } else { WordCase::Lowercase },
121 follows_punctuation: prev.is_new_subclause(),
122 }),
123 false => None,
124 }
125 }
126
127 fn feed(self, c: char, before: usize, now: usize, prev: CharClass, class: CharClass) -> Option<Self> {
128 if class.is_new_word() {
129 return None;
130 }
131 if !prev.is_in_word() && class == CharClass::InWord {
132 return Self::new(c, before, now, prev, class);
133 }
134 let second = self.end - self.start == 1;
135 let case = if c.is_ascii_punctuation() { self.case } else { self.case.feed(c.is_uppercase(), second) };
136 Some(Self { end: now, case, ..self })
137 }
138
139 fn is_all_upper(self) -> bool {
140 matches!(self.case, WordCase::AllUpper) && self.end - self.start > 1
141 }
142}
143
144enum Verdict { AllUpper, Capitalise, Lowercase, Keep }
145
146/// A string buffer that applies a text case as characters arrive, word by word.
147#[derive(Debug, Clone)]
148pub struct CaseFolder {
149 case: Case,
150 pristine: bool,
151 class: CharClass,
152 buf: String,
153 last_reconfig: usize,
154 last_word: Option<WordData>,
155}
156
157impl Default for CaseFolder {
158 fn default() -> Self {
159 Self {
160 case: Case::default(),
161 pristine: true,
162 class: CharClass::default(),
163 buf: String::new(),
164 last_reconfig: 0,
165 last_word: None,
166 }
167 }
168}
169
170impl CaseFolder {
171
172 pub fn with_case(case: Case) -> Self {
173 Self { case, ..Default::default() }
174 }
175
176 pub fn push_str(&mut self, s: &str) {
177 match self.case {
178 Case::NoTransform => {
179 self.buf.push_str(s);
180 self.class = CharClass::default();
181 }
182 Case::Uppercase => {
183 self.buf.extend(s.chars().flat_map(char::to_uppercase));
184 self.class = CharClass::default();
185 }
186 Case::Lowercase => {
187 self.buf.extend(s.chars().flat_map(char::to_lowercase));
188 self.class = CharClass::default();
189 }
190 _ => for c in s.chars() { self.push(c) },
191 }
192 self.pristine = false;
193 }
194
195 /// Stops the end being trimmed back past this point, as math content would be.
196 pub fn prevent_trimming(&mut self) {
197 self.last_reconfig = self.buf.len();
198 self.pristine = false;
199 }
200
201 /// Pushes a case-protected span untouched.
202 pub fn push_verbatim(&mut self, s: &str) {
203 let conf = std::mem::replace(&mut self.case, Case::NoTransform);
204 self.last_reconfig = self.buf.len();
205 self.push_str(s);
206 self.last_reconfig = self.buf.len();
207 self.case = conf;
208 self.class = CharClass::InWord;
209 }
210
211 pub fn push(&mut self, c: char) {
212 let prev = self.class;
213 let hyphen = matches!(self.case, Case::Title);
214 self.class = prev.step(c, hyphen);
215 let before = self.buf.len();
216 match self.case {
217 Case::Title | Case::Sentence if self.pristine && c.is_whitespace() => {}
218 Case::Title => match prev.is_new_word() {
219 true => self.buf.extend(c.to_uppercase()),
220 false => self.buf.extend(c.to_lowercase()),
221 },
222 Case::Sentence => {
223 if prev.is_new_sentence() {
224 self.buf.extend(c.to_uppercase());
225 } else if matches!(prev, CharClass::MaybeNewSentence | CharClass::MaybeSubclause) {
226 self.buf.push(c);
227 } else {
228 self.buf.extend(c.to_lowercase());
229 }
230 }
231 Case::FirstUpper => match self.pristine && c.is_lowercase() {
232 true => self.buf.extend(c.to_uppercase()),
233 false => self.buf.push(c),
234 },
235 Case::AllUpper => match prev.is_new_word() && c.is_lowercase() {
236 true => self.buf.extend(c.to_uppercase()),
237 false => self.buf.push(c),
238 },
239 Case::Uppercase => self.buf.extend(c.to_uppercase()),
240 Case::Lowercase => self.buf.extend(c.to_lowercase()),
241 Case::NoTransform => self.buf.push(c),
242 }
243 let now = self.buf.len();
244 self.last_word = match self.last_word {
245 Some(data) => {
246 let next = data.feed(c, before, now, prev, self.class);
247 let continuing = matches!(next, Some(n) if n.start == data.start);
248 if !continuing {
249 self.process_word();
250 }
251 next
252 }
253 None => WordData::new(c, before, now, prev, self.class),
254 };
255 self.pristine = self.pristine && c.is_whitespace();
256 }
257
258 fn find_word(&self) -> &str {
259 let data = match self.last_word {
260 Some(d) => d,
261 None => return "",
262 };
263 let mut end = data.start;
264 while end < data.end && self.buf[end..].chars().next().map(char::is_alphanumeric).unwrap_or(false) {
265 end += 1;
266 while !self.buf.is_char_boundary(end) { end += 1; }
267 }
268 &self.buf[data.start..end]
269 }
270
271 fn process_word(&mut self) {
272 let data = match self.last_word {
273 Some(d) => d,
274 None => return,
275 };
276 let verdict = match self.case {
277 Case::Title => {
278 if data.is_all_upper() {
279 Verdict::AllUpper
280 } else if !data.follows_punctuation && NEVER_CAPITALIZE.binary_search(&self.find_word()).is_ok() {
281 Verdict::Lowercase
282 } else {
283 Verdict::Keep
284 }
285 }
286 Case::Sentence => {
287 if data.case == WordCase::HasNonStartUpper {
288 Verdict::Capitalise
289 } else if data.is_all_upper() {
290 Verdict::AllUpper
291 } else if ALWAYS_CAPITALIZE.binary_search(&self.find_word()).is_ok() {
292 Verdict::Capitalise
293 } else {
294 Verdict::Keep
295 }
296 }
297 _ => Verdict::Keep,
298 };
299 match verdict {
300 Verdict::AllUpper => map_chars(&mut self.buf, data.start..data.end, |c| match c.is_lowercase() {
301 true => Some(c.to_uppercase().collect()),
302 false => None,
303 }),
304 Verdict::Capitalise => capitalise_char(&mut self.buf, data.start),
305 Verdict::Lowercase => match self.buf[data.start..].chars().next() {
306 Some(c) if c.is_uppercase() => {
307 map_chars(&mut self.buf, data.start..data.start + c.len_utf8(), |c| Some(c.to_lowercase().collect()));
308 }
309 _ => {}
310 },
311 Verdict::Keep => {}
312 }
313 }
314
315 fn may_trim_end(&mut self) {
316 if matches!(self.case, Case::Title | Case::Sentence) {
317 while self.buf.len() > self.last_reconfig && self.buf.ends_with(char::is_whitespace) {
318 self.buf.pop();
319 }
320 }
321 }
322
323 pub fn case(&self) -> Case {
324 self.case
325 }
326
327 /// Changes the case for what follows, trimming what the old case wrote.
328 pub fn reconfigure(&mut self, case: Case) {
329 if self.case == case {
330 return;
331 }
332 self.may_trim_end();
333 self.last_reconfig = self.buf.len();
334 self.pristine = true;
335 self.case = case;
336 }
337
338 pub fn finish(mut self) -> String {
339 self.process_word();
340 self.may_trim_end();
341 self.buf
342 }
343
344 pub fn is_empty(&self) -> bool {
345 self.buf.is_empty()
346 }
347
348 pub fn len(&self) -> usize {
349 self.buf.len()
350 }
351
352 pub fn ends_with(&self, c: char) -> bool {
353 self.buf.ends_with(c)
354 }
355
356 pub fn as_string_mut(&mut self) -> &mut String {
357 &mut self.buf
358 }
359
360 pub fn as_str(&self) -> &str {
361 &self.buf
362 }
363
364 /// Records an edit made through `as_string_mut`, so the word being folded is forgotten.
365 pub fn mark_changed(&mut self) {
366 self.last_reconfig = self.buf.len();
367 self.last_word = None;
368 }
369}
370
371fn map_chars<F: FnMut(char) -> Option<Vec<char>>>(buf: &mut String, range: std::ops::Range<usize>, mut f: F) {
372 let mut i = range.start;
373 let mut orig = range.start;
374 while let Some(ch) = buf[i..].chars().next() {
375 if orig >= range.end {
376 break;
377 }
378 orig += ch.len_utf8();
379 match f(ch) {
380 None => i += ch.len_utf8(),
381 Some(rep) => {
382 let mut end = i + ch.len_utf8();
383 for c in rep {
384 let mut tmp = [0u8; 4];
385 buf.replace_range(i..end, c.encode_utf8(&mut tmp));
386 i += c.len_utf8();
387 end = i;
388 }
389 }
390 }
391 }
392}
393
394fn capitalise_char(buf: &mut String, start: usize) {
395 match buf[start..].chars().next() {
396 Some(c) if c.is_lowercase() => map_chars(buf, start..start + c.len_utf8(), |c| Some(c.to_uppercase().collect())),
397 _ => {}
398 }
399}