ATLAS Offline Software
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 
16 #include <RootCoreUtils/Assert.h>
17 
18 //
19 // method implementations
20 //
21 
22 namespace 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 }
mergePhysValFiles.pattern
pattern
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:25
createLinkingScheme.iter
iter
Definition: createLinkingScheme.py:62
get_generator_info.result
result
Definition: get_generator_info.py:21
RCU_REQUIRE
#define RCU_REQUIRE(x)
Definition: Assert.h:208
RCU
This module defines a variety of assert style macros.
Definition: Assert.cxx:26
RCU::match_expr
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...
Definition: StringUtil.cxx:39
Assert.h
StringUtil.h
XMLtoHeader.count
count
Definition: XMLtoHeader.py:84
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:92
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
python.CaloAddPedShiftConfig.str
str
Definition: CaloAddPedShiftConfig.py:42
python.ExitCodes.what
def what(code)
Definition: ExitCodes.py:73
python.ElectronD3PDObject.matched
matched
Definition: ElectronD3PDObject.py:138
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
RCU::substitute
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...
Definition: StringUtil.cxx:24
DeMoScan.first
bool first
Definition: DeMoScan.py:534
str
Definition: BTagTrackIpAccessor.cxx:11
RCU::glob_to_regexp
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...
Definition: StringUtil.cxx:52