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;
146 opts |= PCRE_CASELESS;
149 opts |= PCRE_MULTILINE;
158 opts |= PCRE_EXTENDED;
164 Error(
"ParseMods",
"illegal pattern modifier: %c", *m);
181 if (
fPCREOpts & PCRE_CASELESS) ret +=
'i';
182 if (
fPCREOpts & PCRE_MULTILINE) ret +=
'm';
184 if (
fPCREOpts & PCRE_EXTENDED) ret +=
'x';
197 pcre_free(
fPriv->fPCRE);
205 &errstr, &patIndex, 0);
209 throw std::runtime_error
210 (
TString::Format(
"TPRegexp::Compile() compilation of TPRegexp(%s) failed at: %d because %s",
213 Error(
"Compile",
"compilation of TPRegexp(%s) failed at: %d because %s",
228 if (
fPriv->fPCREExtra)
229 pcre_free(
fPriv->fPCREExtra);
236 fPriv->fPCREExtra = pcre_study(
fPriv->fPCRE, 0, &errstr);
238 if (!
fPriv->fPCREExtra && errstr) {
239 Error(
"Optimize",
"Optimization of TPRegexp(%s) failed: %s",
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());
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]);
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);
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
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;
437 if (offVec[0] != offVec[1])
441 if (offVec[1] == s.
Length())
443 offset = offVec[1]+1;
449 final += s(last,s.
Length()-last);
488 return fPriv->fPCRE != 0;
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)
603 fNMaxMatches(nMatchMax),
605 fAddressOfLastString(0),
606 fLastGlobalPosition(0)
621 fNMaxMatches(nMatchMax),
623 fAddressOfLastString(0),
624 fLastGlobalPosition(0)
637 fNMaxMatches(r.fNMaxMatches),
639 fAddressOfLastString(0),
640 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),
A zero length substring is legal.
Bool_t IsValid() const
Returns true if underlying PCRE structure has been successfully generated via regexp compilation...
TString fLastStringMatched
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.
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.
void ToLower()
Change string to lower-case.
TString GetModifiers() const
Return PCRE modifier options as string.
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).
const char * Data() const
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
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.
UInt_t ParseMods(const TString &mods) const
Translate Perl modifier flags into pcre flags.
TPMERegexp()
Default constructor. This regexp will match an empty string.
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.
Int_t MatchInternal(const TString &s, Int_t start, Int_t nMaxMatch, TArrayI *pos=0) const
Perform the actual matching - protected method.
Bool_t NextToken()
Get the next token, it is stored in this TString.
static Bool_t fgThrowAtCompileError
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
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Int_t ReplaceSubs(const TString &s, TString &final, const TString &replacePattern, Int_t *ovec, Int_t nmatch) const
Returns the number of expanded '$' constructs.
void Reset(const TString &s, const TString &opts="", Int_t nMatchMax=-1)
Reset the pattern and options.
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
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.
void ResetGlobalState()
Reset state of global match.