hpp-util  4.12.0
Debugging tools for the HPP project.
string.hh
Go to the documentation of this file.
1 // Copyright (C) 2020 by Joseph Mirabel, CNRS.
2 //
3 // This file is part of the hpp-util.
4 //
5 // This software is provided "as is" without warranty of any kind,
6 // either expressed or implied, including but not limited to the
7 // implied warranties of fitness for a particular purpose.
8 //
9 // See the COPYING file for more information.
10 
11 #ifndef HPP_UTIL_STRING_HH
12 # define HPP_UTIL_STRING_HH
13 # include <hpp/util/config.hh>
14 
15 #include <vector>
16 #include <string>
17 #include <cstring>
18 #include <iterator>
19 #include <algorithm>
20 
21 namespace hpp {
22 namespace util {
23 
24 template<typename InputIt, typename Predicate>
25 bool string_split(InputIt first, InputIt last, const char& c, Predicate p)
26 {
27  while (true) {
28  InputIt next = std::find(first, last, c);
29  if (p(first, next)) return true;
30  if (next == last) return false;
31  first = std::next(next);
32  }
33 }
34 
35 template<typename InputIt, typename Predicate>
36 bool string_split(InputIt first, InputIt last, const char* c, Predicate p)
37 {
38  auto n = std::strlen(c);
39  while (true) {
40  InputIt next = std::find_if(first, last,
41  [&c, &n](char l)->bool{ return c+n != std::find(c, c+n, l); });
42  if (p(first, next)) return true;
43  if (next == last) return false;
44  first = std::next(next);
45  }
46 }
47 
48 template<typename InputIt>
49 std::vector<std::string>
50 string_split(InputIt first, InputIt last, const char& c)
51 {
52  std::vector<std::string> strings;
53  string_split(first, last, c, [&strings](InputIt begin, InputIt end) {
54  strings.emplace_back(&(*begin), std::distance(begin, end));
55  return false;
56  });
57  return strings;
58 }
59 
60 template<typename InputIt>
61 std::vector<std::string>
62 string_split(InputIt first, InputIt last, const char* c)
63 {
64  std::vector<std::string> strings;
65  string_split(first, last, c, [&strings](InputIt begin, InputIt end) {
66  strings.emplace_back(&(*begin), std::distance(begin, end));
67  return false;
68  });
69  return strings;
70 }
71 
72 inline bool iequal(const std::string& a, const std::string& b)
73 {
74  return (a.size() == b.size()) && std::equal(a.begin(), a.end(), b.begin(),
75  [](char a, char b)->bool { return std::tolower(a) == std::tolower(b); });
76 }
77 
78 } // end of namespace util.
79 } // end of namespace hpp.
80 
81 #endif
Definition: assertion.hh:22
bool iequal(const std::string &a, const std::string &b)
Definition: string.hh:72
bool string_split(InputIt first, InputIt last, const char &c, Predicate p)
Definition: string.hh:25