Newer
Older
/*************************************************************************
String.h - description
-------------------
begin : 2004/04/10 16:35
copyright : (C) 2004 by Tomas Oberhuber
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
#include <iostream>
#include <sstream>
#include <vector>
Tomáš Oberhuber
committed
#ifdef HAVE_MPI
#include <mpi.h>
#endif
/**
* \brief Class for managing strings.
*
* The following example shows common use of String.
*
* \par Example
* \include StringExample.cpp
* \par Output
* \include StringExample.out
*
* In addition to methods of this class, check the following related functions:
*
* \ref convertToString
*
* \ref operator+
*
* \ref mpiSend
*
* \ref mpiReceive
public:
/**
* \brief This enum defines how the operation split of string is to be performed.
*/
NoSkip, ///< Do not skip empty characters
SkipEmpty ///< Skip empty characters.
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
};
/**
* \brief Default constructor.
*
* Constructs an empty string object.
*/
String() = default;
/**
* \brief Default copy constructor.
*/
String( const String& ) = default;
/**
* \brief Default move constructor.
*/
String( String&& ) = default;
/**
* \brief Initialization by \e std::string.
*/
String( const std::string& str ) : std::string( str ) {}
/**
* \brief Default copy assignment operator.
*/
String& operator=( const String& ) = default;
/**
* \brief Default move assignment operator.
*/
String& operator=( String&& ) = default;
/**
* \brief Inherited constructors.
*/
using std::string::string;
/**
* \brief Inherited assignment operators.
*/
using std::string::operator=;
/**
* \brief Returns the number of characters in given string. Equivalent to \ref getSize.
*/
int getLength() const;
/**
* \brief Returns the number of characters in given string.
*/
int getSize() const;
/**
* \brief Returns size of allocated storage for given string.
*
* \par Example
* \include StringExampleGetAllocatedSize.cpp
* \par Output
* \include StringExampleGetAllocatedSize.out
*/
int getAllocatedSize() const;
/**
* \brief Reserves space for given \e size.
*
* Requests to allocate storage space of given \e size to avoid memory reallocation.
* It allocates one more byte for the terminating 0.
* @param size Number of characters.
*
* \par Example
* \include StringExampleSetSize.cpp
* \par Output
* \include StringExampleSetSize.out
*/
void setSize( int size );
/**
* \brief Returns pointer to data.
*
* It returns the content of the given string as a constant pointer to char.
*/
const char* getString() const;
/**
* \brief Operator for accessing particular chars of the string.
*
* This function overloads \ref operator[]. It returns a reference to
* the character at position \e i in given string.
* The character can not be changed be user.
*/
const char& operator[]( int i ) const;
/**
* \brief Operator for accessing particular chars of the string.
*
* It returns the character at the position \e i in given string as
* a modifiable reference.
*/
char& operator[]( int i );
/**
* Operators for single characters.
*/
/**
* \brief This function overloads \ref operator+=.
*
* Appends character \e str to this string.
*/
String& operator+=( char str );
/**
* \brief This function concatenates strings and returns a newly constructed string object.
*/
String operator+( char str ) const;
/**
* \brief This function checks whether the given string is equal to \e str.
*
* It returns \e true when the given string is equal to \e str. Otherwise it returns \e false.
*/
bool operator==( char str ) const;
/**
* \brief This function overloads \ref operator!=.
*
* It returns \e true when the given string is NOT equal to \e str. Otherwise it returns \e true.
*/
bool operator!=( char str ) const;
/**
* Operators for C strings.
*/
/**
* \brief This function overloads \ref operator+=.
*
* It appends the C string \e str to this string.
*/
String& operator+=( const char* str );
/**
* \brief This function concatenates C strings \e str and returns a newly constructed string object.
*/
String operator+( const char* str ) const;
/**
* \brief This function overloads \ref operator==.
*
* It returns \e true when the given string is equal to \e str. Otherwise it returns \e false.
*/
bool operator==( const char* str ) const;
/**
* \brief This function overloads \ref operator!=.
*
* It returns \e true when the given string is NOT equal to \e str. Otherwise it returns \e true.
*/
bool operator!=( const char* str ) const;
/**
* Operators for std::string.
*/
/**
* \brief This function overloads \ref operator+=.
*
* It appends the C string \e str to this string.
*/
String& operator+=( const std::string& str );
/**
* \brief This function concatenates C strings \e str and returns a newly constructed string object.
*/
String operator+( const std::string& str ) const;
/**
* \brief This function overloads \ref operator==.
*
* It returns \e true when the given string is equal to \e str. Otherwise it returns \e false.
*/
bool operator==( const std::string& str ) const;
/**
* \brief This function overloads \ref operator!=.
*
* It returns \e true when the given string is NOT equal to \e str. Otherwise it returns \e true.
*/
bool operator!=( const std::string& str ) const;
/**
* Operators for String.
*/
/**
* \brief This function overloads \ref operator+=.
*
* It appends the C string \e str to this string.
*/
String& operator+=( const String& str );
/**
* \brief This function concatenates C strings \e str and returns a newly constructed string object.
*/
String operator+( const String& str ) const;
/**
* \brief This function overloads \ref operator==.
*
* It returns \e true when the given string is equal to \e str. Otherwise it returns \e false.
*/
bool operator==( const String& str ) const;
/**
* \brief This function overloads \ref operator!=.
*
* It returns \e true when the given string is NOT equal to \e str. Otherwise it returns \e true.
*/
bool operator!=( const String& str ) const;
/**
* \brief Cast to bool operator.
*
* This operator converts string to boolean expression (true or false).
* It returns \e true if the string is NOT empty. Otherwise it returns \e false.
*/
operator bool() const;
/** \brief Cast to bool with negation operator.
*
* This operator converts string to boolean expression (false or true).
* It returns \e true if the string is empty. Otherwise it returns \e false.
*/
bool operator!() const;
/**
* \brief This method replaces part of the string.
*
* It replaces \e pattern in this string with a string \e replaceWith.
* If parameter \e count is defined, the function makes replacement only count occurrences,
* of the given pattern. If \e count is zero, all pattern occurrences are replaced.
*
* @param pattern to be replaced.
* @param replaceWith string the \e pattern will be replaced with.
* @param count number of occurrences to be replaced. All occurrences are replaced if \e count is zero..
*
* \par Example
* \include StringExampleReplace.cpp
* \par Output
* \include StringExampleReplace.out
*/
String replace( const String& pattern,
const String& replaceWith,
int count = 0 ) const;
/**
* \brief Trims/strips this string.
*
* Removes all 'spaces' from given string except for single 'spaces' between words.
*
* @param strip can be used to change the character to be removed.
*
* \par Example
* \include StringExampleStrip.cpp
* \par Output
* \include StringExampleStrip.out
*/
String strip( char strip = ' ' ) const;
/**
* \brief Splits string into list of strings with respect to given \e separator.
*
* This method splits the string into sequence of substrings divided by occurrences of \e separator.
* It returns the list of those strings via std::vector. When \e separator does not appear
* anywhere in the given string, this function returns a single-element list
* containing given sting. If \e skipEmpty equals \e SkipEmpty no empty substrings are
* inserted into the resulting container.
*
* @param separator is a character separating substrings in given string.
* @param skipEmpty
*
* \par Example
* \include StringExampleSplit.cpp
* \par Output
* \include StringExampleSplit.out
*/
std::vector< String > split( const char separator = ' ', SplitSkip skipEmpty = SplitSkip::NoSkip ) const;
/**
* \brief Checks if the string starts with given prefix.
*/
bool startsWith( const String& prefix ) const;
/**
* \brief Checks if the string ends with given suffix.
*/
bool endsWith( const String& suffix ) const;
* \brief Returns concatenation of \e string1 and \e string2.
String operator+( char string1, const String& string2 );
* \brief Returns concatenation of \e string1 and \e string2.
String operator+( const char* string1, const String& string2 );
* \brief Returns concatenation of \e string1 and \e string2.
String operator+( const std::string& string1, const String& string2 );
/**
* \brief Converts \e value of type \e T to a String.
*
* \tparam T can be any type fir which operator << is defined.
*/
template< typename T >
String convertToString( const T& value )
{
std::stringstream str;
str << value;
return String( str.str().data() );
* \brief Specialization of function \ref convertToString for boolean.
* The boolean type is converted to 'true' or 'false'.
template<> inline String convertToString( const bool& b )
{
if( b ) return "true";
return "false";
}
#ifdef HAVE_MPI
/**
* \brief Sends the string to the target MPI process.
*
* @param str string to be sent
* @param target target MPI process ID
* @param tag MPI tag
* @param mpi_comm MPI communication group
*/
void mpiSend( const String& str, int target, int tag = 0, MPI_Comm mpi_comm = MPI_COMM_WORLD );
/**
* \brief Receives a string from the target MPI process.
*
* @param str says where the received string is to be saved to
* @param source source MPI process ID
* @param tag MPI tag
* @param mpi_comm MPI communication group
*/
void mpiReceive( String& str, int source, int tag = 0, MPI_Comm mpi_comm = MPI_COMM_WORLD );
//! Broadcast to other nodes in MPI cluster
// void MPIBcast( String& str, int root, MPI_Comm mpi_comm = MPI_COMM_WORLD );
#endif