forked from ahmidou/SpliceAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtilityImpl.cpp
161 lines (140 loc) · 3.75 KB
/
StringUtilityImpl.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
// Copyright 2010-2013 Fabric Engine Inc. All rights reserved.
#include "StringUtilityImpl.h"
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
using namespace FabricSpliceImpl;
std::string StringUtilityImpl::getUniqueString(const std::string & name, const stringMap & names)
{
std::string key = name;
char buffer[10];
int suffixIndex = 1;
while(names.find(key) != names.end())
{
sprintf(buffer, "%d", suffixIndex);
suffixIndex++;
key = name + buffer;
}
return key;
}
stringVector StringUtilityImpl::splitString(const std::string & name, char delimiter, bool stripSpaces)
{
std::stringstream ss(name);
std::string item;
stringVector result;
while(std::getline(ss, item, delimiter))
{
if(stripSpaces)
result.push_back(stripString(item, ' '));
else
result.push_back(item);
}
return result;
}
stringVector StringUtilityImpl::partitionString(const std::string & name, char delimiter)
{
stringVector result;
size_t pos = name.find(delimiter);
if(pos == std::string::npos)
{
result.push_back(name);
result.push_back("");
result.push_back("");
}
else
{
result.push_back(name.substr(0, pos));
result.push_back(std::string()+delimiter);
result.push_back(name.substr(pos + 1, name.length() - pos));
}
return result;
}
std::string StringUtilityImpl::stripString(const std::string & name, char charToRemove)
{
if(name.length() == 0)
return name;
size_t start = 0;
size_t end = name.length()-1;
while(name[start] == charToRemove && start != end)
start++;
while(name[end] == charToRemove && start != end)
end--;
return name.substr(start, end - start + 1);
}
std::string StringUtilityImpl::replaceString(const std::string & name, char search, char replacement)
{
std::string result = name;
for(size_t i=0;i<result.length();i++)
{
if(result[i] == search)
result[i] = replacement;
}
return result;
}
std::string StringUtilityImpl::removeCharFromString(const std::string & name, char search)
{
std::string result;
for(size_t i=0;i<name.length();i++)
{
if(name[i] == search)
continue;
result += name[i];
}
return result;
}
std::string StringUtilityImpl::removeDoubleSpacesFromString(const std::string & name)
{
std::string result;
for(size_t i=0;i<name.length();i++)
{
if(result.length() > 0)
{
if(name[i] == ' ' && result[result.length()-1] == ' ')
continue;
}
result += name[i];
}
return result;
}
bool StringUtilityImpl::startsWith(const std::string & name, const std::string & start)
{
if(name.length() < start.length() || start.length() == 0)
return false;
if(name == start)
return true;
return name.substr(0, start.length()) == start;
}
bool StringUtilityImpl::endsWith(const std::string & name, const std::string & end)
{
if(name.length() < end.length() || end.length() == 0)
return false;
if(name == end)
return true;
return name.substr(name.length()-end.length(), end.length()) == end;
}
std::string StringUtilityImpl::truncateLeft(const std::string & name, unsigned int charsToRemove)
{
if(charsToRemove > name.length())
return "";
return name.substr(charsToRemove, name.length() - charsToRemove);
}
std::string StringUtilityImpl::truncateRight(const std::string & name, unsigned int charsToRemove)
{
if(charsToRemove > name.length())
return "";
return name.substr(0, name.length() - charsToRemove);
}
int StringUtilityImpl::findString(const std::string & name, const std::string & search)
{
if(name.length() < search.length())
return -1;
if(name == search)
return 0;
for(size_t i=0;i<1 + name.length() - search.length();i++)
{
std::string segment = name.substr(i, search.length());
if(segment == search)
return i;
}
return -1;
}