TPRegexp.h

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TPRegexp.h 26600 2008-12-02 18:56:10Z brun $
00002 // Author: Eddy Offermann   24/06/05
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TPRegexp
00013 #define ROOT_TPRegexp
00014 
00015 //////////////////////////////////////////////////////////////////////////
00016 //                                                                      //
00017 // TPRegexp                                                             //
00018 //                                                                      //
00019 // C++ Wrapper for the "Perl Compatible Regular Expressions" library    //
00020 //  The PCRE lib can be found at:                                       //
00021 //              http://www.pcre.org/                                    //
00022 //                                                                      //
00023 // Extensive documentation about Regular expressions in Perl can be     //
00024 // found at :                                                           //
00025 //              http://perldoc.perl.org/perlre.html                     //
00026 //                                                                      //
00027 //////////////////////////////////////////////////////////////////////////
00028 
00029 #ifndef ROOT_Rtypes
00030 #include "Rtypes.h"
00031 #endif
00032 #ifndef ROOT_TString
00033 #include "TString.h"
00034 #endif
00035 #ifndef ROOT_TArrayI
00036 #include "TArrayI.h"
00037 #endif
00038 
00039 struct PCREPriv_t;
00040 
00041 
00042 class TPRegexp {
00043 
00044 protected:
00045    enum {
00046       kPCRE_GLOBAL     = 0x80000000,
00047       kPCRE_OPTIMIZE   = 0x40000000,
00048       kPCRE_DEBUG_MSGS = 0x20000000,
00049       kPCRE_INTMASK    = 0x0FFF
00050    };
00051 
00052    TString     fPattern;
00053    PCREPriv_t *fPriv;
00054    UInt_t      fPCREOpts;
00055 
00056    void     Compile();
00057    void     Optimize();
00058    UInt_t   ParseMods(const TString &mods) const;
00059    Int_t    ReplaceSubs(const TString &s, TString &final,
00060                         const TString &replacePattern,
00061                         Int_t *ovec, Int_t nmatch) const;
00062 
00063    Int_t    MatchInternal(const TString& s, Int_t start,
00064                           Int_t nMaxMatch, TArrayI *pos=0);
00065 
00066    Int_t    SubstituteInternal(TString &s, const TString &replace,
00067                                Int_t start, Int_t nMaxMatch0,
00068                                Bool_t doDollarSubst);
00069 
00070 public:
00071    TPRegexp();
00072    TPRegexp(const TString &pat);
00073    TPRegexp(const TPRegexp &p);
00074    virtual ~TPRegexp();
00075 
00076    Int_t      Match(const TString &s, const TString &mods="",
00077                     Int_t start=0, Int_t nMaxMatch=10, TArrayI *pos=0);
00078    TObjArray *MatchS(const TString &s, const TString &mods="",
00079                      Int_t start=0, Int_t nMaxMatch=10);
00080    Bool_t     MatchB(const TString &s, const TString &mods="",
00081                      Int_t start=0, Int_t nMaxMatch=10) {
00082                            return (Match(s,mods,start,nMaxMatch) > 0); }
00083    Int_t      Substitute(TString &s, const TString &replace,
00084                          const TString &mods="", Int_t start=0,
00085                          Int_t nMatchMax=10);
00086 
00087    TString GetPattern()   const { return fPattern; }
00088    TString GetModifiers() const;
00089 
00090    TPRegexp &operator=(const TPRegexp &p);
00091 
00092    ClassDef(TPRegexp,0)  // Perl Compatible Regular Expression Class
00093 };
00094 
00095 
00096 class TPMERegexp : protected TPRegexp {
00097 
00098 private:
00099    TPMERegexp& operator=(const TPMERegexp&);  // Not implemented
00100 
00101 protected:
00102    Int_t    fNMaxMatches;         // maximum number of matches
00103    Int_t    fNMatches;            // number of matches returned from last pcre_exec call
00104    TArrayI  fMarkers;             // last set of indexes of matches
00105 
00106    TString  fLastStringMatched;   // copy of the last TString matched
00107    void    *fAddressOfLastString; // used for checking for change of TString in global match
00108 
00109    Int_t    fLastGlobalPosition;  // end of last match when kPCRE_GLOBAL is set
00110 
00111 public:
00112    TPMERegexp();
00113    TPMERegexp(const TString& s, const TString& opts = "", Int_t nMatchMax = 10);
00114    TPMERegexp(const TString& s, UInt_t opts, Int_t nMatchMax = 10);
00115    TPMERegexp(const TPMERegexp& r);
00116 
00117    virtual ~TPMERegexp() {}
00118 
00119    void    Reset(const TString& s, const TString& opts = "", Int_t nMatchMax = -1);
00120    void    Reset(const TString& s, UInt_t opts, Int_t nMatchMax = -1);
00121 
00122    Int_t   GetNMaxMatches()   const { return fNMaxMatches; }
00123    void    SetNMaxMatches(Int_t nm) { fNMaxMatches = nm; }
00124 
00125    Int_t   GetGlobalPosition() const { return fLastGlobalPosition; }
00126    void    AssignGlobalState(const TPMERegexp& re);
00127    void    ResetGlobalState();
00128 
00129    Int_t   Match(const TString& s, UInt_t start = 0);
00130    Int_t   Split(const TString& s, Int_t maxfields = 0);
00131    Int_t   Substitute(TString& s, const TString& r, Bool_t doDollarSubst=kTRUE);
00132 
00133    Int_t   NMatches() const { return fNMatches; }
00134    TString operator[](Int_t);
00135 
00136    virtual void Print(Option_t* option="");
00137 
00138    ClassDef(TPMERegexp, 0); // Wrapper for Perl-like regular expression matching.
00139 };
00140 
00141 
00142 class TStringToken : public TString {
00143 
00144 protected:
00145    const TString fFullStr;
00146    TPRegexp      fSplitRe;
00147    Bool_t        fReturnVoid;
00148    Int_t         fPos;
00149 
00150 public:
00151    TStringToken(const TString& fullStr, const TString& splitRe, Bool_t retVoid=kFALSE);
00152    virtual ~TStringToken() {}
00153 
00154    Bool_t NextToken();
00155    Bool_t AtEnd() const { return fPos >= fFullStr.Length(); }
00156 
00157    ClassDef(TStringToken,0) // String tokenizer using PCRE for finding next tokens.
00158 };
00159 
00160 #endif

Generated on Tue Jul 5 14:10:29 2011 for ROOT_528-00b_version by  doxygen 1.5.1