Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Last active March 5, 2019 21:22
Show Gist options
  • Save heatblazer/ea0640f746ebe917152897cb08c3f274 to your computer and use it in GitHub Desktop.
Save heatblazer/ea0640f746ebe917152897cb08c3f274 to your computer and use it in GitHub Desktop.
test split
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
static int split(const char* str, char delim, std::vector<std::string>& out)
{
const char* begin = str;
const char* it = strchr(str, delim);
if (it != NULL)
{
std::string data;
for(const char* i = begin; i != it ; i++)
{
data.push_back(*i);
}
out.push_back(data);
it++;
split(it, delim, out);
}
else
{
std::string data;
str--;
while (*str++ != '\0')
{
data.push_back(*str);
}
out.push_back(data);
}
}
template<typename K, typename V> struct Pair
{
K key;
V value;
Pair(){}
Pair(const K& k, const V& v) : key{k}, value{v} {}
};
struct Person
{
Pair<std::string, std::string> table[4];
Person() {}
};
static void MakePerson(Person& p, const std::vector<std::string> data, int tableoffset)
{
size_t size = data.size()/2;
for(int i=0; i < size; ++i)
{
p.table[i].key = data[i];
p.table[i].value = data[i+tableoffset];
}
}
int main() {
Person p{};
std::vector<std::string> data;
const char* test = "user, host, port, pass, john, yahoo.com, 80, blabla123";
split(test, ',', data);
MakePerson(p, data, 4);
for(int i=0; i < 4; ++i)
{
std::cout << p.table[i].key << ":" << p.table[i].value << "\r\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment