StringCompare.h
Go to the documentation of this file.
1 #ifndef _utl_StringCompare_h_
2 #define _utl_StringCompare_h_
3 
4 #include <string>
5 #include <algorithm>
6 #include <cctype>
7 
8 
9 namespace utl {
10 
25  public:
26  bool operator()(const char a, const char b) const
27  { return std::tolower(a) == std::tolower(b); }
28  };
29 
30 
35  template<class Predicate>
36  inline
37  bool
38  StringEquivalent(const std::string& a, const std::string& b, Predicate p)
39  {
40  return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin(), p);
41  }
42 
43 
48  inline
49  bool
50  StringEquivalent(const std::string& a, const std::string& b)
51  {
52  return StringEquivalent(a, b, CharEqualNoCase());
53  }
54 
55 
59  template<class Predicate>
60  inline
61  bool
62  ContainsSubstring(const std::string& a, const std::string& b, Predicate p)
63  {
64  return std::search(a.begin(), a.end(), b.begin(), b.end(), p) != a.end();
65  }
66 
67 
71  inline
72  bool
73  ContainsSubstring(const std::string& a, const std::string& b)
74  {
75  return ContainsSubstring(a, b, std::equal_to<char>());
76  }
77 
78 
82  inline
83  bool
84  ContainsSubstringEquivalent(const std::string& a, const std::string& b)
85  {
86  return ContainsSubstring(a, b, CharEqualNoCase());
87  }
88 
89 }
90 
91 
92 #endif
bool StringEquivalent(const std::string &a, const std::string &b, Predicate p)
Utility to compare strings for equivalence. It takes a predicate to determine the equivalence of indi...
Definition: StringCompare.h:38
bool ContainsSubstring(const std::string &a, const std::string &b, Predicate p)
Utility to search for a substring within a string according to a given search predicate.
Definition: StringCompare.h:62
bool ContainsSubstringEquivalent(const std::string &a, const std::string &b)
Syntactic sugar for searching within a string for a substring. Search is case-insensitive.
Definition: StringCompare.h:84
Utility class to compare characters in a case-independent way.
Definition: StringCompare.h:24
bool operator()(const char a, const char b) const
Definition: StringCompare.h:26

, generated on Tue Sep 26 2023.