[ruby-core:109545] [Ruby master Bug#18956] Kernel#sprintf - %c handles negative Integer argument in a confusing way
From:
"mame (Yusuke Endoh)" <noreply@...>
Date:
2022-08-18 09:44:50 UTC
List:
ruby-core #109545
Issue #18956 has been updated by mame (Yusuke Endoh).
@naruse, @nobu, and @matz agreed that a negative integer to `%c` should raise an explicit exception.
----------------------------------------
Bug #18956: Kernel#sprintf - %c handles negative Integer argument in a confusing way
https://bugs.ruby-lang.org/issues/18956#change-98716
* Author: andrykonchin (Andrew Konchin)
* Status: Open
* Priority: Normal
* ruby -v: 3.0.3
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
Integer argument for `%c `means a character codepoint.
I've noticed two outcomes when argument is negative:
- exception
- broken/incorrect string
When exception is raised - its message a bit misleading and confusing:
```ruby
sprintf("%c", -1000)
# => invalid character (ArgumentError)
sprintf("%c".encode('ascii'), -1)
# => 4294967295 out of char range (RangeError)
```
`invalid character` means there is a character, but actual argument is a codepoint. `4294967295 out of char range` is about codepoint, but it mentions `4294967295` instead of actual argument `-1`.
```ruby
sprintf("%c", -1)
# => "\xFF"
```
In this case no exception is risen but the string is not valid:
```ruby
sprintf("%c", -1).codepoints
# => invalid byte sequence in UTF-8 (ArgumentError)
sprintf("%c", -1).valid_encoding?
# => false
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>