Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use string_view instead of string #92

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/binding/node_data_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ template <> Napi::Object get_at(const Napi::Array &candidates, const size_t ind)
template <> size_t get_size(const Napi::Array &candidates) { return candidates.Length(); }

template <> void set_at(Napi::Array &candidates, CandidateString &&value, const size_t iCandidate) {
candidates.Set(iCandidate, move(value));
candidates.Set(iCandidate, move(string(value)));
}

template <> void set_at(Napi::Array &candidates, Napi::Number &&value, const uint32_t iCandidate) {
Expand Down Expand Up @@ -78,7 +78,7 @@ template <> Napi::Array copy(const Napi::Array &arr, const Napi::Env &env) {
return arr_copy;
}

template <> CandidateString get_at(const Napi::Object &candidates, const string ind) {
template <> string get_at(const Napi::Object &candidates, const string ind) {
return candidates.Get(ind).ToString().Utf8Value();
}

Expand All @@ -98,6 +98,10 @@ template <> void set_at(Napi::Object &candidates, const string &value, const str
candidates.Set(index, value);
}

template <> void set_at(Napi::Object &candidates, const string_view &value, const string index) {
candidates.Set(index, string(value));
}

template <> void set_at(Napi::Object &candidates, const size_t &value, const string index) {
candidates.Set(index, value);
}
Expand Down
16 changes: 8 additions & 8 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class SafeString : public std::string {
using Element = SafeString;
using CandidateString = SafeString;
#else
using Element = string;
using CandidateString = string;
using Element = string_view;
using CandidateString = string_view;
#endif

using CandidateIndex = size_t;
Expand All @@ -44,24 +44,24 @@ using Score = float;

struct PreparedQuery {
Element query;
Element query_lw;
Element core;
Element core_lw;
Element core_up;
string query_lw;
string core;
string core_lw;
string core_up;
int depth = 0;
Element ext;
std::set<char> charCodes{};

explicit PreparedQuery(const Element &q, const char pathSeparator);
};

Element ToLower(const Element &s) {
string ToLower(const Element &s) {
auto snew = string(s.size(), ' '); // new string
std::transform(s.begin(), s.end(), snew.begin(), ::tolower);
return snew;
}

Element ToUpper(const Element &s) {
string ToUpper(const Element &s) {
auto snew = string(s.size(), ' '); // new string
std::transform(s.begin(), s.end(), snew.begin(), ::toupper);
return snew;
Expand Down
2 changes: 1 addition & 1 deletion src/matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void get_wrap(const CandidateString &string, const Element &query, const Options
const auto tagClose = "</strong>"s;

if (string == query) {
*out = tagOpen + string + tagClose;
*out = tagOpen + std::string(string) + tagClose;
return;
}

Expand Down
5 changes: 3 additions & 2 deletions src/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace zadeh {
// Optional chars
// Those char improve the score if present, but will not block the match (score=0) if absent.

Element coreChars(Element query) {
auto coreChars(string query) {
for (const auto ch : " _-:/\\") {
query.erase(std::remove(query.begin(), query.end(), ch), query.end());
}
Expand All @@ -33,7 +33,8 @@ std::set<char> getCharCodes(const Element &str) {
}

PreparedQuery::PreparedQuery(const Element &q, const char pathSeparator)
: query(q), query_lw(ToLower(q)), core(coreChars(q)), core_lw(ToLower(core)), core_up(ToUpper(core)) {
: query(q), query_lw(ToLower(q)), core(coreChars(string(q))), core_lw(ToLower(core)),
core_up(ToUpper(core)) {
depth = countDir(query, query.size(), pathSeparator);
ext = getExtension(query_lw);
charCodes = getCharCodes(query_lw);
Expand Down