From f1e9673605cc36597405001d317606a46aef5db9 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 22 Oct 2021 23:07:32 -0500 Subject: [PATCH 1/2] feat: use string_view instead of string --- src/binding/node_data_interface.h | 10 +++++++--- src/common.h | 4 ++-- src/matcher.h | 2 +- src/query.h | 5 +++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/binding/node_data_interface.h b/src/binding/node_data_interface.h index 40d7c796..a81c81b1 100644 --- a/src/binding/node_data_interface.h +++ b/src/binding/node_data_interface.h @@ -19,8 +19,8 @@ template <> Napi::Number init(const size_t value, const Napi::Env &env) { /** Napi::Array Data Interface */ template <> Napi::Array init(const size_t len, const Napi::Env &env) { return Napi::Array::New(env, len); } -template <> string get_at(const Napi::Array &candidates, const size_t ind) { - return candidates.Get(ind).ToString().Utf8Value(); +template <> string_view get_at(const Napi::Array &candidates, const size_t ind) { + return string_view(candidates.Get(ind).ToString().Utf8Value()); } #ifndef ENV32BIT // only enable if size_t is not unint32_t @@ -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) { @@ -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); } diff --git a/src/common.h b/src/common.h index db874808..da461759 100644 --- a/src/common.h +++ b/src/common.h @@ -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; diff --git a/src/matcher.h b/src/matcher.h index d5f837cc..10b7deb0 100644 --- a/src/matcher.h +++ b/src/matcher.h @@ -246,7 +246,7 @@ void get_wrap(const CandidateString &string, const Element &query, const Options const auto tagClose = ""s; if (string == query) { - *out = tagOpen + string + tagClose; + *out = tagOpen + std::string(string) + tagClose; return; } diff --git a/src/query.h b/src/query.h index c4a8c32f..4026f28e 100644 --- a/src/query.h +++ b/src/query.h @@ -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) { +Element coreChars(string query) { for (const auto ch : " _-:/\\") { query.erase(std::remove(query.begin(), query.end(), ch), query.end()); } @@ -33,7 +33,8 @@ std::set 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); From e38b9ad584f4f7522e281c48a141a0c59c74671d Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 22 Oct 2021 23:21:48 -0500 Subject: [PATCH 2/2] fix: use string where needed --- src/binding/node_data_interface.h | 6 +++--- src/common.h | 12 ++++++------ src/query.h | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/binding/node_data_interface.h b/src/binding/node_data_interface.h index a81c81b1..212f39f9 100644 --- a/src/binding/node_data_interface.h +++ b/src/binding/node_data_interface.h @@ -19,8 +19,8 @@ template <> Napi::Number init(const size_t value, const Napi::Env &env) { /** Napi::Array Data Interface */ template <> Napi::Array init(const size_t len, const Napi::Env &env) { return Napi::Array::New(env, len); } -template <> string_view get_at(const Napi::Array &candidates, const size_t ind) { - return string_view(candidates.Get(ind).ToString().Utf8Value()); +template <> string get_at(const Napi::Array &candidates, const size_t ind) { + return candidates.Get(ind).ToString().Utf8Value(); } #ifndef ENV32BIT // only enable if size_t is not unint32_t @@ -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(); } diff --git a/src/common.h b/src/common.h index da461759..90bd9dac 100644 --- a/src/common.h +++ b/src/common.h @@ -44,10 +44,10 @@ 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 charCodes{}; @@ -55,13 +55,13 @@ struct PreparedQuery { 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; diff --git a/src/query.h b/src/query.h index 4026f28e..fddedb66 100644 --- a/src/query.h +++ b/src/query.h @@ -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(string query) { +auto coreChars(string query) { for (const auto ch : " _-:/\\") { query.erase(std::remove(query.begin(), query.end(), ch), query.end()); }