Class: Regexp
- Defined in:
- gems/carbuncle-support/mrblib/regexp_compat.rb,
.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb,
.doc_tmp/iij/mruby-regexp-pcre/src/mruby_regexp_pcre.c
Instance Attribute Summary collapse
-
#last_match ⇒ Object
readonly
Returns the value of attribute last_match.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #== ⇒ Object
- #===(str) ⇒ Object
- #=~(str) ⇒ Object
- #casefold? ⇒ Boolean
- #get_disable_option_string ⇒ Object
- #get_enable_option_string ⇒ Object
- #initialize ⇒ Object constructor
- #inspect ⇒ Object
- #match ⇒ Object
- #match?(*args) ⇒ Boolean
-
#name_push(name, index) ⇒ Object
private.
- #named_captures ⇒ Object
- #names ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File '.doc_tmp/iij/mruby-regexp-pcre/src/mruby_regexp_pcre.c', line 90
mrb_value
regexp_pcre_initialize(mrb_state *mrb, mrb_value self)
{
int erroff = 0, coptions;
const char *errstr = NULL;
struct mrb_regexp_pcre *reg = NULL;
mrb_value source, opt = mrb_nil_value();
unsigned char *name_table, *tabptr;
int i, namecount, name_entry_size;
mrb_get_args(mrb, "o|o", &source, &opt);
if (mrb_obj_is_kind_of(mrb, source, mrb_class_get(mrb, "Regexp"))) {
opt = mrb_iv_get(mrb, source, mrb_intern_lit(mrb, "@options"));
source = mrb_iv_get(mrb, source, mrb_intern_lit(mrb, "@source"));
}
source = mrb_string_type(mrb, source);
reg = (struct mrb_regexp_pcre *)DATA_PTR(self);
if (reg) {
mrb_regexp_free(mrb, reg);
}
DATA_TYPE(self) = &mrb_regexp_type;
DATA_PTR(self) = NULL;
reg = mrb_malloc(mrb, sizeof(struct mrb_regexp_pcre));
reg->re = NULL;
DATA_PTR(self) = reg;
coptions = mrb_mruby_to_pcre_options(opt);
source = mrb_str_new(mrb, RSTRING_PTR(source), RSTRING_LEN(source));
reg->re = pcre_compile(RSTRING_PTR(source), coptions, &errstr, &erroff, NULL);
if (reg->re == NULL) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid regular expression");
}
mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@source"), source);
mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@options"), mrb_fixnum_value(mrb_pcre_to_mruby_options(coptions)));
pcre_fullinfo(reg->re, NULL, PCRE_INFO_NAMECOUNT, &namecount);
if (namecount > 0) {
pcre_fullinfo(reg->re, NULL, PCRE_INFO_NAMETABLE, &name_table);
pcre_fullinfo(reg->re, NULL, PCRE_INFO_NAMEENTRYSIZE, &name_entry_size);
tabptr = name_table;
for (i = 0; i < namecount; i++) {
int n = (tabptr[0] << 8) | tabptr[1];
mrb_funcall(mrb, self, "name_push", 2, mrb_str_new(mrb, (const char *)(tabptr + 2), strlen((const char *)tabptr + 2)), mrb_fixnum_value(n));
tabptr += name_entry_size;
}
}
return self;
}
|
Instance Attribute Details
#last_match ⇒ Object (readonly)
Returns the value of attribute last_match.
4 5 6 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 4 def last_match @last_match end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 2 def @options end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
3 4 5 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 3 def source @source end |
Class Method Details
.compile(*args) ⇒ Object
10 11 12 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 10 def self.compile(*args) self.new(*args) end |
.escape(str) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 83 def self.escape(str) escape_table = { "\ " => '\\ ', # '?\ ' is a space "[" => '\\[', "]" => '\\]', "{" => '\\{', "}" => '\\}', "(" => '\\(', ")" => '\\)', "|" => '\\|', "-" => '\\-', "*" => '\\*', "." => '\\.', "\\" => '\\\\', "?" => '\\?', "+" => '\\+', "^" => '\\^', "$" => '\\$', "#" => '\\#', "\n" => '\\n', "\r" => '\\r', "\f" => '\\f', "\t" => '\\t', "\v" => '\\v', } s = "" str.each_char do |c| if escape_table[c] s += escape_table[c] else s += c end end s end |
.last_match ⇒ Object
14 15 16 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 14 def self.last_match @last_match end |
.quote(str) ⇒ Object
6 7 8 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 6 def self.quote(str) self.escape(str) end |
Instance Method Details
#== ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File '.doc_tmp/iij/mruby-regexp-pcre/src/mruby_regexp_pcre.c', line 210
static mrb_value
regexp_equal(mrb_state *mrb, mrb_value self)
{
mrb_value other;
struct mrb_regexp_pcre *self_reg, *other_reg;
mrb_get_args(mrb, "o", &other);
if (mrb_obj_equal(mrb, self, other)) {
return mrb_true_value();
}
if (mrb_type(other) != MRB_TT_DATA || DATA_TYPE(other) != &mrb_regexp_type) {
return mrb_false_value();
}
self_reg = (struct mrb_regexp_pcre *)DATA_PTR(self);
other_reg = (struct mrb_regexp_pcre *)DATA_PTR(other);
if (!self_reg || !other_reg) {
mrb_raise(mrb, E_RUNTIME_ERROR, "Invalid Regexp");
}
if (mrb_str_equal(mrb, mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@source")),
mrb_iv_get(mrb, other, mrb_intern_lit(mrb, "@source")))) {
return mrb_true_value();
}
return mrb_false_value();
}
|
#===(str) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 29 def ===(str) unless str.is_a? String if str.is_a? Symbol str = str.to_s else return false end end self.match(str) != nil end |
#=~(str) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 18 def =~(str) return nil unless str m = self.match(str) if m m.begin(0) else nil end end |
#casefold? ⇒ Boolean
40 41 42 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 40 def casefold? (@options & IGNORECASE) > 0 end |
#get_disable_option_string ⇒ Object
75 76 77 78 79 80 81 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 75 def get_disable_option_string s = "" s += "m" if @options & MULTILINE == 0 s += "i" if @options & IGNORECASE == 0 s += "x" if @options & EXTENDED == 0 s end |
#get_enable_option_string ⇒ Object
67 68 69 70 71 72 73 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 67 def get_enable_option_string s = "" s += "m" if @options & MULTILINE > 0 s += "i" if @options & IGNORECASE > 0 s += "x" if @options & EXTENDED > 0 s end |
#inspect ⇒ Object
59 60 61 62 63 64 65 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 59 def inspect s = "/" s += @source s += "/" s += get_enable_option_string s end |
#match ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File '.doc_tmp/iij/mruby-regexp-pcre/src/mruby_regexp_pcre.c', line 142
mrb_value
regexp_pcre_match(mrb_state *mrb, mrb_value self)
{
struct mrb_matchdata *mrb_md;
int rc;
int ccount, matchlen;
int *match;
struct RClass *c;
mrb_value md, str;
int i;
mrb_int pos;
pcre_extra extra;
struct mrb_regexp_pcre *reg;
reg = (struct mrb_regexp_pcre *)mrb_get_datatype(mrb, self, &mrb_regexp_type);
if (!reg)
return mrb_nil_value();
pos = 0;
mrb_get_args(mrb, "S|i", &str, &pos);
// XXX: RSTRING_LEN(str) >= pos ...
rc = pcre_fullinfo(reg->re, NULL, PCRE_INFO_CAPTURECOUNT, &ccount);
if (rc < 0) {
/* fullinfo error */
return mrb_nil_value();
}
matchlen = ccount + 1;
match = mrb_malloc(mrb, sizeof(int) * matchlen * 3);
extra.flags = PCRE_EXTRA_MATCH_LIMIT_RECURSION;
extra.match_limit_recursion = 1000;
rc = pcre_exec(reg->re, &extra, RSTRING_PTR(str), RSTRING_LEN(str), pos, 0, match, matchlen * 3);
if (rc < 0) {
mrb_free(mrb, match);
return mrb_nil_value();
}
/* XXX: need current scope */
mrb_obj_iv_set(mrb, (struct RObject *)mrb_class_real(RDATA(self)->c), mrb_intern_lit(mrb, "@last_match"), mrb_nil_value());
c = mrb_class_get(mrb, "MatchData");
md = mrb_funcall(mrb, mrb_obj_value(c), "new", 0);
mrb_md = (struct mrb_matchdata *)mrb_get_datatype(mrb, md, &mrb_matchdata_type);
mrb_md->ovector = match;
mrb_md->length = matchlen;
mrb_iv_set(mrb, md, mrb_intern_lit(mrb, "@regexp"), self);
mrb_iv_set(mrb, md, mrb_intern_lit(mrb, "@string"), mrb_str_dup(mrb, str));
/* XXX: need current scope */
mrb_obj_iv_set(mrb, (struct RObject *)mrb_class_real(RDATA(self)->c), mrb_intern_lit(mrb, "@last_match"), md);
mrb_gv_set(mrb, mrb_intern_lit(mrb, "$~"), md);
mrb_gv_set(mrb, mrb_intern_lit(mrb, "$&"), mrb_funcall(mrb, md, "to_s", 0));
mrb_gv_set(mrb, mrb_intern_lit(mrb, "$`"), mrb_funcall(mrb, md, "pre_match", 0));
mrb_gv_set(mrb, mrb_intern_lit(mrb, "$'"), mrb_funcall(mrb, md, "post_match", 0));
for (i = 1; i < 10; i++) {
char sym[8];
snprintf(sym, sizeof(sym), "$%d", i);
mrb_gv_set(mrb, mrb_intern_cstr(mrb, sym), mrb_funcall(mrb, md, "[]", 1, mrb_fixnum_value(i)));
}
return md;
}
|
#match?(*args) ⇒ Boolean
2 3 4 |
# File 'gems/carbuncle-support/mrblib/regexp_compat.rb', line 2 def match?(*args) match(*args.map(&:to_s)).present? end |
#name_push(name, index) ⇒ Object
private
143 144 145 146 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 143 def name_push(name, index) @names ||= {} @names[name.to_sym] = index - 1 end |
#named_captures ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 120 def named_captures h = {} if @names @names.each do |k, v| h[k.to_s] = [v + 1] end end h end |
#names ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 130 def names if @names ar = Array.new(@names.size) @names.each do |k, v| ar[v] = k.to_s end ar else [] end end |
#to_s ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File '.doc_tmp/iij/mruby-regexp-pcre/mrblib/regexp_pcre.rb', line 44 def to_s s = "(?" s += get_enable_option_string if @options & MULTILINE == 0 or @options & IGNORECASE == 0 or @options & EXTENDED == 0 s += "-" s += get_disable_option_string end s += ":" s += @source s += ")" s end |