-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringHelper.cpp
132 lines (114 loc) · 2.55 KB
/
StringHelper.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
#include "Global.h"
#include "StringHelper.h"
string join(vector<string> strings, string delimiter)
{
stringstream result;
for (auto str = strings.begin(); str != strings.end(); ) {
result << *str;
if (++str != strings.end()) result << '.';
}
return result.str();
}
string join(string delimiter, int n, ...)
{
va_list args;
va_start(args, n);
vector<string> s;
for(int i = 0; i < n; i++)
{
s.push_back(va_arg(args, string));
}
va_end(args);
return join(s, delimiter);
}
const char *join (vector<const char *> strings, const char delimiter)
{
int len = 0;
for (auto str = strings.begin(); str != strings.end(); ++str) {
len += strlen (*str) + 1;
}
if (!len) return NULL;
auto result = new char[len];
auto ptr = result;
for (auto str = strings.begin(); str != strings.end(); ++str) {
strcpy (ptr, *str);
ptr += strlen (*str);
*ptr++ = delimiter;
}
*--ptr = '\0';
return result;
}
const char *join (vector<const char *> strings, const char *delimiter)
{
int len = 0;
int delimiter_len = strlen (delimiter);
for (auto str = strings.begin(); str != strings.end(); ++str) {
len += strlen (*str) + delimiter_len;
}
if (!len) return NULL;
len = len - delimiter_len + 1;
auto result = new char[len];
auto ptr = result;
for (auto str = strings.begin(); str != strings.end(); ++str) {
strcpy (ptr, *str);
ptr += strlen (*str);
strcpy (ptr, delimiter);
ptr += delimiter_len;
}
*(ptr-delimiter_len) = '\0';
return result;
}
bool endsWith(string const &fullString, string const &ending)
{
if (fullString.length() < ending.length()) return false;
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
}
// Returns pointer after last slash
char *SkipFolders(char *ptr)
{
char *name_start = ptr;
while(*ptr) {
if(*ptr == '\\' || *ptr == '/') {
name_start = ptr + 1;
}
ptr++;
}
return name_start;
}
// Returns pointer after last dot
char *SkipFileName(char *ptr)
{
char *ext_start = ptr;
while(*ptr) {
if(*ptr == '.') {
ext_start = ptr + 1;
}
ptr++;
}
return ext_start;
}
char *SkipWhiteSpace(char *ptr)
{
while(*ptr == ' ' || *ptr == '\t') ptr++;
return ptr;
}
char *SkipLine(char *ptr)
{
while(*ptr != '\0' && *ptr != '\n') ptr++;
return ptr;
}
char *SkipWord(char *ptr)
{
while(*ptr != '\0' && (*ptr == '_' || isalnum(*ptr))) ptr++;
return ptr;
}
char *SkipLineTillComment(char *ptr)
{
while(*ptr != '\0' && *ptr != '\n' && *ptr != ';') ptr++;
return ptr;
}
char *SkipEqu(char *ptr)
{
while(*ptr != '\0' && (*ptr == '=' || isalnum(*ptr))) ptr++;
return ptr;
}