summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authorYukihiro Matsumoto <matz@ruby-lang.org>1995-04-10 18:36:06 +0900
committerTakashi Kokubun <takashikkbn@gmail.com>2019-08-17 22:09:32 +0900
commitb2420d8ffa4d347a75efbbdc376f4ce65c0eb172 (patch)
treebd40c44d9155d9cb10232a0e962dc1cc221c2c8b /string.c
parent11e21a36bc935088f88a7cd1548f8c74c3bf6099 (diff)
version 0.73v0_73
https://cache.ruby-lang.org/pub/ruby/1.0/ruby-0.73.tar.gz Mon Apr 10 18:36:06 1995 Yukihiro Matsumoto (matz@ix-02) * version 0.73 Fri Apr 7 13:51:08 1995 Yukihiro Matsumoto (matz@ix-02) * cons.c->assoc.c: consの余計な機能は外してpairとしての機能だけを 残した.enumerableをincludeするのもやめた. * string.c(esub): 文字列置換イテレータ.perlのs///eの相当する.
Diffstat (limited to 'string.c')
-rw-r--r--string.c46
1 files changed, 43 insertions, 3 deletions
diff --git a/string.c b/string.c
index 73c1678974..c733faf9e4 100644
--- a/string.c
+++ b/string.c
@@ -674,15 +674,14 @@ str_sub(str, pat, val, once)
VALUE val;
int once;
{
- VALUE sub;
int beg, end, offset, n;
for (offset=0, n=0;
(beg=research(pat, str, offset)) >= 0;
offset=BEG(0)+STRLEN(val)) {
end = END(0)-1;
- sub = re_regsub(val);
- str_replace2(str, beg, end, sub);
+ val = re_regsub(val);
+ str_replace2(str, beg, END(0)-1, val);
n++;
if (once) break;
}
@@ -811,6 +810,37 @@ Fstr_gsub(str, pat, val)
return Fstr_sub_internal(str, pat, val, 0);
}
+static VALUE
+Fstr_esub(str, pat)
+ VALUE str, pat;
+{
+ VALUE val;
+ int beg, end, offset, n;
+
+ str_modify(str);
+ switch (TYPE(pat)) {
+ case T_REGEXP:
+ break;
+
+ case T_STRING:
+ pat = re_regcomp(pat);
+ break;
+
+ default:
+ /* type failed */
+ Check_Type(pat, T_REGEXP);
+ }
+
+ offset=0;
+ while ((beg=research(pat, str, offset)) >= 0) {
+ val = rb_yield(re_nth_match(0));
+ val = obj_as_string(val);
+ str_replace2(str, beg, END(0)-1, val);
+ offset=BEG(0)+STRLEN(val);
+ }
+ return (VALUE)str;
+}
+
extern VALUE rb_lastline;
static VALUE
@@ -830,6 +860,14 @@ Fgsub(obj, pat, val)
}
static VALUE
+Fesub(obj, pat)
+ VALUE obj, pat;
+{
+ Check_Type(rb_lastline, T_STRING);
+ return Fstr_esub(rb_lastline, pat);
+}
+
+static VALUE
Fstr_reverse(str)
struct RString *str;
{
@@ -1669,6 +1707,7 @@ Init_String()
rb_define_method(C_String, "sub", Fstr_sub, 2);
rb_define_method(C_String, "gsub", Fstr_gsub, 2);
+ rb_define_method(C_String, "esub", Fstr_esub, 1);
rb_define_method(C_String, "chop", Fstr_chop, 0);
rb_define_method(C_String, "strip", Fstr_strip, 0);
@@ -1684,6 +1723,7 @@ Init_String()
rb_define_private_method(C_Kernel, "sub", Fsub, 2);
rb_define_private_method(C_Kernel, "gsub", Fgsub, 2);
+ rb_define_private_method(C_Kernel, "esub", Fesub, 1);
pr_str = rb_intern("to_s");
}