summaryrefslogtreecommitdiff
path: root/ext/strscan/strscan.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-12-28 14:55:43 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-12-28 14:55:43 +0000
commit1c0416e6eed8fa11e02c406fca40c9c1f6f87450 (patch)
tree6ccaf84184b95c99b6e80b3ff231bfa0c8a31a61 /ext/strscan/strscan.c
parentf413b838141884fef9cff48891b9a494d2d8f3aa (diff)
* ext/strscan/strscan.c (str_new): new function for allocate an string
with encoding propagation. (extract_range): use str_new. (extract_beg_len): ditto. (strscan_peek): ditto. (strscan_rest): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14772 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/strscan/strscan.c')
-rw-r--r--ext/strscan/strscan.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c
index fc4a7cf..8c94d1c 100644
--- a/ext/strscan/strscan.c
+++ b/ext/strscan/strscan.c
@@ -127,12 +127,20 @@ infect(VALUE str, struct strscanner *p)
}
static VALUE
+str_new(struct strscanner *p, const char *ptr, long len)
+{
+ VALUE str = rb_str_new(ptr, len);
+ rb_enc_copy(str, p->str);
+ return str;
+}
+
+static VALUE
extract_range(struct strscanner *p, long beg_i, long end_i)
{
if (beg_i > S_LEN(p)) return Qnil;
if (end_i > S_LEN(p))
end_i = S_LEN(p);
- return infect(rb_str_new(S_PBEG(p) + beg_i, end_i - beg_i), p);
+ return infect(str_new(p, S_PBEG(p) + beg_i, end_i - beg_i), p);
}
static VALUE
@@ -141,7 +149,7 @@ extract_beg_len(struct strscanner *p, long beg_i, long len)
if (beg_i > S_LEN(p)) return Qnil;
if (beg_i + len > S_LEN(p))
len = S_LEN(p) - beg_i;
- return infect(rb_str_new(S_PBEG(p) + beg_i, len), p);
+ return infect(str_new(p, S_PBEG(p) + beg_i, len), p);
}
/* =======================================================================
@@ -737,7 +745,7 @@ strscan_peek(VALUE self, VALUE vlen)
len = NUM2LONG(vlen);
if (EOS_P(p))
- return infect(rb_str_new("", 0), p);
+ return infect(str_new(p, "", 0), p);
if (p->curr + len > S_LEN(p))
len = S_LEN(p) - p->curr;
@@ -999,7 +1007,7 @@ strscan_rest(VALUE self)
GET_SCANNER(self, p);
if (EOS_P(p)) {
- return infect(rb_str_new("", 0), p);
+ return infect(str_new(p, "", 0), p);
}
return extract_range(p, p->curr, S_LEN(p));
}