diff options
author | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2021-12-20 12:11:21 +1300 |
---|---|---|
committer | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2021-12-21 12:25:42 +1300 |
commit | 71bbc40ffa4bb16ef0fc31f8015f61709fac36ce (patch) | |
tree | 7ae2e609c7b43d7393df633a3857c72402e85cfb /io_buffer.c | |
parent | 49166fc74a9cd0bf48baa08d32e020183ad46723 (diff) | |
download | ruby-71bbc40ffa4bb16ef0fc31f8015f61709fac36ce.tar.gz |
Rename `to_str` -> `get_string` and add support for encodings.
Diffstat (limited to 'io_buffer.c')
-rw-r--r-- | io_buffer.c | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/io_buffer.c b/io_buffer.c index cc022a7c9f..075a9bfb78 100644 --- a/io_buffer.c +++ b/io_buffer.c @@ -717,35 +717,6 @@ rb_io_buffer_slice(VALUE self, VALUE _offset, VALUE _length) return instance; } -VALUE -rb_io_buffer_to_str(int argc, VALUE *argv, VALUE self) -{ - struct rb_io_buffer *data = NULL; - TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, data); - - size_t offset = 0; - size_t length = data->size; - - if (argc == 0) { - // Defaults. - } - else if (argc == 1) { - offset = NUM2SIZET(argv[0]); - length = data->size - offset; - } - else if (argc == 2) { - offset = NUM2SIZET(argv[0]); - length = NUM2SIZET(argv[1]); - } - else { - rb_error_arity(argc, 0, 2); - } - - rb_io_buffer_validate(data, offset, length); - - return rb_usascii_str_new((char*)data->base + offset, length); -} - void rb_io_buffer_get_mutable(VALUE self, void **base, size_t *size) { @@ -1144,6 +1115,37 @@ io_buffer_set(VALUE self, VALUE type, VALUE _offset, VALUE value) return SIZET2NUM(offset); } +static VALUE +io_buffer_get_string(int argc, VALUE *argv, VALUE self) +{ + if (argc > 3) rb_error_arity(argc, 0, 3); + + struct rb_io_buffer *data = NULL; + TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, data); + + size_t offset = 0; + size_t length = data->size; + rb_encoding *encoding = NULL; + + if (argc >= 1) { + offset = NUM2SIZET(argv[0]); + } + + if (argc >= 2 && !RB_NIL_P(argv[1])) { + length = NUM2SIZET(argv[1]); + } else { + length = data->size - offset; + } + + if (argc >= 3) { + encoding = rb_find_encoding(argv[2]); + } + + rb_io_buffer_validate(data, offset, length); + + return rb_enc_str_new((char*)data->base + offset, length, encoding); +} + void rb_io_buffer_clear(VALUE self, uint8_t value, size_t offset, size_t length) { @@ -1278,7 +1280,6 @@ Init_IO_Buffer(void) // Manipulation: rb_define_method(rb_cIOBuffer, "slice", rb_io_buffer_slice, 2); - rb_define_method(rb_cIOBuffer, "to_str", rb_io_buffer_to_str, -1); rb_define_method(rb_cIOBuffer, "copy", io_buffer_copy, 2); rb_define_method(rb_cIOBuffer, "<=>", rb_io_buffer_compare, 1); rb_define_method(rb_cIOBuffer, "resize", io_buffer_resize, 1); @@ -1298,4 +1299,5 @@ Init_IO_Buffer(void) // Data access: rb_define_method(rb_cIOBuffer, "get", io_buffer_get, 2); rb_define_method(rb_cIOBuffer, "set", io_buffer_set, 3); + rb_define_method(rb_cIOBuffer, "get_string", io_buffer_get_string, -1); } |