DiaDes  0.1
DIAgnosis of Discrete-Event System
StringTools.hh
Go to the documentation of this file.
1 #ifndef __DIADES__UTILS__STRINGTOOLS__HH
2 #define __DIADES__UTILS__STRINGTOOLS__HH
3 
13 #include<string>
14 #include<numeric>
16 
17 
18 namespace Diades
19 {
20  namespace Utils
21  {
22 
23 
30  struct StringHash
31  {
32 
33  inline size_t operator()(const std::string & s) const
34  {
35  size_t hash = 5381;
36  int c;
37  for(unsigned i = 0; i < std::string::npos; ++i)
38  {
39  c = s[i];
40  hash = ((hash << 5) + hash) + c;
41  }
42  return hash;
43  }
44  };
45 
55  template<class CharType, class CharTraits, class InputIterator>
56  InputIterator
57  getFileExtension(const std::basic_string<CharType, CharTraits> & fileName,
58  InputIterator begin, InputIterator end)
59  {
60  typedef std::basic_string<CharType, CharTraits> String;
61  size_t idx;
62  idx = fileName.find_last_of('.');
63  if(idx != String::npos)
64  {
65  String suffix = fileName.substr(idx + 1);
66  bool found = false;
67  while((!found) && (begin != end))
68  {
69  found = (suffix == *begin);
70  if(!found)
71  {
72  ++begin;
73  }
74  }
75  }
76  else
77  {
78  begin = end;
79  }
80  return begin;
81  }
82 
90  template<class CharType, class CharTraits>
91  bool
92  hasOnlyWhiteSpaces(const std::basic_string<CharType, CharTraits> & sequence,
93  typename std::basic_string<CharType, CharTraits>::size_type & index)
94  {
95  index = 0;
96  bool foundSomething = false;
97  while(!foundSomething && index != sequence.size())
98  {
99  foundSomething = (sequence.at(index) != ' ')
100  &&
101  (sequence.at(index) != '\t');
102  if(!foundSomething)
103  {
104  ++index;
105  }
106  }
107  if(!foundSomething)
108  {
109  index = std::basic_string<CharType, CharTraits>::npos;
110  }
111  return !foundSomething;
112  }
113 
128  template< typename CharT,
129  CharT separator,
130  typename Traits,
131  typename Alloc>
133  {
134  public:
135  typedef std::basic_string<CharT, Traits, Alloc> String;
136 
140  String operator()(String str1, const String & str2) const
141  {
142  return std::move(str1) + separator + str2;
143  }
144  };
145 
158  template< typename CharT,
159  CharT left,
160  CharT right,
161  typename Traits,
162  typename Alloc
163  >
165  {
166  public:
167  typedef std::basic_string<CharT, Traits, Alloc> String;
168 
172  String operator()(String str) const
173  {
174  return left + std::move(str) + right;
175  }
176  };
177 
184  template< typename CharT,
185  typename Traits,
186  typename Alloc
187  >
189  {
190  public:
191  typedef std::basic_string<CharT, Traits, Alloc> String;
192 
196  String operator()(String str) const
197  {
198  return std::move(str);
199  }
200  };
201 
216  template<typename _Property, typename BinaryDisjunction, typename Delimiter>
218  {
219  public:
220  using Property = _Property;
221 
222  template<typename PropertyIterator>
223  Property operator()(PropertyIterator begin, PropertyIterator end) const
224  {
225  Property result;
226  if(begin != end)
227  {
228  result = std::accumulate(++begin, end, *begin, BinaryDisjunction());
229  }
230  return Delimiter()(result);
231  }
232  };
233 
248  template<typename _Property, typename BinaryConjunction, typename Delimiter>
250  {
251  public:
252  using Property = _Property;
253 
254  template<typename PropertyIterator>
255  Property operator()(PropertyIterator begin, PropertyIterator end)
256  {
257  Property result;
258  if(begin != end)
259  {
260  result = std::accumulate(++begin, end, *begin, BinaryConjunction());
261  }
262  return Delimiter()(result);
263  }
264  };
265 
266 
267 
268  };
269 };
270 #endif
std::basic_string< CharT, Traits, Alloc > String
Definition: StringTools.hh:167
InputIterator getFileExtension(const std::basic_string< CharType, CharTraits > &fileName, InputIterator begin, InputIterator end)
Definition: StringTools.hh:57
std::basic_string< CharT, Traits, Alloc > String
Definition: StringTools.hh:135
Property operator()(PropertyIterator begin, PropertyIterator end)
Definition: StringTools.hh:255
String operator()(String str1, const String &str2) const
Definition: StringTools.hh:140
size_t operator()(const std::string &s) const
Definition: StringTools.hh:33
String operator()(String str) const
Definition: StringTools.hh:172
std::basic_string< CharT, Traits, Alloc > String
Definition: StringTools.hh:191
Property operator()(PropertyIterator begin, PropertyIterator end) const
Definition: StringTools.hh:223
Namespace of the Diades project.
String operator()(String str) const
Definition: StringTools.hh:196
bool hasOnlyWhiteSpaces(const std::basic_string< CharType, CharTraits > &sequence, typename std::basic_string< CharType, CharTraits >::size_type &index)
Definition: StringTools.hh:92