Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/psf/string_utils.rb

Overview

Extending String class with new methods

Instance Method Summary collapse

Instance Method Details

#byteslice!(start_index, length = nil) ⇒ Object

In place version of String#byteslice(index, length)



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/psf/string_utils.rb', line 6

def byteslice!(start_index, length = nil)
  byte_start_index = start_index
  byte_length = length || (bytesize - byte_start_index)

  byte_start_index = bytesize + byte_start_index if byte_start_index.negative?
  return nil if byte_start_index.negative? || byte_start_index >= bytesize

  byte_length = bytesize - byte_start_index if byte_start_index + byte_length > bytesize

  out = byteslice(byte_start_index, byte_length)
  replace(byteslice(0, byte_start_index) + byteslice(byte_start_index + byte_length..-1))
  out
end