Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
Gem::AtomicFileWriter.byteslice_at_char_boundary
byteslice_at_char_boundary(string, max_bytesize)
lib/rubygems/util/atomic_file_writer.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/lib/rubygems/util/atomic_file_writer.rb, line 102
def self.byteslice_at_char_boundary(string, max_bytesize)
sliced = string.byteslice(0, max_bytesize)
if string.valid_encoding?
sliced = sliced.byteslice(0, sliced.bytesize - 1) until sliced.valid_encoding?
end
sliced
end
Returns the longest prefix of string that is at most max_bytesize bytes and ends on a character boundary. A string that is invalid in its own encoding would be cut back to its last valid prefix, discarding an unbounded part of the name, so it is sliced as raw bytes instead and can still be cut mid-character.
See also
.open