Class: Regexp
- Inherits:
-
Object
- Object
- Regexp
- Defined in:
- mrbgems/mruby-regexp/src/regexp.c,
mrbgems/mruby-regexp/mrblib/regexp.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#== ⇒ Object
Regexp#== (and eql?).
-
#=== ⇒ Object
Regexp#===(str).
-
#=~ ⇒ Object
Regexp#=~(str).
-
#casefold? ⇒ Boolean
Regexp#casefold?.
-
#eql? ⇒ Boolean
Regexp#== (and eql?).
-
#hash ⇒ Object
Regexp#hash.
-
#initialize ⇒ Object
constructor
Regexp.new(pattern, flags=nil) Regexp.new(regexp) Regexp.compile(pattern, flags=nil).
-
#initialize_copy ⇒ Object
Regexp#initialize_copy - what dup and clone leave the copy holding.
- #inspect ⇒ Object
-
#match ⇒ Object
Regexp#match(str, pos=0).
-
#match? ⇒ Boolean
Regexp#match?(str, pos=0).
-
#named_captures ⇒ Object
Return named captures hash: => [group_number, ...], ... Hash is derived on every call and the caller cannot corrupt the table.
-
#names ⇒ Object
Return the capture names in group order.
-
#options ⇒ Object
Regexp#options - convert internal flags to Ruby constants Internal: IGNORECASE=1, MULTILINE=2, DOTALL=4, EXTENDED=8 Ruby: IGNORECASE=1, EXTENDED=2, MULTILINE=4.
-
#source ⇒ Object
Regexp#source.
-
#to_s ⇒ Object
Regexp#to_s - (?on-off:source) format.
Constructor Details
#initialize ⇒ Object
Regexp.new(pattern, flags=nil) Regexp.new(regexp) Regexp.compile(pattern, flags=nil)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 207 static mrb_value regexp_init(mrb_state *mrb, mrb_value self) { mrb_value pattern; mrb_value flags_val = mrb_nil_value(); uint32_t flags; mrb_get_args(mrb, "o|o", &pattern, &flags_val); /* If pattern is a Regexp, copy its source and flags */ if (mrb_obj_is_kind_of(mrb, pattern, mrb_class_get_id(mrb, MRB_SYM(Regexp)))) { flags = get_iflags(mrb, pattern); /* Copying is a reading of the argument's source, so an argument that has none is refused here rather than compiled from a nil below. */ pattern = re_check_initialized(mrb, pattern); } else { if (!mrb_string_p(pattern)) { mrb_raise(mrb, E_TYPE_ERROR, "wrong argument type (expected String or Regexp)"); } flags = parse_flags(mrb, flags_val); } return re_initialize(mrb, self, pattern, flags); } |
Class Method Details
.compile(pattern, *args) ⇒ Object
2 3 4 |
# File 'mrbgems/mruby-regexp/mrblib/regexp.rb', line 2 def self.compile(pattern, *args) new(pattern, *args) end |
Instance Method Details
#== ⇒ Object
Regexp#== (and eql?)
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 965 static mrb_value regexp_eql(mrb_state *mrb, mrb_value self) { mrb_value other; mrb_get_args(mrb, "o", &other); /* The one object equal to an uninitialized Regexp is itself: identity holds without reading either source, which is what CRuby answers first too. */ if (mrb_obj_eq(mrb, self, other)) return mrb_true_value(); if (!mrb_obj_is_kind_of(mrb, other, mrb_class_get_id(mrb, MRB_SYM(Regexp)))) { return mrb_false_value(); } mrb_value src1 = re_check_initialized(mrb, self); mrb_value src2 = re_check_initialized(mrb, other); if (!mrb_str_equal(mrb, src1, src2)) return mrb_false_value(); return mrb_bool_value(get_iflags(mrb, self) == get_iflags(mrb, other)); } |
#=== ⇒ Object
Regexp#===(str)
663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 663 static mrb_value regexp_case_match(mrb_state *mrb, mrb_value self) { mrb_value str, md; mrb_get_args(mrb, "o", &str); if (!mrb_string_p(str) && !mrb_symbol_p(str)) return mrb_false_value(); str = match_operand(mrb, str); md = exec_match(mrb, self, str, 0, FALSE, FALSE); return mrb_bool_value(!mrb_nil_p(md)); } |
#=~ ⇒ Object
Regexp#=~(str)
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 643 static mrb_value regexp_match_op(mrb_state *mrb, mrb_value self) { mrb_value str; mrb_get_args(mrb, "o", &str); if (mrb_nil_p(str)) { clear_match_globals(mrb); return mrb_nil_value(); } str = match_operand(mrb, str); mrb_value md = exec_match(mrb, self, str, 0, FALSE, FALSE); if (mrb_nil_p(md)) return mrb_nil_value(); mrb_match_data *m = DATA_GET_PTR(mrb, md, &matchdata_type, mrb_match_data); return mrb_int_value(mrb, re_byte_to_char(mrb, m->source, m->captures[0])); } |
#casefold? ⇒ Boolean
Regexp#casefold?
719 720 721 722 723 724 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 719 static mrb_value regexp_casefold_p(mrb_state *mrb, mrb_value self) { re_check_initialized(mrb, self); return mrb_bool_value((get_iflags(mrb, self) & RE_FLAG_IGNORECASE) != 0); } |
#eql? ⇒ Boolean
Regexp#== (and eql?)
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 965 static mrb_value regexp_eql(mrb_state *mrb, mrb_value self) { mrb_value other; mrb_get_args(mrb, "o", &other); /* The one object equal to an uninitialized Regexp is itself: identity holds without reading either source, which is what CRuby answers first too. */ if (mrb_obj_eq(mrb, self, other)) return mrb_true_value(); if (!mrb_obj_is_kind_of(mrb, other, mrb_class_get_id(mrb, MRB_SYM(Regexp)))) { return mrb_false_value(); } mrb_value src1 = re_check_initialized(mrb, self); mrb_value src2 = re_check_initialized(mrb, other); if (!mrb_str_equal(mrb, src1, src2)) return mrb_false_value(); return mrb_bool_value(get_iflags(mrb, self) == get_iflags(mrb, other)); } |
#hash ⇒ Object
Regexp#hash
985 986 987 988 989 990 991 992 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 985 static mrb_value regexp_hash(mrb_state *mrb, mrb_value self) { mrb_value src = re_check_initialized(mrb, self); uint32_t h = mrb_str_hash(mrb, src); h ^= get_iflags(mrb, self) * 0x9e3779b9; /* mix flags into hash */ return mrb_int_value(mrb, (mrb_int)h); } |
#initialize_copy ⇒ Object
Regexp#initialize_copy - what dup and clone leave the copy holding
The compiled pattern is not part of what mrb_iv_copy() carries over, and it cannot be: one pattern is owned by one object and freed with it, so a copy that took the original's pointer would hand regexp_free() the same block twice. Without this the copy kept the original's @source and @flags and nothing else, which made it a Regexp that answered source/options/to_s/== correctly and then refused every match on a NULL DATA_PTR. The copy compiles its own pattern from the same source and flags instead, which is what CRuby's rb_reg_init_copy() does.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 245 static mrb_value regexp_init_copy(mrb_state *mrb, mrb_value self) { mrb_value orig = mrb_get_arg1(mrb); if (mrb_obj_eq(mrb, self, orig)) return self; if (mrb_type(self) != mrb_type(orig) || mrb_obj_class(mrb, self) != mrb_obj_class(mrb, orig)) { mrb_raise(mrb, E_TYPE_ERROR, "initialize_copy should take same class object"); } /* Copying is a reading of the original's source, so an original that has none is refused here as it is in regexp_init(). */ mrb_value src = re_check_initialized(mrb, orig); return re_initialize(mrb, self, src, get_iflags(mrb, orig)); } |
#inspect ⇒ Object
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 934 static mrb_value regexp_inspect(mrb_state *mrb, mrb_value self) { mrb_value src = mrb_iv_get(mrb, self, MRB_IVSYM(source)); uint32_t flags; /* The one reader that answers for an uninitialized Regexp instead of raising: an inspect that raises would leave the object undisplayable in a backtrace or a debugger, which is where it is most likely to be met. CRuby's rb_reg_inspect falls back to the default form for the same reason, and this prints that same `#<Regexp:0x...>`. What it falls back on is the pair re_check_initialized() raises on, so the same objects are refused here and displayed rather than read: a written or inherited @source without a pattern behind it prints the default form, as it does in CRuby, where @source is not an IV at all and rb_reg_inspect tests the pattern for itself. */ if (!DATA_PTR(self) || !mrb_string_p(src)) return mrb_any_to_s(mrb, self); flags = get_iflags(mrb, self); mrb_value result = mrb_str_new_lit(mrb, "/"); mrb_str_cat_str(mrb, result, src); mrb_str_cat_lit(mrb, result, "/"); mrb_re_flags_cat(mrb, result, flags); return result; } |
#match ⇒ Object
Regexp#match(str, pos=0)
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 520 static mrb_value regexp_match(mrb_state *mrb, mrb_value self) { mrb_value str; mrb_value block = mrb_nil_value(); mrb_int pos = 0; mrb_value md; mrb_get_args(mrb, "o|i&", &str, &pos, &block); if (mrb_nil_p(str)) { clear_match_globals(mrb); return mrb_nil_value(); } str = match_operand(mrb, str); pos = re_char_to_byte(mrb, str, pos); if (pos < 0) { clear_match_globals(mrb); return mrb_nil_value(); } md = exec_match(mrb, self, str, pos, FALSE, FALSE); if (!mrb_nil_p(md) && !mrb_nil_p(block)) { return mrb_yield(mrb, block, md); } return md; } |
#match? ⇒ Boolean
Regexp#match?(str, pos=0)
631 632 633 634 635 636 637 638 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 631 static mrb_value regexp_match_p(mrb_state *mrb, mrb_value self) { mrb_value str; mrb_int pos = 0; mrb_get_args(mrb, "o|i", &str, &pos); return exec_match_p(mrb, self, str, pos); } |
#named_captures ⇒ Object
Return named captures hash: => [group_number, ...], ... Hash is derived on every call and the caller cannot corrupt the table.
9 10 11 12 13 14 15 16 |
# File 'mrbgems/mruby-regexp/mrblib/regexp.rb', line 9 def named_captures __check_initialized table = @named_captures return {} unless table result = {} table.each { |name, groups| result[name] = groups.dup } result end |
#names ⇒ Object
Return the capture names in group order
19 20 21 22 23 |
# File 'mrbgems/mruby-regexp/mrblib/regexp.rb', line 19 def names __check_initialized table = @named_captures table ? table.keys : [] end |
#options ⇒ Object
Regexp#options - convert internal flags to Ruby constants Internal: IGNORECASE=1, MULTILINE=2, DOTALL=4, EXTENDED=8 Ruby: IGNORECASE=1, EXTENDED=2, MULTILINE=4
704 705 706 707 708 709 710 711 712 713 714 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 704 static mrb_value regexp_options(mrb_state *mrb, mrb_value self) { re_check_initialized(mrb, self); uint32_t iflags = get_iflags(mrb, self); mrb_int opts = 0; if (iflags & RE_FLAG_IGNORECASE) opts |= 1; /* Regexp::IGNORECASE */ if (iflags & RE_FLAG_EXTENDED) opts |= 2; /* Regexp::EXTENDED */ if (iflags & RE_FLAG_MULTILINE) opts |= 4; /* Regexp::MULTILINE */ return mrb_fixnum_value(opts); } |
#source ⇒ Object
Regexp#source
693 694 695 696 697 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 693 static mrb_value regexp_source(mrb_state *mrb, mrb_value self) { return re_check_initialized(mrb, self); } |
#to_s ⇒ Object
Regexp#to_s - (?on-off:source) format
The flags that are off are named after a '-', and that run is left out only when none of them are. Spelling them out is what keeps the result meaningful once it is interpolated into another pattern: written as "(?i:a)", the embedded source in /#/a/ib/m would pick up the enclosing pattern's flags instead of carrying only its own.
A source that already opens with an option group has it folded into those flags rather than printed inside them, so the printed form names each option once. Regexp#inspect prints the source as written and is not touched; CRuby draws the same line.
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 |
# File 'mrbgems/mruby-regexp/src/regexp.c', line 903 static mrb_value regexp_to_s(mrb_state *mrb, mrb_value self) { mrb_value src = re_check_initialized(mrb, self); uint32_t flags = get_iflags(mrb, self); const char *ptr; mrb_int len; char off[RE_FLAG_LETTER_COUNT]; mrb_int noff = 0; re_fold_leading_group(mrb, src, &ptr, &len, &flags); mrb_value result = mrb_str_new_lit(mrb, "(?"); for (size_t i = 0; i < RE_FLAG_LETTER_COUNT; i++) { if (flags & re_flag_letters[i].bit) { mrb_str_cat(mrb, result, &re_flag_letters[i].letter, 1); } else { off[noff++] = re_flag_letters[i].letter; } } if (noff > 0) { mrb_str_cat_lit(mrb, result, "-"); mrb_str_cat(mrb, result, off, noff); } mrb_str_cat_lit(mrb, result, ":"); mrb_str_cat(mrb, result, ptr, len); mrb_str_cat_lit(mrb, result, ")"); return result; } |