Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
46  TString fPattern;
47  PCREPriv_t *fPriv;
48  UInt_t fPCREOpts;
49 
50  static Bool_t fgThrowAtCompileError;
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 
90  static Bool_t GetThrowAtCompileError();
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:
146  const TString fFullStr;
147  TPRegexp fSplitRe;
148  Bool_t fReturnVoid;
149  Int_t fPos;
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