sqry-core 11.0.4

Core library for sqry - semantic code search engine
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
//! Scalar (non-SIMD) fallback implementations
//!
//! This module provides reference implementations of search operations
//! without SIMD optimization. These serve as:
//! - Fallback for CPUs without SIMD support
//! - Reference for property testing (SIMD ≡ scalar validation)
//! - Baseline for performance comparisons

use super::SearchResult;
use std::collections::HashSet;

/// Search for needle in haystack using Boyer-Moore-Horspool algorithm
///
/// Returns the byte offset of the first match, or None if not found.
///
/// # Algorithm
/// Boyer-Moore-Horspool (BMH) is a simplified version of Boyer-Moore that:
/// - Builds a skip table based on the needle
/// - Scans haystack left-to-right, but checks pattern right-to-left
/// - Skips ahead based on mismatched character
///
/// Time complexity: O(n*m) worst case, O(n/m) average case
/// Space complexity: O(256) for skip table
///
/// # Examples
/// ```
/// use sqry_core::search::simd::scalar::search;
///
/// assert_eq!(search(b"hello world", b"world"), Some(6));
/// assert_eq!(search(b"hello", b"xyz"), None);
/// ```
#[must_use]
pub fn search(haystack: &[u8], needle: &[u8]) -> SearchResult {
    if needle.is_empty() {
        return Some(0);
    }
    if haystack.len() < needle.len() {
        return None;
    }

    // Special case: single-byte needle (optimize common case)
    if needle.len() == 1 {
        let target = needle[0];
        return haystack.iter().position(|&b| b == target);
    }

    // Build skip table for Boyer-Moore-Horspool
    let skip_table = build_skip_table(needle);
    let needle_len = needle.len();
    let last_idx = needle_len - 1;

    let mut pos = 0;
    while pos <= haystack.len() - needle_len {
        // Check if pattern matches at current position (right-to-left)
        let mut j = last_idx;
        loop {
            if haystack[pos + j] != needle[j] {
                // Mismatch: skip ahead based on last character
                let skip_char = haystack[pos + last_idx];
                pos += skip_table[skip_char as usize];
                break;
            }

            if j == 0 {
                // Full match found
                return Some(pos);
            }
            j -= 1;
        }
    }

    None
}

/// Build skip table for Boyer-Moore-Horspool algorithm
///
/// The skip table tells us how far we can skip ahead when we find a mismatch.
/// For each character, we store the distance from the end of the needle.
///
/// # Example
/// For needle "hello":
/// - 'h' → skip 4 (distance from end)
/// - 'e' → skip 3
/// - 'l' → skip 1 (rightmost 'l')
/// - 'o' → skip 0 (last character)
/// - all other chars → skip 5 (`needle.len()`)
fn build_skip_table(needle: &[u8]) -> [usize; 256] {
    let mut table = [needle.len(); 256];
    let last_idx = needle.len() - 1;

    for (i, &byte) in needle.iter().enumerate().take(last_idx) {
        table[byte as usize] = last_idx - i;
    }

    table
}

/// Extract trigrams from text using sliding window
///
/// A trigram is a 3-character substring. This function extracts all unique
/// trigrams using a sliding window approach.
///
/// For strings shorter than 3 characters, returns the original string.
///
/// # Examples
/// ```
/// use sqry_core::search::simd::scalar::extract_trigrams;
///
/// assert_eq!(extract_trigrams("hello"), vec!["hel", "ell", "llo"]);
/// assert_eq!(extract_trigrams("ab"), vec!["ab"]);
/// ```
#[must_use]
pub fn extract_trigrams(text: &str) -> Vec<String> {
    let chars: Vec<char> = text.chars().collect();

    if chars.len() < 3 {
        return vec![text.to_string()];
    }

    let mut trigrams = Vec::new();
    let mut seen = HashSet::new();

    for i in 0..=chars.len() - 3 {
        let trigram: String = chars[i..i + 3].iter().collect();
        if seen.insert(trigram.clone()) {
            trigrams.push(trigram);
        }
    }

    trigrams
}

