norm 0.1.1

A collection of distance metrics on strings
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
use core::mem::transmute;

use super::query::{Condition, FzfQuery, Pattern};
use crate::utils;

/// The parser used to parse strings into [`FzfQuery`]s.
///
/// Queries can be parsed according to fzf's [extended-search mode][esm] via
/// [`parse`][FzfParser::parse]. If this is not desired, use
/// [`parse_not_extended`][FzfParser::parse_not_extended] instead.
///
/// [esm]: https://github.com/junegunn/fzf#search-syntax
#[derive(Clone)]
pub struct FzfParser {
    /// TODO: docs
    chars: Vec<char>,

    /// TODO: docs
    patterns: Vec<Pattern<'static>>,

    /// TODO: docs
    conditions: Vec<Condition<'static>>,
}

impl Default for FzfParser {
    #[inline]
    fn default() -> Self {
        Self {
            chars: vec![char::default(); 64],
            patterns: vec![Pattern::default(); 64],
            conditions: vec![Condition::default(); 64],
        }
    }
}

impl core::fmt::Debug for FzfParser {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("FzfParser").finish_non_exhaustive()
    }
}

impl FzfParser {
    /// Creates a new `FzfParser`.
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Parses the given query string according to fzf's
    /// [extended-search mode][esm].
    ///
    /// In extended-search mode certain characters change how the query is
    /// matched in candidates.
    ///
    /// In particular:
    ///
    /// | Pattern | Matches                                      |
    /// | ------- | -------------------------------------------- |
    /// | `foo`   | candidates that fuzzy-match `"foo"`          |
    /// | `'foo`  | candidates that include `"foo"`              |
    /// | `^foo`  | candidates that start with `"foo"`           |
    /// | `foo$`  | candidates that end with `"foo"`             |
    /// | `!foo`  | candidates that **don't** include `"foo"`    |
    /// | `!^foo` | candidates that **don't** start with `"foo"` |
    /// | `!foo$` | candidates that **don't**  end with `"foo"`  |
    ///
    /// It's also possible to query for multiple patterns by separating them
    /// with spaces or with the pipe character `"|"`. A space acts as a logical
    /// AND operator, while a pipe character acts as a logical OR operator.
    ///
    /// For example, the query `"^main .c$ | .rs$"` would only match candidates
    /// that start with `"main"` and end with either `".c"` or `".rs"`.
    /// Spaces can be escaped with a backslash if they're part of a pattern,
    /// e.g. `"foo\ baz"` will match `"foo bar baz"` but not `"baz foo"`.
    ///
    /// Note that like in fzf, but unlike in logical expressions, the pipe
    /// character (OR) has a higher precedence than the space character (AND),
    /// so that `"foo bar | baz"` gets parsed as `"foo && (bar || baz)"`, and
    /// **not** as `"(foo && bar) || baz"`;
    ///
    /// If you want to treat all the characters in the query as fuzzy-matching,
    /// use [`parse_not_extended`][FzfParser::parse_not_extended] instead.
    ///
    /// [esm]: https://github.com/junegunn/fzf#search-syntax
    #[inline]
    pub fn parse<'a>(&'a mut self, query: &str) -> FzfQuery<'a> {
        let max_chars = query.len();

        if self.chars.len() < max_chars {
            self.chars.resize(max_chars, char::default());
        }

        // The theoretical maximum number of conditions that could be included
        // in the query.
        //
        // The actual number of conditions (which we'll only know after
        // parsing) matches this maximum on space-separated queries of
        // multiple ascii characters, e.g. `a b c d`.
        let max_conditions = query.len() / 2 + 1;

        if self.conditions.len() < max_conditions {
            self.conditions.resize(max_conditions, Condition::default());
        }

        if self.patterns.len() < max_conditions {
            self.patterns.resize(max_conditions, Pattern::default());
        }

        let patterns: &'a mut [Pattern<'static>] =
            self.patterns.as_mut_slice();

        // SAFETY: todo.
        let patterns = unsafe {
            transmute::<&'a mut [Pattern<'static>], &'a mut [Pattern<'a>]>(
                patterns,
            )
        };

        let mut num_conditions = 0;

        for condition in
            Patterns::new(patterns, &mut self.chars, query).map(Condition::new)
        {
            // SAFETY: todo
            let condition = unsafe {
                transmute::<Condition, Condition<'static>>(condition)
            };

            self.conditions[num_conditions] = condition;

            num_conditions += 1;
        }

        FzfQuery::new_extended(&self.conditions[..num_conditions])
    }

    /// Parses the given query string without using fzf's extended-search mode.
    ///
    /// All the characters in the query string are used for fuzzy-matching,
    /// with no special meaning attached to any of them. This is equivalent to
    /// calling `fzf` with the `--no-extended` flag.
    ///
    /// If you want to apply fzf's extended-search mode to the query, parse it
    /// with [`parse`][FzfParser::parse] instead.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use norm::fzf::{FzfParser, FzfV2};
    /// # use norm::Metric;
    /// let mut fzf = FzfV2::new();
    /// let mut parser = FzfParser::new();
    /// let mut ranges = Vec::new();
    ///
    /// let query = parser.parse_not_extended("^bar | baz$");
    ///
    /// let distance =
    ///     fzf.distance_and_ranges(query, "^foo bar | baz $ foo", &mut ranges);
    ///
    /// // We expect a match since the characters in the query fuzzy-match the
    /// // candidate.
    /// //
    /// // If we parsed the query by calling `parse` there wouldn't have been a
    /// // match since the candidate doesn't start with `"bar"` nor does it end
    /// // with `"baz"`.
    /// assert!(distance.is_some());
    ///
    /// assert_eq!(ranges, [0..1, 5..14, 15..16]);
    /// ```
    #[inline]
    pub fn parse_not_extended<'a>(&'a mut self, query: &str) -> FzfQuery<'a> {
        let mut char_len = 0;

        for ch in query.chars() {
            self.chars[char_len] = ch;
            char_len += 1;
        }

        FzfQuery::new_not_extended(&self.chars[..char_len])
    }
}

