ATLAS Offline Software
Loading...
Searching...
No Matches
StringUtil.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
8#include <string>
9#include <string_view>
10#include <regex>
11
12namespace RCU
13{
14 std::string substitute(std::string_view str, std::string_view pattern,
15 std::string_view with)
16 {
17 RCU_REQUIRE(!pattern.empty());
18
19 std::string result(str);
20 std::string::size_type pos = 0;
21
22 while ((pos = result.find(pattern, pos)) != std::string::npos) {
23 result.replace(pos, pattern.size(), with);
24 pos += with.size();
25 }
26 return result;
27 }
28
29 bool match_expr(const std::regex& expr, std::string_view str)
30 {
31 return std::regex_match(str.begin(), str.end(), expr);
32 }
33
34 std::string glob_to_regexp(std::string_view glob)
35 {
36 std::string result;
37 result.reserve(glob.size() * 2);
38
39 for (char c : glob)
40 {
41 switch (c)
42 {
43 case '*':
44 result += ".*";
45 break;
46 case '?':
47 result += '.';
48 break;
49 case '^': case '$': case '+': case '.': case '\\':
50 case '(': case ')': case '[': case ']': case '{': case '}': case '|':
51 result += '\\';
52 result += c;
53 break;
54 default:
55 result += c;
56 break;
57 }
58 }
59 return result;
60 }
61}
#define RCU_REQUIRE(x)
Definition Assert.h:203
This module defines a variety of assert style macros.
Definition Assert.cxx:23
bool match_expr(const std::regex &expr, std::string_view str)
returns: whether we can match the entire string with the regular expression guarantee: strong failure...
std::string substitute(std::string_view str, std::string_view pattern, std::string_view with)
effects: substitute all occurences of "pattern" with "with" in the string "str" returns: the substitu...
std::string glob_to_regexp(std::string_view glob)
returns: a string that is the regular expression equivalent of the given glob expression guarantee: s...