Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions doc/format_specifications.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ It consists of:

- A leading percent character.
- Zero or more _flags_ (each is a character).
- An optional _width_ _specifier_ (an integer).
- An optional _precision_ _specifier_ (a period followed by a non-negative integer).
- An optional _width_ _specifier_ (an integer, or <tt>*</tt>).
- An optional _precision_ _specifier_ (a period followed by a non-negative
integer, or <tt>*</tt>).
- A _type_ _specifier_ (a character).

Except for the leading percent character,
Expand Down Expand Up @@ -125,13 +126,6 @@ Left-pad with zeros instead of spaces:
sprintf('%6d', 100) # => " 100"
sprintf('%06d', 100) # => "000100"

=== <tt>'*'</tt> Flag

Use the next argument as the field width:

sprintf('%d', 20, 14) # => "20"
sprintf('%*d', 20, 14) # => " 14"

=== <tt>'n$'</tt> Flag

Format the (1-based) <tt>n</tt>th argument into this field:
Expand All @@ -152,6 +146,11 @@ of the formatted field:
# Ignore if too small.
sprintf('%1d', 100) # => "100"

If the width specifier is <tt>'*'</tt> instead of an integer, the actual minimum
width is taken from the argument list:

sprintf('%*d', 20, 14) # => " 14"

== Precision Specifier

A precision specifier is a decimal point followed by zero or more
Expand Down Expand Up @@ -194,6 +193,11 @@ the number of characters to write:
sprintf('%s', Time.now) # => "2022-05-04 11:59:16 -0400"
sprintf('%.10s', Time.now) # => "2022-05-04"

If the precision specifier is <tt>'*'</tt> instead of a non-negative integer,
the actual precision is taken from the argument list:

sprintf('%.*d', 20, 1) # => "00000000000000000001"

== Type Specifier Details and Examples

=== Specifiers +a+ and +A+
Expand Down
2 changes: 1 addition & 1 deletion io.c
Original file line number Diff line number Diff line change
Expand Up @@ -4947,7 +4947,7 @@ rb_io_each_codepoint(VALUE io)
fptr->cbuf.off += n;
fptr->cbuf.len -= n;
rb_yield(UINT2NUM(c));
rb_io_check_byte_readable(fptr);
rb_io_check_char_readable(fptr);
}
}
NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
Expand Down
18 changes: 18 additions & 0 deletions test/ruby/test_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,24 @@ def test_each_codepoint_closed
}
end

def test_each_codepoint_with_ungetc
bug21562 = '[ruby-core:123176] [Bug #21562]'
with_read_pipe("") {|p|
p.binmode
p.ungetc("aa")
a = ""
p.each_codepoint { |c| a << c }
assert_equal("aa", a, bug21562)
}
with_read_pipe("") {|p|
p.set_encoding("ascii-8bit", universal_newline: true)
p.ungetc("aa")
a = ""
p.each_codepoint { |c| a << c }
assert_equal("aa", a, bug21562)
}
end

def test_rubydev33072
t = make_tempfile
path = t.path
Expand Down