/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see .
*/
#include "split.hpp"
#include
#include
#include
#include
#include
#include
namespace dincore {
std::vector split (std::string_view parList, char parSeparator, bool parTrim, bool parDeleteEmpty) {
using OutRange = boost::iterator_range;
using boost::token_finder;
using boost::adaptors::transformed;
using boost::adaptors::filtered;
using std::string_view;
using boost::iter_split;
using boost::trim_copy;
std::vector out_range;
//See:
//https://stackoverflow.com/questions/27999941/how-to-use-boostsplit-with-booststring-ref-in-boost-1-55
//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>(
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_view(&*r.begin(), r.size());}) |
filtered([](string_view r){return not r.empty();})
);
}
else if (parTrim) {
return boost::copy_range>(
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_view(&*r.begin(), r.size());})
);
}
else if (parDeleteEmpty) {
return boost::copy_range>(
iter_split(out_range, parList, token_finder([parSeparator](char c){return parSeparator==c;})) |
transformed([](const OutRange& r){return string_view(&*r.begin(), r.size());}) |
filtered([](string_view r){return not r.empty();})
);
}
else {
return boost::copy_range>(
iter_split(out_range, parList, token_finder([parSeparator](char c){return parSeparator==c;})) |
transformed([](const OutRange& r){return string_view(&*r.begin(), r.size());})
);
}
}
} //namespace dincore