ATLAS Offline Software
IOVDbStringFunctions.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
3 */
4 #include "IOVDbStringFunctions.h"
5 #include <regex>
6 #include <iostream>
7 #include <algorithm>
8 
9 namespace IOVDbNamespace{
10  std::string
11  spaceStrip( const std::string & input){
12  // return the input string stripped of leading/trailing spaces
13  std::string::size_type idx1=input.find_first_not_of(" \n\r\t");
14  std::string::size_type idx2=input.find_last_not_of(" \n\r\t");
15  if (idx1==std::string::npos || idx2==std::string::npos) {
16  return "";
17  } else {
18  return std::string(input.substr(idx1,1+idx2-idx1));
19  }
20  }
21 
22  int
23  makeChannel(const std::string& strval, const int defchan) {
24  // construct a cool channelId from the string value (numeric)
25  // if empty, use the default value
26  if (!strval.empty()) return std::stoi(strval);
27  return defchan;
28  }
29 
30  unsigned long long
31  iovFromTimeString(const std::string & iovString){
32  if (iovString.empty()) return 0LL;
33  unsigned long long time=std::stoi(iovString);
34  return time*1000000000LL;
35  }
36 
37  unsigned long long
38  iovFromRunString(const std::string & runString){
39  if (runString.empty()) return 0LL;
40  unsigned long long run=std::stoi(runString);
41  return run<<32LL;
42  }
43 
44  unsigned long long
45  iovFromLumiBlockString(const std::string & lbString){
46  if (lbString.empty()) return 0LL;
47  unsigned long long lb=std::stoll(lbString.c_str());
48  return lb;
49  }
50 
51  std::string
52  sanitiseJsonString(const std::string & dataString){
53  const std::string regex="\n";
54  const std::regex lf(regex);
55  return std::regex_replace(dataString,lf,"\\n");
56  }
57 
58  int
59  parseClid(const std::string & addrHeaderStr){
60  //string of form
61  //<addrHeader><address_header service_type="256" clid="12345" /></addrHeader>
62  std::string regex=R"delim(clid\s*=\s*"([0-9]*)")delim";
64  std::smatch clidMatch;
65  bool match=std::regex_search(addrHeaderStr, clidMatch,re);
66  return (match) ? std::stoi(clidMatch[1]) : -1;
67  }
68 
69  std::string
70  parseTypename(const std::string & description){
71  std::string regex=R"delim(<typeName>\s*([^\s]+)\s*</typeName>)delim";
73  std::smatch typeMatch;
74  bool match=std::regex_search(description, typeMatch,re);
75  return (match) ? std::string(typeMatch[1]) : std::string("");
76  }
77 
78  std::string
79  deleteRegex(const std::string & original, const std::string & regex){
80  const std::regex delre(regex);
81  return std::regex_replace(original,delre,"");
82  }
83 
84  std::string
85  quote(const std::string & sentence){
86  std::string out;
87  out.reserve(sentence.size() + 2);
88  out += '\"';
89  out += sentence;
90  out += '\"';
91  return out;
92  }
93 
94  std::string
95  unescapeQuotes(const std::string & original){
96  const std::string regex=R"delim(\\")delim";
98  return std::regex_replace(original, re,"\"");
99  }
100 
101  std::string
102  unescapeBackslash(const std::string & original){
103  const std::string regex=R"delim([\\]+)delim";
104  const std::string replace=R"delim(\)delim";
105  const std::regex re(regex);
106  return std::regex_replace(original, re,replace);
107  }
108 
109  std::string
110  sanitiseFilename(const std::string & fname){
111  std::string newName{fname};
112  auto oldEnd = newName.end();
113  auto newEnd = std::remove(newName.begin(), oldEnd, '/');
114  //keep this line for reference
115  //std::replace(newName.begin(), newName.end(), '/', '^');
116  newName.erase(newEnd, newName.end());
117  return newName;
118  }
119 
120  std::string
121  sanitiseCrestTag(const std::string & fname){
122  const std::string newName{sanitiseFilename(fname)};
123  return newName;
124  }
125 
126  std::string
127  replaceNULL(const std::string & possibleNULL){
128  const std::string &original{possibleNULL};
129  const std::string regex=R"delim( NULL)delim";
130  const std::regex nullre(regex);
131  return std::regex_replace(original,nullre," null");
132  }
133 
134  std::string
135  sanitiseXml(const std::string & pseudoXmlString){
136  std::string result;
137  unsigned int strSize(pseudoXmlString.size());
138  unsigned int bufsize(strSize*1.1);
139  result.reserve(bufsize);
140  for(size_t pos = 0; pos != strSize; ++pos) {
141  switch(pseudoXmlString[pos]) {
142  case '\"': result.append("\\\""); break;
143 
144  default: result.append(&pseudoXmlString[pos], 1); break;
145  }
146  }
147  return result;
148  }
149 
150  bool
151  looksLikeMagicTag(const std::string & candidateTag){
152  return (candidateTag.compare(0,7, "TagInfo")==0 and
153  candidateTag.find('/')!=std::string::npos);
154  }
155 
156  bool
157  tagIsMagic(const std::string & candidateTag){
158  const std::string regex=R"delim(TagInfo(Major|Minor)/.*)delim";
159  const std::regex magicx(regex);
160  return std::regex_match(candidateTag, magicx);
161  }
162 
163  std::vector<std::string>
164  parseMagicTag(const std::string & v){
165  std::vector<std::string> result;
166  std::string regex7=R"delim(TagInfo(Major|Minor)/([^/]*)/?([^/]*)?)delim";
167  std::regex matchmagic(regex7);
168  std::smatch x;
169  bool foundmagic=std::regex_match(v,x,matchmagic);
170  if (foundmagic){
171  for (const auto & i:x)
172  if (i!="") result.push_back(i);
173  }
174  return result;
175  }
176 
177  std::vector<std::string>
178  parseLinkNames(const std::string &linktext){
179  std::vector<std::string> v{};
180  if (linktext.empty()) return v;
181  //regex:
182  //(anything except colon, multiple times) then _possibly_ (two colons and string of anything except colons)
183  // anything except colon) then (colon or end-of-line)
184  const std::string linkRegexStr{"([^:]*(::[^:]*)?)(:|$)"};
185  std::regex linkMatchSpec(linkRegexStr);
186  //give a token iterator using the regex and returning the first substring (i.e. the
187  //bit before a single colon or line end, which would be for example "ALink" or "MyContext::AnotherLink" )
188  std::sregex_token_iterator pos(linktext.cbegin(), linktext.cend(),linkMatchSpec,{1});
189  std::sregex_token_iterator e;
190  for (;pos!=e;++pos) {
191  // the resulting text _should not_ contain spaces, but strip it to be sure.
192  if (not pos->str().empty()) v.push_back(spaceStrip(pos->str()));
193  }
194  return v;
195  }
196 
197  std::pair<std::string, std::string>
198  tag2PrefixTarget(const std::vector<std::string> & tagParseResults){
199  std::pair<std::string, std::string> pair;
200  std::string &prefix{pair.first};
201  std::string &target{pair.second};
202  if (tagParseResults.size() == 4){ //4 is the size of result set if there is a prefix
203  prefix = tagParseResults[2]; //index of first path
204  target = tagParseResults[3]; //index of second path
205  } else {
206  target = tagParseResults[2];
207  }
208  return pair;
209  }
210 
211  bool
212  replaceServiceType71(std::string & addrHeader){
213  const std::size_t svcType = addrHeader.find("service_type=\"71\"");
214  if (svcType != std::string::npos) {
215  addrHeader.replace(svcType, 17, "service_type=\"256\"");
216  return true;
217  }
218  return false;
219  }
220 
221 
222 }
IOVDbNamespace::spaceStrip
std::string spaceStrip(const std::string &input)
Trim leading and trailing spaces,return a new trimmed string.
Definition: IOVDbStringFunctions.cxx:11
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
IOVDbNamespace::parseMagicTag
std::vector< std::string > parseMagicTag(const std::string &v)
Takes a tag of form TagInfo{Major|Minor}/<tag> or TagInfo{Major|Minor}/<prefix>/<tag> and resolve it ...
Definition: IOVDbStringFunctions.cxx:164
IOVDbNamespace::tag2PrefixTarget
std::pair< std::string, std::string > tag2PrefixTarget(const std::vector< std::string > &tagParseResults)
Takes a vector<string> containing {"<fulltag>", "Major|Minor", "<prefix>", "<tag>"} and returns a pai...
Definition: IOVDbStringFunctions.cxx:198
get_generator_info.result
result
Definition: get_generator_info.py:21
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
python.TriggerAPI.TriggerAPISession.lf
lf
Definition: TriggerAPISession.py:390
IOVDbNamespace::deleteRegex
std::string deleteRegex(const std::string &original, const std::string &regex)
Delete characters of a matching regex from the input string.
Definition: IOVDbStringFunctions.cxx:79
IOVDbNamespace::sanitiseJsonString
std::string sanitiseJsonString(const std::string &dataString)
Sanitise json string, escaping raw carriage returns.
Definition: IOVDbStringFunctions.cxx:52
IOVDbNamespace::iovFromTimeString
unsigned long long iovFromTimeString(const std::string &iovString)
Take a string integer giving a time in seconds and convert it to a ULL in nanoseconds.
Definition: IOVDbStringFunctions.cxx:31
x
#define x
IOVDbNamespace::parseLinkNames
std::vector< std::string > parseLinkNames(const std::string &linktext)
Parse string of format "A:X::B:C" to "A" , "X::B", "C".
Definition: IOVDbStringFunctions.cxx:178
IOVDbNamespace::replaceServiceType71
bool replaceServiceType71(std::string &addrHeader)
Definition: IOVDbStringFunctions.cxx:212
IOVDbNamespace::sanitiseCrestTag
std::string sanitiseCrestTag(const std::string &fname)
return valid CREST tag name from folder name
Definition: IOVDbStringFunctions.cxx:121
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
IOVDbStringFunctions.h
IOVDbNamespace::iovFromLumiBlockString
unsigned long long iovFromLumiBlockString(const std::string &lbString)
String representation of lumiblock just converted to LL (as in original code) and returned as a ULL.
Definition: IOVDbStringFunctions.cxx:45
python.BunchSpacingUtils.lb
lb
Definition: BunchSpacingUtils.py:88
lumiFormat.i
int i
Definition: lumiFormat.py:85
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
run
Definition: run.py:1
IOVDbNamespace::parseTypename
std::string parseTypename(const std::string &description)
Extract the typename from a folder description.
Definition: IOVDbStringFunctions.cxx:70
IOVDbNamespace::unescapeBackslash
std::string unescapeBackslash(const std::string &original)
Definition: IOVDbStringFunctions.cxx:102
IOVDbNamespace::sanitiseFilename
std::string sanitiseFilename(const std::string &fname)
Replace the '/' of a file path with '^'.
Definition: IOVDbStringFunctions.cxx:110
MakeNewFileFromOldAndSubstitution.newName
dictionary newName
Definition: ICHEP2016/MakeNewFileFromOldAndSubstitution.py:95
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
python.PyAthena.v
v
Definition: PyAthena.py:154
IOVDbNamespace::replaceNULL
std::string replaceNULL(const std::string &possibleNULL)
replace an uppercase NULL (such as returned by oracle) in string with a lowercase null (such as used ...
Definition: IOVDbStringFunctions.cxx:127
IOVDbNamespace::tagIsMagic
bool tagIsMagic(const std::string &candidateTag)
Resolve magic tag.
Definition: IOVDbStringFunctions.cxx:157
copySelective.target
string target
Definition: copySelective.py:37
WriteCalibToCool.strval
strval
Definition: WriteCalibToCool.py:583
re
const boost::regex re(r_e)
IOVDbNamespace::unescapeQuotes
std::string unescapeQuotes(const std::string &original)
Definition: IOVDbStringFunctions.cxx:95
IOVDbNamespace::looksLikeMagicTag
bool looksLikeMagicTag(const std::string &candidateTag)
Looks like it should be magic.
Definition: IOVDbStringFunctions.cxx:151
IOVDbNamespace::quote
std::string quote(const std::string &sentence)
Enclose a string in ".
Definition: IOVDbStringFunctions.cxx:85
IOVDbNamespace::sanitiseXml
std::string sanitiseXml(const std::string &pseudoXmlString)
for use when converting cool folder description JSON
Definition: IOVDbStringFunctions.cxx:135
IOVDbNamespace::parseClid
int parseClid(const std::string &addrHeaderStr)
Extract the Class ID (an integer) from a string of form <addrHeader><address_header service_type="256...
Definition: IOVDbStringFunctions.cxx:59
IOVDbNamespace::makeChannel
cool::ChannelId makeChannel(const std::string &strval, const cool::ChannelId defchan)
Create a ChannelId from a string; if string is empty, return the default channel number given.
Definition: IOVDbCoolFunctions.cxx:128
match
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition: hcg.cxx:356
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88
IOVDbNamespace
Definition: Base64Codec.cxx:16
IOVDbNamespace::iovFromRunString
unsigned long long iovFromRunString(const std::string &runString)
Take a string run number and convert it to an ULL representing run<<32.
Definition: IOVDbStringFunctions.cxx:38