feruca 0.12.0

An implementation of the Unicode Collation Algorithm
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use crate::ascii::{
    AsciiResult, compare_ascii_primary_non_ignorable, fill_codepoints_and_compare_ascii,
};
use crate::cea::{LazyPrimaryResult, compare_primary_streaming, compare_primary_streaming_utf8};
use crate::consts::{ARABIC_INTERLEAVED, ARABIC_SCRIPT, CLDR_ROOT, DUCET, LOW_CLDR, LOW_DUCET};
use crate::first_weight::try_initial;
use crate::normalize::make_nfd;
use crate::prefix::{find_byte_prefix, find_prefix_shifted};
use crate::sort_key::compare_incremental;
use crate::tables::CollationTable;
use crate::{Locale, Tailoring};
use bstr::{B, ByteSlice};
use std::cmp::Ordering;

const LAZY_UTF8_PRIMARY_MIN_COMBINED_BYTES: usize = 64;

#[cfg(feature = "pipeline-stats")]
/// Diagnostic counters for `Collator::collate`.
///
/// These counters are intended for benchmarking and pipeline experiments. They are available only
/// when the `pipeline-stats` feature is enabled.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PipelineStats {
    /// Total calls to `Collator::collate`.
    pub comparisons: u64,
    /// Calls that returned from the initial exact-equality check.
    pub equal_early: u64,
    /// Calls where a byte prefix was trimmed before UTF-8 decoding.
    pub byte_prefix_trimmed: u64,
    /// Total bytes trimmed by byte-prefix trimming, counted once per compared pair.
    pub byte_prefix_bytes_trimmed: u64,
    /// Calls resolved by the non-ignorable ASCII primary fast path.
    pub ascii_primary_resolved: u64,
    /// Calls that attempted lazy UTF-8 primary streaming before filling code point buffers.
    pub lazy_utf8_primary_attempts: u64,
    /// Calls resolved by lazy UTF-8 primary streaming before filling code point buffers.
    pub lazy_utf8_primary_resolved: u64,
    /// Calls where lazy UTF-8 primary streaming had to discard its work and use full fallback.
    pub lazy_utf8_full_fallback: u64,
    /// Calls resolved while filling code point buffers with ASCII-aware comparison.
    pub fill_ascii_resolved: u64,
    /// Input sides normalized to NFD.
    pub nfd_normalizations: u64,
    /// Total code points decoded into the comparison buffers before NFD normalization.
    pub codepoints_decoded: u64,
    /// Calls where a code point prefix was trimmed after UTF-8 decoding and normalization.
    pub codepoint_prefix_trimmed: u64,
    /// Total code points trimmed by code point prefix trimming, counted once per compared pair.
    pub codepoint_prefix_codepoints_trimmed: u64,
    /// Calls resolved by the initial-primary check.
    pub initial_primary_resolved: u64,
    /// Calls resolved by streaming primary collation element generation.
    pub streaming_primary_resolved: u64,
    /// Total code points consumed by streaming primary generation after code point prefix trimming.
    pub codepoints_consumed_primary: u64,
    /// Calls that reached secondary/tertiary/quaternary comparison.
    pub later_levels_reached: u64,
    /// Calls resolved by secondary/tertiary/quaternary comparison.
    pub later_levels_resolved: u64,
    /// Calls resolved by byte-value tiebreaking after equivalent collation weights.
    pub tiebreak_resolved: u64,
}

#[cfg(feature = "pipeline-stats")]
impl PipelineStats {
    const ZEROED: Self = Self {
        comparisons: 0,
        equal_early: 0,
        byte_prefix_trimmed: 0,
        byte_prefix_bytes_trimmed: 0,
        ascii_primary_resolved: 0,
        lazy_utf8_primary_attempts: 0,
        lazy_utf8_primary_resolved: 0,
        lazy_utf8_full_fallback: 0,
        fill_ascii_resolved: 0,
        nfd_normalizations: 0,
        codepoints_decoded: 0,
        codepoint_prefix_trimmed: 0,
        codepoint_prefix_codepoints_trimmed: 0,
        initial_primary_resolved: 0,
        streaming_primary_resolved: 0,
        codepoints_consumed_primary: 0,
        later_levels_reached: 0,
        later_levels_resolved: 0,
        tiebreak_resolved: 0,
    };
}

