-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring_utilities.cpp
More file actions
169 lines (132 loc) · 3.54 KB
/
Copy pathstring_utilities.cpp
File metadata and controls
169 lines (132 loc) · 3.54 KB
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
// Source code by Shawn Halayka
// Source code is in the public domain
#include "string_utilities.h"
using std::vector;
using std::string;
string string_utilities::lower_string(const string &src_string)
{
string temp = src_string;
for(string::iterator i = temp.begin(); i != temp.end(); i++)
*i = tolower(*i);
return temp;
}
string string_utilities::upper_string(const string &src_string)
{
string temp = src_string;
for(string::iterator i = temp.begin(); i != temp.end(); i++)
*i = toupper(*i);
return temp;
}
string string_utilities::trim_whitespace_string(const string &src_string)
{
string temp_string;
for(size_t i = 0; i < src_string.length(); i++)
if(' ' != src_string[i] && '\t' != src_string[i])
temp_string += src_string[i];
return temp_string;
}
vector<string> string_utilities::stl_str_tok(string token, const string &src_string)
{
vector<string> dest_vector;
size_t prev_pos = 0;
// set the current position to wherever we find the first token at
// 0 says start at the beginning of the string
size_t curr_pos = src_string.find(token, 0);
// if we've reached the end of the string, and no token was found
// add the entire string as one giant chunk
if(curr_pos == string::npos)
{
dest_vector.push_back(src_string);
return dest_vector;
}
// else, if the string isn't blank, push the substring into the vector
if("" != src_string.substr(prev_pos, curr_pos-prev_pos))
dest_vector.push_back(src_string.substr(prev_pos, curr_pos-prev_pos));
// keep doing it until we hit beyond the end of the source string
while(curr_pos!=string::npos)
{
prev_pos = curr_pos+token.length();
curr_pos = src_string.find(token, prev_pos);
if("" != src_string.substr(prev_pos, curr_pos-prev_pos))
dest_vector.push_back(src_string.substr(prev_pos, curr_pos-prev_pos));
}
return dest_vector;
}
bool string_utilities::is_short_signed_int(const string &src_string)
{
if(src_string == "" || src_string.size() > 5)
return false;
string temp = lower_string(src_string);
for(size_t i = 0; i < temp.size(); i++)
{
if(temp[i] == '-' && i != 0)
return false;
if(!isdigit(temp[i]) && temp[i] != '-')
return false;
}
return true;
}
bool string_utilities::is_unsigned_int(const string &src_string)
{
if(src_string == "" || src_string.length() > 10)
return false;
string temp = lower_string(src_string);
for(size_t i = 0; i < temp.size(); i++)
{
if(!isdigit(temp[i]))
return false;
}
double num = atof(temp.c_str());
if(num > 4294967295.0)
return false;
return true;
}
bool string_utilities::is_real_number(const string &src_string)
{
//ie:
//1
//-23e4
//1.E2
//-2.717
//.31415e1
//-7.53e-9
//7.53e+9
if(src_string == "")
return false;
string temp = lower_string(src_string);
bool found_dot = false;
bool found_e = false;
bool found_digit = false;
for(size_t i = 0; i < temp.size(); i++)
{
if(isdigit(temp[i]))
{
if(found_digit == false)
found_digit = true;
}
else if(temp[i] == 'e')
{
if(found_e == true || found_digit == false || i == temp.size() - 1)
return false;
else
found_e = true;
}
else if(temp[i] == '-' || temp[i] == '+')
{
if(!(i == 0 || temp[i-1] == 'e') || i == temp.size() - 1)
return false;
}
else if(temp[i] == '.')
{
if(found_dot == true || (i != 0 && temp[i-1] == 'e'))
return false;
else
found_dot = true;
}
else
{
return false;
}
}
return true;
}