Project
Loading...
Searching...
No Matches
AliasExpander.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
13#include <algorithm>
14#include <fmt/format.h>
15#include <sstream>
16
17namespace
18{
19
20std::vector<std::string> splitString(const std::string& src, char delim)
21{
22 std::stringstream ss(src);
23 std::string token;
24 std::vector<std::string> tokens;
25
26 while (std::getline(ss, token, delim)) {
27 if (!token.empty()) {
28 tokens.push_back(std::move(token));
29 }
30 }
31
32 return tokens;
33}
34
35std::vector<std::string> extractList(const std::string& slist)
36{
37 auto dots = slist.find(",");
38 if (dots == std::string::npos) {
39 return {};
40 }
41 return splitString(slist, ',');
42}
43
44std::vector<std::string> extractRange(std::string range)
45{
46 auto dots = range.find("..");
47 if (dots == std::string::npos) {
48 return extractList(range);
49 }
50
51 auto braceStart = range.find("{");
52 auto braceEnd = range.find("}");
53
54 if (
55 (braceStart != std::string::npos &&
56 braceEnd == std::string::npos) ||
57 (braceStart == std::string::npos &&
58 braceEnd != std::string::npos)) {
59 // incomplete custom pattern
60 return {};
61 }
62
63 std::string intFormat;
64 std::string sa, sb;
65
66 if (braceStart != std::string::npos &&
67 braceEnd != std::string::npos) {
68 intFormat = range.substr(braceStart, braceEnd - braceStart + 1);
69 range.erase(braceStart, braceEnd);
70 dots = range.find("..");
71 sa = range.substr(0, dots);
72 sb = range.substr(dots + 2);
73 } else {
74 sa = range.substr(0, dots);
75 sb = range.substr(dots + 2);
76 auto size = std::max(sa.size(), sb.size());
77 intFormat = "{:" + fmt::format("0{}d", size) + "}";
78 }
79
80 auto a = std::stoi(sa);
81 auto b = std::stoi(sb);
82 std::vector<std::string> result;
83
84 for (auto i = a; i <= b; i++) {
85 auto substituted = fmt::format(fmt::runtime(intFormat), i);
86 result.push_back(substituted);
87 }
88 return result;
89}
90} // namespace
91
92namespace o2::dcs
93{
94std::vector<std::string> expandAlias(const std::string& pattern)
95{
96 auto leftBracket = pattern.find("[");
97 auto rightBracket = pattern.find("]");
98
99 // no bracket at all -> return pattern simply
100 if (leftBracket == std::string::npos && rightBracket == std::string::npos) {
101 return {pattern};
102 }
103
104 // no matching bracket -> wrong pattern -> return nothing
105 if ((leftBracket == std::string::npos &&
106 rightBracket != std::string::npos) ||
107 (leftBracket != std::string::npos &&
108 rightBracket == std::string::npos)) {
109 return {};
110 }
111 auto rangeStr = pattern.substr(leftBracket + 1, rightBracket - leftBracket - 1);
112
113 auto range = extractRange(rangeStr);
114
115 // incorrect range -> return nothing
116 if (range.empty()) {
117 return {};
118 }
119
120 auto newPattern = pattern.substr(0, leftBracket) +
121 "{:s}" +
122 pattern.substr(rightBracket + 1);
123
124 std::vector<std::string> result;
125
126 for (auto r : range) {
127 auto substituted = fmt::format(fmt::runtime(newPattern), r);
128 result.emplace_back(substituted);
129 }
130
132}
133
134std::vector<std::string> expandAliases(const std::vector<std::string>& patternedAliases)
135{
136 std::vector<std::string> result;
137
138 for (auto a : patternedAliases) {
139 auto e = expandAlias(a);
140 result.insert(result.end(), e.begin(), e.end());
141 }
142 // sort to get a predictable result
143 std::sort(result.begin(), result.end());
144
145 return result;
146}
147} // namespace o2::dcs
int32_t i
std::vector< std::string > splitString(const std::string &src, char delim, bool trim=false)
GLenum src
Definition glcorearb.h:1767
GLuint64EXT * result
Definition glcorearb.h:5662
GLsizeiptr size
Definition glcorearb.h:659
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLenum GLint * range
Definition glcorearb.h:1899
GLboolean r
Definition glcorearb.h:1233
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
std::vector< std::string > expandAliases(const std::vector< std::string > &patternedAliases)
std::vector< std::string > expandAlias(const std::string &pattern)
std::array< uint16_t, 5 > pattern