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
use std::cmp::Ordering;
use std::collections::BinaryHeap;

use hlbc::types::RefFun;
use hlbc::Bytecode;

#[cfg(feature = "tantivy")]
mod tantivy;

pub trait Searcher {
    fn search(&self, code: &Bytecode, needle: &str, limit: usize) -> Vec<RefFun>;
}

struct Comp<T>(T, f32);

impl<T> Eq for Comp<T> {}

impl<T> PartialEq<Self> for Comp<T> {
    fn eq(&self, other: &Comp<T>) -> bool {
        self.1.eq(&other.1)
    }
}

impl<T> PartialOrd<Self> for Comp<T> {
    fn partial_cmp(&self, other: &Comp<T>) -> Option<Ordering> {
        other.1.partial_cmp(&self.1)
    }
}

impl<T> Ord for Comp<T> {
    fn cmp(&self, other: &Comp<T>) -> Ordering {
        other.1.total_cmp(&self.1)
    }
}

pub fn top_candidates<'a, T>(n: usize, results: impl Iterator<Item = (T, f32)>) -> Vec<(T, f32)> {
    let mut top = BinaryHeap::with_capacity(n + 1);
    for (c, score) in results {
        if score > 0.0 {
            top.push(Comp(c, score));
            if top.len() > n {
                top.pop();
            }
        }
    }
    top.into_sorted_vec()
        .into_iter()
        .map(|c| (c.0, c.1))
        .collect()
}

pub struct Contains;

impl Searcher for Contains {
    fn search(&self, code: &Bytecode, needle: &str, limit: usize) -> Vec<RefFun> {
        let needle_len = needle.len() as f32;
        top_candidates(
            limit,
            code.functions().map(|f| {
                let name = f.name(code);
                let len = name.len() as f32;
                (
                    f.findex(),
                    if name.contains(needle) {
                        needle_len / len
                    } else if needle.contains(&*name) {
                        len / needle_len
                    } else {
                        0.0
                    },
                )
            }),
        )
        .into_iter()
        .map(|(c, s)| c)
        .collect()
    }
}

// pub struct Memchr;
//
// impl Searcher for Memchr {
//     fn with_needle<'a>(&self, needle: &'a str) -> Box<dyn Matcher + 'a> {
//         Box::new(MemchrMatcher(memchr::memmem::Finder::new(needle)))
//     }
// }
//
// pub struct MemchrMatcher<'a>(memchr::memmem::Finder<'a>);
//
// impl Matcher for MemchrMatcher<'_> {
//     fn eval(&self, candidate: &str) -> f32 {
//         if self.0.find(candidate.as_bytes()).is_some() {
//             self.0.needle().len() as f32 / candidate.len() as f32
//         } else if memchr::memmem::find(self.0.needle(), candidate.as_bytes()).is_some() {
//             candidate.len() as f32 / self.0.needle().len() as f32
//         } else {
//             0.0
//         }
//     }
// }

pub struct ClangdSearcher(fuzzy_matcher::clangd::ClangdMatcher);

impl ClangdSearcher {
    pub fn new() -> Self {
        Self(fuzzy_matcher::clangd::ClangdMatcher::default().ignore_case())
    }
}

impl Searcher for ClangdSearcher {
    fn search(&self, code: &Bytecode, needle: &str, limit: usize) -> Vec<RefFun> {
        top_candidates(
            limit,
            code.functions().map(|f| {
                (
                    f.findex(),
                    fuzzy_matcher::FuzzyMatcher::fuzzy_match(&self.0, &f.name(code), needle)
                        .map(|s| s as f32)
                        .unwrap_or(0.0),
                )
            }),
        )
        .into_iter()
        .map(|(c, s)| c)
        .collect()
    }
}

pub struct SkimSearcher(fuzzy_matcher::skim::SkimMatcherV2);

impl SkimSearcher {
    pub fn new() -> Self {
        Self(fuzzy_matcher::skim::SkimMatcherV2::default().ignore_case())
    }
}

impl Searcher for SkimSearcher {
    fn search(&self, code: &Bytecode, needle: &str, limit: usize) -> Vec<RefFun> {
        top_candidates(
            limit,
            code.functions().map(|f| {
                (
                    f.findex(),
                    fuzzy_matcher::FuzzyMatcher::fuzzy_match(&self.0, &f.name(code), needle)
                        .map(|s| s as f32)
                        .unwrap_or(0.0),
                )
            }),
        )
        .into_iter()
        .map(|(c, s)| c)
        .collect()
    }
}