Project

General

Profile

Feature #12024 ยป 0001-Add-String.buffer-for-creating-strings-with-large-ca.patch

jeremyevans0 (Jeremy Evans), 01/26/2016 07:12 PM

View differences:

string.c
#define STR_BUF_MIN_SIZE 128
VALUE
rb_str_buf_new(long capa)
static VALUE
rb_str_buf_new_with_class(VALUE obj, long capa)
{
VALUE str = str_alloc(rb_cString);
VALUE str = str_alloc(obj);
if (capa < STR_BUF_MIN_SIZE) {
capa = STR_BUF_MIN_SIZE;
......
}
VALUE
rb_str_buf_new(long capa)
{
rb_str_buf_new_with_class(rb_cString, capa);
}
VALUE
rb_str_buf_new_cstr(const char *ptr)
{
VALUE str;
......
return rb_check_string_type(str);
}
/*
* call-seq:
* String.buffer(capacity) -> string
*
* Creates empty string with the given internal capacity. This
* can increase performance compared to using String.new, if you
* will be concatenating a large amount of data to the string.
*
* String.buffer(100000) #=> ""
*/
static VALUE
rb_str_s_buffer(VALUE obj, VALUE capacity)
{
return rb_str_buf_new_with_class(obj, NUM2LONG(capacity));
}
static char*
str_nth_len(const char *p, const char *e, long *nthp, rb_encoding *enc)
{
......
rb_include_module(rb_cString, rb_mComparable);
rb_define_alloc_func(rb_cString, empty_str_alloc);
rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1);
rb_define_singleton_method(rb_cString, "buffer", rb_str_s_buffer, 1);
rb_define_method(rb_cString, "initialize", rb_str_init, -1);
rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1);
test/ruby/test_string.rb
assert_equal(Encoding::UTF_8, S(src, encoding: "utf-8").encoding)
end
def test_s_buffer
assert_equal("", @cls.buffer(1000))
assert_equal(@cls, @cls.buffer(1000).class)
assert_operator(ObjectSpace.memsize_of(@cls.buffer(10000)), :>, ObjectSpace.memsize_of(@cls.buffer(1000)))
assert_equal("", @cls.buffer(-1000))
assert_equal(ObjectSpace.memsize_of(@cls.buffer(-10000)), ObjectSpace.memsize_of(@cls.buffer(-1000)))
end
def test_AREF # '[]'
assert_equal("A", S("AooBar")[0])
assert_equal("B", S("FooBaB")[-1])
    (1-1/1)