mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-02-17 11:45:50 +00:00
Add a generic split() function.
This commit is contained in:
parent
40f98fb382
commit
527f8cedea
2 changed files with 38 additions and 12 deletions
|
@ -19,13 +19,13 @@
|
||||||
#define id913CE9D2F60745349F39F2C82455973E
|
#define id913CE9D2F60745349F39F2C82455973E
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
|
||||||
#include <boost/utility/string_ref.hpp>
|
#include <boost/utility/string_ref.hpp>
|
||||||
#include "duckhandy/compatibility.h"
|
#include "duckhandy/compatibility.h"
|
||||||
|
|
||||||
namespace dincore {
|
namespace dincore {
|
||||||
std::vector<boost::string_ref> split_and_trim ( const std::string& parList, char parSeparator ) 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 ( const std::string& parCommaSeparatedList ) 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
|
} //namespace dincore
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -25,11 +25,15 @@
|
||||||
#include <ciso646>
|
#include <ciso646>
|
||||||
|
|
||||||
namespace dincore {
|
namespace dincore {
|
||||||
std::vector<boost::string_ref> split_tags (const std::string& parCommaSeparatedList) {
|
std::vector<boost::string_ref> split_tags (boost::string_ref parCommaSeparatedList) {
|
||||||
return split_and_trim(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 OutRange = boost::iterator_range<std::string::const_iterator>;
|
||||||
using boost::token_finder;
|
using boost::token_finder;
|
||||||
using boost::adaptors::transformed;
|
using boost::adaptors::transformed;
|
||||||
|
@ -45,11 +49,33 @@ 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/iter_split.html
|
||||||
//http://www.boost.org/doc/libs/1_60_0/doc/html/boost/algorithm/token_finder.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
|
//https://stackoverflow.com/questions/20781090/difference-between-boostsplit-vs-boostiter-split
|
||||||
return boost::copy_range<std::vector<string_ref>>(
|
if (parTrim and parDeleteEmpty) {
|
||||||
iter_split(out_range, parList, token_finder([parSeparator](char c){return parSeparator==c;})) |
|
return boost::copy_range<std::vector<string_ref>>(
|
||||||
transformed([](const OutRange& r){return trim_copy(r);}) |
|
iter_split(out_range, parList, token_finder([parSeparator](char c){return parSeparator==c;})) |
|
||||||
transformed([](const OutRange& r){return string_ref(&*r.begin(), r.size());}) |
|
transformed([](const OutRange& r){return trim_copy(r);}) |
|
||||||
filtered([](const string_ref& r){return not r.empty();})
|
transformed([](const OutRange& r){return string_ref(&*r.begin(), r.size());}) |
|
||||||
);
|
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
|
} //namespace dincore
|
||||||
|
|
Loading…
Add table
Reference in a new issue