Move range facet parsing to cpp file.

This commit is contained in:
Kishore Nallan 2023-05-03 13:47:30 +05:30
parent 8ebe92dbe2
commit 7515e733db
3 changed files with 58 additions and 58 deletions

View File

@ -23,54 +23,7 @@ struct StringUtils {
static size_t split_facet(const std::string& s, std::vector<std::string> & result,
const bool keep_empty = false, const size_t start_index = 0,
const size_t max_values = (std::numeric_limits<size_t>::max()-1)) {
std::string::const_iterator substart = s.begin()+start_index, subend;
size_t end_index = start_index;
std::string delim(""), temp("");
std::string current_str=s;
while (true) {
auto range_pos = current_str.find("(");
auto normal_pos = current_str.find(",");
if(range_pos == std::string::npos && normal_pos == std::string::npos){
if(!current_str.empty()){
result.push_back(trim(current_str));
}
break;
}
else if(range_pos < normal_pos){
delim="),";
subend = std::search(substart, s.end(), delim.begin(), delim.end());
temp = std::string(substart, subend);
}
else{
delim=",";
subend = std::search(substart, s.end(), delim.begin(), delim.end());
temp = std::string(substart, subend);
}
end_index += temp.size() + delim.size();
temp = trim(temp);
if (keep_empty || !temp.empty()) {
result.push_back(temp);
}
if(result.size() == max_values) {
break;
}
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
current_str = std::string(substart, s.end());
}
return std::min(end_index, s.size());
}
const size_t max_values = (std::numeric_limits<size_t>::max()-1));
// Adapted from: http://stackoverflow.com/a/236180/131050
static size_t split(const std::string& s, std::vector<std::string> & result, const std::string& delim,

View File

@ -490,6 +490,56 @@ Option<bool> StringUtils::split_include_fields(const std::string& include_fields
return Option<bool>(true);
}
size_t StringUtils::split_facet(const std::string &s, std::vector<std::string> &result, const bool keep_empty,
const size_t start_index, const size_t max_values) {
std::string::const_iterator substart = s.begin()+start_index, subend;
size_t end_index = start_index;
std::string delim(""), temp("");
std::string current_str=s;
while (true) {
auto range_pos = current_str.find("(");
auto normal_pos = current_str.find(",");
if(range_pos == std::string::npos && normal_pos == std::string::npos){
if(!current_str.empty()){
result.push_back(trim(current_str));
}
break;
}
else if(range_pos < normal_pos){
delim="),";
subend = std::search(substart, s.end(), delim.begin(), delim.end());
temp = std::string(substart, subend) + (*subend == ')' ? ")" : "");
}
else{
delim=",";
subend = std::search(substart, s.end(), delim.begin(), delim.end());
temp = std::string(substart, subend);
}
end_index += temp.size() + delim.size();
temp = trim(temp);
if (keep_empty || !temp.empty()) {
result.push_back(temp);
}
if(result.size() == max_values) {
break;
}
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
current_str = std::string(substart, s.end());
}
return std::min(end_index, s.size());
}
/*size_t StringUtils::unicode_length(const std::string& bytes) {
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf8conv;
return utf8conv.from_bytes(bytes).size();

View File

@ -322,24 +322,21 @@ TEST(StringUtilsTest, ShouldSplitRangeFacet){
std::string range_facets_string = "score(fail:[0, 40], pass:[40, 100]), grade(A:[80,100], B:[60, 80], C:[40, 60])";
std::vector<std::string> range_facets;
StringUtils::split_facet(range_facets_string, range_facets);
ASSERT_STREQ("score(fail:[0, 40], pass:[40, 100])", range_facets[0].c_str());
ASSERT_STREQ("grade(A:[80,100], B:[60, 80], C:[40, 60])", range_facets[1].c_str());
ASSERT_EQ("score(fail:[0, 40], pass:[40, 100])", range_facets[0]);
ASSERT_EQ("grade(A:[80,100], B:[60, 80], C:[40, 60])", range_facets[1]);
std::string facets_string = "score, grade";
std::vector<std::string> facets;
StringUtils::split_facet(facets_string, facets);
ASSERT_STREQ("score", facets[0].c_str());
ASSERT_STREQ("grade", facets[1].c_str());
ASSERT_EQ("score", facets[0]);
ASSERT_EQ("grade", facets[1]);
std::string mixed_facets_string = "score, grade(A:[80,100], B:[60, 80], C:[40, 60]), rank";
std::vector<std::string> mixed_facets;
StringUtils::split_facet(mixed_facets_string, mixed_facets);
ASSERT_STREQ("score", mixed_facets[0].c_str());
ASSERT_STREQ("grade(A:[80,100], B:[60, 80], C:[40, 60])", mixed_facets[1].c_str());
ASSERT_STREQ("rank", mixed_facets[2].c_str());
ASSERT_EQ("score", mixed_facets[0]);
ASSERT_EQ("grade(A:[80,100], B:[60, 80], C:[40, 60])", mixed_facets[1]);
ASSERT_EQ("rank", mixed_facets[2]);
// empty string should produce empty list
std::vector<std::string> lines_empty;