-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.cpp
194 lines (162 loc) · 4.39 KB
/
str.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
using namespace std;
class str
{
public:
string s;
public:
str()
{
s = "";
}
str(string string)
{
s = string;
}
char firstC()
{
const char* t = " \t\n\r\f\v";
return s[s.find_first_not_of(t)];
}
bool firstIs(char isChar)
{
return firstC() == isChar;
}
char lastC()
{
const char* t = " \t\n\r\f\v";
return s[s.find_last_not_of(t)];
}
bool lastIs(char isChar)
{
return lastC() == isChar;
}
bool has(string find)
{
return s.find(find) != string::npos;
}
bool has(string find1, string find2)
{
return s.find(find1) != string::npos && s.find(find2) != string::npos;
}
str getSpacing()
{
const string& whitespace = " \t";
const auto strBegin = s.find_first_not_of(whitespace);
if (strBegin == string::npos) return str();
return s.substr(0, strBegin);
}
// " Hello World " -> "Hello World"
str trim()
{
const string& whitespace = " \t";
const auto strBegin = s.find_first_not_of(whitespace);
if (strBegin == string::npos) return str();
const auto strEnd = s.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return s.substr(strBegin, strRange);
}
// "str, int | str, str | int" -> [str, int], [str, str], [int]
vector<str> split(char splitOn)
{
string tmp;
stringstream ss(s);
vector<str> seglist;
while(getline(ss, tmp, splitOn)){
seglist.push_back(tmp);
}
return seglist;
}
// get values inside first set of ( )
str returns()
{
//return s.substr(s.find("(") + 1, s.find(")") - 5);
return extractBetween("(", ")");
}
// (str, int, int)returnBack(int x) -> returnBack(int x)
str afterReturns()
{
return getAfter(")");
}
str getBefore(string until)
{
return s.substr(0, s.find(until));
}
str getAfter(string after)
{
return s.substr(s.find(after) + after.length());
}
// int k = holdingTuple<1> with < and > would reutrn 1
str extractBetween(string start, string end)
{
return s.substr(s.find(start) + start.length(), s.find(end) - s.find(start) - (start.length() - end.length()) - 1);
}
str replace(string find, string replace)
{
int pos = s.find(find);
string replacedString = s;
while (pos != string::npos)
{
replacedString.replace(pos, find.length(), replace);
pos = replacedString.find(find);
}
return replacedString;
}
// get vairable names from string "int height, double width, string name" -> "height, width, name"
str getReturnNames()
{
string returns = s;
string returnNames = "";
int start = 0;
for (int i = 0; i < returns.length(); i++)
{
if (returns[i] == ' ')
start = i + 1;
else if (returns[i] == ',')
returnNames += returns.substr(start, i - start) + ", ";
}
returnNames += returns.substr(start);
return returnNames;
}
// get vairable types from string "int height, double width, string name" -> "int, double, string"
str getReturnTypes()
{
if (!has(",")) return getBefore(" ");
string returns = s;
string returnTypes = "";
int start = 0;
for (int i = 0; i < returns.length(); i++)
{
if (returns[i] == ' ')
returnTypes += returns.substr(start, i - start);
else if (returns[i] == ',')
{
start = i + 1;
returnTypes += ",";
}
}
//returnTypes += returns.substr(start);
return returnTypes;
}
int getReturnAmount()
{
int amount = 0;
if (!has(",")) return amount;
amount++;
string returns = getReturnTypes().s;
for (int i = 0; i < returns.length(); i++)
if (returns[i] == ',')
amount++;
return amount;
}
friend ostream& operator<<(ostream& output, const str& stringOut)
{
output << stringOut.s;
return output;
}
};