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
#![warn(missing_docs)]
//! Approximate pattern matching crate

const INITPAT: u32 = 0x80000000; // 0100,0000,0000,0000,0000,0000,0000,0000
const MAXCHAR: usize = 0x10000;
const INITSTATE: [u32; 4] = [INITPAT, 0, 0, 0];

/// Approximate pattern matching engine
pub struct Asearch {
    shiftpat: [u32; MAXCHAR],
    acceptpat: u32,
    epsilon: u32,
}

impl Asearch {
    /// Create a new approximate pattern matching engine
    ///
    /// # Arguments
    ///
    /// - `source` - text which is searched for.
    pub fn new(source: impl Into<String>) -> Asearch {
        let mut shiftpat: [u32; MAXCHAR] = [0; MAXCHAR];
        let mut mask = INITPAT;
        let mut epsilon: u32 = 0;
        for item in &unpack(source.into()) {
            // 0x20 is a space
            if *item == 0x20 {
                epsilon |= mask;
            } else {
                shiftpat[*item] |= mask;
                shiftpat[to_upper(*item)] |= mask;
                shiftpat[to_lower(*item)] |= mask;
                mask >>= 1;
            }
        }
        Asearch {
            acceptpat: mask,
            shiftpat,
            epsilon,
        }
    }

    fn state(&self, text: impl Into<String>) -> [u32; 4] {
        let mut i0 = INITSTATE[0];
        let mut i1 = INITSTATE[1];
        let mut i2 = INITSTATE[2];
        let mut i3 = INITSTATE[3];
        for item in &unpack(text.into()) {
            let mask = self.shiftpat[*item];
            i3 = (i3 & self.epsilon) | ((i3 & mask) >> 1) | (i2 >> 1) | i2;
            i2 = (i2 & self.epsilon) | ((i2 & mask) >> 1) | (i1 >> 1) | i1;
            i1 = (i1 & self.epsilon) | ((i1 & mask) >> 1) | (i0 >> 1) | i0;
            i0 = (i0 & self.epsilon) | ((i0 & mask) >> 1);
            i1 |= i0 >> 1;
            i2 |= i1 >> 1;
            i3 |= i2 >> 1;
        }
        [i0, i1, i2, i3]
    }

    /// Do approximate pattern matching
    ///
    /// # Arguments
    ///
    /// - `text` - text which is searched.
    /// - `ambig` - Levenshtein distance.
    ///
    /// # Examples
    ///
    /// ```
    /// // pattern: abcde
    /// {
    ///     let asearch = asearch::Asearch::new("abcde");
    ///
    ///     assert!(asearch.find("abcde", 0));
    ///     assert!(asearch.find("aBCDe", 0));
    ///     assert!(asearch.find("abXcde", 1));
    ///     assert!(asearch.find("ab?de", 1));
    ///     assert!(asearch.find("abXXde", 2));
    ///     assert!(!asearch.find("abXcde", 0));
    ///     assert!(!asearch.find("ab?de", 0));
    ///     assert!(!asearch.find("abde", 0));
    ///     assert!(!asearch.find("abXXde", 1));
    ///     assert!(asearch.find("abcde", 1));
    ///     assert!(!asearch.find("abcd", 0));
    ///     assert!(asearch.find("abcd", 1));
    ///     assert!(asearch.find("bcde", 2));
    /// }
    ///
    /// // pattern: ab de
    /// {
    ///     let asearch = asearch::Asearch::new("ab de");
    ///     assert!(asearch.find("abcde", 0));
    ///     assert!(asearch.find("abccde", 0));
    ///     assert!(asearch.find("abXXXXXXXde", 0));
    ///     assert!(asearch.find("ababcccccxede", 1));
    ///     assert!(!asearch.find("abcccccxe", 0));
    /// }
    ///
    /// // pattern: partial match
    /// {
    ///     let asearch = asearch::Asearch::new(" java ");
    ///
    ///     assert!(asearch.find("javascript", 0));
    ///     assert!(asearch.find("abcjavascript", 0));
    ///     assert!(asearch.find("jabascript", 1));
    ///     assert!(asearch.find("awesome jabascript", 1));
    /// }
    ///
    /// // pattern: unicode
    /// {
    ///     let asearch = asearch::Asearch::new("漢字文字列");
    ///
    ///     assert!(asearch.find("漢字文字列", 0));
    ///     assert!(!asearch.find("漢字の文字列", 0));
    ///     assert!(asearch.find("漢字の文字列", 1));
    ///     assert!(!asearch.find("漢字文字", 0));
    ///     assert!(asearch.find("漢字文字", 1));
    ///     assert!(!asearch.find("漢字文字烈", 0));
    ///     assert!(asearch.find("漢字文字烈", 1));
    ///     assert!(!asearch.find("漢和辞典", 2));
    /// }
    /// ```
    pub fn find(&self, text: impl Into<String>, ambig: u8) -> bool {
        let ambig_ = if (ambig as usize) < INITSTATE.len() {
            ambig as usize
        } else {
            INITSTATE.len() - 1
        };

        let s = self.state(text.into());
        (s[ambig_] & self.acceptpat) != 0
    }
}

/// convert each char to a code point
/// They are used for indice of the finite automaton
fn unpack(text: impl Into<String>) -> Vec<usize> {
    text.into()
        .chars()
        .into_iter()
        .map(|c| c as usize)
        .collect()
}

fn is_upper(c: usize) -> bool {
    (0x41..=0x5a).contains(&c)
}
fn is_lower(c: usize) -> bool {
    (0x61..=0x7a).contains(&c)
}
fn to_lower(c: usize) -> usize {
    if is_upper(c) {
        c + 0x20
    } else {
        c
    }
}
fn to_upper(c: usize) -> usize {
    if is_lower(c) {
        c - 0x20
    } else {
        c
    }
}