• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/Regex.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/text/Regex.h>
00018 #include <spl/collection/Vector.h>
00019 
00020 void Regex::Compile()
00021 {
00022         int er = regcomp(&m_regex, m_expr.GetChars(), m_flags);
00023         if (0 != er)
00024         {
00025                 char buf[256];
00026                 regerror(er, &m_regex, buf, 256);
00027                 throw new InvalidArgumentException(buf);
00028         }
00029 }
00030 
00031 Regex::Regex()
00032 : m_expr(""), m_flags(RegexOptions::NONE)
00033 {
00034         Compile();
00035 }
00036 
00037 Regex::Regex(const String& expr, RegexOptions::Options flags)
00038 : m_expr(expr), m_flags(flags)
00039 {
00040         Compile();
00041 }
00042 
00043 Regex::Regex(const Regex& re)
00044 : m_expr(re.m_expr), m_flags(re.m_flags)
00045 {
00046         Compile();
00047 }
00048 
00049 Regex::~Regex()
00050 {
00051         regfree(&m_regex);
00052 }
00053 
00054 Regex& Regex::operator =(const Regex& re)
00055 {
00056         regfree(&m_regex);
00057         m_expr = re.m_expr;
00058         m_flags = re.m_flags;
00059         Compile();
00060 
00061         return *this;
00062 }
00063 
00064 bool Regex::IsMatch(const String& txt) const
00065 {
00066         return 0 == regexec(&m_regex, txt.GetChars(), 0, NULL, 0);
00067 }
00068 
00069 RefCountPtr<Array<StringPtr> > Regex::Matches(const String& txt) const
00070 {
00071         Vector<StringPtr> matches;
00072         regmatch_t match;
00073 
00074         int ok = regexec(&m_regex, txt.GetChars(), 1, &match, 0);
00075         if (REG_NOMATCH == ok)
00076         {
00077                 return matches.ToArray();
00078         }
00079         if (0 != ok)
00080         {
00081                 char buf[256];
00082                 regerror(ok, &m_regex, buf, 256);
00083                 throw new InvalidArgumentException(buf);
00084         }
00085 
00086         int pos = 0;
00087         do
00088         {
00089                 pos += match.rm_so;
00090                 int len = match.rm_eo - match.rm_so;
00091                 matches.Add(StringPtr(new String(txt.GetChars(), pos, len)));
00092                 pos += len;
00093         }
00094         while (0 == (ok = regexec(&m_regex, txt.GetChars() + pos, 1, &match, REG_NOTBOL)));
00095 
00096         return matches.ToArray();
00097 }
00098 
00099 StringPtr Regex::Match(const String& txt, int offset) const
00100 {
00101         regmatch_t match;
00102         int ok = regexec(&m_regex, txt.GetChars() + offset, 1, &match, 0);
00103         if (REG_NOMATCH == ok)
00104         {
00105                 return StringPtr(new String());
00106         }
00107         if (0 != ok)
00108         {
00109                 char buf[256];
00110                 regerror(ok, &m_regex, buf, 256);
00111                 throw new InvalidArgumentException(buf);
00112         }
00113 
00114         return StringPtr(new String(txt.GetChars() + match.rm_so, match.rm_eo - match.rm_so));
00115 }
00116 
00117 #if defined(DEBUG)
00118 void Regex::CheckMem() const
00119 {
00120         m_expr.CheckMem();
00121 }
00122 
00123 void Regex::ValidateMem() const
00124 {
00125         m_expr.ValidateMem();
00126 }
00127 #endif
00128