/// Convert ASCII text to lowercase
///
/// Only ASCII characters (A-Z) are converted to lowercase (a-z).
/// Non-ASCII characters and already-lowercase characters are preserved.
///
/// # Examples
/// ```
/// use sqry_core::search::simd::scalar::to_lowercase_ascii;
///
/// assert_eq!(to_lowercase_ascii("HELLO"), "hello");
/// assert_eq!(to_lowercase_ascii("Hello123"), "hello123");
/// ```
#[must_use]
pub fn to_lowercase_ascii(text: &str) -> String {
    text.chars()
        .map(|c| {
            if c.is_ascii_uppercase() {
                c.to_ascii_lowercase()
            } else {
                c
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::collection::vec;
    use proptest::prelude::*;

    const MAX_HAYSTACK_LEN: usize = 256;
    const MAX_NEEDLE_LEN: usize = 96;
    const MAX_EXTRA_LEN: usize = 128;

    // Substring search tests (TS-1.1)
    #[test]
    fn test_search_basic_match() {
        assert_eq!(search(b"hello", b"ll"), Some(2));
    }

    #[test]
    fn test_search_no_match() {
        assert_eq!(search(b"hello", b"world"), None);
    }

    #[test]
    fn test_search_empty_haystack() {
        assert_eq!(search(b"", b"x"), None);
    }

    #[test]
    fn test_search_empty_needle() {
        assert_eq!(search(b"hello", b""), Some(0));
    }

    #[test]
    fn test_search_repeated_pattern() {
        // Should return first match
        assert_eq!(search(b"aaaaaa", b"aa"), Some(0));
    }

    #[test]
    fn test_search_single_byte_start() {
        assert_eq!(search(b"hello", b"h"), Some(0));
    }

    #[test]
    fn test_search_single_byte_end() {
        assert_eq!(search(b"hello", b"o"), Some(4));
    }

    #[test]
    fn test_search_match_at_end() {
        assert_eq!(search(b"hello world", b"world"), Some(6));
    }

    #[test]
    fn test_search_large_haystack_no_match() {
        let haystack = "abcdefgh".repeat(1000);
        assert_eq!(search(haystack.as_bytes(), b"xyz"), None);
    }

    #[test]
    fn test_search_large_haystack_match() {
        let haystack = "abc".repeat(1000) + "xyz";
        let result = search(haystack.as_bytes(), b"xyz");
        assert!(result.is_some());
        assert_eq!(
            &haystack.as_bytes()[result.unwrap()..result.unwrap() + 3],
            b"xyz"
        );
    }

    #[test]
    fn test_search_exact_match() {
        assert_eq!(search(b"hello", b"hello"), Some(0));
    }

    #[test]
    fn test_search_needle_longer() {
        assert_eq!(search(b"hello", b"hello world"), None);
    }

    // Trigram extraction tests (TS-1.2)
    #[test]
    fn test_trigram_single() {
        assert_eq!(extract_trigrams("abc"), vec!["abc"]);
    }

    #[test]
    fn test_trigram_two() {
        let mut trigrams = extract_trigrams("abcd");
        trigrams.sort();
        assert_eq!(trigrams, vec!["abc", "bcd"]);
    }

    #[test]
    fn test_trigram_too_short() {
        assert_eq!(extract_trigrams("ab"), vec!["ab"]);
    }

    #[test]
    fn test_trigram_empty() {
        assert_eq!(extract_trigrams(""), vec![""]);
    }

    #[test]
    fn test_trigram_repeated_chars() {
        assert_eq!(extract_trigrams("aaa"), vec!["aaa"]);
    }

    #[test]
    fn test_trigram_full_sliding_window() {
        let mut trigrams = extract_trigrams("abcdefgh");
        trigrams.sort();
        assert_eq!(trigrams, vec!["abc", "bcd", "cde", "def", "efg", "fgh"]);
    }

    #[test]
    fn test_trigram_realistic_symbol() {
        let trigrams = extract_trigrams("createCompilerHost");
        // Should extract 16 unique trigrams (length - 2)
        assert_eq!(trigrams.len(), 16);
        assert!(trigrams.contains(&"cre".to_string()));
        assert!(trigrams.contains(&"rea".to_string()));
        assert!(trigrams.contains(&"ate".to_string()));
    }

    #[test]
    fn test_trigram_large_input() {
        let input = "a".repeat(1000);
        let trigrams = extract_trigrams(&input);
        // All trigrams are "aaa", so only 1 unique
        assert_eq!(trigrams.len(), 1);
        assert_eq!(trigrams[0], "aaa");
    }

    // ASCII lowercase tests (TS-1.3)
    #[test]
    fn test_lowercase_all_uppercase() {
        assert_eq!(to_lowercase_ascii("HELLO"), "hello");
    }

    #[test]
    fn test_lowercase_all_lowercase() {
        assert_eq!(to_lowercase_ascii("hello"), "hello");
    }

    #[test]
    fn test_lowercase_mixed_case() {
        assert_eq!(to_lowercase_ascii("HeLLo"), "hello");
    }

    #[test]
    fn test_lowercase_alphanumeric() {
        assert_eq!(to_lowercase_ascii("ABC123XYZ"), "abc123xyz");
    }

    #[test]
    fn test_lowercase_empty() {
        assert_eq!(to_lowercase_ascii(""), "");
    }

    #[test]
    fn test_lowercase_realistic_symbol() {
        assert_eq!(
            to_lowercase_ascii("createCompilerHost"),
            "createcompilerhost"
        );
    }

    // Skip table tests
    #[test]
    fn test_build_skip_table() {
        let table = build_skip_table(b"hello");

        // 'h' is at position 0, distance from end (index 4) = 4
        assert_eq!(table[b'h' as usize], 4);

        // 'e' is at position 1, distance from end = 3
        assert_eq!(table[b'e' as usize], 3);

        // 'l' appears at positions 2 and 3, rightmost is 3, distance = 1
        assert_eq!(table[b'l' as usize], 1);

        // 'o' is at last position (index 4), not in skip table (would be 0)
        // But we only add chars up to last_idx-1, so 'o' gets default value
        assert_eq!(table[b'o' as usize], 5); // needle.len()

        // Character not in needle gets default (needle.len())
        assert_eq!(table[b'x' as usize], 5);
    }

    // Property tests for scalar baseline (TS-2.1)
    // These establish correctness guarantees that SIMD implementations must match

    proptest! {
        #[test]
        fn prop_search_finds_substr_if_present(
            haystack in vec(any::<u8>(), 0..=MAX_HAYSTACK_LEN),
            needle in vec(any::<u8>(), 0..=MAX_NEEDLE_LEN),
        ) {
            prop_assume!(!needle.is_empty());

            let result = search(&haystack, &needle);
            if let Some(pos) = result {
                prop_assert!(pos + needle.len() <= haystack.len());
                prop_assert_eq!(&haystack[pos..pos + needle.len()], needle.as_slice());
            } else {
                prop_assert!(!haystack.windows(needle.len()).any(|w| w == needle.as_slice()));
            }
        }

        #[test]
        fn prop_search_empty_needle_returns_zero(
            haystack in vec(any::<u8>(), 0..=MAX_HAYSTACK_LEN)
        ) {
            prop_assert_eq!(search(&haystack, &[]), Some(0));
        }

        #[test]
        fn prop_search_needle_longer_returns_none(
            haystack in vec(any::<u8>(), 0..=MAX_HAYSTACK_LEN),
            extra in vec(any::<u8>(), 1..=MAX_EXTRA_LEN),
        ) {
            let mut needle = haystack.clone();
            needle.extend(extra.iter());
            prop_assert!(needle.len() > haystack.len());
            prop_assert!(search(&haystack, &needle).is_none());
        }

        #[test]
        fn prop_trigram_length_correct(text in any::<String>()) {
            let trigrams = extract_trigrams(&text);
            let char_count = text.chars().count();

            if char_count < 3 {
                prop_assert_eq!(trigrams.len(), 1);
                prop_assert_eq!(trigrams[0].as_str(), text.as_str());
            } else {
                prop_assert!(trigrams.iter().all(|t| t.chars().count() == 3));
            }
        }

        #[test]
        fn prop_trigram_count_bounded(text in any::<String>()) {
            let trigrams = extract_trigrams(&text);
            let char_count = text.chars().count();

            if char_count < 3 {
                prop_assert_eq!(trigrams.len(), 1);
            } else {
                prop_assert!(trigrams.len() <= char_count.saturating_sub(2));
            }
        }

        #[test]
        fn prop_lowercase_preserves_length(text in any::<String>()) {
            if text.is_ascii() {
                let lower = to_lowercase_ascii(&text);
                prop_assert_eq!(lower.len(), text.len());
            }
        }

        #[test]
        fn prop_lowercase_idempotent(text in any::<String>()) {
            let lower1 = to_lowercase_ascii(&text);
            let lower2 = to_lowercase_ascii(&lower1);
            prop_assert_eq!(lower1, lower2);
        }

        #[test]
        fn prop_lowercase_only_affects_ascii_uppercase(text in any::<String>()) {
            let lower = to_lowercase_ascii(&text);
            let only_ascii_changes = text.chars().zip(lower.chars()).all(|(orig, lc)| {
                if orig.is_ascii_uppercase() {
                    lc == orig.to_ascii_lowercase()
                } else {
                    lc == orig
                }
            });
            prop_assert!(only_ascii_changes);
        }
    }
}