00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "../debug/DebugUtils.h"
00040 #include "Regex.h"
00041
00042 namespace oasys {
00043
00044 Regex::Regex(const char* regex, int cflags)
00045 {
00046 compilation_err_ = regcomp(®ex_, regex, cflags);
00047 }
00048
00049 Regex::~Regex()
00050 {
00051 if (compilation_err_ == 0)
00052 regfree(®ex_);
00053 }
00054
00055 int
00056 Regex::match(const char* str, int flags)
00057 {
00058 if (compilation_err_ != 0) {
00059 return compilation_err_;
00060 }
00061
00062 return regexec(®ex_, str, MATCH_LIMIT, matches_, flags);
00063 }
00064
00065 int
00066 Regex::match(const char* regex, const char* str, int cflags, int rflags)
00067 {
00068 Regex r(regex, cflags);
00069 return r.match(str, rflags);
00070 }
00071
00072 int
00073 Regex::num_matches()
00074 {
00075 for(size_t i = 0; i<MATCH_LIMIT; ++i) {
00076 if (matches_[i].rm_so == -1) {
00077 return i;
00078 }
00079 }
00080
00081 return MATCH_LIMIT;
00082 }
00083
00084 const regmatch_t&
00085 Regex::get_match(size_t i)
00086 {
00087 ASSERT(i <= MATCH_LIMIT);
00088 return matches_[i];
00089 }
00090
00091 std::string
00092 Regex::regerror_str(int err)
00093 {
00094 char buf[1024];
00095 size_t len = regerror(err, ®ex_, buf, sizeof(buf));
00096 return std::string(buf, len);
00097 }
00098
00099 Regsub::Regsub(const char* regex, const char* sub_spec, int flags)
00100 : Regex(regex, flags), sub_spec_(sub_spec)
00101 {
00102 }
00103
00104 Regsub::~Regsub()
00105 {
00106 }
00107
00108 int
00109 Regsub::subst(const char* str, std::string* result, int flags)
00110 {
00111 int match_err = match(str, flags);
00112 if (match_err != 0) {
00113 return match_err;
00114 }
00115
00116 size_t len = sub_spec_.length();
00117 size_t i = 0;
00118 int nmatches = num_matches();
00119
00120 result->clear();
00121
00122 while (i < len) {
00123 if (sub_spec_[i] == '\\') {
00124
00125
00126 char c = sub_spec_[i + 1];
00127
00128
00129 if (c == '\\') {
00130 result->push_back('\\');
00131 result->push_back('\\');
00132 i += 2;
00133 continue;
00134 }
00135
00136
00137 int match_num = c - '0';
00138 if ((match_num >= 0) && (match_num < nmatches))
00139 {
00140 regmatch_t* match = &matches_[match_num];
00141 result->append(str + match->rm_so, match->rm_eo - match->rm_so);
00142 i += 2;
00143 continue;
00144 }
00145 else
00146 {
00147
00148 result->clear();
00149 return REG_ESUBREG;;
00150 }
00151
00152 } else {
00153
00154 result->push_back(sub_spec_[i]);
00155 ++i;
00156 }
00157 }
00158
00159 return 0;
00160 }
00161
00162 int
00163 Regsub::subst(const char* regex, const char* str,
00164 const char* sub_spec, std::string* result,
00165 int cflags, int rflags)
00166 {
00167 Regsub r(regex, sub_spec, cflags);
00168 return r.subst(str, result, rflags);
00169 }
00170
00171 }