Home | History | Annotate | Download | only in Core
      1 //===-- RegularExpression.h -------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef liblldb_DBRegex_h_
     11 #define liblldb_DBRegex_h_
     12 #if defined(__cplusplus)
     13 
     14 #include <regex.h>
     15 #include <stdint.h>
     16 
     17 #include <string>
     18 #include <vector>
     19 
     20 namespace llvm
     21 {
     22     class StringRef;
     23 }
     24 
     25 namespace lldb_private {
     26 
     27 //----------------------------------------------------------------------
     28 /// @class RegularExpression RegularExpression.h "lldb/Core/RegularExpression.h"
     29 /// @brief A C++ wrapper class for regex.
     30 ///
     31 /// This regular expression class wraps the posix regex functions
     32 /// \c regcomp(), \c regerror(), \c regexec(), and \c regfree() from
     33 /// the header file in \c /usr/include/regex\.h.
     34 //----------------------------------------------------------------------
     35 class RegularExpression
     36 {
     37 public:
     38     class Match
     39     {
     40     public:
     41         Match (uint32_t max_matches) :
     42             m_matches ()
     43         {
     44             if (max_matches > 0)
     45                 m_matches.resize(max_matches + 1);
     46         }
     47 
     48         void
     49         Clear()
     50         {
     51             const size_t num_matches = m_matches.size();
     52             regmatch_t invalid_match = { -1, -1 };
     53             for (size_t i=0; i<num_matches; ++i)
     54                 m_matches[i] = invalid_match;
     55         }
     56 
     57         size_t
     58         GetSize () const
     59         {
     60             return m_matches.size();
     61         }
     62 
     63         regmatch_t *
     64         GetData ()
     65         {
     66             if (m_matches.empty())
     67                 return NULL;
     68             return m_matches.data();
     69         }
     70 
     71         bool
     72         GetMatchAtIndex (const char* s, uint32_t idx, std::string& match_str) const;
     73 
     74         bool
     75         GetMatchAtIndex (const char* s, uint32_t idx, llvm::StringRef& match_str) const;
     76 
     77         bool
     78         GetMatchSpanningIndices (const char* s, uint32_t idx1, uint32_t idx2, llvm::StringRef& match_str) const;
     79 
     80     protected:
     81 
     82         std::vector<regmatch_t> m_matches; ///< Where parenthesized subexpressions results are stored
     83     };
     84     //------------------------------------------------------------------
     85     /// Default constructor.
     86     ///
     87     /// The default constructor that initializes the object state such
     88     /// that it contains no compiled regular expression.
     89     //------------------------------------------------------------------
     90     RegularExpression ();
     91 
     92     //------------------------------------------------------------------
     93     /// Constructor that takes a regulare expression with flags.
     94     ///
     95     /// Constructor that compiles \a re using \a flags and stores the
     96     /// resulting compiled regular expression into this object.
     97     ///
     98     /// @param[in] re
     99     ///     A c string that represents the regular expression to
    100     ///     compile.
    101     ///
    102     /// @param[in] flags
    103     ///     Flags that are passed the the \c regcomp() function.
    104     //------------------------------------------------------------------
    105     explicit
    106     RegularExpression (const char* re, int flags);
    107 
    108     // This one uses flags = REG_EXTENDED.
    109     explicit
    110     RegularExpression (const char* re);
    111 
    112     //------------------------------------------------------------------
    113     /// Destructor.
    114     ///
    115     /// Any previosuly compiled regular expression contained in this
    116     /// object will be freed.
    117     //------------------------------------------------------------------
    118     ~RegularExpression ();
    119 
    120     RegularExpression (const RegularExpression &rhs);
    121 
    122     const RegularExpression & operator=(const RegularExpression &rhs);
    123 
    124     //------------------------------------------------------------------
    125     /// Compile a regular expression.
    126     ///
    127     /// Compile a regular expression using the supplied regular
    128     /// expression text and flags. The compied regular expression lives
    129     /// in this object so that it can be readily used for regular
    130     /// expression matches. Execute() can be called after the regular
    131     /// expression is compiled. Any previosuly compiled regular
    132     /// expression contained in this object will be freed.
    133     ///
    134     /// @param[in] re
    135     ///     A NULL terminated C string that represents the regular
    136     ///     expression to compile.
    137     ///
    138     /// @param[in] flags
    139     ///     Flags that are passed the the \c regcomp() function.
    140     ///
    141     /// @return
    142     ///     \b true if the regular expression compiles successfully,
    143     ///     \b false otherwise.
    144     //------------------------------------------------------------------
    145     bool
    146     Compile (const char* re);
    147 
    148     bool
    149     Compile (const char* re, int flags);
    150 
    151     //------------------------------------------------------------------
    152     /// Executes a regular expression.
    153     ///
    154     /// Execute a regular expression match using the compiled regular
    155     /// expression that is already in this object against the match
    156     /// string \a s. If any parens are used for regular expression
    157     /// matches \a match_count should indicate the number of regmatch_t
    158     /// values that are present in \a match_ptr. The regular expression
    159     /// will be executed using the \a execute_flags
    160     ///
    161     /// @param[in] string
    162     ///     The string to match against the compile regular expression.
    163     ///
    164     /// @param[in] match
    165     ///     A pointer to a RegularExpression::Match structure that was
    166     ///     properly initialized with the desired number of maximum
    167     ///     matches, or NULL if no parenthesized matching is needed.
    168     ///
    169     /// @param[in] execute_flags
    170     ///     Flags to pass to the \c regexec() function.
    171     ///
    172     /// @return
    173     ///     \b true if \a string matches the compiled regular
    174     ///     expression, \b false otherwise.
    175     //------------------------------------------------------------------
    176     bool
    177     Execute (const char* string, Match *match = NULL, int execute_flags = 0) const;
    178 
    179     size_t
    180     GetErrorAsCString (char *err_str, size_t err_str_max_len) const;
    181 
    182     //------------------------------------------------------------------
    183     /// Free the compiled regular expression.
    184     ///
    185     /// If this object contains a valid compiled regular expression,
    186     /// this function will free any resources it was consuming.
    187     //------------------------------------------------------------------
    188     void
    189     Free ();
    190 
    191     //------------------------------------------------------------------
    192     /// Access the regular expression text.
    193     ///
    194     /// Returns the text that was used to compile the current regular
    195     /// expression.
    196     ///
    197     /// @return
    198     ///     The NULL terminated C string that was used to compile the
    199     ///     current regular expression
    200     //------------------------------------------------------------------
    201     const char*
    202     GetText () const;
    203 
    204     int
    205     GetCompileFlags () const
    206     {
    207         return m_compile_flags;
    208     }
    209 
    210     //------------------------------------------------------------------
    211     /// Test if valid.
    212     ///
    213     /// Test if this object contains a valid regular expression.
    214     ///
    215     /// @return
    216     ///     \b true if the regular expression compiled and is ready
    217     ///     for execution, \b false otherwise.
    218     //------------------------------------------------------------------
    219     bool
    220     IsValid () const;
    221 
    222     void
    223     Clear ()
    224     {
    225         Free();
    226         m_re.clear();
    227         m_compile_flags = 0;
    228         m_comp_err = 1;
    229     }
    230 
    231     int
    232     GetErrorCode() const
    233     {
    234         return m_comp_err;
    235     }
    236 
    237     bool
    238     operator < (const RegularExpression& rhs) const;
    239 
    240 private:
    241     //------------------------------------------------------------------
    242     // Member variables
    243     //------------------------------------------------------------------
    244     std::string m_re;   ///< A copy of the original regular expression text
    245     int m_comp_err;     ///< Error code for the regular expression compilation
    246     regex_t m_preg;     ///< The compiled regular expression
    247     int     m_compile_flags; ///< Stores the flags from the last compile.
    248 };
    249 
    250 } // namespace lldb_private
    251 
    252 #endif  // #if defined(__cplusplus)
    253 #endif  // liblldb_DBRegex_h_
    254