Logo ROOT   6.08/07
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 #ifndef ROOT_Rtypes
30 #include "Rtypes.h"
31 #endif
32 #ifndef ROOT_TString
33 #include "TString.h"
34 #endif
35 #ifndef ROOT_TArrayI
36 #include "TArrayI.h"
37 #endif
38 
39 struct PCREPriv_t;
40 
41 
42 class TPRegexp {
43 
44 protected:
45  enum {
46  kPCRE_GLOBAL = 0x80000000,
47  kPCRE_OPTIMIZE = 0x40000000,
48  kPCRE_DEBUG_MSGS = 0x20000000,
49  kPCRE_INTMASK = 0x0FFF
50  };
51 
53  PCREPriv_t *fPriv;
55 
57 
58  void Compile();
59  void Optimize();
60  UInt_t ParseMods(const TString &mods) const;
61  Int_t ReplaceSubs(const TString &s, TString &final,
62  const TString &replacePattern,
63  Int_t *ovec, Int_t nmatch) const;
64 
65  Int_t MatchInternal(const TString& s, Int_t start,
66  Int_t nMaxMatch, TArrayI *pos=0) const;
67 
68  Int_t SubstituteInternal(TString &s, const TString &replace,
69  Int_t start, Int_t nMaxMatch0,
70  Bool_t doDollarSubst) const;
71 
72 public:
73  TPRegexp();
74  TPRegexp(const TString &pat);
75  TPRegexp(const TPRegexp &p);
76  virtual ~TPRegexp();
77 
78  Bool_t IsValid() const;
79 
80  Int_t Match(const TString &s, const TString &mods="",
81  Int_t start=0, Int_t nMaxMatch=10, TArrayI *pos=0);
82  TObjArray *MatchS(const TString &s, const TString &mods="",
83  Int_t start=0, Int_t nMaxMatch=10);
84  Bool_t MatchB(const TString &s, const TString &mods="",
85  Int_t start=0, Int_t nMaxMatch=10) {
86  return (Match(s,mods,start,nMaxMatch) > 0); }
87  Int_t Substitute(TString &s, const TString &replace,
88  const TString &mods="", Int_t start=0,
89  Int_t nMatchMax=10);
90 
91  TString GetPattern() const { return fPattern; }
92  TString GetModifiers() const;
93 
94  TPRegexp &operator=(const TPRegexp &p);
95 
97  static void SetThrowAtCompileError(Bool_t throwp);
98 
99  ClassDef(TPRegexp,0) // Perl Compatible Regular Expression Class
100 };
101 
102 
103 class TPMERegexp : protected TPRegexp {
104 
105 private:
106  TPMERegexp& operator=(const TPMERegexp&); // Not implemented
107 
108 protected:
109  Int_t fNMaxMatches; // maximum number of matches
110  Int_t fNMatches; // number of matches returned from last pcre_exec call
111  TArrayI fMarkers; // last set of indexes of matches
112 
113  TString fLastStringMatched; // copy of the last TString matched
114  void *fAddressOfLastString; // used for checking for change of TString in global match
115 
116  Int_t fLastGlobalPosition; // end of last match when kPCRE_GLOBAL is set
117 
118 public:
119  TPMERegexp();
120  TPMERegexp(const TString& s, const TString& opts = "", Int_t nMatchMax = 10);
121  TPMERegexp(const TString& s, UInt_t opts, Int_t nMatchMax = 10);
122  TPMERegexp(const TPMERegexp& r);
123 
124  virtual ~TPMERegexp() {}
125 
126  void Reset(const TString& s, const TString& opts = "", Int_t nMatchMax = -1);
127  void Reset(const TString& s, UInt_t opts, Int_t nMatchMax = -1);
128 
129  Int_t GetNMaxMatches() const { return fNMaxMatches; }
130  void SetNMaxMatches(Int_t nm) { fNMaxMatches = nm; }
131 
132  Int_t GetGlobalPosition() const { return fLastGlobalPosition; }
133  void AssignGlobalState(const TPMERegexp& re);
134  void ResetGlobalState();
135 
136  Int_t Match(const TString& s, UInt_t start = 0);
137  Int_t Split(const TString& s, Int_t maxfields = 0);
138  Int_t Substitute(TString& s, const TString& r, Bool_t doDollarSubst=kTRUE);
139 
140  Int_t NMatches() const { return fNMatches; }
141  TString operator[](Int_t);
142 
143  virtual void Print(Option_t* option="");
144 
145  ClassDef(TPMERegexp, 0); // Wrapper for Perl-like regular expression matching.
146 };
147 
148 
149 class TStringToken : public TString {
150 
151 protected:
156 
157 public:
158  TStringToken(const TString& fullStr, const TString& splitRe, Bool_t retVoid=kFALSE);
159  virtual ~TStringToken() {}
160 
161  Bool_t NextToken();
162  Bool_t AtEnd() const { return fPos >= fFullStr.Length(); }
163 
164  ClassDef(TStringToken,0) // String tokenizer using PCRE for finding next tokens.
165 };
166 
167 #endif
Int_t NMatches() const
Definition: TPRegexp.h:140
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:113
#define Split(a, ahi, alo)
Definition: triangle.c:4775
Int_t GetGlobalPosition() const
Definition: TPRegexp.h:132
An array of TObjects.
Definition: TObjArray.h:39
Int_t fPos
Definition: TPRegexp.h:155
Int_t fNMatches
Definition: TPRegexp.h:110
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:84
const char Option_t
Definition: RtypesCore.h:62
TArrayI fMarkers
Definition: TPRegexp.h:111
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:137
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
const Bool_t kFALSE
Definition: Rtypes.h:92
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:153
Array of integers (32 bits per element).
Definition: TArrayI.h:29
PCREPriv_t * fPriv
Definition: TPRegexp.h:53
Int_t fNMaxMatches
Definition: TPRegexp.h:109
#define ClassDef(name, id)
Definition: Rtypes.h:254
Provides iteration through tokens of a given string.
Definition: TPRegexp.h:149
void Compile()
Compile the fPattern.
Definition: TPRegexp.cxx:195
virtual ~TPMERegexp()
Definition: TPRegexp.h:124
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:130
TRandom2 r(17)
void * fAddressOfLastString
Definition: TPRegexp.h:114
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:390
Bool_t AtEnd() const
Definition: TPRegexp.h:162
void Reset(Detail::TBranchProxy *x)
UInt_t fPCREOpts
Definition: TPRegexp.h:54
void Print(std::ostream &os, const OptionType &opt)
virtual ~TStringToken()
Definition: TPRegexp.h:159
static Bool_t fgThrowAtCompileError
Definition: TPRegexp.h:56
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:152
Wrapper for PCRE library (Perl Compatible Regular Expressions).
Definition: TPRegexp.h:103
Int_t fLastGlobalPosition
Definition: TPRegexp.h:116
Bool_t fReturnVoid
Definition: TPRegexp.h:154
Int_t GetNMaxMatches() const
Definition: TPRegexp.h:129
const Bool_t kTRUE
Definition: Rtypes.h:91
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
TPRegexp()
Default ctor.
Definition: TPRegexp.cxx:48
TString GetPattern() const
Definition: TPRegexp.h:91
TString fPattern
Definition: TPRegexp.h:52
void Optimize()
Send the pattern through the optimizer.
Definition: TPRegexp.cxx:227