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
7#include <stdexcept>
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 if (pattern.empty())
18 throw std::runtime_error ("substitute: pattern must not be empty");
19
20 std::string result(str);
21 std::string::size_type pos = 0;
22
23 while ((pos = result.find(pattern, pos)) != std::string::npos) {
24 result.replace(pos, pattern.size(), with);
25 pos += with.size();
26 }
27 return result;
28 }
29
30 bool match_expr(const std::regex& expr, std::string_view str)
31 {
32 return std::regex_match(str.begin(), str.end(), expr);
33 }
34
35 std::string glob_to_regexp(std::string_view glob)
36 {
37 std::string result;
38 result.reserve(glob.size() * 2);
39
40 for (char c : glob)
41 {
42 switch (c)
43 {
44 case '*':
45 result += ".*";
46 break;
47 case '?':
48 result += '.';
49 break;
50 case '^': case '$': case '+': case '.': case '\\':
51 case '(': case ')': case '[': case ']': case '{': case '}': case '|':
52 result += '\\';
53 result += c;
54 break;
55 default:
56 result += c;
57 break;
58 }
59 }
60 return result;
61 }
62}
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...