-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_util.cpp
95 lines (83 loc) · 2.11 KB
/
string_util.cpp
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* UniversalContainer library.
* Copyright Jason Denton, 2008,2010.
* Made available under the new BSD license, as described in LICENSE
*
* Send comments and bug reports to jason.denton@gmail.com
* http://www.greatpanic.com/code.html
*/
#include <string>
#include <map>
#include <vector>
#include <typeinfo>
/*
Generic string processing that I find useful.
*/
using namespace std;
//strip whitespace off either end of strings
std::string string_chomp(const std::string& str)
{
string ret;
int start = str.find_first_not_of("\n \t");
int end = str.find_last_not_of("\n \t");
if (start >= 0)
ret = str.substr(start, end-start+1);
return ret;
}
/*
Like string_tokens, but splits when any character in delim is found.
This is equivalent to k&r str_tok
*/
std::vector<std::string> string_tokens(const std::string& str,
const std::string& delim)
{
std::vector<std::string> list;
std::string next;
size_t start = 0;
size_t pos = str.find_first_of(delim);
bool end = false;
while (!end) {
next = str.substr(start,pos-start);
list.push_back(next);
start = pos+1;
if (pos == std::string::npos) end = true;
else pos = str.find_first_of(delim,start);
}
return list;
}
/*
Split a string into tokens, deliminated by the compelete
string delim. Deliminators not output.
*/
std::vector<std::string> string_pieces(const std::string& str,
const std::string& delim)
{
std::vector<std::string> list;
std::string next;
size_t start = 0;
size_t pos = str.find(delim);
bool end = false;
size_t inc = delim.length();
while (!end) {
next = str.substr(start,pos-start);
list.push_back(next);
start = pos+inc;
if (pos == std::string::npos) end = true;
else pos = str.find(delim,start);
}
return list;
}
/*
Split a string into two parts
*/
std::vector<std::string> string_split(const std::string& str,
const std::string& delim)
{
std::vector<std::string> list;
size_t pos = str.find_first_of(delim);
if (pos != string::npos) {
list.push_back(str.substr(0,pos));
list.push_back(str.substr(pos+1));
}
return list;
}