-
Notifications
You must be signed in to change notification settings - Fork 2
/
libretta_string_utils.cpp
75 lines (56 loc) · 1.49 KB
/
libretta_string_utils.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
/***********************************************************
* this code by Peter Semiletov is Public Domain *
**********************************************************/
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
#include <streambuf>
#include "libretta_string_utils.h"
vector<string> string_split (const string &s, char delim)
{
vector<string> result;
stringstream stream (s);
string line;
while (getline (stream, line, delim))
{
result.push_back (line);
}
return result;
}
vector<string> string_split (string s, const string &delim)
{
vector<string> result;
size_t delen = delim.length();
size_t start = 0;
size_t end = s.find (delim);
while (end != std::string::npos)
{
string t = s.substr (start, end - start);
if (! t.empty())
result.push_back (t);
start = end + delen;
end = s.find (delim, start);
}
return result;
}
string string_file_load (const string &fname)
{
std::ifstream t (fname.c_str());
std::string s ((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return s;
}
string string_replace_all (const string &s, const string &from, const string &to)
{
string result = s;
size_t i = 0;
do
{
i = result.find (from);
if (i != string::npos)
result = result.replace (i, from.length(), to);
}
while (i != string::npos);
return result;
}