Commit 8a6d6bd5 authored by Julien Wadel's avatar Julien Wadel
Browse files

Add an insenstive case search function to replace std::find without having to deal with locale.

parent 136ad4e3
......@@ -20,6 +20,7 @@
#ifndef BCTBX_UTILS_H
#define BCTBX_UTILS_H
#include <regex>
#include <string>
#include <vector>
......@@ -48,6 +49,14 @@ namespace bctoolbox {
// Return the current state of memory as a string. This is currently implemented only for Windows.
BCTBX_PUBLIC std::string getMemoryReportAsString();
/**
* The purpose is to replace std::find for search with case insensitive, without having to deal with locale.
* Set 'results' as string results from regex_search.
*
* Return 0 if successful, -1 otherwise. (Linphone convention)
*/
BCTBX_PUBLIC int find(std::smatch * results, const std::string &stringWords, const std::string &filter);
}
......
......@@ -18,7 +18,8 @@
*/
#include "bctoolbox/utils.hh"
#include<sstream>
#include <codecvt>
#include <sstream>
using namespace std;
......@@ -117,3 +118,25 @@ std::string bctoolbox::Utils::getMemoryReportAsString() {
#endif
return ossReport.str();
}
// tolower/towlower/use_facet doesn't work on all cases, beacause it depends on locale. A solution is to use regex with icase. Maybe be inefficient but that seems to work.
int bctoolbox::Utils::find(smatch * results, const string &stringWords, const string &filter){
wstring_convert<codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
// We need to use wstring to add brackets on utf16.
wstring filterLC = converter.from_bytes(filter);
// Build Regex
wstring regexLC = L".*";
regexLC.reserve(filterLC.size() *3 + 5);// Size optimization : brackets*chars + *_encapsulation + '\0'
for(size_t i = 0 ; i < filterLC.size() ; ++i){
regexLC += L"[";
regexLC += filterLC[i];
regexLC += L"]";
}
regexLC += L".*";
// revert back to string because wregex doesn't seem to work.
string regexStr = converter.to_bytes(regexLC);
regex entry_regex(regexStr, regex_constants::extended | regex_constants::nosubs | regex_constants::icase);
//setlocale(LC_CTYPE, ""); // useful for 'towlower' functions but useless for regex.
return regex_search(stringWords, *results, entry_regex) ? 0 : -1;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment