OpenASIP 2.2
Loading...
Searching...
No Matches
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
40TCEString::TCEString() : std::string() {
41}
42
43TCEString::TCEString(const char* text) : std::string(text) {
44}
45
46TCEString::TCEString(const std::string& text) : std::string(text) {
47}
48
49TCEString::TCEString(const char c) : std::string(std::string("") + c) {
50}
51
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 */
62bool
63TCEString::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 */
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 */
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
113std::vector<TCEString>
114TCEString::split(const std::string& delim) const {
115 return StringTools::chopString(*this, delim);
116}
117
119TCEString::appendIf(bool expression, stringCRef ifTrue) {
120 if (expression) {
121 append(ifTrue);
122 }
123 return *this;
124}
125
126// stream operators for easier string construction
129 *this += rhs;
130 return *this;
131}
132
134TCEString::operator<<(const char* rhs) {
135 *this += TCEString(rhs);
136 return *this;
137}
138
140TCEString::operator<<(const int rhs) {
141 *this += Conversion::toString(rhs);
142 return *this;
143}
144
146TCEString::operator+(int val) const {
147 return TCEString(*this) += Conversion::toString(val);
148}
149
151TCEString::operator+(char c) const {
152 return *this + TCEString(c);
153}
154
156TCEString::toUpper(const TCEString& str, const std::locale& loc) {
157 TCEString newStr(str);
158 return toUpper(newStr, loc);
159}
160
161std::string
162TCEString::toUpper(const std::string& str, const std::locale& loc) {
163 std::string newStr(str);
164 return toUpper(newStr, loc);
165}
166
168TCEString::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
175std::string&
176TCEString::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
183std::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 */
200std::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 */
218std::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 */
228std::string
229TCEString::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 */
240std::string
241TCEString::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 */
250unsigned
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 */
277std::string
278TCEString::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
287std::string
289 std::stringstream ss;
290 ss << "0x" << std::hex << std::uppercase << num;
291 return ss.str();
292}
293
294std::string
296 std::stringstream ss;
297 ss << "0x" << std::hex << std::uppercase << num;
298 return ss.str();
299}
300
301
302/**
303 * Implementation of lhs < rhs string comparison case insensitively.
304 *
305 * @param lhs The left side string.
306 * @param rhs Thr rigth side string.
307 * @return Boolean result from comparison.
308 */
309bool
311 const TCEString& lhs, const TCEString& rhs) const {
312 return lhs.lower() < rhs.lower();
313}
314
315
const std::string & stringCRef
Definition TCEString.hh:45
static std::string toString(const T &source)
static std::string stringToUpper(const std::string &source)
static std::string stringToLower(const std::string &source)
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)
TCEString operator+(int val) const
Definition TCEString.cc:146
static unsigned replace(std::string &str, const std::string &oldPattern, const std::string &newPattern)
Definition TCEString.cc:251
TCEString lower() const
Definition TCEString.cc:78
static TCEString toUpper(const TCEString &str, const std::locale &loc=std::locale())
Definition TCEString.cc:156
TCEString upper() const
Definition TCEString.cc:86
TCEString capitalize() const
Definition TCEString.cc:106
static std::string filterDigits(const std::string &str)
Definition TCEString.cc:278
bool ciEqual(const TCEString &other) const
Definition TCEString.cc:63
static std::string & appendToNonEmpty(std::string &toAppend, stringCRef appender)
Definition TCEString.cc:201
TCEString & replaceString(const std::string &old, const std::string &newString)
Definition TCEString.cc:94
TCEString & operator<<(const TCEString &rhs)
Definition TCEString.cc:128
TCEString & appendIf(bool expression, stringCRef ifTrue)
Definition TCEString.cc:119
std::vector< TCEString > split(const std::string &delim) const
Definition TCEString.cc:114
static std::string intToHexString(int num)
Definition TCEString.cc:288
virtual ~TCEString()
Definition TCEString.cc:52
static std::string unsignedToHexString(unsigned num)
Definition TCEString.cc:295
static std::string toLower(const std::string &str, const std::locale &loc=std::locale())
Definition TCEString.cc:184
static std::string applyIf(bool expression, stringCRef ifTrue, stringCRef ifFalse)
Definition TCEString.cc:229
bool operator()(const TCEString &lhs, const TCEString &rhs) const
Definition TCEString.cc:310