const OR_BLOCK_SEPARATOR: &[char] = &['|'];

/// TODO: docs
struct Patterns<'buf, 's> {
    /// TODO: docs
    buf: &'buf mut [Pattern<'buf>],

    /// TODO: docs
    allocated: usize,

    /// TODO: docs
    words: Words<'buf, 's>,

    /// TODO: docs
    next: Option<Pattern<'buf>>,
}

impl<'buf, 's> Patterns<'buf, 's> {
    #[inline]
    fn alloc(&mut self, pattern: Pattern<'buf>) {
        self.buf[self.allocated] = pattern;
        self.allocated += 1;
    }

    #[inline]
    fn new(
        patterns_buf: &'buf mut [Pattern<'buf>],
        char_buf: &'buf mut [char],
        s: &'s str,
    ) -> Self {
        Self {
            buf: patterns_buf,
            allocated: 0,
            words: Words::new(char_buf, s),
            next: None,
        }
    }
}

impl<'buf, 's> Iterator for Patterns<'buf, 's> {
    type Item = &'buf [Pattern<'buf>];

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let prev_allocated = self.allocated;

        // Whether we're expecting the next word yielded by `self.words` to be
        // a "|". This is set to true after getting a word, and set to false
        // after a "|".
        let mut looking_for_or;

        if let Some(first_pattern) = self.next.take() {
            self.alloc(first_pattern);
            looking_for_or = true;
        } else {
            looking_for_or = false;
        }

        loop {
            let Some(word) = self.words.next() else {
                break;
            };

            let word_is_condition = word != OR_BLOCK_SEPARATOR;

            if word_is_condition {
                let Some(word) = Pattern::parse(word) else { continue };

                if looking_for_or {
                    self.next = Some(word);
                    break;
                } else {
                    self.alloc(word);
                    looking_for_or = true;
                    continue;
                }
            }

            looking_for_or = false;
        }

        if self.allocated == prev_allocated {
            return None;
        }

        let patterns = &self.buf[prev_allocated..self.allocated];

        // SAFETY: todo
        let patterns =
            unsafe { transmute::<&[Pattern], &'buf [Pattern]>(patterns) };

        Some(patterns)
    }
}

