cel_regex.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00004 //
00005 // (C) 2003-2006 Celartem Technology Inc. All rights reserved.
00006 //----------------------------------------------------------------------------
00007 
00008 #ifndef _cel_regex_h_
00009 #define _cel_regex_h_
00010 
00011 #include "cel_string.h"
00012 #include "cel_autoptr.h"
00013 
00014 namespace Celartem
00015 {
00019     enum RegexOptionFlag
00020     {
00021         regex_default = 0,       
00022         regex_multiline = 1,     
00023         regex_ignoreCase = 2,    
00024         regex_longest_match = 4, 
00025         regex_ignore_empty = 8,  
00026     };
00027 
00028     //------------------------------------------------------------------------
00034     //------------------------------------------------------------------------
00035     class RegEx : public Referable
00036     {
00037     public:
00052         virtual AutoPtr<Region> search(
00053             const UChar1 *str,
00054             const UChar1 *rangePtr = NULL, const UChar1 *rangeEnd = NULL,
00055             const UChar1 *strEnd = NULL) = 0;
00056 
00068         virtual AutoPtr<Region> match(
00069             const char *str,
00070             const char *strAt = NULL,
00071             const char *strEnd = NULL) = 0;
00072 
00084         static AutoPtr<RegEx> create(
00085             const UChar1 *inPattern, size_t length,
00086             int inOptionFlags = regex_default);
00087     };
00088 
00089     //------------------------------------------------------------------------
00141     //------------------------------------------------------------------------
00142     class RegularExpression
00143     {
00144     public:
00153         RegularExpression(
00154             const UChar1 *inPattern,
00155             int inOptionFlags = regex_default)
00156         {
00157             init(inPattern);
00158         }
00159 
00168         RegularExpression(
00169             const String& inPattern,
00170             int inOptionFlags = regex_default)
00171             : m_regex(RegEx::create(
00172                 inPattern.c_str(), inPattern.getLength(), inOptionFlags))
00173         {
00174             init(inPattern.c_str());
00175         }
00176 
00185         RegularExpression(
00186             const utf8s& inPattern,
00187             int inOptionFlags = regex_default)
00188         {
00189             init(inPattern.m_str);
00190         }
00191 
00200         void init(const UChar1 *inPattern,
00201             int inOptionFlags = regex_default)
00202         {
00203             m_regex = RegEx::create(
00204                 inPattern, StringUtil<UChar1>::strlen(inPattern),
00205                 inOptionFlags);
00206         }
00207 
00216         inline bool findFirst(const String& str)
00217         {
00218             m_region = m_regex->search(str.c_str());
00219             if(m_region.isValid())
00220                 m_region->lockString(str);
00221             return m_region.isValid();
00222         }
00223 
00239         inline bool findFirst(
00240             const UChar1 *str,
00241             const UChar1 *rangePtr = NULL, const UChar1 *rangeEnd = NULL,
00242             const UChar1 *strEnd = NULL)
00243         {
00244             m_region = m_regex->search(
00245                 str, rangePtr, rangeEnd, strEnd);
00246             return m_region.isValid();
00247         }
00248 
00255         inline bool findNext()
00256         {
00257             const UChar1* p = m_region->getPtr();
00258             m_region = m_regex->search(
00259                 m_region->getBufferPtr(),
00260                 m_region->getEndPtr(0), m_region->getRangeEndPtr(),
00261                 m_region->getBufferEndPtr());
00262             if(!m_region.isValid())
00263                 return false;
00264             if(p == m_region->getPtr())
00265                 return false;
00266             return true;
00267         }
00268 
00274         inline bool isVaild() const
00275         {
00276             return m_region.isValid();
00277         }
00278 
00285         inline size_t getCount() const
00286         {
00287             return m_region->getCount();
00288         }
00289 
00298         inline size_t getPos(size_t n = 0) const
00299         {
00300             return m_region->getPos(n);
00301         }
00302 
00311         inline size_t getEndPos(size_t n = 0) const
00312         {
00313             return m_region->getEndPos(n);
00314         }
00315 
00323         inline size_t getLength(size_t n = 0) const
00324         {
00325             return m_region->getLength(n);
00326         }
00327 
00335         inline const UChar1 *getPtr(size_t n = 0) const
00336         {
00337             return m_region->getPtr(n);
00338         }
00339 
00347         inline String getString(size_t n = 0) const
00348         {
00349             return String(utf8s(m_region->getPtr(n)), m_region->getLength(n));
00350         }
00351 
00352     private:
00353         AutoPtr<RegEx> m_regex;
00354         AutoPtr<Region> m_region;
00355     };
00356 
00357 } // namespace Celartem
00358 
00359 #endif // _cel_regex_h_

This document is automatically generated using doxygen 1.5.4 at Fri Jun 27 18:21:54 2008.