1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* string.c (rb_str_drop): new function to drop first bytes.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18540 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2008-08-12 10:07:08 +00:00
parent 8938f611f0
commit 26bc383ef8
3 changed files with 39 additions and 0 deletions

View file

@ -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

View file

@ -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);

View file

@ -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 */