-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringTools.cpp
164 lines (145 loc) · 3.39 KB
/
StringTools.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// StringTools.cpp
// Tools
//
// Created by Jacob Gonzalez on 11/04/2015.
// Copyright (c) 2015 Jacob Gonzalez. All rights reserved.
//
#include <fstream>
#include <sstream>
#include "StringTools.h"
int StringTools::string_height(std::string instring)
{
vstring lines = split(instring, "\n");
return lines.get_count();
}
int StringTools::string_width(std::string instring)
{
size_t count = 0;
count = instring.find_first_of("\n");
if (count == std::string::npos)
{
count = instring.length();
}
return count;
}
Point<int> StringTools::string_size(std::string instring)
{
Point<int> size(string_width(instring), string_height(instring));
return size;
}
std::string StringTools::replace_all(std::string instring,
std::string from,
std::string to)
{
size_t i = 0;
while ((i = instring.find(from)) != std::string::npos)
{
instring.replace(i, from.length(), to);
i += to.length();
}
return instring;
}
std::string StringTools::readall(std::string filename)
{
std::ifstream file(filename.c_str());
std::stringstream ss;
ss << file.rdbuf();
if (!file)
{
return "$NO FILE";
}
file.close();
return ss.str();
}
vstring StringTools::readlines(std::string filename)
{
vstring lines;
std::string line;
std::ifstream file(filename.c_str());
if (!file)
{
return lines;
}
while (getline(file, line))
{
lines.add(line);
}
file.close();
return lines;
}
vstring StringTools::tokens(std::string instring, std::string del)
{
vstring parts;
_recSplit(instring, parts, del, true);
return parts;
}
vstring StringTools::split(std::string instring, std::string del)
{
vstring parts;
_recSplit(instring, parts, del, false);
return parts;
}
void StringTools::_recSplit(std::string instring, vstring &parts,
std::string del, bool tokens)
{
size_t i = !tokens ? instring.find(del) : instring.find_first_of(del);
if (i == std::string::npos)
{
if (!instring.empty())
{
parts.add(instring);
}
return;
}
else if (i > 0 && i < instring.length())
{
parts.add(instring.substr(0, i));
}
if (!tokens)
{
if (i+del.length() <= instring.length())
{
_recSplit(instring.substr(i+del.length()), parts, del, tokens);
}
}
else
{
if (i+1 <= instring.length())
{
_recSplit(instring.substr(i+1), parts, del, tokens);
}
}
}
std::string StringTools::trim(std::string instring, std::string whitespaces)
{
return ltrim(rtrim(instring, whitespaces), whitespaces);
}
std::string StringTools::ltrim(std::string instring, std::string whitespaces)
{
size_t i = instring.find_first_not_of(whitespaces);
if (i == std::string::npos)
{
return instring;
}
else
{
return instring.substr(i, instring.size());
}
}
std::string StringTools::rtrim(std::string instring, std::string whitespaces)
{
size_t i = instring.find_last_not_of(whitespaces);
if (i == std::string::npos)
{
return instring;
}
else
{
return instring.substr(0, i+1);
}
}
std::string StringTools::trim(std::string instring)
{
return trim(instring, " \t\n");
}