OpenASIP  2.0
TCEString.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 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 TCEString.cc
26  *
27  * Definition of TCEString class.
28  *
29  * @author Viljami Korhonen 2008 (viljami.korhonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 #include <string>
33 #include <sstream>
34 #include <algorithm>
35 #include <cctype>
36 
37 #include "TCEString.hh"
38 #include "Conversion.hh"
39 
40 TCEString::TCEString() : std::string() {
41 }
42 
43 TCEString::TCEString(const char* text) : std::string(text) {
44 }
45 
46 TCEString::TCEString(const std::string& text) : std::string(text) {
47 }
48 
49 TCEString::TCEString(const char c) : std::string(std::string("") + c) {
50 }
51 
53 }
54 
55 /**
56  * Returns true if two strings are equal if the case is not taken into account.
57  *
58  * @param a A string.
59  * @param b Another string.
60  * @return True if strings equal case-insensitively.
61  */
62 bool
63 TCEString::ciEqual(const TCEString& other) const {
64  unsigned int len = length();
65  if (len != other.length()) return false;
66  for (unsigned int i = 0; i < len; i++) {
67  if (tolower((*this)[i]) != tolower(other[i])) {
68  return false;
69  }
70  }
71  return true;
72 }
73 
74 /**
75  * Turns the string to lowercase.
76  */
79  return StringTools::stringToLower(*this);
80 }
81 
82 /**
83  * Turns the string to uppercase.
84  */
87  return StringTools::stringToUpper(*this);
88 }
89 
90 /**
91  * Replaces all occurrences of string 'old' with 'newString'
92  */
93 TCEString&
95  const std::string& old, const std::string& newString) {
96 
97  TCEString::replace(*this, old, newString);
98  return *this;
99 }
100 
101 /**
102  * Return a copy of the string with its first character in upper case and
103  * the rest in lower case.
104  */
105 TCEString
107  if (size() == 0) return "";
108  if (size() == 1) return upper();
109  return StringTools::stringToUpper(substr(0, 1)) +
110  StringTools::stringToLower(substr(1, size() - 1));
111 }
112 
113 std::vector<TCEString>
114 TCEString::split(const std::string& delim) const {
115  return StringTools::chopString(*this, delim);
116 }
117 
118 TCEString&
119 TCEString::appendIf(bool expression, stringCRef ifTrue) {
120  if (expression) {
121  append(ifTrue);
122  }
123  return *this;
124 }
125 
126 // stream operators for easier string construction
127 TCEString&
129  *this += rhs;
130  return *this;
131 }
132 
133 TCEString&
134 TCEString::operator<<(const char* rhs) {
135  *this += TCEString(rhs);
136  return *this;
137 }
138 
139 TCEString&
140 TCEString::operator<<(const int rhs) {
141  *this += Conversion::toString(rhs);
142  return *this;
143 }
144 
145 TCEString
146 TCEString::operator+(int val) const {
147  return TCEString(*this) += Conversion::toString(val);
148 }
149 
150 TCEString
151 TCEString::operator+(char c) const {
152  return *this + TCEString(c);
153 }
154 
155 TCEString
156 TCEString::toUpper(const TCEString& str, const std::locale& loc) {
157  TCEString newStr(str);
158  return toUpper(newStr, loc);
159 }
160 
161 std::string
162 TCEString::toUpper(const std::string& str, const std::locale& loc) {
163  std::string newStr(str);
164  return toUpper(newStr, loc);
165 }
166 
167 TCEString&
168 TCEString::toUpper(TCEString& str, const std::locale& loc) {
169  for (size_t i = 0; i < str.size(); i++) {
170  str.at(i) = std::toupper(str.at(i), loc);
171  }
172  return str;
173 }
174 
175 std::string&
176 TCEString::toUpper(std::string& str, const std::locale& loc) {
177  for (size_t i = 0; i < str.size(); i++) {
178  str.at(i) = std::toupper(str.at(i), loc);
179  }
180  return str;
181 }
182 
183 std::string
185  const std::string& str, const std::locale& loc) {
186  std::string result(str);
187  for (size_t i = 0; i < result.size(); i++) {
188  result.at(i) = std::tolower(result.at(i), loc);
189  }
190  return result;
191 }
192 
193 /**
194  * Appends a string if the target string is not empty.
195  *
196  * @param toAppend The target string.
197  * @param appender The string to append with.
198  * @return Reference to original possibly appended with appender.
199  */
200 std::string&
202  std::string& toAppend, stringCRef appender) {
203  if (!toAppend.empty()) {
204  return toAppend += appender;
205  } else {
206  return toAppend;
207  }
208 }
209 
210 /**
211  * Returns string of target string appended with another string if the target
212  * string is not empty.
213  *
214  * @param toAppend The target string.
215  * @param appender The string to append with.
216  * @return String initialized with toAppend and possibly appended with appender.
217  */
218 std::string
220  stringCRef toAppend, stringCRef appender) {
221  std::string tmp(toAppend);
222  return appendToNonEmpty(tmp, appender);
223 }
224 
225 /**
226  * Returns first string if "expression" is true. Otherwise return second.
227  */
228 std::string
229 TCEString::applyIf(bool expression, stringCRef ifTrue, stringCRef ifFalse) {
230  if (expression) {
231  return ifTrue;
232  } else {
233  return ifFalse;
234  }
235 }
236 
237 /**
238  * Returns string if "expression" is true. Otherwise return empty string.
239  */
240 std::string
241 TCEString::applyIf(bool expression, stringCRef ifTrue) {
242  return applyIf(expression, ifTrue, "");
243 }
244 
245 /**
246  * Replaces all occurrences of oldPattern in str with newPattern.
247  *
248  * @return Number of made replacements.
249  */
250 unsigned
252  std::string& str,
253  const std::string& oldPattern,
254  const std::string& newPattern) {
255 
256  unsigned replacementCount = 0;
257 
258  std::string::size_type location = str.find(oldPattern);
259  while (location != std::string::npos) {
260  str.replace(
261  str.begin() + location,
262  str.begin() + location + oldPattern.length(),
263  newPattern.c_str());
264  replacementCount++;
265  // Avoid infinite recursion if replacing with a string
266  // that also contains the searched string. This happens
267  // when escaping reserved characters such as '_' -> '\\_'.
268  location = str.find(oldPattern, location + newPattern.length());
269  }
270  return replacementCount;
271 }
272 
273 
274 /**
275  * Returns string in which all non-digit characters have been removed.
276  */
277 std::string
278 TCEString::filterDigits(const std::string& str) {
279  std::string result;
280  result.reserve(str.size());
281  for (auto c : str) {
282  if (std::isdigit(c)) result += c;
283  }
284  return result;
285 }
286 
287 
288 /**
289  * Implementation of lhs < rhs string comparison case insensitively.
290  *
291  * @param lhs The left side string.
292  * @param rhs Thr rigth side string.
293  * @return Boolean result from comparison.
294  */
295 bool
297  const TCEString& lhs, const TCEString& rhs) const {
298  return lhs.lower() < rhs.lower();
299 }
300 
TCEString::appendIf
TCEString & appendIf(bool expression, stringCRef ifTrue)
Definition: TCEString.cc:119
TCEString::TCEString
TCEString()
Definition: TCEString.cc:40
TCEString::lower
TCEString lower() const
Definition: TCEString.cc:78
TCEString::split
std::vector< TCEString > split(const std::string &delim) const
Definition: TCEString.cc:114
TCEString::applyIf
static std::string applyIf(bool expression, stringCRef ifTrue, stringCRef ifFalse)
Definition: TCEString.cc:229
TCEString::replace
static unsigned replace(std::string &str, const std::string &oldPattern, const std::string &newPattern)
Definition: TCEString.cc:251
TCEString::capitalize
TCEString capitalize() const
Definition: TCEString.cc:106
TCEString::upper
TCEString upper() const
Definition: TCEString.cc:86
Conversion::toString
static std::string toString(const T &source)
stringCRef
const typedef std::string & stringCRef
Definition: TCEString.hh:42
TCEString.hh
StringTools::stringToUpper
static std::string stringToUpper(const std::string &source)
Definition: StringTools.cc:143
TCEString::ciEqual
bool ciEqual(const TCEString &other) const
Definition: TCEString.cc:63
TCEString::~TCEString
virtual ~TCEString()
Definition: TCEString.cc:52
Conversion.hh
TCEString::filterDigits
static std::string filterDigits(const std::string &str)
Definition: TCEString.cc:278
TCEString::replaceString
TCEString & replaceString(const std::string &old, const std::string &newString)
Definition: TCEString.cc:94
TCEString::ICLess::operator()
bool operator()(const TCEString &lhs, const TCEString &rhs) const
Definition: TCEString.cc:296
TCEString::appendToNonEmpty
static std::string & appendToNonEmpty(std::string &toAppend, stringCRef appender)
Definition: TCEString.cc:201
TCEString
Definition: TCEString.hh:53
StringTools::chopString
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)
Definition: StringTools.cc:181
TCEString::operator<<
TCEString & operator<<(const TCEString &rhs)
Definition: TCEString.cc:128
TCEString::operator+
TCEString operator+(int val) const
Definition: TCEString.cc:146
TCEString::toLower
static std::string toLower(const std::string &str, const std::locale &loc=std::locale())
Definition: TCEString.cc:184
TCEString::toUpper
static TCEString toUpper(const TCEString &str, const std::locale &loc=std::locale())
Definition: TCEString.cc:156
StringTools::stringToLower
static std::string stringToLower(const std::string &source)
Definition: StringTools.cc:160