Project

General

Profile

Actions

Feature #4447

closed

add String#byteslice() method

Added by sunaku (Suraj Kurapati) over 13 years ago. Updated over 13 years ago.

Status:
Closed
Target version:
-
[ruby-core:35376]

Description

=begin
Please add a String#byteslice() method to the Ruby 1.9 core API.

Without that method, I am forced to inefficiently perform byte-based
string slicing by (1) unpacking the entire string into an Array (with
String#unpack or worse: my_string.bytes.to_a) then (2) slicing that
Array and finally (3) joining the sliced Array into a string (with
Array#pack or worse: my_array.map(&:chr).join), all as shown below:

class String
unless method_defined? :byteslice
##
# Does the same thing as String#slice but
# operates on bytes instead of characters.
#
def byteslice(args)
unpack('C
').slice(args).pack('C')
end
end
end

Thanks for your consideration.
=end


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #18972: String#byteslice should return BINARY (aka ASCII-8BIT) StringsRejectedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0