/// An iterator over the words of a string.
///
/// Here, a "word" is simply a string of consecutive non-ascii-space
/// characters. Escaped spaces are treated as non-space characters.
///
/// # Examples
///
/// ```rust
/// # use norm::fzf::words;
/// let mut words = words("foo 'bar' \"baz\"");
/// assert_eq!(words.next().as_deref(), Some("foo"));
/// assert_eq!(words.next().as_deref(), Some("'bar'"));
/// assert_eq!(words.next().as_deref(), Some("\"baz\""));
/// assert_eq!(words.next(), None);
/// ```
///
/// ```rust
/// # use norm::fzf::words;
/// let mut words = words("foo\\ bar baz");
/// assert_eq!(words.next().as_deref(), Some("foo bar"));
/// assert_eq!(words.next().as_deref(), Some("baz"));
/// assert_eq!(words.next(), None);
/// ```
///
/// ```rust
/// # use norm::fzf::words;
/// let mut words = words("foo \\ bar");
/// assert_eq!(words.next().as_deref(), Some("foo"));
/// assert_eq!(words.next().as_deref(), Some(" bar"));
/// assert_eq!(words.next(), None);
/// ```
#[doc(hidden)]
pub struct Words<'buf, 'sentence> {
    /// TODO: docs
    buf: &'buf mut [char],

    /// TODO: docs
    allocated: usize,

    /// TODO: docs
    s: &'sentence str,
}

impl<'buf, 'sentence> Words<'buf, 'sentence> {
    /// TODO: docs
    #[inline]
    fn alloc(&mut self, s: &str) {
        for ch in s.chars() {
            self.buf[self.allocated] = ch;
            self.allocated += 1;
        }
    }

    /// TODO: docs
    #[inline]
    fn new(buf: &'buf mut [char], s: &'sentence str) -> Self {
        Self { buf, s: utils::strip_leading_spaces(s), allocated: 0 }
    }
}

impl<'buf> Iterator for Words<'buf, '_> {
    type Item = &'buf [char];

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        if self.s.is_empty() {
            return None;
        }

        let prev_allocated = self.allocated;

        let mut word_byte_end = 0;

        let mut s = self.s;

        loop {
            match memchr::memchr(b' ', s.as_bytes()) {
                Some(0) => break,

                Some(offset) if s.as_bytes()[offset - 1] == b'\\' => {
                    // Push everything up to (but not including) the escape.
                    self.alloc(&s[..offset - 1]);

                    // ..skip the escape..

                    // ..and push the space.
                    self.alloc(" ");

                    s = &s[offset + 1..];

                    word_byte_end += offset + 1;
                },

                Some(offset) => {
                    let s = &s[..offset];
                    self.alloc(s);
                    word_byte_end += s.len();
                    break;
                },

                None => {
                    self.alloc(s);
                    word_byte_end += s.len();
                    break;
                },
            }
        }

        self.s = utils::strip_leading_spaces(&self.s[word_byte_end..]);

        let word = &self.buf[prev_allocated..self.allocated];

        // SAFETY: todo
        let word = unsafe { transmute::<&[char], &'buf [char]>(word) };

        Some(word)
    }
}

/// TODO: docs
#[cfg(feature = "__tests")]
#[doc(hidden)]
pub fn parse(s: &str) -> FzfQuery<'static> {
    let parser = Box::leak(Box::new(FzfParser::new()));
    parser.parse(s)
}

#[cfg(test)]
mod parse_tests {
    use super::super::query::*;
    use super::*;

