mirror of
https://github.com/valitydev/osquery-1.git
synced 2024-11-08 18:33:54 +00:00
971bee4441
fbshipit-source-id: 8ffef5e6a393ac67ce56dcb74845402e43d964a0
55 lines
1.6 KiB
C++
55 lines
1.6 KiB
C++
/**
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under both the Apache 2.0 license (found in the
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
* in the COPYING file in the root directory of this source tree).
|
|
* You may select, at your option, one of the above-listed licenses.
|
|
*/
|
|
|
|
#include "split.h"
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
namespace osquery {
|
|
|
|
std::vector<std::string> split(const std::string& s, const std::string& delim) {
|
|
std::vector<std::string> elems;
|
|
boost::split(elems, s, boost::is_any_of(delim));
|
|
auto start =
|
|
std::remove_if(elems.begin(), elems.end(), [](const std::string& t) {
|
|
return t.size() == 0;
|
|
});
|
|
elems.erase(start, elems.end());
|
|
for (auto& each : elems) {
|
|
boost::algorithm::trim(each);
|
|
}
|
|
return elems;
|
|
}
|
|
|
|
std::vector<std::string> split(const std::string& s,
|
|
char delim,
|
|
size_t occurences) {
|
|
auto delims = std::string(1, delim);
|
|
// Split the string normally with the required delimiter.
|
|
auto content = split(s, delims);
|
|
// While the result split exceeds the number of requested occurrences, join.
|
|
std::vector<std::string> accumulator;
|
|
std::vector<std::string> elems;
|
|
for (size_t i = 0; i < content.size(); i++) {
|
|
if (i < occurences) {
|
|
elems.push_back(content.at(i));
|
|
} else {
|
|
accumulator.push_back(content.at(i));
|
|
}
|
|
}
|
|
// Join the optional accumulator.
|
|
if (accumulator.size() > 0) {
|
|
elems.push_back(boost::algorithm::join(accumulator, delims));
|
|
}
|
|
return elems;
|
|
}
|
|
|
|
}
|