utils.cpp 1.07 KB
Newer Older
1 2 3
#include <vector>
#include <string>

4
#include <villas/utils.hpp>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

namespace villas {
namespace utils {

std::vector<std::string>
tokenize(std::string s, std::string delimiter)
{
	std::vector<std::string> tokens;

	size_t lastPos = 0;
	size_t curentPos;

	while((curentPos = s.find(delimiter, lastPos)) != std::string::npos) {
		const size_t tokenLength = curentPos - lastPos;
		tokens.push_back(s.substr(lastPos, tokenLength));

		// advance in string
		lastPos = curentPos + delimiter.length();
	}

	// check if there's a last token behind the last delimiter
	if(lastPos != s.length()) {
		const size_t lastTokenLength = s.length() - lastPos;
		tokens.push_back(s.substr(lastPos, lastTokenLength));
	}

	return tokens;
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
std::string
join(std::vector<std::string> strings, std::string delimiter)
{
	std::string out;

	for(size_t i = 0; i < strings.size(); i++) {
		const auto& s = strings[i];

		if(s.length() > 0) {
			out += strings[i];

			if(i < (strings.size() - 1) and strings[i+1].length() > 0)
				out += delimiter;
		}
	}

	return out;
}

53 54
} // namespace utils
} // namespace villas