mirror of
https://github.com/MaskRay/ccls.git
synced 2025-02-21 16:09:40 +00:00
WIP fuzzy search
This commit is contained in:
parent
9826ff193b
commit
349b982f66
41
src/fuzzy.cc
Normal file
41
src/fuzzy.cc
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include "fuzzy.h"
|
||||||
|
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
Matcher::Matcher(const std::string& search) {
|
||||||
|
std::string real_search;
|
||||||
|
real_search.reserve(search.size() * 3 + 2);
|
||||||
|
for (auto c : search) {
|
||||||
|
real_search += ".*";
|
||||||
|
real_search += c;
|
||||||
|
}
|
||||||
|
real_search += ".*";
|
||||||
|
|
||||||
|
regex = std::regex(real_search,
|
||||||
|
std::regex_constants::ECMAScript |
|
||||||
|
std::regex_constants::icase |
|
||||||
|
std::regex_constants::optimize
|
||||||
|
//std::regex_constants::nosubs
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Matcher::IsMatch(const std::string& value) {
|
||||||
|
//std::smatch match;
|
||||||
|
//return std::regex_match(value, match, regex);
|
||||||
|
return std::regex_match(value, regex, std::regex_constants::match_any);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_SUITE("Matcher");
|
||||||
|
|
||||||
|
TEST_CASE("sanity") {
|
||||||
|
Matcher m("abc");
|
||||||
|
// TODO: check case
|
||||||
|
CHECK(m.IsMatch("abc"));
|
||||||
|
CHECK(m.IsMatch("fooabc"));
|
||||||
|
CHECK(m.IsMatch("fooabc"));
|
||||||
|
CHECK(m.IsMatch("abc"));
|
||||||
|
CHECK(m.IsMatch("abcfoo"));
|
||||||
|
CHECK(m.IsMatch("11a11b11c11"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_SUITE_END();
|
10
src/fuzzy.h
Normal file
10
src/fuzzy.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
|
struct Matcher {
|
||||||
|
Matcher(const std::string& search);
|
||||||
|
|
||||||
|
bool IsMatch(const std::string& value);
|
||||||
|
std::regex regex;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user