Project

General

Profile

Actions

Bug #18956

closed

Kernel#sprintf - %c handles negative Integer argument in a confusing way

Added by andrykonchin (Andrew Konchin) about 2 months ago. Updated about 2 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:109429]

Description

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:

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.

sprintf("%c", -1)
# => "\xFF"

In this case no exception is risen but the string is not valid:

sprintf("%c", -1).codepoints
# => invalid byte sequence in UTF-8 (ArgumentError)
sprintf("%c", -1).valid_encoding?
# => false
Actions #1

Updated by andrykonchin (Andrew Konchin) about 2 months ago

  • Description updated (diff)

Updated by mame (Yusuke Endoh) about 2 months ago

@naruse (Yui NARUSE), @nobu (Nobuyoshi Nakada), and @matz (Yukihiro Matsumoto) agreed that a negative integer to %c should raise an explicit exception.

Actions #3

Updated by nobu (Nobuyoshi Nakada) about 2 months ago

  • Status changed from Open to Closed

Applied in changeset git|d2483393cbcb4dcfa0000fa8166bb7fa7ed9f7b4.


[Bug #18956] Negative codepoints are invalid characters

Actions

Also available in: Atom PDF