/// The `Collator` struct is the entry point for this library's API. It defines the options to be
/// used in collation. The method `collate` will then compare two string references (or byte slices)
/// according to the selected options, and return an `Ordering` value.
///
/// You can choose between two tables of character weights: DUCET and CLDR. With the CLDR table,
/// there is a further choice of locale tailoring. The `Root` locale represents the table in its
/// unmodified form. The `ArabicScript` locale shifts the weights of Arabic-script letters so that
/// they sort before the Latin script; and the `ArabicInterleaved` locale mixes the two scripts, so
/// that, e.g., _alif_ sorts between A and B, and _bā’_ between B and C. Further locales will be
/// added over time.
///
/// You can also choose between two approaches to the handling of variable-weight characters:
/// "non-ignorable" and "shifted." Finally, you can select whether to use byte-value comparison as a
/// tiebreaker when two strings produce identical Unicode Collation Algorithm sort keys.
///
/// The default for `Collator` is to use the CLDR table with the `Root` locale; to use the "shifted"
/// approach for variable-weight characters; and to break ties with byte-value comparison. This
/// should be a good starting point for collation in many languages.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct Collator {
    /// The table of weights to be used: DUCET or CLDR (with a choice of locale for the latter)
    pub tailoring: Tailoring,
    /// The approach to handling variable-weight characters: "non-ignorable" (i.e., `false`) or
    /// "shifted" (i.e., `true`)
    pub shifting: bool,
    /// Whether to use byte-value comparison as a tiebreaker when two strings produce identical
    /// Unicode Collation Algorithm sort keys
    pub tiebreak: bool,
    a_chars: Vec<u32>,
    b_chars: Vec<u32>,
    a_cea: Vec<u32>,
    b_cea: Vec<u32>,
    #[cfg(feature = "pipeline-stats")]
    stats: PipelineStats,
}

impl Default for Collator {
    fn default() -> Self {
        Self::new(Tailoring::default(), true, true)
    }
}

impl Collator {
    /// Create a new `Collator` with the specified options. NB: it is also possible to call
    /// `Collator::default()`.
    #[must_use]
    pub fn new(tailoring: Tailoring, shifting: bool, tiebreak: bool) -> Self {
        Self {
            tailoring,
            shifting,
            tiebreak,
            a_chars: Vec::new(),
            b_chars: Vec::new(),
            a_cea: vec![0; 64],
            b_cea: vec![0; 64],

            #[cfg(feature = "pipeline-stats")]
            stats: PipelineStats::ZEROED,
        }
    }

    /// Return diagnostic counters for this collator.
    ///
    /// This method is available only when the `pipeline-stats` feature is enabled.
    #[cfg(feature = "pipeline-stats")]
    #[must_use]
    pub const fn stats(&self) -> &PipelineStats {
        &self.stats
    }

    /// Reset diagnostic counters for this collator.
    ///
    /// This method is available only when the `pipeline-stats` feature is enabled.
    #[cfg(feature = "pipeline-stats")]
    pub const fn clear_stats(&mut self) {
        self.stats = PipelineStats::ZEROED;
    }

