summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--include/ruby/intern.h1
-rw-r--r--string.c34
3 files changed, 39 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index c320605570..b6eddbd944 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Tue Aug 12 19:07:02 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * string.c (rb_str_drop): new function to drop first bytes.
+
Tue Aug 12 18:58:48 2008 Koichi Sasada <ko1@atdot.net>
* vm.c, vm_insnhelper.c (vm_define_method): move
diff --git a/include/ruby/intern.h b/include/ruby/intern.h
index 58432e3fe8..fb5e1380f3 100644
--- a/include/ruby/intern.h
+++ b/include/ruby/intern.h
@@ -580,6 +580,7 @@ int rb_str_hash_cmp(VALUE,VALUE);
int rb_str_comparable(VALUE, VALUE);
int rb_str_cmp(VALUE, VALUE);
VALUE rb_str_equal(VALUE str1, VALUE str2);
+VALUE rb_str_drop(VALUE, long);
void rb_str_update(VALUE, long, long, VALUE);
VALUE rb_str_inspect(VALUE);
VALUE rb_str_dump(VALUE);
diff --git a/string.c b/string.c
index 9262dceea1..fbe6ea88bb 100644
--- a/string.c
+++ b/string.c
@@ -2829,9 +2829,43 @@ rb_str_aref_m(int argc, VALUE *argv, VALUE str)
return rb_str_aref(str, argv[0]);
}
+VALUE
+rb_str_drop(VALUE str, long len)
+{
+ char *ptr = RSTRING_PTR(str);
+ long olen = RSTRING_LEN(str), nlen;
+
+ str_modifiable(str);
+ if (len > olen) len = olen;
+ nlen = olen - len;
+ if (nlen <= RSTRING_EMBED_LEN_MAX) {
+ char *oldptr = ptr;
+ int fl = (RBASIC(str)->flags & (STR_NOEMBED|ELTS_SHARED));
+ STR_SET_EMBED(str);
+ STR_SET_EMBED_LEN(str, nlen);
+ ptr = RSTRING(str)->as.ary;
+ memcpy(ptr, oldptr + len, nlen);
+ if (fl == STR_NOEMBED) xfree(oldptr);
+ }
+ else {
+ if (!STR_SHARED_P(str)) rb_str_new4(str);
+ ptr = RSTRING(str)->as.heap.ptr += len;
+ RSTRING(str)->as.heap.len = nlen;
+ }
+ ptr[nlen] = 0;
+ ENC_CODERANGE_CLEAR(str);
+ return str;
+}
+
static void
rb_str_splice_0(VALUE str, long beg, long len, VALUE val)
{
+ if (beg == 0 && RSTRING_LEN(val) == 0) {
+ rb_str_drop(str, len);
+ OBJ_INFECT(str, val);
+ return;
+ }
+
rb_str_modify(str);
if (len < RSTRING_LEN(val)) {
/* expand string */