Skip to content

Commit

Permalink
utils:: Add utils::string_split function
Browse files Browse the repository at this point in the history
This patch implements a helper function that splits std::string into
std::vector with the given delimiter.

Signed-off-by: Honggyu Kim <[email protected]>
  • Loading branch information
honggyukim committed Jan 14, 2023
1 parent 70c11fb commit f1a90ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <sstream>
#include <fstream>
#include <vector>

#include "utils.h"

Expand Down Expand Up @@ -41,6 +42,18 @@ std::string get_comm_name(void)
return comm;
}

std::vector<std::string> string_split(std::string str, char delim)
{
std::vector<std::string> vstr;
std::stringstream ss(str);
std::string s;

while (getline(ss, s, delim))
vstr.push_back(s);

return vstr;
}

static enum_table ht_mmap_prot[] = {
{ "PROT_NONE", 0 },
{ "PROT_READ", 1 },
Expand Down
3 changes: 3 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <string>
#include <chrono>
#include <vector>

#ifndef _GNU_SOURCE
# define _GNU_SOURCE
Expand All @@ -31,6 +32,8 @@ std::string asprintf(const char* fmt, ...);

std::string get_comm_name(void);

std::vector<std::string> string_split(std::string str, char delim);

struct enum_table {
const char *str;
int val;
Expand Down

0 comments on commit f1a90ab

Please sign in to comment.