Issue #13626 has been updated by janko (Janko Marohni).


I support adding `String#byteslice!`. I've been using `String#byteslice` in custom IO-like objects that implement `IO#read` semantics, as the strings I work with don't necessarily have to be in binary encoding (otherwise I'd just use `String#slice`), they can also be in UTF-8. Since `IO#read` needs to work in terms of bytes, that's why I needed  `String#byteslice`.

I've used the exact idiom from Samuel's original description in three different projects already:

* https://github.com/janko-m/down/blob/ac4a32f296cb9cd8c12fc46a01a7e2f7c5fcd1b2/lib/down/chunked_io.rb#L169-L170
* https://github.com/janko-m/goliath-rack_proxy/blob/7b359ff3ddfa3cba23c32220389abb39481735a9/lib/goliath/rack_proxy.rb#L134-L135
* https://github.com/socketry/falcon/blob/12b8818812b23c920e545e6b4c91e08e5348ee04/lib/falcon/adapters/input.rb#L80-L81

`String#byteslice!` would allow reducing the code and probably end up with fewer strings at the end.


----------------------------------------
Feature #13626: Add String#byteslice!
https://bugs.ruby-lang.org/issues/13626#change-72795

* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
It's a common pattern in IO buffering, to read a part of a string while leaving the remainder.

~~~
# Consume only part of the read buffer:
result = @read_buffer.byteslice(0, size)
@read_buffer = @read_buffer.byteslice(size, @read_buffer.bytesize)
~~~

It would be nice if this code could be simplified to:

~~~
result = @read_buffer.byteslice!(size)
~~~

Additionally, this allows a significantly improved implementation by the interpreter.




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>