ug4
string_util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Andreas Vogel
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__COMMON_STRING_UTIL__
34 #define __H__COMMON_STRING_UTIL__
35 
36 #include <string>
37 #include <vector>
38 #include <algorithm>
39 #include <sstream>
40 #include <cctype>
41 #include "hash_function.h"
42 #include "common/ug_config.h"
43 #include "stringify.h"
44 
45 namespace ug{
46 
61 UG_API void TokenizeString( const std::string& str, std::vector<std::string>& vToken,
62  const char delimiter=',' );
63 
71 UG_API std::vector<std::string> TokenizeString( const std::string& str,
72  const char delimiter=',' );
73 
81 UG_API std::vector<std::string> TokenizeString( const char* str,
82  const char delimiter=',' );
83 
92 UG_API void TokenizeTrimString(const std::string& str, std::vector<std::string>& vToken,
93  const char delimiter=',');
94 
102 UG_API std::vector<std::string> TokenizeTrimString( const std::string& str,
103  const char delimiter=',' );
104 
109 UG_API void RemoveWhitespaceFromString(std::string& string);
110 
116 UG_API std::string TrimString(const std::string& str);
117 
126 UG_API std::string SnipString(const std::string& str, size_t totalSize,
127  size_t replaceLast = 0, const char replace = '.');
128 
137 UG_API std::string SnipStringFront(const std::string& str, size_t totalSize,
138  size_t replaceFront = 0, const char replace = '.');
139 
147 UG_API int NumberOfDigits(int n);
148 
160 UG_API void AppendCounterToString( std::string& str, std::string indicator,
161  int counter, int maxCounter=-1 );
162 
171 UG_API std::string AppendSpacesToString(std::string& str, int totalLength);
172 
180 template <> UG_API size_t hash_key(const std::string& str);
181 
188 std::string::size_type GetDirectorySeperatorPos(const std::string &str);
189 
198 UG_API std::string FilenameWithoutPath(const std::string &str);
199 
208 UG_API std::string PathFromFilename(const std::string &str);
209 
218 UG_API std::string FilenameWithoutExtension(std::string str);
219 
228 UG_API std::string FilenameAndPathWithoutExtension(std::string str);
229 
237 UG_API std::string GetFilenameExtension(const std::string &str);
238 
249 UG_API std::string ReplaceAll( std::string target, const std::string& oldstr,
250  const std::string& newstr );
251 
259 UG_API bool StartsWith(const std::string& str, const std::string& search);
260 
268 UG_API bool Contains(const std::string& str, const std::string& search);
269 
277 UG_API std::string ToLower(std::string str);
278 
286 UG_API std::string ToUpper(std::string str);
287 
295 UG_API std::vector<std::string> FindDuplicates(const std::vector<std::string>& vec);
296 
303 UG_API std::string repeat(char c, int nr);
304 
316 UG_API size_t LevenshteinDistance( const std::string& s1, const std::string& s2 );
317 
318 
328 UG_API std::string GetFileLines( const char *filename, size_t fromline, size_t toline,
329  bool includeLineNumbers );
330 
337 UG_API std::string GetFileLine(const char *filename, size_t line);
338 
348 UG_API bool IsLonger(const std::string &a, const std::string &b);
349 
350 
357 template<typename T>
358 inline std::string ToString(const T &t)
359 {
360  std::stringstream out;
361  out << t;
362  return out.str();
363 }
364 
372 UG_API std::string XMLStringEscape(std::string s);
373 
380 UG_API bool WildcardMatch(const char *str, const char *pattern);
381 
392 UG_API std::string XMLStringEscape(std::string s);
393 
394 // end group ugbase_common_util_strings
396 
416 UG_API std::string ConfigShift(std::string s);
417 
418 template<typename T>
419 inline std::string OstreamShift(const T &t)
420 {
421  std::stringstream ss; ss << t;
422  return ConfigShift(ss.str());
423 }
424 
431 std::string GetBytesSizeString(size_t s, int length=0);
432 
433 inline const char *TrueFalseString(bool b)
434 {
435  return b ? "TRUE" : "FALSE";
436 }
437 
438 inline const char *OnOffString(bool b)
439 {
440  return b ? "ON" : "OFF";
441 }
442 
443 } // end namespace ug
444 
445 #endif /*__H__COMMON_STRING_UTIL__*/
int NumberOfDigits(int nsigned)
returns the number of digits of an integer (expressed with base 10)
Definition: string_util.cpp:136
std::string ToString(const T &t)
Convert a object supporting 'std::cout << obj' to a string.
Definition: string_util.h:358
void RemoveWhitespaceFromString(std::string &str)
removes all white space from a string, also within the string
Definition: string_util.cpp:50
string GetFileLine(const char *filename, size_t line)
get a specific line of a file
Definition: string_util.cpp:383
bool WildcardMatch(const char *str, const char *pattern)
wildcard matches like bla.* or *.bla or t?st
Definition: string_util.cpp:388
string GetFileLines(const char *filename, size_t fromline, size_t toline, bool includeLineNumbers)
get some specified lines of a file
Definition: string_util.cpp:359
string repeat(char c, int nr)
Builds a string with specified repetitions of given character.
Definition: string_util.cpp:346
size_t hash_key(const TKey &key)
The hashing method can be specialized for different types.
Definition: hash_function.h:50
#define UG_API
Definition: ug_config.h:65
size_t target(SM_edge< typename T::value_type > const &e, ug::BidirectionalMatrix< T > const &m)
Definition: bidirectional_boost.h:100
the ug namespace
bool Contains(const string &str, const string &search)
Definition: string_util.cpp:249
string FilenameAndPathWithoutExtension(string str)
Definition: string_util.cpp:217
string ToLower(string str)
Definition: string_util.cpp:269
string ReplaceAll(string target, const string &oldstr, const string &newstr)
Definition: string_util.cpp:231
string ConfigShift(string s)
Definition: string_util.cpp:457
string FilenameWithoutPath(const string &str)
Definition: string_util.cpp:195
bool IsLonger(const string &a, const string &b)
Definition: string_util.cpp:354
const char * TrueFalseString(bool b)
Definition: string_util.h:433
void TokenizeTrimString(const string &str, vector< string > &vToken, const char delimiter)
Definition: string_util.cpp:83
vector< string > FindDuplicates(const vector< string > &vec)
Definition: string_util.cpp:279
size_t LevenshteinDistance(const string &s1, const string &s2)
Definition: string_util.cpp:311
void TokenizeString(const string &str, vector< string > &vToken, const char delimiter)
Definition: string_util.cpp:56
const char * OnOffString(bool b)
Definition: string_util.h:438
bool StartsWith(const string &str, const string &begin)
Definition: string_util.cpp:245
string TrimString(const string &str)
Definition: string_util.cpp:104
string ToUpper(string str)
Definition: string_util.cpp:274
string::size_type GetDirectorySeperatorPos(const string &str)
Definition: string_util.cpp:183
string PathFromFilename(const string &str)
Definition: string_util.cpp:202
string SnipStringFront(const string &str, size_t totalSize, size_t replaceFirst, const char replace)
Definition: string_util.cpp:124
string GetBytesSizeString(size_t s, int length)
Definition: string_util.cpp:489
std::string OstreamShift(const T &t)
Definition: string_util.h:419
string FilenameWithoutExtension(string str)
Definition: string_util.cpp:209
string AppendSpacesToString(string &str, int totalLength)
Definition: string_util.cpp:175
string GetFilenameExtension(const string &str)
Definition: string_util.cpp:224
void AppendCounterToString(string &str, string indicator, int counter, int maxCounter)
Definition: string_util.cpp:154
string XMLStringEscape(string s)
Definition: string_util.cpp:430
string SnipString(const string &str, size_t totalSize, size_t replaceLast, const char replace)
Definition: string_util.cpp:112