    #[test]
    fn parse_query_empty() {
        assert!(parse("").is_empty());
    }

    #[test]
    fn parse_query_single_fuzzy() {
        let query = parse("foo");

        let SearchMode::NotExtended(pattern) = query.search_mode else {
            panic!();
        };

        assert_eq!(pattern.into_string(), "foo");

        assert_eq!(pattern.match_type, MatchType::Fuzzy);
    }

    #[test]
    fn parse_query_upstream_extended() {
        let query = parse(
            "aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ | ^iii$ ^xxx | 'yyy | \
             zzz$ | !ZZZ |",
        );

        let SearchMode::Extended(conditions) = query.search_mode else {
            panic!();
        };

        assert_eq!(conditions.len(), 9);

        let pattern = conditions[0].or_patterns()[0];
        assert_eq!(pattern.match_type, MatchType::Fuzzy);
        assert!(!pattern.is_inverse);

        let pattern = conditions[1].or_patterns()[0];
        assert_eq!(pattern.match_type, MatchType::Exact);
        assert!(!pattern.is_inverse);

        let pattern = conditions[2].or_patterns()[0];
        assert_eq!(pattern.match_type, MatchType::PrefixExact);
        assert!(!pattern.is_inverse);

        let pattern = conditions[3].or_patterns()[0];
        assert_eq!(pattern.match_type, MatchType::SuffixExact);
        assert!(!pattern.is_inverse);

        let pattern = conditions[4].or_patterns()[0];
        assert_eq!(pattern.match_type, MatchType::Exact);
        assert!(pattern.is_inverse);

        let pattern = conditions[5].or_patterns()[0];
        assert_eq!(pattern.match_type, MatchType::Fuzzy);
        assert!(pattern.is_inverse);

        let pattern = conditions[6].or_patterns()[0];
        assert_eq!(pattern.match_type, MatchType::PrefixExact);
        assert!(pattern.is_inverse);

        let pattern = conditions[7].or_patterns()[0];
        assert_eq!(pattern.match_type, MatchType::SuffixExact);
        assert!(pattern.is_inverse);

        let pattern = conditions[7].or_patterns()[1];
        assert_eq!(pattern.match_type, MatchType::EqualExact);
        assert!(!pattern.is_inverse);

        let pattern = conditions[8].or_patterns()[0];
        assert_eq!(pattern.match_type, MatchType::PrefixExact);
        assert!(!pattern.is_inverse);

        let pattern = conditions[8].or_patterns()[1];
        assert_eq!(pattern.match_type, MatchType::Exact);
        assert!(!pattern.is_inverse);

        let pattern = conditions[8].or_patterns()[2];
        assert_eq!(pattern.match_type, MatchType::SuffixExact);
        assert!(!pattern.is_inverse);

        let pattern = conditions[8].or_patterns()[3];
        assert_eq!(pattern.match_type, MatchType::Exact);
        assert!(pattern.is_inverse);
    }
}

#[cfg(test)]
mod patterns_tests {
    use super::*;