    /// This is the primary method in the library. It accepts as arguments two string references or
    /// byte slices; compares them using the options chosen; and returns an `Ordering` value. This
    /// is designed to be passed to the `sort_by` (or `sort_unstable_by`) function in the standard
    /// library. Simple usage might look like the following...
    ///
    /// ```
    /// use feruca::Collator;
    ///
    /// let mut collator = Collator::default();
    ///
    /// let mut names = ["Peng", "Peña", "Ernie", "Émile"];
    /// names.sort_unstable_by(|a, b| collator.collate(a, b));
    ///
    /// let expected = ["Émile", "Ernie", "Peña", "Peng"];
    /// assert_eq!(names, expected);
    /// ```
    #[allow(clippy::too_many_lines)]
    pub fn collate<T: AsRef<[u8]> + Ord + ?Sized>(&mut self, a: &T, b: &T) -> Ordering {
        #[cfg(feature = "pipeline-stats")]
        {
            self.stats.comparisons += 1;
        }

        // Early out; equal is equal
        if a == b {
            #[cfg(feature = "pipeline-stats")]
            {
                self.stats.equal_early += 1;
            }

            return Ordering::Equal;
        }

        let a_bytes = a.as_ref();
        let b_bytes = b.as_ref();
        let mut ctx = None;

        let byte_offset = if has_byte_prefix(a_bytes, b_bytes) {
            let current_ctx =
                ctx.get_or_insert_with(|| CollationContext::new(self.shifting, self.tailoring));
            find_byte_prefix(a_bytes, b_bytes, current_ctx)
        } else {
            0
        };

        #[cfg(feature = "pipeline-stats")]
        if byte_offset > 0 {
            self.stats.byte_prefix_trimmed += 1;
            self.stats.byte_prefix_bytes_trimmed += u64::try_from(byte_offset).unwrap_or(u64::MAX);
        }

        let a_bytes = &a_bytes[byte_offset..];
        let b_bytes = &b_bytes[byte_offset..];

        if !self.shifting {
            let current_ctx =
                ctx.get_or_insert_with(|| CollationContext::new(self.shifting, self.tailoring));
            if let Some(comparison) =
                compare_ascii_primary_non_ignorable(a_bytes, b_bytes, current_ctx.low)
            {
                #[cfg(feature = "pipeline-stats")]
                {
                    self.stats.ascii_primary_resolved += 1;
                }

                return comparison;
            }
        }

        if a_bytes.len() + b_bytes.len() >= LAZY_UTF8_PRIMARY_MIN_COMBINED_BYTES {
            let current_ctx =
                ctx.get_or_insert_with(|| CollationContext::new(self.shifting, self.tailoring));
            #[cfg(feature = "pipeline-stats")]
            {
                self.stats.lazy_utf8_primary_attempts += 1;
            }
            match compare_primary_streaming_utf8(
                &mut self.a_cea,
                &mut self.b_cea,
                a_bytes,
                b_bytes,
                current_ctx,
            ) {
                LazyPrimaryResult::Decided(comparison) => {
                    #[cfg(feature = "pipeline-stats")]
                    {
                        self.stats.lazy_utf8_primary_resolved += 1;
                    }

                    return comparison;
                }
                LazyPrimaryResult::NeedsFullFallback => {
                    #[cfg(feature = "pipeline-stats")]
                    {
                        self.stats.lazy_utf8_full_fallback += 1;
                    }
                }
            }
        }

        // Validate UTF-8 and make iterators for u32 code points
        let mut a_iter = B(a_bytes).chars().map(|c| c as u32);
        let mut b_iter = B(b_bytes).chars().map(|c| c as u32);

        // Clear code point Vecs
        self.a_chars.clear();
        self.b_chars.clear();

        // While iterating through input strings and filling code point Vecs, try to get a result by
        // comparing ASCII characters. This can avoid a lot of computation.
        let ascii_result = fill_codepoints_and_compare_ascii(
            &mut a_iter,
            &mut b_iter,
            &mut self.a_chars,
            &mut self.b_chars,
        );

        #[cfg(feature = "pipeline-stats")]
        {
            self.stats.codepoints_decoded +=
                u64::try_from(self.a_chars.len() + self.b_chars.len()).unwrap_or(u64::MAX);
        }

        let (a_needs_nfd, b_needs_nfd) = match ascii_result {
            AsciiResult::Done(o) => {
                #[cfg(feature = "pipeline-stats")]
                {
                    self.stats.fill_ascii_resolved += 1;
                }

                return o;
            }
            AsciiResult::Continue {
                a_needs_nfd,
                b_needs_nfd,
            } => (a_needs_nfd, b_needs_nfd),
        };

        // Normalize to NFD if necessary
        if a_needs_nfd {
            #[cfg(feature = "pipeline-stats")]
            {
                self.stats.nfd_normalizations += 1;
            }

            make_nfd(&mut self.a_chars);
        }
        if b_needs_nfd {
            #[cfg(feature = "pipeline-stats")]
            {
                self.stats.nfd_normalizations += 1;
            }

            make_nfd(&mut self.b_chars);
        }

        // Define collation context for subsequent steps
        let ctx = ctx.get_or_insert_with(|| CollationContext::new(self.shifting, self.tailoring));

        // In shifted mode, trimming a shared code point prefix can avoid carrying variable-weight
        // CEs into later levels. In non-ignorable mode, earlier byte/ASCII/lazy-primary paths have
        // usually already done enough prefix work that this pass is just overhead.
        let offset = if self.shifting {
            find_prefix_shifted(&self.a_chars, &self.b_chars, ctx)
        } else {
            0
        };

        #[cfg(feature = "pipeline-stats")]
        if offset > 0 {
            self.stats.codepoint_prefix_trimmed += 1;
            self.stats.codepoint_prefix_codepoints_trimmed +=
                u64::try_from(offset).unwrap_or(u64::MAX);
        }

        // Prefix trimming may reveal that one Vec is a prefix of the other
        if self.a_chars[offset..].is_empty() || self.b_chars[offset..].is_empty() {
            return self.a_chars.len().cmp(&self.b_chars.len());
        }

        // One last early out: if the opening code points of the Vecs are different, and neither
        // requires checking for a multi-code-point sequence, then we can try comparing their first
        // primary weights. If those are different, and both non-zero, it's decisive.
        if let Some(o) = try_initial(ctx, &self.a_chars[offset..], &self.b_chars[offset..]) {
            #[cfg(feature = "pipeline-stats")]
            {
                self.stats.initial_primary_resolved += 1;
            }

            return o;
        }

        // Otherwise, compare primary weights while generating collation elements. If primary
        // weights tie, the generated buffers are complete and can be reused for later levels.
        if let Some(comparison) = compare_primary_streaming(
            &mut self.a_cea,
            &mut self.b_cea,
            &mut self.a_chars,
            &mut self.b_chars,
            ctx,
            offset,
            #[cfg(feature = "pipeline-stats")]
            &mut self.stats,
        ) {
            #[cfg(feature = "pipeline-stats")]
            {
                self.stats.streaming_primary_resolved += 1;
            }

            return comparison;
        }

        // Sort keys are processed incrementally, until they yield a result
        #[cfg(feature = "pipeline-stats")]
        {
            self.stats.later_levels_reached += 1;
        }

        let comparison = compare_incremental(&self.a_cea, &self.b_cea, ctx.shifting);

        if comparison == Ordering::Equal && self.tiebreak {
            #[cfg(feature = "pipeline-stats")]
            {
                self.stats.tiebreak_resolved += 1;
            }

            return a.cmp(b);
        }

        #[cfg(feature = "pipeline-stats")]
        if comparison != Ordering::Equal {
            self.stats.later_levels_resolved += 1;
        }

        comparison
    }
}

fn has_byte_prefix(a: &[u8], b: &[u8]) -> bool {
    a.first().zip(b.first()).is_some_and(|(x, y)| x == y)
}

pub struct CollationContext {
    pub shifting: bool,
    pub cldr: bool,
    pub table: &'static CollationTable,
    pub low: &'static [u32],
}

impl CollationContext {
    fn new(shifting: bool, tailoring: Tailoring) -> Self {
        let cldr = tailoring != Tailoring::Ducet;

        Self {
            shifting,
            cldr,
            table: get_collation_table(tailoring),
            low: if cldr { &LOW_CLDR } else { &LOW_DUCET },
        }
    }
}

fn get_collation_table(tailoring: Tailoring) -> &'static CollationTable {
    match tailoring {
        Tailoring::Cldr(Locale::ArabicScript) => &ARABIC_SCRIPT,
        Tailoring::Cldr(Locale::ArabicInterleaved) => &ARABIC_INTERLEAVED,
        Tailoring::Cldr(Locale::Root) => &CLDR_ROOT,
        Tailoring::Ducet => &DUCET,
    }
}