OpenASIP  2.0
StringTools.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2019 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file StringTools.cc
26  *
27  * Definitions.
28  *
29  * @author Pekka Jääskeläinen 2004 (pjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 #include <cctype>
35 #include <algorithm>
36 #include <sstream>
37 
38 #include "StringTools.hh"
39 
40 
41 using std::string;
42 using std::vector;
43 using std::isspace;
44 using std::copy;
45 using std::istringstream;
46 
47 
48 /**
49  * Removes leading and trailing whitespace from the string.
50  *
51  * @return The trimmed string.
52  * @param source The string to trim.
53  */
54 std::string
55 StringTools::trim(const std::string& source) {
56 
57  string result = "";
58  int i = 0;
59 
60  // remove leading white space
61  while (i < static_cast<int>(source.size()) && isspace(source[i])) {
62  ++i;
63  }
64  result = source.substr(i);
65 
66  i = result.size() - 1;
67  while (i >= 0 && isspace(result[i])) {
68  --i;
69  }
70  result = result.substr(0, i+1);
71 
72  return result;
73 }
74 
75 
76 /**
77  * Converts string to char*.
78  *
79  * @param source The string to be converted.
80  * @return The string as a char*.
81  */
82 char*
83 StringTools::stringToCharPtr(const std::string& source) {
84  char* ch = new char[source.size() + 1];
85  copy(source.begin(), source.end(), ch);
86  ch[source.size()] = 0;
87  return ch;
88 }
89 
90 
91 /**
92  * Checks whether a string contains a char or not.
93  *
94  * @param source The investigated string.
95  * @param ch Character which is checked whether it is in a string or not.
96  * @param caseSensitive Flag indicating whether checking is case sensitive
97  * or not.
98  * @return True, if source string contains ch, false otherwise.
99  */
100 bool
102  const std::string& source,
103  char ch,
104  bool caseSensitive) {
105 
106  string::size_type pos = 0;
107  if (!caseSensitive) {
108  char upC = toupper(ch);
109  string upString = StringTools::stringToUpper(source);
110  pos = upString.find(upC, 0);
111  } else {
112  pos = source.find(ch, 0);
113  }
114 
115  return pos != string::npos;
116 }
117 
118 /**
119  * Checks whether a string ends with the given search string.
120  *
121  * @param source The investigated string.
122  * @param searchString The string to search from the end.
123  * @return True, if source ends with searchString.
124  */
125 bool
127  const std::string& source,
128  const std::string& searchString) {
129 
130  return source.size() >= searchString.size() &&
131  source.substr(
132  source.size() - searchString.size(), searchString.size()) ==
133  searchString;
134 }
135 
136 /**
137  * Converts a string to upper case letters.
138  *
139  * @param source String to be converted.
140  * @return String as upper case letters.
141  */
142 string
143 StringTools::stringToUpper(const std::string& source) {
144  string upString;
145  upString.reserve(source.length());
146  for (unsigned int i = 0; i < source.length(); ++i) {
147  upString.push_back(toupper(source[i]));
148  }
149  return upString;
150 }
151 
152 
153 /**
154  * Converts a string to lower case letters.
155  *
156  * @param source String to be converted.
157  * @return String as lower case letters.
158  */
159 string
160 StringTools::stringToLower(const std::string& source) {
161 
162  string lowString;
163  lowString.reserve(source.length());
164  for (unsigned int i = 0; i < source.length(); ++i) {
165  lowString.push_back(tolower(source[i]));
166  }
167  return lowString;
168 }
169 
170 
171 /**
172  * Chops string using a given delimiter.
173  *
174  * Result is returned as a vector. Extra blanks are ignored.
175  *
176  * @param source String to be chopped.
177  * @param delimiter Delimiter used.
178  * @return A vector that contains chopped strings.
179  */
180 vector<TCEString>
182  const std::string& source,
183  const std::string& delimiter) {
184 
185  string line = trim(source);
186  vector<TCEString> results;
187  while (line.length() > 0) {
188  string::size_type location = line.find(delimiter);
189  if (location == string::npos) {
190  results.push_back(line);
191  line = "";
192  } else {
193  results.push_back(line.substr(0, location));
194  line.replace(0, location + 1, "");
195  line = trim(line);
196  }
197  }
198  return results;
199 }
200 
201 
202 /**
203  * Chops string using a given delimiter.
204  *
205  * Result is returned as a vector. Extra blanks are ignored.
206  *
207  * @param source String to be chopped.
208  * @param delimiter Delimiter used.
209  * @param store String vector reference where result strings are stored.
210  */
211 void
213  const std::string& source,
214  const std::string& delimiter,
215  std::vector<std::string>& results) {
216 
217  string line = trim(source);
218  while (line.length() > 0) {
219  string::size_type location = line.find(delimiter);
220  if (location == string::npos) {
221  results.push_back(line);
222  line = "";
223  } else {
224  results.push_back(line.substr(0, location));
225  line.replace(0, location + 1, "");
226  line = trim(line);
227  }
228  }
229 }
230 
231 
232 /**
233  * Returns true if two strings are equal if the case is not taken into account.
234  *
235  * @param a A string.
236  * @param b Another string.
237  * @return True if strings equal case-insensitively.
238  */
239 bool
240 StringTools::ciEqual(const std::string& a, const std::string& b) {
241  return (stringToLower(a) == stringToLower(b));
242 }
243 
244 
245 /**
246  * Splits a string into rows.
247  *
248  * The original string is split in to rows containing a maximum of 'rowLength'
249  * characters. After 'rowLength' characters a new-line character is inserted
250  * to the string to be returned. If the given string has less than 'rowLength'
251  * characters, the string is returned as it was. Also, the "last row" may have
252  * less than 'rowLength' characters.
253  *
254  * If 'rowLength' is 0, the original string is returned.
255  *
256  * @param original The string to be split into rows.
257  * @param rowLength The length of a row in the returned string.
258  * @return A string that has been split into rows containing a maximum of
259  * 'rowLength' characters.
260  */
261 std::string
263  const std::string& original,
264  const unsigned int rowLength) {
265 
266  if (rowLength == 0) {
267  return original;
268  } else {
269 
270  unsigned int counter = 0;
271  string newString = "";
272 
273  while (counter < original.size()) {
274  newString += original.substr(counter, 1);
275  counter++;
276 
277  if (counter % rowLength == 0 && counter != original.size()) {
278  newString += "\n";
279  }
280  }
281 
282  return newString;
283  }
284 }
285 
286 /**
287  * Replaces all occurrences of 'occurrence' in 'source' with 'newString'
288  *
289  * @param source The source string to be modified
290  * @param occurrence The string occurrences to be replaced
291  * @param newString The string that's replaced over the found occurrences
292  * @return A string containing the modifications
293  */
294 std::string
296  const std::string& source,
297  const std::string& occurrence,
298  const std::string& newString) {
299 
300  std::string modifiedString(source);
301  std::string::size_type location = modifiedString.find(occurrence);
302  while (location != std::string::npos) {
303  modifiedString.replace(modifiedString.begin() + location,
304  modifiedString.begin() + location + occurrence.length(),
305  newString.c_str());
306  location = modifiedString.find(occurrence);
307  }
308 
309  return modifiedString;
310 }
311 
312 /**
313  * Returns a (vhdl) indention string
314  *
315  * @param level Indention level (one level is 2 spaces)
316  * @return Indention string
317  */
318 std::string
320  string indent;
321  for (int i = 0; i < level; i++) {
322  indent += " ";
323  }
324  return indent;
325 }
326 
327 
StringTools::endsWith
static bool endsWith(const std::string &source, const std::string &searchString)
Definition: StringTools.cc:126
StringTools::containsChar
static bool containsChar(const std::string &source, char ch, bool caseSensitive=true)
Definition: StringTools.cc:101
StringTools::stringToCharPtr
static char * stringToCharPtr(const std::string &source)
Definition: StringTools.cc:83
StringTools::indent
static std::string indent(int level)
Definition: StringTools.cc:319
StringTools::replaceAllOccurrences
static std::string replaceAllOccurrences(const std::string &source, const std::string &occurrence, const std::string &newString)
Definition: StringTools.cc:295
StringTools::stringToUpper
static std::string stringToUpper(const std::string &source)
Definition: StringTools.cc:143
StringTools.hh
StringTools::ciEqual
static bool ciEqual(const std::string &a, const std::string &b)
Definition: StringTools.cc:240
StringTools::trim
static std::string trim(const std::string &source)
Definition: StringTools.cc:55
StringTools::chopString
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)
Definition: StringTools.cc:181
StringTools::stringToLower
static std::string stringToLower(const std::string &source)
Definition: StringTools.cc:160
StringTools::splitToRows
static std::string splitToRows(const std::string &original, const unsigned int rowLength)
Definition: StringTools.cc:262