ffead.server.doc
StringUtil.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * StringUtil.cpp
18  *
19  * Created on: Aug 5, 2012
20  * Author: Sumeet
21  */
22 
23 #include "StringUtil.h"
24 
25 StringUtil::StringUtil() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 StringUtil::~StringUtil() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 void StringUtil::toUpper(string &str)
35 {
36  transform(str.begin(), str.end(), str.begin(), ::toupper);
37 }
38 
39 string StringUtil::toUpperCopy(const string& str)
40 {
41  string strn = str;
42  toUpper(strn);
43  return strn;
44 }
45 
46 void StringUtil::toLower(string& str)
47 {
48  transform(str.begin(), str.end(), str.begin(), ::tolower);
49 }
50 
51 string StringUtil::toLowerCopy(const string& str)
52 {
53  string strn = str;
54  toLower(strn);
55  return strn;
56 }
57 
58 void StringUtil::replaceFirst(string &str, const string& ths, const string& with)
59 {
60  size_t start_pos = str.find(ths);
61  if(start_pos != std::string::npos)
62  {
63  str.replace(start_pos, ths.length(), with);
64  }
65 }
66 
67 string StringUtil::replaceFirstCopy(const string &str, const string& ths, const string& with)
68 {
69  string strn = str;
70  replaceFirst(strn, ths, with);
71  return strn;
72 }
73 
74 void StringUtil::replaceAll(string &str, const string& ths, const string& with)
75 {
76  if(ths.empty())
77  return;
78  size_t start_pos = 0;
79  while((start_pos = str.find(ths, start_pos)) != std::string::npos) {
80  str.replace(start_pos, ths.length(), with);
81  start_pos += with.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
82  }
83 }
84 
85 string StringUtil::replaceAllCopy(const string &str, const string& ths, const string& with)
86 {
87  string strn = str;
88  replaceAll(strn, ths, with);
89  return strn;
90 }
91 
92 void StringUtil::eraseAll(string& str, const string& ths)
93 {
94  if(ths.empty())
95  return;
96  size_t start_pos = 0;
97  while((start_pos = str.find(ths)) != std::string::npos) {
98  str.erase(start_pos, ths.length());
99  }
100 }
101 
102 void StringUtil::capitalized(string& str)
103 {
104  str[0] = toupper(str[0]);
105 }
106 
107 string StringUtil::capitalizedCopy(const string& str)
108 {
109  string strn(str);
110  capitalized(strn);
111  return strn;
112 }
113 
114 void StringUtil::replaceLast(string& str, const string& ths, const string& with)
115 {
116  size_t start_pos = str.find_last_of(ths);
117  if(start_pos != std::string::npos)
118  {
119  str.replace(start_pos, ths.length(), with);
120  }
121 }
122 
123 string StringUtil::replaceLastCopy(const string& str, const string& ths, const string& with)
124 {
125  string strn = str;
126  replaceLast(strn, ths, with);
127  return strn;
128 }
129 
130 vector<string> StringUtil::split(const string& input, const string& delimiter)
131 {
132  vector<string> output;
133  split(output, input, delimiter);
134  return output;
135 
136 }
137 
138 void StringUtil::splitInternal(vector<string> &output, const string& input, const string& delimiter)
139 {
140  size_t start = 0;
141 
142  string temp = input;
143  start = temp.find(delimiter);
144  while(start!=string::npos)
145  {
146  if(start!=0)
147  {
148  output.push_back(temp.substr(0, start));
149  }
150  else
151  {
152  output.push_back("");
153  }
154  if(temp.length()>start+delimiter.length())
155  {
156  temp = temp.substr(start+delimiter.length());
157  start = temp.find(delimiter);
158  }
159  else
160  {
161  temp = temp.substr(start);
162  break;
163  }
164  }
165  replaceFirst(temp, delimiter, "");
166  output.push_back(temp);
167 }
168 
169 void StringUtil::split(vector<string> &output, const string& input, const string& delimiter)
170 {
171  output.clear();
172  splitInternal(output, input, delimiter);
173 }
174 
175 vector<string> StringUtil::split(const string& input, vector<string> delimiters)
176 {
177  vector<string> output;
178  split(output, input, delimiters);
179  return output;
180 }
181 
182 void StringUtil::split(vector<string>& output, const string& input, vector<string> delimiters)
183 {
184  output.clear();
185  output.push_back(input);
186  for (int var = 0; var < (int)delimiters.size(); ++var) {
187  vector<string> output1;
188  for (int var1 = 0; var1 < (int)output.size(); ++var1) {
189  splitInternal(output1, output.at(var1), delimiters.at(var));
190  }
191  output.swap(output1);
192  }
193 }
194 
195 void StringUtil::trim(string& str)
196 {
197  string c = " ";
198  if(str=="")return;
199  size_t p2 = str.find_last_not_of(c);
200  while(str[p2]==' ' || str[p2]=='\t' || str[p2]=='\n' || str[p2]=='\r')
201  {
202  str = str.substr(0, p2);
203  p2 = str.find_last_not_of(str[p2]);
204  }
205  if (p2 == string::npos)
206  p2 = str.length();
207  size_t p1 = str.find_first_not_of(c);
208  while(str[p1]==' ' || str[p1]=='\t' || str[p1]=='\n' || str[p1]=='\r')
209  {
210  str = str.substr(p1);
211  p1 = str.find_first_not_of(str[p1]);
212  }
213  if (p1 == string::npos)
214  p1 = 0;
215  str = str.substr(p1, (p2-p1)+1);
216 }
217 
218 string StringUtil::trimCopy(const string& str)
219 {
220  string strn(str);
221  trim(strn);
222  return strn;
223 }