Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:20

0001 // @(#)root/base:$Id$
0002 // Author: Eddy Offermann   24/06/05
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TPRegexp
0013 #define ROOT_TPRegexp
0014 
0015 //////////////////////////////////////////////////////////////////////////
0016 //                                                                      //
0017 // TPRegexp                                                             //
0018 //                                                                      //
0019 // C++ Wrapper for the "Perl Compatible Regular Expressions" library    //
0020 //  The PCRE lib can be found at:                                       //
0021 //              http://www.pcre.org/                                    //
0022 //                                                                      //
0023 // Extensive documentation about Regular expressions in Perl can be     //
0024 // found at :                                                           //
0025 //              http://perldoc.perl.org/perlre.html                     //
0026 //                                                                      //
0027 //////////////////////////////////////////////////////////////////////////
0028 
0029 #include "Rtypes.h"
0030 #include "TString.h"
0031 #include "TArrayI.h"
0032 
0033 struct PCREPriv_t;
0034 
0035 
0036 class TPRegexp {
0037 
0038 protected:
0039    enum {
0040       kPCRE_GLOBAL     = 0x80000000,
0041       kPCRE_OPTIMIZE   = 0x40000000,
0042       kPCRE_DEBUG_MSGS = 0x20000000,
0043       kPCRE_INTMASK    = 0x0FFF
0044    };
0045 
0046    TString     fPattern;
0047    PCREPriv_t *fPriv;
0048    UInt_t      fPCREOpts;
0049 
0050    static Bool_t fgThrowAtCompileError;
0051 
0052    void     Compile();
0053    void     Optimize();
0054    UInt_t   ParseMods(const TString &mods) const;
0055    Int_t    ReplaceSubs(const TString &s, TString &final,
0056                         const TString &replacePattern,
0057                         Int_t *ovec, Int_t nmatch) const;
0058 
0059    Int_t    MatchInternal(const TString& s, Int_t start,
0060                           Int_t nMaxMatch, TArrayI *pos = nullptr) const;
0061 
0062    Int_t    SubstituteInternal(TString &s, const TString &replace,
0063                                Int_t start, Int_t nMaxMatch0,
0064                                Bool_t doDollarSubst) const;
0065 
0066 public:
0067    TPRegexp();
0068    TPRegexp(const TString &pat);
0069    TPRegexp(const TPRegexp &p);
0070    virtual ~TPRegexp();
0071 
0072    Bool_t     IsValid() const;
0073 
0074    Int_t      Match(const TString &s, const TString &mods="",
0075                     Int_t start=0, Int_t nMaxMatch=10, TArrayI *pos = nullptr);
0076    TObjArray *MatchS(const TString &s, const TString &mods="",
0077                      Int_t start=0, Int_t nMaxMatch=10);
0078    Bool_t     MatchB(const TString &s, const TString &mods="",
0079                      Int_t start=0, Int_t nMaxMatch=10) {
0080                            return (Match(s,mods,start,nMaxMatch) > 0); }
0081    Int_t      Substitute(TString &s, const TString &replace,
0082                          const TString &mods="", Int_t start=0,
0083                          Int_t nMatchMax=10);
0084 
0085    TString GetPattern()   const { return fPattern; }
0086    TString GetModifiers() const;
0087 
0088    TPRegexp &operator=(const TPRegexp &p);
0089 
0090    static Bool_t GetThrowAtCompileError();
0091    static void   SetThrowAtCompileError(Bool_t throwp);
0092 
0093    ClassDef(TPRegexp,0)  // Perl Compatible Regular Expression Class
0094 };
0095 
0096 
0097 class TPMERegexp : protected TPRegexp {
0098 
0099 private:
0100    TPMERegexp& operator=(const TPMERegexp&) = delete;
0101 
0102 protected:
0103    Int_t    fNMaxMatches;         // maximum number of matches
0104    Int_t    fNMatches;            // number of matches returned from last pcre_exec call
0105    TArrayI  fMarkers;             // last set of indexes of matches
0106 
0107    TString  fLastStringMatched;   // copy of the last TString matched
0108    void    *fAddressOfLastString; // used for checking for change of TString in global match
0109 
0110    Int_t    fLastGlobalPosition;  // end of last match when kPCRE_GLOBAL is set
0111 
0112 public:
0113    TPMERegexp();
0114    TPMERegexp(const TString& s, const TString& opts = "", Int_t nMatchMax = 10);
0115    TPMERegexp(const TString& s, UInt_t opts, Int_t nMatchMax = 10);
0116    TPMERegexp(const TPMERegexp& r);
0117 
0118    virtual ~TPMERegexp() {}
0119 
0120    void    Reset(const TString& s, const TString& opts = "", Int_t nMatchMax = -1);
0121    void    Reset(const TString& s, UInt_t opts, Int_t nMatchMax = -1);
0122 
0123    Int_t   GetNMaxMatches()   const { return fNMaxMatches; }
0124    void    SetNMaxMatches(Int_t nm) { fNMaxMatches = nm; }
0125 
0126    Int_t   GetGlobalPosition() const { return fLastGlobalPosition; }
0127    void    AssignGlobalState(const TPMERegexp& re);
0128    void    ResetGlobalState();
0129 
0130    Int_t   Match(const TString& s, UInt_t start = 0);
0131    Int_t   Split(const TString& s, Int_t maxfields = 0);
0132    Int_t   Substitute(TString& s, const TString& r, Bool_t doDollarSubst=kTRUE);
0133 
0134    Int_t   NMatches() const { return fNMatches; }
0135    TString operator[](Int_t);
0136 
0137    virtual void Print(Option_t* option="");
0138 
0139    ClassDefOverride(TPMERegexp, 0); // Wrapper for Perl-like regular expression matching.
0140 };
0141 
0142 
0143 class TStringToken : public TString {
0144 
0145 protected:
0146    const TString fFullStr;
0147    TPRegexp      fSplitRe;
0148    Bool_t        fReturnVoid;
0149    Int_t         fPos;
0150 
0151 public:
0152    TStringToken(const TString& fullStr, const TString& splitRe, Bool_t retVoid=kFALSE);
0153    virtual ~TStringToken() {}
0154 
0155    Bool_t NextToken();
0156    Bool_t AtEnd() const { return fPos >= fFullStr.Length(); }
0157 
0158    ClassDefOverride(TStringToken,0) // String tokenizer using PCRE for finding next tokens.
0159 };
0160 
0161 #endif