1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-29 01:33:46 +00:00

Add a generic split() function.

This commit is contained in:
King_DuckZ 2017-08-16 21:09:15 +01:00
parent 40f98fb382
commit 527f8cedea
2 changed files with 38 additions and 12 deletions

View file

@ -19,13 +19,13 @@
#define id913CE9D2F60745349F39F2C82455973E
#include <vector>
#include <string>
#include <boost/utility/string_ref.hpp>
#include "duckhandy/compatibility.h"
namespace dincore {
std::vector<boost::string_ref> split_and_trim ( const std::string& parList, char parSeparator ) a_pure;
std::vector<boost::string_ref> split_tags ( const std::string& parCommaSeparatedList ) a_pure;
std::vector<boost::string_ref> split_and_trim ( boost::string_ref parList, char parSeparator ) a_pure;
std::vector<boost::string_ref> split_tags ( boost::string_ref parCommaSeparatedList ) a_pure;
std::vector<boost::string_ref> split ( boost::string_ref parList, char parSeparator, bool parTrim, bool parDeleteEmpty ) a_pure;
} //namespace dincore
#endif

View file

@ -25,11 +25,15 @@
#include <ciso646>
namespace dincore {
std::vector<boost::string_ref> split_tags (const std::string& parCommaSeparatedList) {
return split_and_trim(parCommaSeparatedList, ',');
std::vector<boost::string_ref> split_tags (boost::string_ref parCommaSeparatedList) {
return split(parCommaSeparatedList, ',', true, true);
}
std::vector<boost::string_ref> split_and_trim (const std::string& parList, char parSeparator) {
std::vector<boost::string_ref> split_and_trim (boost::string_ref parList, char parSeparator) {
return split(parList, parSeparator, true, true);
}
std::vector<boost::string_ref> split (boost::string_ref parList, char parSeparator, bool parTrim, bool parDeleteEmpty) {
using OutRange = boost::iterator_range<std::string::const_iterator>;
using boost::token_finder;
using boost::adaptors::transformed;
@ -45,6 +49,7 @@ namespace dincore {
//http://www.boost.org/doc/libs/1_60_0/doc/html/boost/algorithm/iter_split.html
//http://www.boost.org/doc/libs/1_60_0/doc/html/boost/algorithm/token_finder.html
//https://stackoverflow.com/questions/20781090/difference-between-boostsplit-vs-boostiter-split
if (parTrim and parDeleteEmpty) {
return boost::copy_range<std::vector<string_ref>>(
iter_split(out_range, parList, token_finder([parSeparator](char c){return parSeparator==c;})) |
transformed([](const OutRange& r){return trim_copy(r);}) |
@ -52,4 +57,25 @@ namespace dincore {
filtered([](const string_ref& r){return not r.empty();})
);
}
else if (parTrim) {
return boost::copy_range<std::vector<string_ref>>(
iter_split(out_range, parList, token_finder([parSeparator](char c){return parSeparator==c;})) |
transformed([](const OutRange& r){return trim_copy(r);}) |
transformed([](const OutRange& r){return string_ref(&*r.begin(), r.size());})
);
}
else if (parDeleteEmpty) {
return boost::copy_range<std::vector<string_ref>>(
iter_split(out_range, parList, token_finder([parSeparator](char c){return parSeparator==c;})) |
transformed([](const OutRange& r){return string_ref(&*r.begin(), r.size());}) |
filtered([](const string_ref& r){return not r.empty();})
);
}
else {
return boost::copy_range<std::vector<string_ref>>(
iter_split(out_range, parList, token_finder([parSeparator](char c){return parSeparator==c;})) |
transformed([](const OutRange& r){return string_ref(&*r.begin(), r.size());})
);
}
}
} //namespace dincore