| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Catalin Popescu |
|---|
| 31 |
|
|---|
| 32 |
#include "common/base/re.h" |
|---|
| 33 |
|
|---|
| 34 |
namespace re { |
|---|
| 35 |
|
|---|
| 36 |
const char* RE::ErrorName(int err) { |
|---|
| 37 |
switch ( err ) { |
|---|
| 38 |
case 0: return "REG_OK"; |
|---|
| 39 |
case REG_NOMATCH: |
|---|
| 40 |
return "REG_NOMATCH : regexec() failed to match."; |
|---|
| 41 |
case REG_BADPAT: |
|---|
| 42 |
return "REG_BADPAT : Invalid regular expression."; |
|---|
| 43 |
case REG_ECOLLATE: |
|---|
| 44 |
return "REG_ECOLLATE : Invalid collating element referenced."; |
|---|
| 45 |
case REG_ECTYPE: |
|---|
| 46 |
return "REG_ECTYPE : Invalid character class type referenced."; |
|---|
| 47 |
case REG_EESCAPE: |
|---|
| 48 |
return "REG_EESCAPE : Trailing '\\' in pattern."; |
|---|
| 49 |
case REG_ESUBREG: |
|---|
| 50 |
return "REG_ESUBREG : Number in \"\\digit\" invalid or in error."; |
|---|
| 51 |
case REG_EBRACK: |
|---|
| 52 |
return "REG_EBRACK : \"[]\" imbalance."; |
|---|
| 53 |
case REG_EPAREN: |
|---|
| 54 |
return "REG_EPAREN : \"\\(\\)\" or \"()\" imbalance."; |
|---|
| 55 |
case REG_EBRACE: |
|---|
| 56 |
return "REG_EBRACE : \"\\{\\}\" imbalance."; |
|---|
| 57 |
case REG_BADBR: |
|---|
| 58 |
return "REG_BADBR : Content of \"\\{\\}\" invalid: not a number, " |
|---|
| 59 |
"number too large, more than two numbers, first larger than second."; |
|---|
| 60 |
case REG_ERANGE: |
|---|
| 61 |
return "REG_ERANGE : Invalid endpoint in range expression."; |
|---|
| 62 |
case REG_ESPACE: |
|---|
| 63 |
return "REG_ESPACE : Out of memory."; |
|---|
| 64 |
case REG_BADRPT: |
|---|
| 65 |
return "REG_BADRPT : '?', '*', or '+' not preceded by valid " |
|---|
| 66 |
"regular expression."; |
|---|
| 67 |
} |
|---|
| 68 |
return "REG_UNKNOWN: unknown error"; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
RE::RE(const string& regex, int cflags) |
|---|
| 72 |
: regex_(regex), |
|---|
| 73 |
err_(0), |
|---|
| 74 |
match_begin_(true) { |
|---|
| 75 |
match_.rm_eo = 0; |
|---|
| 76 |
err_ = regcomp(®_, regex.c_str(), cflags); |
|---|
| 77 |
} |
|---|
| 78 |
RE::RE(const char* regex, int cflags) |
|---|
| 79 |
: regex_(regex), |
|---|
| 80 |
err_(0), |
|---|
| 81 |
match_begin_(true) { |
|---|
| 82 |
match_.rm_eo = 0; |
|---|
| 83 |
err_ = regcomp(®_, regex, cflags); |
|---|
| 84 |
} |
|---|
| 85 |
RE::~RE() { |
|---|
| 86 |
regfree(®_); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
bool RE::Matches(const char* s) { |
|---|
| 90 |
if ( err_ ) return false; |
|---|
| 91 |
const int status = regexec(®_, s, size_t(0), NULL, 0); |
|---|
| 92 |
return status == 0; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
bool RE::MatchNext(const char* s, string* ret) { |
|---|
| 96 |
if ( err_ ) return false; |
|---|
| 97 |
const int begin = match_begin_ ? 0 : match_.rm_eo; |
|---|
| 98 |
if ( !*(s + match_.rm_eo) ) |
|---|
| 99 |
return false; |
|---|
| 100 |
const int status = regexec(®_, s + match_.rm_eo, 1, &match_, |
|---|
| 101 |
match_begin_ ? 0 : REG_NOTBOL); |
|---|
| 102 |
if ( status != 0 ) { |
|---|
| 103 |
MatchEnd(); |
|---|
| 104 |
return false; |
|---|
| 105 |
} |
|---|
| 106 |
match_.rm_so += begin; |
|---|
| 107 |
match_.rm_eo += begin; |
|---|
| 108 |
ret->assign(s + match_.rm_so, match_.rm_eo - match_.rm_so); |
|---|
| 109 |
match_begin_ = false; |
|---|
| 110 |
return true; |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|