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;
150 opts |= PCRE_CASELESS;
153 opts |= PCRE_MULTILINE;
162 opts |= PCRE_EXTENDED;
168 Error(
"ParseMods",
"illegal pattern modifier: %c", *
m);
185 if (
fPCREOpts & PCRE_CASELESS) ret +=
'i';
186 if (
fPCREOpts & PCRE_MULTILINE) ret +=
'm';
188 if (
fPCREOpts & PCRE_EXTENDED) ret +=
'x';
201 pcre_free(
fPriv->fPCRE);
209 &errstr, &patIndex, 0);
213 throw std::runtime_error
214 (
TString::Format(
"TPRegexp::Compile() compilation of TPRegexp(%s) failed at: %d because %s",
217 Error(
"Compile",
"compilation of TPRegexp(%s) failed at: %d because %s",
232 if (
fPriv->fPCREExtra)
233 pcre_free(
fPriv->fPCREExtra);
240 fPriv->fPCREExtra = pcre_study(
fPriv->fPCRE, 0, &errstr);
242 if (!
fPriv->fPCREExtra && errstr) {
243 Error(
"Optimize",
"Optimization of TPRegexp(%s) failed: %s",
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());
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]);
313 s.Length(), start, 0,
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);
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;
412 s.Length(), offset, 0,
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);
433 final += replacePattern;
441 if (offVec[0] != offVec[1])
445 if (offVec[1] ==
s.Length())
447 offset = offVec[1]+1;
453 final +=
s(last,
s.Length()-last);
492 return fPriv->fPCRE != 0;
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)
607 fNMaxMatches(nMatchMax),
609 fAddressOfLastString(0),
610 fLastGlobalPosition(0)
625 fNMaxMatches(nMatchMax),
627 fAddressOfLastString(0),
628 fLastGlobalPosition(0)
641 fNMaxMatches(
r.fNMaxMatches),
643 fAddressOfLastString(0),
644 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();
839 Bool_t last_empty = (nOffset ==
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;
880 Ssiz_t pos = 0, len =
s.Length();
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),
void Info(const char *location, const char *msgfmt,...)
void Error(const char *location, const char *msgfmt,...)
Array of integers (32 bits per element).
void Set(Int_t n)
Set size of this array to n ints.
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
Collectable string class.
Wrapper for PCRE library (Perl Compatible Regular Expressions).
Int_t fLastGlobalPosition
void ResetGlobalState()
Reset state of global match.
void * fAddressOfLastString
virtual void Print(Option_t *option="")
Print the regular expression and modifier options.
Int_t Split(const TString &s, Int_t maxfields=0)
Splits into at most maxfields.
TPMERegexp()
Default constructor. This regexp will match an empty string.
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...
TString operator[](Int_t)
Returns the sub-string from the internal fMarkers vector.
Int_t Match(const TString &s, UInt_t start=0)
Runs a match on s against the regex 'this' was created with.
void Reset(const TString &s, const TString &opts="", Int_t nMatchMax=-1)
Reset the pattern and options.
TString fLastStringMatched
void AssignGlobalState(const TPMERegexp &re)
Copy global-match state from 're; so that this regexp can continue parsing the string from where 're'...
void Compile()
Compile the fPattern.
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.
Bool_t IsValid() const
Returns true if underlying PCRE structure has been successfully generated via regexp compilation.
TPRegexp & operator=(const TPRegexp &p)
Assignment operator.
UInt_t ParseMods(const TString &mods) const
Translate Perl modifier flags into pcre flags.
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.
Int_t MatchInternal(const TString &s, Int_t start, Int_t nMaxMatch, TArrayI *pos=0) const
Perform the actual matching - protected method.
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 Bool_t fgThrowAtCompileError
Int_t ReplaceSubs(const TString &s, TString &final, const TString &replacePattern, Int_t *ovec, Int_t nmatch) const
Returns the number of expanded '$' constructs.
virtual ~TPRegexp()
Cleanup.
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...
TString GetModifiers() const
Return PCRE modifier options as string.
static Bool_t GetThrowAtCompileError()
Get value of static flag controlling whether exception should be thrown upon an error during regular ...
void Optimize()
Send the pattern through the optimizer.
static void SetThrowAtCompileError(Bool_t throwp)
Set static flag controlling whether exception should be thrown upon an error during regular expressio...
Provides iteration through tokens of a given string.
TStringToken(const TString &fullStr, const TString &splitRe, Bool_t retVoid=kFALSE)
Constructor.
Bool_t NextToken()
Get the next token, it is stored in this TString.
char & operator()(Ssiz_t i)
void ToLower()
Change string to lower-case.
const char * Data() const
TString & operator=(char s)
Assign character c to TString.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
A zero length substring is legal.
static constexpr double s