mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
lib/utils: add string tokenizer
This commit is contained in:
parent
35d96ed277
commit
3d0afd671e
3 changed files with 51 additions and 0 deletions
15
fpga/include/villas/utils.hpp
Normal file
15
fpga/include/villas/utils.hpp
Normal file
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
namespace villas {
|
||||
namespace utils {
|
||||
|
||||
std::vector<std::string>
|
||||
tokenize(std::string s, std::string delimiter);
|
||||
|
||||
} // namespace utils
|
||||
} // namespace villas
|
||||
|
|
@ -24,6 +24,7 @@ set(SOURCES
|
|||
plugin.c
|
||||
plugin.cpp
|
||||
utils.c
|
||||
utils.cpp
|
||||
list.c
|
||||
log.c
|
||||
log_config.c
|
||||
|
|
35
fpga/lib/utils.cpp
Normal file
35
fpga/lib/utils.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "utils.hpp"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
} // namespace villas
|
Loading…
Add table
Reference in a new issue