more string utilities

This commit is contained in:
project-bo4 2023-09-06 05:29:10 -07:00
parent 2358dcf30b
commit dcd5558821
2 changed files with 23 additions and 0 deletions

View File

@ -193,4 +193,24 @@ namespace utils::string
return str; return str;
} }
double match(const std::string& input, const std::string& text)
{
if (text == input) return 1.00; // identical
size_t offset = to_lower(text).find(to_lower(input));
if (offset == std::string::npos) return 0.00; // mismatch
int len_variance = text.length() - input.length();
int match_percent = 100 - (1 + len_variance + offset);
return ((double)match_percent / 100);
}
bool compare(const std::string& s1, const std::string& s2, bool sensetive)
{
if (sensetive && (s1 == s2)) return true;
if (!sensetive && (to_lower(s1) == to_lower(s2))) return true;
return false;
}
} }

View File

@ -98,4 +98,7 @@ namespace utils::string
std::wstring convert(const std::string& str); std::wstring convert(const std::string& str);
std::string replace(std::string str, const std::string& from, const std::string& to); std::string replace(std::string str, const std::string& from, const std::string& to);
double match(const std::string& input, const std::string& text);
bool compare(const std::string& s1, const std::string& s2, bool sensetive = false);
} }