C++ mpi module for stochmagnet_main Package
functions_string.h
1 #ifndef FUNCTIONS_STRING_H
2 #define FUNCTIONS_STRING_H
3 
4 //types of the code
5 #include "types.h"
6 #include "functions_type.h"
7 
8 //regex header for string manipulation
9 #include <regex>
10 
11 
12 namespace functions_string {
13 
14 
19  inline tString& ltrim(tString& s) {
20  s=std::regex_replace(s, std::regex("^\\s+"), std::string(""));
21  return s;
22  }
27  inline tString& rtrim(tString& s) {
28  s=std::regex_replace(s, std::regex("\\s+$"), std::string(""));
29  return s;
30  }
31 
36  inline tString& trim(tString& s) {
37  return ltrim(rtrim(s));
38  }
39 
46  inline void replaceAll(const tString& word,const tString& rWord,tString& str) {
47  tIndex pos = str.find(word);
48  tIndex lWord=word.length();
49  tIndex lrWord=rWord.length();
50  // Repeat till end is reached
51  while( pos != std::string::npos)
52  {
53  // Replace this occurrence of Sub String
54  str.replace(pos, lWord , rWord);
55  // Get the next occurrence from the current position
56  pos =str.find(word, pos + lrWord);
57  }
58  }
65  inline void replace(const tString& word,const tString& rWord,tString& str) {
66  tIndex pos = str.find(word);
67  tIndex lWord=word.length();
68  // Repeat till end is reached
69  if ( pos != std::string::npos) {
70  // Replace this occurrence of Sub String
71  str.replace(pos, lWord , rWord);
72 
73  }
74  }
78  inline void toLower(tString& s) {
79  std::transform(s.cbegin(), s.cend(), s.begin(),
80  [](unsigned char c){ return std::tolower(c);});
81  }
85  inline void toUpper(tString& s) {
86  tString r=s;//returned string
87  std::transform(s.cbegin(), s.cend(), s.begin(),
88  [](unsigned char c){ return std::toupper(c);});
89  }
90 
97  inline void tokenize(tString& str,
98  const tString& delim,
99  std::vector<tString>& words) {
100  char *rest=&str[0];
101  char *token;
102  tString word;
103  words.clear();
104  while ( (token = strtok_r(rest,delim.c_str(),&rest))) {
105  word=token;
106  words.push_back(trim(word));
107  }
108  }
109 }
110 
111 #endif
112 
113