35 pcre_extra *fPCREExtra;
37 PCREPriv_t() { fPCRE = 0; fPCREExtra = 0; }
50 fPriv =
new PCREPriv_t;
60 fPriv =
new PCREPriv_t;
70 fPriv =
new PCREPriv_t;
80 pcre_free(fPriv->fPCRE);
81 if (fPriv->fPCREExtra)
82 pcre_free(fPriv->fPCREExtra);
94 pcre_free(fPriv->fPCRE);
96 if (fPriv->fPCREExtra)
97 pcre_free(fPriv->fPCREExtra);
98 fPriv->fPCREExtra = 0;
140 const char *
m = modStr;
144 opts |= kPCRE_GLOBAL;
147 opts |= PCRE_CASELESS;
150 opts |= PCRE_MULTILINE;
153 opts |= kPCRE_OPTIMIZE;
159 opts |= PCRE_EXTENDED;
162 opts |= kPCRE_DEBUG_MSGS;
165 Error(
"ParseMods",
"illegal pattern modifier: %c", *m);
181 if (fPCREOpts & kPCRE_GLOBAL) ret +=
'g';
182 if (fPCREOpts & PCRE_CASELESS) ret +=
'i';
183 if (fPCREOpts & PCRE_MULTILINE) ret +=
'm';
184 if (fPCREOpts & PCRE_DOTALL) ret +=
's';
185 if (fPCREOpts & PCRE_EXTENDED) ret +=
'x';
186 if (fPCREOpts & kPCRE_OPTIMIZE) ret +=
'o';
187 if (fPCREOpts & kPCRE_DEBUG_MSGS) ret +=
'd';
198 pcre_free(fPriv->fPCRE);
200 if (fPCREOpts & kPCRE_DEBUG_MSGS)
201 Info(
"Compile",
"PREGEX compiling %s", fPattern.Data());
205 fPriv->fPCRE = pcre_compile(fPattern.Data(), fPCREOpts & kPCRE_INTMASK,
206 &errstr, &patIndex, 0);
209 if (fgThrowAtCompileError) {
210 throw std::runtime_error
211 (
TString::Format(
"TPRegexp::Compile() compilation of TPRegexp(%s) failed at: %d because %s",
212 fPattern.Data(), patIndex, errstr).
Data());
214 Error(
"Compile",
"compilation of TPRegexp(%s) failed at: %d because %s",
215 fPattern.Data(), patIndex, errstr);
220 if (fPriv->fPCREExtra || (fPCREOpts & kPCRE_OPTIMIZE))
229 if (fPriv->fPCREExtra)
230 pcre_free(fPriv->fPCREExtra);
232 if (fPCREOpts & kPCRE_DEBUG_MSGS)
233 Info(
"Optimize",
"PREGEX studying %s", fPattern.Data());
237 fPriv->fPCREExtra = pcre_study(fPriv->fPCRE, 0, &errstr);
239 if (!fPriv->fPCREExtra && errstr) {
240 Error(
"Optimize",
"Optimization of TPRegexp(%s) failed: %s",
241 fPattern.Data(), errstr);
253 const char *p = replacePattern;
257 while (state != -1) {
271 }
else if (!isdigit(p[1])) {
272 Error(
"ReplaceSubs",
"badly formed replacement pattern: %s",
273 replacePattern.
Data());
283 if (fPCREOpts & kPCRE_DEBUG_MSGS)
284 Info(
"ReplaceSubs",
"PREGEX appending substr #%d", subnum);
285 if (subnum < 0 || subnum > nrMatch-1) {
286 Error(
"ReplaceSubs",
"bad string number: %d",subnum);
288 const TString subStr = s(offVec[2*subnum],offVec[2*subnum+1]-offVec[2*subnum]);
309 Int_t nrMatch = pcre_exec(fPriv->fPCRE, fPriv->fPCREExtra, s.
Data(),
311 offVec, 3*nMaxMatch);
313 if (nrMatch == PCRE_ERROR_NOMATCH)
315 else if (nrMatch <= 0) {
316 Error(
"Match",
"pcre_exec error = %d", nrMatch);
322 pos->
Set(2*nrMatch, offVec);
339 UInt_t opts = ParseMods(mods);
341 if (!fPriv->fPCRE || opts != fPCREOpts) {
346 return MatchInternal(s, start, nMaxMatch, pos);
371 Int_t nrMatch = Match(s, mods, start, nMaxMatch, &pos);
376 for (
Int_t i = 0; i < nrMatch; i++) {
377 Int_t startp = pos[2*i];
378 Int_t stopp = pos[2*i+1];
379 if (startp >= 0 && stopp >= 0) {
380 const TString subStr = s(pos[2*i], pos[2*i+1]-pos[2*i]);
395 Bool_t doDollarSubst)
const 401 Int_t offset = start;
408 Int_t nrMatch = pcre_exec(fPriv->fPCRE, fPriv->fPCREExtra, s.
Data(),
410 offVec, 3*nMaxMatch);
412 if (nrMatch == PCRE_ERROR_NOMATCH) {
415 }
else if (nrMatch <= 0) {
416 Error(
"Substitute",
"pcre_exec error = %d", nrMatch);
421 if (last <= offVec[0]) {
422 final += s(last,offVec[0]-last);
428 ReplaceSubs(s,
final, replacePattern, offVec, nrMatch);
430 final += replacePattern;
435 if (!(fPCREOpts & kPCRE_GLOBAL))
438 if (offVec[0] != offVec[1])
442 if (offVec[1] == s.
Length())
444 offset = offVec[1]+1;
450 final += s(last,s.
Length()-last);
472 UInt_t opts = ParseMods(mods);
474 if (!fPriv->fPCRE || opts != fPCREOpts) {
479 return SubstituteInternal(s, replacePattern, start, nMaxMatch,
kTRUE);
489 return fPriv->fPCRE != 0;
498 return fgThrowAtCompileError;
507 fgThrowAtCompileError = throwp;
524 Int_t nrMatch = r.
Match(*
this,
"",start,10,&pos);
539 const Int_t nrMatch = r.
Match(*
this,
"",start,10,&pos);
541 *extent = pos[1]-pos[0];
564 return (*
this)(
r, 0);
589 fAddressOfLastString(0),
590 fLastGlobalPosition(0)
764 typedef std::pair<int, int> MarkerLoc_t;
765 typedef std::vector<MarkerLoc_t> MarkerLocVec_t;
768 MarkerLocVec_t oMarks;
775 MarkerLocVec_t oCurrentTrailingEmpties;
778 Int_t nMatchesFound = 0;
784 while ((matchRes =
Match(s, nOffset)) &&
785 ((maxfields < 1) || nMatchesFound < maxfields)) {
789 oMarks.push_back(MarkerLoc_t(nOffset, nOffset + 1));
791 if (nOffset >= s.
Length())
799 if (!oCurrentTrailingEmpties.empty()) {
800 oMarks.insert(oMarks.end(),
801 oCurrentTrailingEmpties.begin(),
802 oCurrentTrailingEmpties.end());
803 oCurrentTrailingEmpties.clear();
805 oMarks.push_back(MarkerLoc_t(nOffset,
fMarkers[0]));
808 if (maxfields == 0) {
810 oCurrentTrailingEmpties.push_back(MarkerLoc_t(nOffset, nOffset));
812 oMarks.push_back(MarkerLoc_t(nOffset, nOffset));
819 for (
Int_t i = 1; i < matchRes; ++i)
826 if (nMatchesFound == 0) {
827 oMarks.push_back(MarkerLoc_t(0, s.
Length()));
831 else if (maxfields > 0 && nMatchesFound >= maxfields) {
832 oMarks[oMarks.size() - 1].second = s.
Length();
837 if (!last_empty || maxfields < 0) {
838 if (!oCurrentTrailingEmpties.empty()) {
839 oMarks.insert(oMarks.end(),
840 oCurrentTrailingEmpties.begin(),
841 oCurrentTrailingEmpties.end());
843 oMarks.push_back(MarkerLoc_t(nOffset, s.
Length()));
851 fMarkers[2*i + 1] = oMarks[i].second;
884 case 0 : ret +=
'\\';
break;
885 case 'l': state = 1;
break;
886 case 'u': state = 2;
break;
887 case 'L': state = 3;
break;
888 case 'U': state = 4;
break;
889 case 'E': state = 0;
break;
890 default : ret +=
'\\'; ret += c;
break;
895 case 0: ret += c;
break;
896 case 1: ret += (
Char_t) tolower(c); state = 0;
break;
897 case 2: ret += (
Char_t) toupper(c); state = 0;
break;
898 case 3: ret += (
Char_t) tolower(c);
break;
899 case 4: ret += (
Char_t) toupper(c);
break;
900 default:
Error(
"TPMERegexp::Substitute",
"invalid state.");
965 fReturnVoid (retVoid),
977 while (fPos < fFullStr.Length()) {
978 if (fSplitRe.Match(fFullStr,
"", fPos, 2, &x)) {
983 fPos = fFullStr.Length() + 1;
985 if (Length() || fReturnVoid)
991 if (fPos == fFullStr.Length() && fReturnVoid) {
993 fPos = fFullStr.Length() + 1;
A zero length substring is legal.
Int_t SubstituteInternal(TString &s, const TString &replace, Int_t start, Int_t nMaxMatch0, Bool_t doDollarSubst) const
Perform pattern substitution with optional back-ref replacement.
TString fLastStringMatched
RooCmdArg Optimize(Int_t flag=2)
Int_t MatchInternal(const TString &s, Int_t start, Int_t nMaxMatch, TArrayI *pos=0) const
Perform the actual matching - protected method.
Collectable string class.
void AssignGlobalState(const TPMERegexp &re)
Copy global-match state from 're; so that this regexp can continue parsing the string from where 're'...
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
Int_t ReplaceSubs(const TString &s, TString &final, const TString &replacePattern, Int_t *ovec, Int_t nmatch) const
Returns the number of expanded '$' constructs.
TObjArray * MatchS(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
Returns a TObjArray of matched substrings as TObjString's.
static void SetThrowAtCompileError(Bool_t throwp)
Set static flag controlling whether exception should be thrown upon an error during regular expressio...
virtual ~TPRegexp()
Cleanup.
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
TString GetModifiers() const
Return PCRE modifier options as string.
void ToLower()
Change string to lower-case.
TString & operator=(char s)
Assign character c to TString.
Int_t Substitute(TString &s, const TString &replace, const TString &mods="", Int_t start=0, Int_t nMatchMax=10)
Substitute replaces the string s by a new string in which matching patterns are replaced by the repla...
Array of integers (32 bits per element).
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Provides iteration through tokens of a given string.
void Compile()
Compile the fPattern.
void Info(const char *location, const char *msgfmt,...)
std::vector< std::vector< double > > Data
Bool_t IsValid() const
Returns true if underlying PCRE structure has been successfully generated via regexp compilation...
void Set(Int_t n)
Set size of this array to n ints.
void Error(const char *location, const char *msgfmt,...)
char & operator()(Ssiz_t i)
Int_t Split(const TString &s, Int_t maxfields=0)
Splits into at most maxfields.
void * fAddressOfLastString
TPRegexp & operator=(const TPRegexp &p)
Assignment operator.
virtual void Print(Option_t *option="")
Print the regular expression and modifier options.
RooCmdArg Index(RooCategory &icat)
TPMERegexp()
Default constructor. This regexp will match an empty string.
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Bool_t NextToken()
Get the next token, it is stored in this TString.
static Bool_t fgThrowAtCompileError
UInt_t ParseMods(const TString &mods) const
Translate Perl modifier flags into pcre flags.
static Bool_t GetThrowAtCompileError()
Get value of static flag controlling whether exception should be thrown upon an error during regular ...
Int_t Match(const TString &s, UInt_t start=0)
Runs a match on s against the regex 'this' was created with.
Wrapper for PCRE library (Perl Compatible Regular Expressions).
Int_t fLastGlobalPosition
void Reset(const TString &s, const TString &opts="", Int_t nMatchMax=-1)
Reset the pattern and options.
Int_t Substitute(TString &s, const TString &r, Bool_t doDollarSubst=kTRUE)
Substitute matching part of s with r, dollar back-ref substitution is performed if doDollarSubst is t...
Int_t Match(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10, TArrayI *pos=0)
The number of matches is returned, this equals the full match + sub-pattern matches.
TString operator[](Int_t)
Returns the sub-string from the internal fMarkers vector.
void Optimize()
Send the pattern through the optimizer.
const char * Data() const
void ResetGlobalState()
Reset state of global match.