34 pcre_extra *fPCREExtra;
36 PCREPriv_t() { fPCRE = 0; fPCREExtra = 0; }
49 fPriv =
new PCREPriv_t;
59 fPriv =
new PCREPriv_t;
69 fPriv =
new PCREPriv_t;
79 pcre_free(fPriv->fPCRE);
80 if (fPriv->fPCREExtra)
81 pcre_free(fPriv->fPCREExtra);
93 pcre_free(fPriv->fPCRE);
95 if (fPriv->fPCREExtra)
96 pcre_free(fPriv->fPCREExtra);
97 fPriv->fPCREExtra = 0;
139 const char *
m = modStr;
143 opts |= kPCRE_GLOBAL;
146 opts |= PCRE_CASELESS;
149 opts |= PCRE_MULTILINE;
152 opts |= kPCRE_OPTIMIZE;
158 opts |= PCRE_EXTENDED;
161 opts |= kPCRE_DEBUG_MSGS;
164 Error(
"ParseMods",
"illegal pattern modifier: %c", *m);
180 if (fPCREOpts & kPCRE_GLOBAL) ret +=
'g';
181 if (fPCREOpts & PCRE_CASELESS) ret +=
'i';
182 if (fPCREOpts & PCRE_MULTILINE) ret +=
'm';
183 if (fPCREOpts & PCRE_DOTALL) ret +=
's';
184 if (fPCREOpts & PCRE_EXTENDED) ret +=
'x';
185 if (fPCREOpts & kPCRE_OPTIMIZE) ret +=
'o';
186 if (fPCREOpts & kPCRE_DEBUG_MSGS) ret +=
'd';
197 pcre_free(fPriv->fPCRE);
199 if (fPCREOpts & kPCRE_DEBUG_MSGS)
200 Info(
"Compile",
"PREGEX compiling %s", fPattern.Data());
204 fPriv->fPCRE = pcre_compile(fPattern.Data(), fPCREOpts & kPCRE_INTMASK,
205 &errstr, &patIndex, 0);
208 if (fgThrowAtCompileError) {
209 throw std::runtime_error
210 (
TString::Format(
"TPRegexp::Compile() compilation of TPRegexp(%s) failed at: %d because %s",
211 fPattern.Data(), patIndex, errstr).
Data());
213 Error(
"Compile",
"compilation of TPRegexp(%s) failed at: %d because %s",
214 fPattern.Data(), patIndex, errstr);
219 if (fPriv->fPCREExtra || (fPCREOpts & kPCRE_OPTIMIZE))
228 if (fPriv->fPCREExtra)
229 pcre_free(fPriv->fPCREExtra);
231 if (fPCREOpts & kPCRE_DEBUG_MSGS)
232 Info(
"Optimize",
"PREGEX studying %s", fPattern.Data());
236 fPriv->fPCREExtra = pcre_study(fPriv->fPCRE, 0, &errstr);
238 if (!fPriv->fPCREExtra && errstr) {
239 Error(
"Optimize",
"Optimization of TPRegexp(%s) failed: %s",
240 fPattern.Data(), errstr);
252 const char *p = replacePattern;
256 while (state != -1) {
270 }
else if (!isdigit(p[1])) {
271 Error(
"ReplaceSubs",
"badly formed replacement pattern: %s",
272 replacePattern.
Data());
282 if (fPCREOpts & kPCRE_DEBUG_MSGS)
283 Info(
"ReplaceSubs",
"PREGEX appending substr #%d", subnum);
284 if (subnum < 0 || subnum > nrMatch-1) {
285 Error(
"ReplaceSubs",
"bad string number: %d",subnum);
287 const TString subStr = s(offVec[2*subnum],offVec[2*subnum+1]-offVec[2*subnum]);
308 Int_t nrMatch = pcre_exec(fPriv->fPCRE, fPriv->fPCREExtra, s.
Data(),
310 offVec, 3*nMaxMatch);
312 if (nrMatch == PCRE_ERROR_NOMATCH)
314 else if (nrMatch <= 0) {
315 Error(
"Match",
"pcre_exec error = %d", nrMatch);
321 pos->
Set(2*nrMatch, offVec);
338 UInt_t opts = ParseMods(mods);
340 if (!fPriv->fPCRE || opts != fPCREOpts) {
345 return MatchInternal(s, start, nMaxMatch, pos);
370 Int_t nrMatch = Match(s, mods, start, nMaxMatch, &pos);
375 for (
Int_t i = 0; i < nrMatch; i++) {
376 Int_t startp = pos[2*i];
377 Int_t stopp = pos[2*i+1];
378 if (startp >= 0 && stopp >= 0) {
379 const TString subStr = s(pos[2*i], pos[2*i+1]-pos[2*i]);
394 Bool_t doDollarSubst)
const 400 Int_t offset = start;
407 Int_t nrMatch = pcre_exec(fPriv->fPCRE, fPriv->fPCREExtra, s.
Data(),
409 offVec, 3*nMaxMatch);
411 if (nrMatch == PCRE_ERROR_NOMATCH) {
414 }
else if (nrMatch <= 0) {
415 Error(
"Substitute",
"pcre_exec error = %d", nrMatch);
420 if (last <= offVec[0]) {
421 final += s(last,offVec[0]-last);
427 ReplaceSubs(s,
final, replacePattern, offVec, nrMatch);
429 final += replacePattern;
434 if (!(fPCREOpts & kPCRE_GLOBAL))
437 if (offVec[0] != offVec[1])
441 if (offVec[1] == s.
Length())
443 offset = offVec[1]+1;
449 final += s(last,s.
Length()-last);
471 UInt_t opts = ParseMods(mods);
473 if (!fPriv->fPCRE || opts != fPCREOpts) {
478 return SubstituteInternal(s, replacePattern, start, nMaxMatch,
kTRUE);
488 return fPriv->fPCRE != 0;
497 return fgThrowAtCompileError;
506 fgThrowAtCompileError = throwp;
523 Int_t nrMatch = r.
Match(*
this,
"",start,10,&pos);
538 const Int_t nrMatch = r.
Match(*
this,
"",start,10,&pos);
540 *extent = pos[1]-pos[0];
563 return (*
this)(
r, 0);
588 fAddressOfLastString(0),
589 fLastGlobalPosition(0)
763 typedef std::pair<int, int> MarkerLoc_t;
764 typedef std::vector<MarkerLoc_t> MarkerLocVec_t;
767 MarkerLocVec_t oMarks;
774 MarkerLocVec_t oCurrentTrailingEmpties;
777 Int_t nMatchesFound = 0;
783 while ((matchRes =
Match(s, nOffset)) &&
784 ((maxfields < 1) || nMatchesFound < maxfields)) {
788 oMarks.push_back(MarkerLoc_t(nOffset, nOffset + 1));
790 if (nOffset >= s.
Length())
798 if (!oCurrentTrailingEmpties.empty()) {
799 oMarks.insert(oMarks.end(),
800 oCurrentTrailingEmpties.begin(),
801 oCurrentTrailingEmpties.end());
802 oCurrentTrailingEmpties.clear();
804 oMarks.push_back(MarkerLoc_t(nOffset,
fMarkers[0]));
807 if (maxfields == 0) {
809 oCurrentTrailingEmpties.push_back(MarkerLoc_t(nOffset, nOffset));
811 oMarks.push_back(MarkerLoc_t(nOffset, nOffset));
818 for (
Int_t i = 1; i < matchRes; ++i)
825 if (nMatchesFound == 0) {
826 oMarks.push_back(MarkerLoc_t(0, s.
Length()));
830 else if (maxfields > 0 && nMatchesFound >= maxfields) {
831 oMarks[oMarks.size() - 1].second = s.
Length();
836 if (!last_empty || maxfields < 0) {
837 if (!oCurrentTrailingEmpties.empty()) {
838 oMarks.insert(oMarks.end(),
839 oCurrentTrailingEmpties.begin(),
840 oCurrentTrailingEmpties.end());
842 oMarks.push_back(MarkerLoc_t(nOffset, s.
Length()));
850 fMarkers[2*i + 1] = oMarks[i].second;
883 case 0 : ret +=
'\\';
break;
884 case 'l': state = 1;
break;
885 case 'u': state = 2;
break;
886 case 'L': state = 3;
break;
887 case 'U': state = 4;
break;
888 case 'E': state = 0;
break;
889 default : ret +=
'\\'; ret +=
c;
break;
894 case 0: ret +=
c;
break;
895 case 1: ret += (
Char_t) tolower(c); state = 0;
break;
896 case 2: ret += (
Char_t) toupper(c); state = 0;
break;
897 case 3: ret += (
Char_t) tolower(c);
break;
898 case 4: ret += (
Char_t) toupper(c);
break;
899 default:
Error(
"TPMERegexp::Substitute",
"invalid state.");
964 fReturnVoid (retVoid),
976 while (fPos < fFullStr.Length()) {
977 if (fSplitRe.Match(fFullStr,
"", fPos, 2, &x)) {
982 fPos = fFullStr.Length() + 1;
984 if (Length() || fReturnVoid)
990 if (fPos == fFullStr.Length() && fReturnVoid) {
992 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.
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.