Logo ROOT   6.10/09
Reference Guide
TPRegexp.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Eddy Offermann 24/06/05
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TPRegexp
13 #define ROOT_TPRegexp
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // TPRegexp //
18 // //
19 // C++ Wrapper for the "Perl Compatible Regular Expressions" library //
20 // The PCRE lib can be found at: //
21 // http://www.pcre.org/ //
22 // //
23 // Extensive documentation about Regular expressions in Perl can be //
24 // found at : //
25 // http://perldoc.perl.org/perlre.html //
26 // //
27 //////////////////////////////////////////////////////////////////////////
28 
29 #include "Rtypes.h"
30 #include "TString.h"
31 #include "TArrayI.h"
32 
33 struct PCREPriv_t;
34 
35 
36 class TPRegexp {
37 
38 protected:
39  enum {
40  kPCRE_GLOBAL = 0x80000000,
41  kPCRE_OPTIMIZE = 0x40000000,
42  kPCRE_DEBUG_MSGS = 0x20000000,
43  kPCRE_INTMASK = 0x0FFF
44  };
45 
47  PCREPriv_t *fPriv;
49 
51 
52  void Compile();
53  void Optimize();
54  UInt_t ParseMods(const TString &mods) const;
55  Int_t ReplaceSubs(const TString &s, TString &final,
56  const TString &replacePattern,
57  Int_t *ovec, Int_t nmatch) const;
58 
59  Int_t MatchInternal(const TString& s, Int_t start,
60  Int_t nMaxMatch, TArrayI *pos=0) const;
61 
62  Int_t SubstituteInternal(TString &s, const TString &replace,
63  Int_t start, Int_t nMaxMatch0,
64  Bool_t doDollarSubst) const;
65 
66 public:
67  TPRegexp();
68  TPRegexp(const TString &pat);
69  TPRegexp(const TPRegexp &p);
70  virtual ~TPRegexp();
71 
72  Bool_t IsValid() const;
73 
74  Int_t Match(const TString &s, const TString &mods="",
75  Int_t start=0, Int_t nMaxMatch=10, TArrayI *pos=0);
76  TObjArray *MatchS(const TString &s, const TString &mods="",
77  Int_t start=0, Int_t nMaxMatch=10);
78  Bool_t MatchB(const TString &s, const TString &mods="",
79  Int_t start=0, Int_t nMaxMatch=10) {
80  return (Match(s,mods,start,nMaxMatch) > 0); }
81  Int_t Substitute(TString &s, const TString &replace,
82  const TString &mods="", Int_t start=0,
83  Int_t nMatchMax=10);
84 
85  TString GetPattern() const { return fPattern; }
86  TString GetModifiers() const;
87 
88  TPRegexp &operator=(const TPRegexp &p);
89 
91  static void SetThrowAtCompileError(Bool_t throwp);
92 
93  ClassDef(TPRegexp,0) // Perl Compatible Regular Expression Class
94 };
95 
96 
97 class TPMERegexp : protected TPRegexp {
98 
99 private:
100  TPMERegexp& operator=(const TPMERegexp&); // Not implemented
101 
102 protected:
103  Int_t fNMaxMatches; // maximum number of matches
104  Int_t fNMatches; // number of matches returned from last pcre_exec call
105  TArrayI fMarkers; // last set of indexes of matches
106 
107  TString fLastStringMatched; // copy of the last TString matched
108  void *fAddressOfLastString; // used for checking for change of TString in global match
109 
110  Int_t fLastGlobalPosition; // end of last match when kPCRE_GLOBAL is set
111 
112 public:
113  TPMERegexp();
114  TPMERegexp(const TString& s, const TString& opts = "", Int_t nMatchMax = 10);
115  TPMERegexp(const TString& s, UInt_t opts, Int_t nMatchMax = 10);
116  TPMERegexp(const TPMERegexp& r);
117 
118  virtual ~TPMERegexp() {}
119 
120  void Reset(const TString& s, const TString& opts = "", Int_t nMatchMax = -1);
121  void Reset(const TString& s, UInt_t opts, Int_t nMatchMax = -1);
122 
123  Int_t GetNMaxMatches() const { return fNMaxMatches; }
124  void SetNMaxMatches(Int_t nm) { fNMaxMatches = nm; }
125 
126  Int_t GetGlobalPosition() const { return fLastGlobalPosition; }
127  void AssignGlobalState(const TPMERegexp& re);
128  void ResetGlobalState();
129 
130  Int_t Match(const TString& s, UInt_t start = 0);
131  Int_t Split(const TString& s, Int_t maxfields = 0);
132  Int_t Substitute(TString& s, const TString& r, Bool_t doDollarSubst=kTRUE);
133 
134  Int_t NMatches() const { return fNMatches; }
135  TString operator[](Int_t);
136 
137  virtual void Print(Option_t* option="");
138 
139  ClassDef(TPMERegexp, 0); // Wrapper for Perl-like regular expression matching.
140 };
141 
142 
143 class TStringToken : public TString {
144 
145 protected:
150 
151 public:
152  TStringToken(const TString& fullStr, const TString& splitRe, Bool_t retVoid=kFALSE);
153  virtual ~TStringToken() {}
154 
155  Bool_t NextToken();
156  Bool_t AtEnd() const { return fPos >= fFullStr.Length(); }
157 
158  ClassDef(TStringToken,0) // String tokenizer using PCRE for finding next tokens.
159 };
160 
161 #endif
Int_t NMatches() const
Definition: TPRegexp.h:134
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.
Definition: TPRegexp.cxx:393
TString fLastStringMatched
Definition: TPRegexp.h:107
#define Split(a, ahi, alo)
Definition: triangle.c:4775
Int_t GetGlobalPosition() const
Definition: TPRegexp.h:126
An array of TObjects.
Definition: TObjArray.h:37
Int_t fPos
Definition: TPRegexp.h:149
Int_t fNMatches
Definition: TPRegexp.h:104
Int_t MatchInternal(const TString &s, Int_t start, Int_t nMaxMatch, TArrayI *pos=0) const
Perform the actual matching - protected method.
Definition: TPRegexp.cxx:304
Bool_t MatchB(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
Definition: TPRegexp.h:78
const char Option_t
Definition: RtypesCore.h:62
TArrayI fMarkers
Definition: TPRegexp.h:105
Int_t ReplaceSubs(const TString &s, TString &final, const TString &replacePattern, Int_t *ovec, Int_t nmatch) const
Returns the number of expanded '$' constructs.
Definition: TPRegexp.cxx:248
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.
Definition: TPRegexp.cxx:367
static void SetThrowAtCompileError(Bool_t throwp)
Set static flag controlling whether exception should be thrown upon an error during regular expressio...
Definition: TPRegexp.cxx:505
virtual ~TPRegexp()
Cleanup.
Definition: TPRegexp.cxx:77
Basic string class.
Definition: TString.h:129
TString GetModifiers() const
Return PCRE modifier options as string.
Definition: TPRegexp.cxx:177
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
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...
Definition: TPRegexp.cxx:469
TPRegexp fSplitRe
Definition: TPRegexp.h:147
Array of integers (32 bits per element).
Definition: TArrayI.h:27
PCREPriv_t * fPriv
Definition: TPRegexp.h:47
Int_t fNMaxMatches
Definition: TPRegexp.h:103
#define ClassDef(name, id)
Definition: Rtypes.h:297
Provides iteration through tokens of a given string.
Definition: TPRegexp.h:143
void Compile()
Compile the fPattern.
Definition: TPRegexp.cxx:195
virtual ~TPMERegexp()
Definition: TPRegexp.h:118
Bool_t IsValid() const
Returns true if underlying PCRE structure has been successfully generated via regexp compilation...
Definition: TPRegexp.cxx:487
void SetNMaxMatches(Int_t nm)
Definition: TPRegexp.h:124
TRandom2 r(17)
void * fAddressOfLastString
Definition: TPRegexp.h:108
TPRegexp & operator=(const TPRegexp &p)
Assignment operator.
Definition: TPRegexp.cxx:89
unsigned int UInt_t
Definition: RtypesCore.h:42
Ssiz_t Length() const
Definition: TString.h:388
Bool_t AtEnd() const
Definition: TPRegexp.h:156
void Reset(Detail::TBranchProxy *x)
const Bool_t kFALSE
Definition: RtypesCore.h:92
UInt_t fPCREOpts
Definition: TPRegexp.h:48
void Print(std::ostream &os, const OptionType &opt)
virtual ~TStringToken()
Definition: TPRegexp.h:153
static Bool_t fgThrowAtCompileError
Definition: TPRegexp.h:50
UInt_t ParseMods(const TString &mods) const
Translate Perl modifier flags into pcre flags.
Definition: TPRegexp.cxx:132
static Bool_t GetThrowAtCompileError()
Get value of static flag controlling whether exception should be thrown upon an error during regular ...
Definition: TPRegexp.cxx:496
const TString fFullStr
Definition: TPRegexp.h:146
Wrapper for PCRE library (Perl Compatible Regular Expressions).
Definition: TPRegexp.h:97
Int_t fLastGlobalPosition
Definition: TPRegexp.h:110
Bool_t fReturnVoid
Definition: TPRegexp.h:148
Int_t GetNMaxMatches() const
Definition: TPRegexp.h:123
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.
Definition: TPRegexp.cxx:336
const Bool_t kTRUE
Definition: RtypesCore.h:91
TPRegexp()
Default ctor.
Definition: TPRegexp.cxx:48
TString GetPattern() const
Definition: TPRegexp.h:85
TString fPattern
Definition: TPRegexp.h:46
void Optimize()
Send the pattern through the optimizer.
Definition: TPRegexp.cxx:227