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 <cstdio>
42 #include <string>
43 #include <cassert>
44 
45 #include "hash_function.h"
46 #include "common/ug_config.h"
47 #include "stringify.h"
48 
49 namespace ug{
50 
65 UG_API void TokenizeString( const std::string& str, std::vector<std::string>& vToken,
66  const char delimiter=',' );
67 
75 UG_API std::vector<std::string> TokenizeString( const std::string& str,
76  const char delimiter=',' );
77 
85 UG_API std::vector<std::string> TokenizeString( const char* str,
86  const char delimiter=',' );
87 
96 UG_API void TokenizeTrimString(const std::string& str, std::vector<std::string>& vToken,
97  const char delimiter=',');
98 
106 UG_API std::vector<std::string> TokenizeTrimString( const std::string& str,
107  const char delimiter=',' );
108 
113 UG_API void RemoveWhitespaceFromString(std::string& string);
114 
120 UG_API std::string TrimString(const std::string& str);
121 
130 UG_API std::string SnipString(const std::string& str, size_t totalSize,
131  size_t replaceLast = 0, const char replace = '.');
132 
141 UG_API std::string SnipStringFront(const std::string& str, size_t totalSize,
142  size_t replaceFront = 0, const char replace = '.');
143 
151 UG_API int NumberOfDigits(int n);
152 
164 UG_API void AppendCounterToString( std::string& str, std::string indicator,
165  int counter, int maxCounter=-1 );
166 
175 UG_API std::string AppendSpacesToString(std::string& str, int totalLength);
176 
184 template <> UG_API size_t hash_key(const std::string& str);
185 
192 std::string::size_type GetDirectorySeperatorPos(const std::string &str);
193 
202 UG_API std::string FilenameWithoutPath(const std::string &str);
203 
212 UG_API std::string PathFromFilename(const std::string &str);
213 
222 UG_API std::string FilenameWithoutExtension(std::string str);
223 
232 UG_API std::string FilenameAndPathWithoutExtension(std::string str);
233 
241 UG_API std::string GetFilenameExtension(const std::string &str);
242 
253 UG_API std::string ReplaceAll( std::string target, const std::string& oldstr,
254  const std::string& newstr );
255 
263 UG_API bool StartsWith(const std::string& str, const std::string& search);
264 
272 UG_API bool Contains(const std::string& str, const std::string& search);
273 
281 UG_API std::string ToLower(std::string str);
282 
290 UG_API std::string ToUpper(std::string str);
291 
299 UG_API std::vector<std::string> FindDuplicates(const std::vector<std::string>& vec);
300 
307 UG_API std::string repeat(char c, int nr);
308 
320 UG_API size_t LevenshteinDistance( const std::string& s1, const std::string& s2 );
321 
322 
332 UG_API std::string GetFileLines( const char *filename, size_t fromline, size_t toline,
333  bool includeLineNumbers );
334 
341 UG_API std::string GetFileLine(const char *filename, size_t line);
342 
352 UG_API bool IsLonger(const std::string &a, const std::string &b);
353 
354 
361 template<typename T>
362 inline std::string ToString(const T &t)
363 {
364  std::stringstream out;
365  out << t;
366  return out.str();
367 }
368 
376 UG_API std::string XMLStringEscape(std::string s);
377 
384 UG_API bool WildcardMatch(const char *str, const char *pattern);
385 
396 UG_API std::string XMLStringEscape(std::string s);
397 
398 // end group ugbase_common_util_strings
400 
420 UG_API std::string ConfigShift(std::string s);
421 
422 template<typename T>
423 inline std::string OstreamShift(const T &t)
424 {
425  std::stringstream ss; ss << t;
426  return ConfigShift(ss.str());
427 }
428 
435 std::string GetBytesSizeString(size_t s, int length=0);
436 
437 inline const char *TrueFalseString(bool b)
438 {
439  return b ? "TRUE" : "FALSE";
440 }
441 
442 inline const char *OnOffString(bool b)
443 {
444  return b ? "ON" : "OFF";
445 }
446 
447 
448 template< typename... Args >
449 inline std::string GetStringPrintf( const char* format, Args... args )
450 {
451  int length = std::snprintf( nullptr, 0, format, args... );
452  assert( length >= 0 );
453 
454  char* buf = new char[length + 1];
455  std::snprintf( buf, length + 1, format, args... );
456 
457  std::string str( buf );
458  delete[] buf;
459  return str;
460 }
461 
462 } // end namespace ug
463 
464 #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:362
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:437
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:442
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:423
std::string GetStringPrintf(const char *format, Args... args)
Definition: string_util.h:449
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