ATLAS Offline Software
Loading...
Searching...
No Matches
StringUtil.cxx
Go to the documentation of this file.
1//
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6// Please feel free to contact me (krumnack@iastate.edu) for bug
7// reports, feature suggestions, praise and complaints.
8
9
10//
11// includes
12//
13
15
17
18//
19// method implementations
20//
21
22namespace RCU
23{
24 std::string substitute (const std::string& str, const std::string& pattern,
25 const std::string& with)
26 {
27 RCU_REQUIRE (!pattern.empty());
28
29 std::string result = str;
30 std::string::size_type pos;
31 while ((pos = result.find (pattern)) != std::string::npos) {
32 // cppcheck-suppress uselessCallsSubstr
33 result = result.substr (0, pos) + with + result.substr (pos + pattern.size());
34 }
35 return result;
36 }
37
38
39 bool match_expr(const std::regex& expr, const std::string& str)
40 {
41 std::match_results<std::string::const_iterator> what;
42 std::size_t count = std::regex_match(str.begin(), str.end(), what, expr);
43 for (std::size_t iter = 0; iter != count; ++iter)
44 {
45 if (what[iter].matched && what[iter].first == str.begin() &&
46 what[iter].second == str.end())
47 return true;
48 }
49 return false;
50 }
51
52 std::string glob_to_regexp (const std::string& glob)
53 {
54 std::string result;
55
56 for (std::string::const_iterator iter = glob.begin(),
57 end = glob.end(); iter != end; ++ iter)
58 {
59 if (*iter == '*')
60 {
61 result += ".*";
62 } else if (*iter == '?')
63 {
64 result += ".";
65 } else if (*iter == '^' || *iter == '$' || *iter == '+' || *iter == '.')
66 {
67 result += '\\';
68 result += *iter;
69 } else
70 {
71 result += *iter;
72 }
73 }
74 return result;
75 }
76}
#define RCU_REQUIRE(x)
Definition Assert.h:208
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
This module defines a variety of assert style macros.
Definition Assert.cxx:26
bool match_expr(const std::regex &expr, const std::string &str)
returns: whether we can match the entire string with the regular expression guarantee: strong failure...
std::string substitute(const std::string &str, const std::string &pattern, const std::string &with)
effects: substitute all occurences of "pattern" with "with" in the string "str" returns: the substitu...
std::string glob_to_regexp(const std::string &glob)
returns: a string that is the regular expression equivalent of the given glob expression guarantee: s...