osquery-1/osquery/core/text.cpp

59 lines
1.7 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
2015-05-12 06:31:13 +00:00
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
2014-08-04 21:12:06 +00:00
#include <vector>
#include <osquery/core.h>
2014-08-04 21:12:06 +00:00
#include <boost/algorithm/string/join.hpp>
2014-08-04 23:08:49 +00:00
#include <boost/algorithm/string/split.hpp>
2014-08-06 22:55:46 +00:00
#include <boost/algorithm/string/trim.hpp>
2014-08-04 21:12:06 +00:00
2014-08-15 07:25:30 +00:00
namespace osquery {
2014-08-04 21:12:06 +00:00
2014-08-15 07:25:30 +00:00
std::vector<std::string> split(const std::string& s, const std::string& delim) {
2014-08-04 21:12:06 +00:00
std::vector<std::string> elems;
2014-08-06 22:55:46 +00:00
boost::split(elems, s, boost::is_any_of(delim));
2014-08-15 07:25:30 +00:00
auto start = std::remove_if(
elems.begin(), elems.end(), [](const std::string& s) { return s == ""; });
2014-08-04 21:12:06 +00:00
elems.erase(start, elems.end());
2014-08-04 23:08:49 +00:00
for (auto& each : elems) {
2014-08-06 22:55:46 +00:00
boost::algorithm::trim(each);
2014-08-04 23:08:49 +00:00
}
2014-08-04 21:12:06 +00:00
return elems;
}
std::vector<std::string> split(const std::string& s,
const std::string& delim,
size_t occurences) {
// Split the string normally with the required delimiter.
auto content = split(s, delim);
// While the result split exceeds the number of requested occurences, 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(join(accumulator, delim));
}
return elems;
}
std::string join(const std::vector<std::string>& s, const std::string& tok) {
return boost::algorithm::join(s, tok);
}
2014-08-15 07:25:30 +00:00
}