mrb_str_format captured raw C pointers (p, end) into the format
string's buffer before the main loop. The %s and %p specifiers call
to_s and inspect, which can invoke Ruby code that mutates the format
string via String#replace, freeing or reallocating its buffer. The
loop then continued iterating with dangling pointers, reading freed
memory and potentially leaking adjacent heap contents into the result.
Duplicate the format string with mrb_str_dup() before the loop. This
is O(1) because mrb_str_dup shares the underlying buffer; if the
original is later mutated via String#replace, str_replace decrements
the shared refcount, leaving our duplicate's buffer intact.
Co-authored-by: Claude <noreply@anthropic.com>
Since presym is now mandatory, mruby.h includes presym.h so that
MRB_SYM() macros are available everywhere without explicit include.
Remove redundant #include <mruby/presym.h> from all source files.
Co-authored-by: Claude <noreply@anthropic.com>
Estimate initial buffer size based on format string to reduce
reallocations. The new formula uses format string length plus
120 bytes base, plus 24 bytes per format specifier, capped at 4096.
This reduces reallocations by ~60% in typical use cases and
improves performance by 2-21% depending on output size.
Co-authored-by: Claude <noreply@anthropic.com>
Extract duplicated UTF-8 codepoint-to-bytes encoding into a shared
function in src/string.c. Update all gems to use it:
- mruby-sprintf: %c specifier
- mruby-io: putc
- mruby-string-ext: Integer#chr
- mruby-pack: pack("U")
- mruby-compiler: Unicode escapes in parser
Also use existing mrb_utf8len() in io.c for character length detection.
Co-authored-by: Claude <noreply@anthropic.com>
For integer arguments, encode UTF-8 directly into a stack buffer
instead of creating a temporary mrb_value string via mrb_str_new()
or calling Integer#chr.
- ~5% faster for single %c
- ~15% faster for multiple %c in one format string
- fixes UTF-8 characters (>= 0x80) which previously raised RangeError
Co-authored-by: Claude <noreply@anthropic.com>
add bounds check at retry label to prevent reading past end of format string
when parsing unterminated named parameters like %<foo without closing >
Co-authored-by: Claude <noreply@anthropic.com>
Replace designated initializer lookup table with a simple switch statement
for C++ compatibility. The switch approach is cleaner and works perfectly
in both C and C++ modes.
Co-authored-by: Claude <noreply@anthropic.com>
Add clear section headers and explanatory comments to the format
handlers in mrb_str_format to improve code maintainability and
readability.
Changes:
- Add format type headers (CHARACTER, STRING, INTEGER, FLOAT)
- Add subsection comments explaining key logic steps
- Improve code organization within each format handler
- Better indentation and logical grouping
This makes the 450-line function much easier to navigate and understand
while maintaining identical functionality (all 1723 tests pass).
Co-authored-by: Claude <noreply@anthropic.com>
Replace the large 500+ line switch statement in mrb_str_format with a
clean lookup table dispatch system for better code organization and
maintainability.
Changes:
- Add format specifier lookup table (format_table[128])
- Define format types (FMT_FLAG, FMT_CHAR, FMT_INTEGER, etc.)
- Replace character-by-character dispatch with O(1) table lookup
- Maintain identical behavior (all 1723 tests pass)
This improves code readability by separating format specification
(data) from handling logic (code), making it easier to understand
and maintain the sprintf implementation.
Co-authored-by: Claude <noreply@anthropic.com>
Replace XML-style markup tags in comments with markdown equivalents:
- <code>...</code> to `...` (inline code)
- <tt>...</tt> to `...` (teletype/monospace)
- <i>...</i> to *...* (italics/emphasis)
- +...+ to `...` (parameter/variable references)
Updated 80+ files across core source, headers, mrbgems, and libraries
to use consistent markdown formatting in documentation comments.
Handled edge cases including special characters like <=> operators.
Co-authored-by: Atlassian Rovo Dev
- %: string formatting operator that uses the string as a format specification
and applies it to the given argument(s), supports both single arguments and
arrays for multiple substitutions, delegates to sprintf for actual formatting
The method now has comprehensive call-seq documentation with practical
examples demonstrating various sprintf formatting patterns including:
- Zero-padded integers: "%05d" % 123
- Multiple substitutions with arrays: "%-5s: %016x" % [name, id]
- Hash-based named substitutions: "foo = %{foo}" % { :foo => 'bar' }
- Named format specifiers: "%{foo}f" % { :foo => 1 }
Co-authored-by: Atlassian Rovo Dev
The `XXX` comment in `sprintf.c` suggested that not validating
the number of arguments for positional format specifiers was a bug.
However, CRuby's `sprintf` also ignores extra arguments in this
case, making the existing behavior correct.
This commit removes the confusing comment and the disabled code
block that went with it, clarifying the intended behavior and
cleaning up the code.
Co-authored-by: Gemini <gemini@google.com>
When calling `mrb_equal()` or `mrb_funcall()` family functions, the GC arena should be restored if the loop is repeated by a non-immediate return value.
In my opinion, restoring the GC arena is unnecessary when a non-immediate (true) value causes the function to return (e.g. the `mrb_ary_index_m()` function).
The patch does not take into account the case of recursive calls and may be incomplete.
- `#include <math.h>` is done in `mruby.h`.
Eliminate the need to worry about the `MRB_NO_FLOAT` macro.
- Include mruby header files before standard header files.
If the standard header file is already placed before `mruby.h`, the standard header file added in the future tends to be placed before `mruby.h`.
This change should some reduce the chances of macros that must be defined becoming undefined in C++ or including problematic header files in a particular mruby build configuration.