ruby/lib/prism/polyfill/append_as_bytes.rb
Earlopain a679597547 [ruby/prism] Fix parser translator crash for certain octal escapes
`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
2025-03-18 13:36:53 -04:00

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