mirror of
https://github.com/ruby/ruby.git
synced 2025-08-24 05:25:34 +02:00

`Integer#chr` performs some validation that we don't want/need. Octal escapes can go above 255, where it will then raise trying to convert.
`append_as_bytes` actually allows to pass a number, so we can just skip that call.
Although, on older rubies of course we still need to handle this in the polyfill.
I don't really like using `pack` but don't know of another way to do so.
For the utf-8 escapes, this is not an issue. Invalid utf-8 in these is simply a syntax error.
161c606b1f
15 lines
371 B
Ruby
15 lines
371 B
Ruby
# frozen_string_literal: true
|
|
|
|
# Polyfill for String#append_as_bytes, which didn't exist until Ruby 3.4.
|
|
if !("".respond_to?(:append_as_bytes))
|
|
String.include(
|
|
Module.new {
|
|
def append_as_bytes(*args)
|
|
args.each do |arg|
|
|
arg = Integer === arg ? [arg].pack("C") : arg.b
|
|
self.<<(arg) # steep:ignore
|
|
end
|
|
end
|
|
}
|
|
)
|
|
end
|