    fn patterns(
        s: &str,
    ) -> impl Iterator<Item = &'static [Pattern<'static>]> + '_ {
        let patterns_buf = vec![Pattern::default(); s.len() / 2 + 1].leak();
        let char_buf = vec![char::default(); s.len()].leak();
        Patterns::new(patterns_buf, char_buf, s)
    }

    fn pattern(s: &str) -> Pattern<'static> {
        Pattern::parse(s.chars().collect::<Vec<_>>().leak()).unwrap()
    }

    #[test]
    fn patterns_empty() {
        let mut blocks = patterns("");
        assert!(blocks.next().is_none());
    }

    #[test]
    fn patterns_single() {
        let mut blocks = patterns("foo");
        assert_eq!(blocks.next().unwrap(), [pattern("foo")]);
        assert_eq!(blocks.next(), None);
    }

    #[test]
    fn patterns_multiple_ors() {
        let mut blocks = patterns("foo | bar | baz");
        assert_eq!(
            blocks.next().unwrap(),
            [pattern("foo"), pattern("bar"), pattern("baz")]
        );
        assert_eq!(blocks.next(), None);
    }

    #[test]
    fn patterns_multiple_ands() {
        let mut blocks = patterns("foo bar baz");
        assert_eq!(blocks.next().unwrap(), [pattern("foo")]);
        assert_eq!(blocks.next().unwrap(), [pattern("bar")]);
        assert_eq!(blocks.next().unwrap(), [pattern("baz")]);
        assert_eq!(blocks.next(), None);
    }

    #[test]
    fn patterns_empty_between_ors() {
        let mut blocks = patterns("foo | | bar");
        assert_eq!(blocks.next().unwrap(), [pattern("foo"), pattern("bar")]);
        assert_eq!(blocks.next(), None);
    }

    #[test]
    fn patterns_multiple_ors_multiple_ands() {
        let mut blocks = patterns("foo | bar baz qux | quux | corge");
        assert_eq!(blocks.next().unwrap(), [pattern("foo"), pattern("bar")]);
        assert_eq!(blocks.next().unwrap(), [pattern("baz")]);
        assert_eq!(
            blocks.next().unwrap(),
            [pattern("qux"), pattern("quux"), pattern("corge")]
        );
        assert_eq!(blocks.next(), None);
    }
}

#[cfg(feature = "__tests")]
#[doc(hidden)]
pub fn words(s: &str) -> impl Iterator<Item = String> {
    let mut buf = Vec::new();

    buf.resize(s.len(), char::default());

    Words::new(&mut buf, s)
        .map(String::from_iter)
        .collect::<Vec<_>>()
        .into_iter()
}

#[cfg(test)]
mod word_tests {
    use super::*;

    #[test]
    fn words_empty() {
        let mut words = words("");
        assert!(words.next().is_none());
    }

    #[test]
    fn words_single() {
        let mut words = words("foo");
        assert_eq!(words.next().as_deref(), Some("foo"));
        assert_eq!(words.next(), None);
    }

    #[test]
    fn words_escaped_escape_escaped_space() {
        let mut words = words("\\\\ ");
        assert_eq!(words.next().as_deref(), Some("\\ "));
        assert_eq!(words.next(), None);
    }

    #[test]
    fn words_multiple() {
        let mut words = words("foo bar");
        assert_eq!(words.next().as_deref(), Some("foo"));
        assert_eq!(words.next().as_deref(), Some("bar"));
        assert_eq!(words.next(), None);
    }

    #[test]
    fn words_multiple_leading_trailing_spaces() {
        let mut words = words("   foo bar   ");
        assert_eq!(words.next().as_deref(), Some("foo"));
        assert_eq!(words.next().as_deref(), Some("bar"));
        assert_eq!(words.next(), None);
    }

    #[test]
    fn words_multiple_escaped_spaces() {
        let mut words = words("foo\\ bar\\ baz");
        assert_eq!(words.next().as_deref(), Some("foo bar baz"));
        assert_eq!(words.next(), None);
    }

    #[test]
    fn words_multiple_standalone_escaped_spaces() {
        let mut words = words(" \\  foo \\ bar \\  ");
        assert_eq!(words.next().as_deref(), Some(" "));
        assert_eq!(words.next().as_deref(), Some("foo"));
        assert_eq!(words.next().as_deref(), Some(" bar"));
        assert_eq!(words.next().as_deref(), Some(" "));
        assert_eq!(words.next(), None);
    }

    #[test]
    fn words_single_escaped_spaces() {
        let mut words = words("\\ ");
        assert_eq!(words.next().as_deref(), Some(" "));
        assert_eq!(words.next(), None);
    }

    #[test]
    fn words_consecutive_escaped_spaces() {
        let mut words = words(" \\ \\ \\  ");
        assert_eq!(words.next().as_deref(), Some("   "));
        assert_eq!(words.next(), None);
    }
}