38 pcre_extra *fPCREExtra;
40 PCREPriv_t() { fPCRE = 0; fPCREExtra = 0; }
53 fPriv =
new PCREPriv_t;
63 fPriv =
new PCREPriv_t;
73 fPriv =
new PCREPriv_t;
83 pcre_free(fPriv->fPCRE);
84 if (fPriv->fPCREExtra)
85 pcre_free(fPriv->fPCREExtra);
97 pcre_free(fPriv->fPCRE);
99 if (fPriv->fPCREExtra)
100 pcre_free(fPriv->fPCREExtra);
101 fPriv->fPCREExtra = 0;
143 const char *
m = modStr;
147 opts |= kPCRE_GLOBAL;
150 opts |= PCRE_CASELESS;
153 opts |= PCRE_MULTILINE;
156 opts |= kPCRE_OPTIMIZE;
162 opts |= PCRE_EXTENDED;
165 opts |= kPCRE_DEBUG_MSGS;
168 Error(
"ParseMods",
"illegal pattern modifier: %c", *m);
184 if (fPCREOpts & kPCRE_GLOBAL) ret +=
'g';
185 if (fPCREOpts & PCRE_CASELESS) ret +=
'i';
186 if (fPCREOpts & PCRE_MULTILINE) ret +=
'm';
187 if (fPCREOpts & PCRE_DOTALL) ret +=
's';
188 if (fPCREOpts & PCRE_EXTENDED) ret +=
'x';
189 if (fPCREOpts & kPCRE_OPTIMIZE) ret +=
'o';
190 if (fPCREOpts & kPCRE_DEBUG_MSGS) ret +=
'd';
201 pcre_free(fPriv->fPCRE);
203 if (fPCREOpts & kPCRE_DEBUG_MSGS)
204 Info(
"Compile",
"PREGEX compiling %s", fPattern.Data());
208 fPriv->fPCRE = pcre_compile(fPattern.Data(), fPCREOpts & kPCRE_INTMASK,
209 &errstr, &patIndex, 0);
212 if (fgThrowAtCompileError) {
213 throw std::runtime_error
214 (
TString::Format(
"TPRegexp::Compile() compilation of TPRegexp(%s) failed at: %d because %s",
215 fPattern.Data(), patIndex, errstr).Data());
217 Error(
"Compile",
"compilation of TPRegexp(%s) failed at: %d because %s",
218 fPattern.Data(), patIndex, errstr);
223 if (fPriv->fPCREExtra || (fPCREOpts & kPCRE_OPTIMIZE))
232 if (fPriv->fPCREExtra)
233 pcre_free(fPriv->fPCREExtra);
235 if (fPCREOpts & kPCRE_DEBUG_MSGS)
236 Info(
"Optimize",
"PREGEX studying %s", fPattern.Data());
240 fPriv->fPCREExtra = pcre_study(fPriv->fPCRE, 0, &errstr);
242 if (!fPriv->fPCREExtra && errstr) {
243 Error(
"Optimize",
"Optimization of TPRegexp(%s) failed: %s",
244 fPattern.Data(), errstr);
256 const char *p = replacePattern;
260 while (state != -1) {
274 }
else if (!isdigit(p[1])) {
275 Error(
"ReplaceSubs",
"badly formed replacement pattern: %s",
276 replacePattern.
Data());
286 if (fPCREOpts & kPCRE_DEBUG_MSGS)
287 Info(
"ReplaceSubs",
"PREGEX appending substr #%d", subnum);
288 if (subnum < 0 || subnum > nrMatch-1) {
289 Error(
"ReplaceSubs",
"bad string number: %d",subnum);
291 const TString subStr =
s(offVec[2*subnum],offVec[2*subnum+1]-offVec[2*subnum]);
312 Int_t nrMatch = pcre_exec(fPriv->fPCRE, fPriv->fPCREExtra, s.
Data(),
314 offVec, 3*nMaxMatch);
316 if (nrMatch == PCRE_ERROR_NOMATCH)
318 else if (nrMatch <= 0) {
319 Error(
"Match",
"pcre_exec error = %d", nrMatch);
325 pos->
Set(2*nrMatch, offVec);
342 UInt_t opts = ParseMods(mods);
344 if (!fPriv->fPCRE || opts != fPCREOpts) {
349 return MatchInternal(s, start, nMaxMatch, pos);
374 Int_t nrMatch = Match(s, mods, start, nMaxMatch, &pos);
379 for (
Int_t i = 0; i < nrMatch; i++) {
380 Int_t startp = pos[2*i];
381 Int_t stopp = pos[2*i+1];
382 if (startp >= 0 && stopp >= 0) {
383 const TString subStr =
s(pos[2*i], pos[2*i+1]-pos[2*i]);
398 Bool_t doDollarSubst)
const 404 Int_t offset = start;
411 Int_t nrMatch = pcre_exec(fPriv->fPCRE, fPriv->fPCREExtra, s.
Data(),
413 offVec, 3*nMaxMatch);
415 if (nrMatch == PCRE_ERROR_NOMATCH) {
418 }
else if (nrMatch <= 0) {
419 Error(
"Substitute",
"pcre_exec error = %d", nrMatch);
424 if (last <= offVec[0]) {
425 final +=
s(last,offVec[0]-last);
431 ReplaceSubs(s,
final, replacePattern, offVec, nrMatch);
433 final += replacePattern;
438 if (!(fPCREOpts & kPCRE_GLOBAL))
441 if (offVec[0] != offVec[1])
445 if (offVec[1] == s.
Length())
447 offset = offVec[1]+1;
453 final +=
s(last,s.
Length()-last);
475 UInt_t opts = ParseMods(mods);
477 if (!fPriv->fPCRE || opts != fPCREOpts) {
482 return SubstituteInternal(s, replacePattern, start, nMaxMatch,
kTRUE);
492 return fPriv->fPCRE != 0;
501 return fgThrowAtCompileError;
510 fgThrowAtCompileError = throwp;
527 Int_t nrMatch = r.
Match(*
this,
"",start,10,&pos);
542 const Int_t nrMatch = r.
Match(*
this,
"",start,10,&pos);
544 *extent = pos[1]-pos[0];
567 return (*
this)(
r, 0);
592 fAddressOfLastString(0),
593 fLastGlobalPosition(0)
767 typedef std::pair<int, int> MarkerLoc_t;
768 typedef std::vector<MarkerLoc_t> MarkerLocVec_t;
771 MarkerLocVec_t oMarks;
778 MarkerLocVec_t oCurrentTrailingEmpties;
781 Int_t nMatchesFound = 0;
787 while ((matchRes =
Match(s, nOffset)) &&
788 ((maxfields < 1) || nMatchesFound < maxfields)) {
792 oMarks.push_back(MarkerLoc_t(nOffset, nOffset + 1));
794 if (nOffset >= s.
Length())
802 if (!oCurrentTrailingEmpties.empty()) {
803 oMarks.insert(oMarks.end(),
804 oCurrentTrailingEmpties.begin(),
805 oCurrentTrailingEmpties.end());
806 oCurrentTrailingEmpties.clear();
808 oMarks.push_back(MarkerLoc_t(nOffset,
fMarkers[0]));
811 if (maxfields == 0) {
813 oCurrentTrailingEmpties.push_back(MarkerLoc_t(nOffset, nOffset));
815 oMarks.push_back(MarkerLoc_t(nOffset, nOffset));
822 for (
Int_t i = 1; i < matchRes; ++i)
829 if (nMatchesFound == 0) {
830 oMarks.push_back(MarkerLoc_t(0, s.
Length()));
834 else if (maxfields > 0 && nMatchesFound >= maxfields) {
835 oMarks[oMarks.size() - 1].second = s.
Length();
840 if (!last_empty || maxfields < 0) {
841 if (!oCurrentTrailingEmpties.empty()) {
842 oMarks.insert(oMarks.end(),
843 oCurrentTrailingEmpties.begin(),
844 oCurrentTrailingEmpties.end());
846 oMarks.push_back(MarkerLoc_t(nOffset, s.
Length()));
854 fMarkers[2*i + 1] = oMarks[i].second;
887 case 0 : ret +=
'\\';
break;
888 case 'l': state = 1;
break;
889 case 'u': state = 2;
break;
890 case 'L': state = 3;
break;
891 case 'U': state = 4;
break;
892 case 'E': state = 0;
break;
893 default : ret +=
'\\'; ret += c;
break;
898 case 0: ret += c;
break;
899 case 1: ret += (
Char_t) tolower(c); state = 0;
break;
900 case 2: ret += (
Char_t) toupper(c); state = 0;
break;
901 case 3: ret += (
Char_t) tolower(c);
break;
902 case 4: ret += (
Char_t) toupper(c);
break;
903 default:
Error(
"TPMERegexp::Substitute",
"invalid state.");
943 Printf(
" %d - %s", i,
operator[](i).Data());
968 fReturnVoid (retVoid),
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).
TStringToken(const TString &fullStr, const TString &splitRe, Bool_t retVoid=kFALSE)
Constructor.
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,...)
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
static constexpr double s
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.