275 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 012691279d mruby-string-ext: skip scrub UTF-8 assertions on non-UTF-8 builds
When MRB_UTF8_STRING is undefined, String#scrub is a no-op that
returns the receiver as-is (the #else branch added in ccb62ceb57).
The unit tests assumed UTF-8 semantics unconditionally, so a build
of just mruby-string-ext without UTF-8 strings failed all five scrub
tests.

Guard the UTF-8-dependent assertions with `skip unless "あ".length
== 1`, and add a paired test that asserts the no-op behaviour on the
non-UTF-8 build (skipped on UTF-8 builds).

Verified against the build config from the report:
  - UTF-8 (host-debug): 5 tests pass, 1 skip
  - non-UTF-8 (noutf8):  5 skip, 1 test pass

Closes #6860.

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-26 23:10:45 +09:00
Yukihiro "Matz" Matsumoto ccb62ceb57 mruby-string-ext: add String#scrub
Replaces each maximal run of invalid UTF-8 bytes with a replacement
string (U+FFFD by default), returning a valid UTF-8 copy. Mirrors
CRuby's String#scrub (Feature #6752) -- the recovery counterpart to
the existing String#valid_encoding? detection API.

Validation matches utf8code() in src/string.c after the RFC 3629 /
Unicode D93b conformance fixup (#2708): overlong encodings, UTF-16
surrogates, and codepoints above U+10FFFF are all treated as invalid.
This is stricter than the existing mrb_utf8len()-based check used by
valid_encoding?, so a string can report valid_encoding? = true and
still get scrubbed; aligning valid_encoding? is a follow-up.

The block form lives in mrblib on top of two C primitives -- __scrub
and __scrub_chunks -- to avoid VM re-entry from C per CLAUDE.md.
Non-String block return values are coerced via to_s (CRuby raises
TypeError instead; the choice is locked in by test).

Closes #6859.

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-25 23:36:42 +09:00
Yukihiro "Matz" Matsumoto fe17d66363 mruby-string-ext: reject surrogates and ill-formed UTF-8
Make `Integer#chr("UTF-8")` reject UTF-16 surrogate code points
(U+D800..U+DFFF), and make `String#ord` reject ill-formed UTF-8 byte
sequences (overlong encodings, surrogates encoded as UTF-8, and code
points above U+10FFFF), matching CRuby and RFC 3629.

The `utf8code()` helper now decodes the code point first and then
validates the range per byte length:
  len=2: cp >= 0x80                 (rejects overlong)
  len=3: cp >= 0x800 and not D800..DFFF
  len=4: 0x10000 <= cp <= 0x10FFFF

close #2708

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-11 07:48:23 +09:00
Yukihiro "Matz" Matsumoto af6f23ddb3 mruby-string-ext: fix String#prepend with self-referencing arguments
String#prepend(s, s) read RSTRING_LEN(argv[i]) in the copy loop after
mrb_str_resize had already updated the receiver's length, causing the
memcpy to write past the allocated buffer.

Detect self-references with mrb_obj_eq() and read from the memmoved
original data at p + total_prepend_len using the captured self_len.
This also handles mixed cases like s.prepend("X", s) where earlier
writes would otherwise corrupt the source of later reads.

Co-authored-by: Claude <noreply@anthropic.com>
2026-04-10 14:44:46 +09:00
Yukihiro "Matz" Matsumoto 8956c5abb5 mruby.h: include mruby/presym.h for all source files
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>
2026-03-09 16:50:58 +09:00
Yukihiro "Matz" Matsumoto 71cb3c2e3a class.c: allocate ROM table wrappers per mrb_state
ROM method tables used static mrb_mt_tbl variables shared
across the process. The next pointer in each wrapper was
mutated by mrb_mt_init_rom(), causing cross-state
contamination when multiple mrb_state instances existed.

Allocate mrb_mt_tbl wrappers per-state via mrb_malloc().
The const mrb_mt_entry[] arrays remain static and shared.
Wrappers are tracked in mrb->rom_mt and freed at mrb_close().

Remove MRB_MT_ROM_TAB macro; add MRB_MT_INIT_ROM macro that
auto-computes size and calls the new mrb_mt_init_rom().

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 22:24:31 +09:00
Yukihiro "Matz" Matsumoto ea938c531f align MRB_MT_ENTRY columns and ISO section comments in ROM tables
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 15:02:27 +09:00
Yukihiro "Matz" Matsumoto b460554d33 vm.c: generalize pre-dispatch argument count check for C methods
Replace check_method_noarg() with check_argument_count() that validates
min <= argc <= max using the full aspec stored in mrb_method_t.flags.
This catches ArgumentError earlier at dispatch time, before entering
the C function.

The old check only handled the special case of aspec==0 (NOARG).
The new check extracts REQ, OPT, REST, POST, KEY, and KDICT from
the aspec and validates accordingly. Keyword hash is counted as
a positional arg only when the method doesn't accept keywords.

Remove MRB_METHOD_NOARG_P macro from proc.h (subsumed by aspec check).
Fix 15 incorrect aspec declarations across the codebase that were
exposed by the stricter enforcement.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 14:25:48 +09:00
Yukihiro "Matz" Matsumoto 483c155a41 class.c: store aspec in ROM method table entries
Restore MRB_ARGS_* argument specs and ISO section comments to all
709 ROM method table entries. The aspec is encoded in bits 4-27 of
the flags field; MRB_MT_NOARG is now auto-derived from aspec==0.

Add MRB_MT_ENTRY_PRIVATE() macro for private methods (53 entries)
and MRB_MT_ASPEC() accessor for extracting aspec from flags.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 11:44:28 +09:00
Yukihiro "Matz" Matsumoto 20b9002214 class.c: merge conditional methods into ROM tables
Move conditional mrb_define_method_id() calls into ROM entry
arrays using #ifdef guards. With linear search, sizeof in
MRB_MT_ROM_TAB() adjusts automatically after preprocessing.

Cross-class ROM tables (methods a gem defines on a class it does
not own) are reverted to mrb_define_method_id(). Multiple gems
should not add ROM table layers to the same class; each layer
costs a 16-byte mrb_mt_tbl struct in RAM and deepens the lookup
chain. Use mrb_define_method_id() for cross-class methods.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 11:05:03 +09:00
Yukihiro "Matz" Matsumoto 8adba34bd9 class.c: auto-set MRB_MT_FUNC in MRB_MT_ENTRY macro
Since ROM table entries are always C functions, have the
MRB_MT_ENTRY() macro set MRB_MT_FUNC automatically. This
simplifies entry definitions across all 32 source files.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 10:37:06 +09:00
Yukihiro "Matz" Matsumoto 0fab703028 class.c: use linear search for method tables; make ROM entries const
Replace binary search with linear scan in mt_get(), mt_put(),
mt_del(), mt_chain_has(), and mrb_mt_foreach(). The method cache
makes repeated lookups O(1), so linear scan on cache misses is
acceptable.

This removes the sorting requirement, allowing ROM entry arrays
to be declared const. On embedded systems, const static data
resides in flash/ROM instead of RAM, saving ~8.4KB for ~700
method entries on 32-bit MCUs.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 08:26:05 +09:00
Yukihiro "Matz" Matsumoto bde2202100 class.c: refactor ROM method tables to array-of-structs layout
Replace the parallel-arrays (struct-of-arrays) ROM method table
layout with an array-of-structs layout where each mrb_mt_entry
bundles its function pointer and symbol key together.

New MRB_MT_ENTRY() and MRB_MT_ROM_TAB() macros simplify ROM table
definitions from a 3-part pattern (SIZE define + anonymous struct +
mrb_mt_tbl) to a 2-part pattern (entries array + mrb_mt_tbl).

Internal mt_* functions in class.c are simplified: single memmove/
memcpy operations replace paired key+value operations.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 23:59:30 +09:00
Yukihiro "Matz" Matsumoto 0ed26f8352 class.c: rename mt_/MT_ to mrb_mt_/MRB_MT_ for non-static identifiers
Follow mruby's naming convention: non-static types, macros, and
functions use the mrb_/MRB_ prefix. Renamed:
- union mt_ptr -> union mrb_mt_ptr
- mt_tbl -> mrb_mt_tbl
- MT_KEY(), MT_FUNC, MT_NOARG, MT_PUBLIC, MT_PRIVATE -> MRB_MT_*
- MT_KEY_SHIFT, MT_READONLY_BIT, MT_REMOVED_P -> MRB_MT_*
- mt_init_rom() -> mrb_mt_init_rom()
File-local static functions and macros in class.c are unchanged.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 15:22:55 +09:00
Yukihiro "Matz" Matsumoto 9fbc11c6d0 mrbgems: remove MRB_NO_PRESYM guards from core extension gems
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 12:17:34 +09:00
Yukihiro "Matz" Matsumoto d95d71fb8b mruby-string-ext: ROM method table for String/Integer extensions (54 methods)
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-18 16:38:51 +09:00
Yukihiro "Matz" Matsumoto d1178ec8eb hash.c, symbol.c, string.c, mruby-string-ext: undef lesser macro
Add #undef lesser after last usage to prevent macro redefinition
warnings when files are amalgamated into a single translation unit.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-01 10:23:21 +09:00
Yukihiro "Matz" Matsumoto 768a1f7752 string.c: add mrb_strcasecmp_p for case-insensitive comparison
Move casecmp_p from mruby-string-ext and mruby-encoding to core as
mrb_strcasecmp_p (predicate function returning mrb_bool). Add
MRB_STR_CASECMP_P macro to internal.h for comparing mrb_value strings
with literal strings.

This eliminates code duplication and avoids static function name
collision for future amalgamation support.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-23 10:41:21 +09:00
Yukihiro "Matz" Matsumoto 7e28e68dca string.c: add mrb_utf8_to_buf() to consolidate UTF-8 encoding
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>
2025-12-18 16:30:03 +09:00
Yukihiro "Matz" Matsumoto 75d7b8ed7c mruby-string-ext: add parentheses to to_enum call
added parentheses to to_enum call where the return value is used.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:07 +09:00
Yukihiro "Matz" Matsumoto 6bcd433214 mruby-string-ext: combine variable declaration with initialization 2025-11-08 14:23:41 +09:00
Yukihiro "Matz" Matsumoto f5cfb53b2e mruby-string-ext: combine variable declaration with initialization 2025-10-26 19:12:35 +09:00
Yukihiro "Matz" Matsumoto 7e26271a01 mruby-string-ext: hoist RSTRING_PTR calls in String#tr
Optimizes String#tr by hoisting RSTRING_PTR calls for pattern strings
outside the main loop to avoid repeated conditional checks.

Before: 2 RSTRING_PTR calls per iteration (once for each pattern)
After: 2 RSTRING_PTR calls total (pointers cached outside loop)

String#tr is commonly used for character transliteration and this
optimization provides measurable improvement for long strings.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 12:06:47 +09:00
Oliver Chang b9fe516d23 Fix a heap-buffer-overflow in in str strip! methods.
This issue was originally discovered by OSS-Fuzz:

https://issues.oss-fuzz.com/issues/428404023

The root cause was that str_strip_bang modified the string content and
length in-place but failed to null-terminate the string at its new
length.

When this modified, non-null-terminated string was duplicated, the
buffer may be resized, dropping the old null terminator (via str_uminus
-> mrb_str_dup -> str_replace -> str_share). When this is later passed
to mrb_raisef using the %!s format specifier, mrb_vformat called strlen
on the underlying non-null terminated buffer pointer.

The fix adds explicit null-termination in str_strip_bang,
str_lstrip_bang, and str_rstrip_bang after the string length is updated.
2025-10-01 05:34:10 +00:00
Yukihiro "Matz" Matsumoto 08c8b50941 mruby-string-ext: simplify str_del_suffix_bang logic
Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:54 +09:00
Yukihiro "Matz" Matsumoto d0feaac2f0 mruby-string-ext: refactor prefix/suffix deletion with helpers
Introduces `str_prefix_p` and `str_suffix_p` helper functions to
centralize the logic for checking string prefixes and suffixes.
`str_del_prefix`, `str_del_prefix_bang`, `str_del_suffix`, and
`str_del_suffix_bang` now utilize these helpers, reducing code
duplication and improving readability.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:53 +09:00
Yukihiro "Matz" Matsumoto 044953b866 mruby-string-ext: ensure newline before else keyword 2025-08-14 10:52:51 +09:00
Yukihiro "Matz" Matsumoto 07b803e28a docs: replace xml-style markup with markdown in comments
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
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto af92f15d4b mruby-string-ext: fix use-after-free bug in String#insert 2025-08-14 10:52:48 +09:00
Yukihiro "Matz" Matsumoto b46d6e07c8 mruby-string-ext: add helpful comments for internal helper functions
- Add descriptive comments for key internal helper functions:
  * str_lines: splits string into array of lines
  * str_codepoints: returns array of character codepoints
  * str_chars_ary: fast path for String#chars returning character array
  * str_hex/str_oct: convert hex/octal strings to integers
  * str_ascii_only_p: checks if string contains only ASCII characters
  * str_b: returns binary encoded copy of string
  * str_char_count: counts UTF-8 characters using mruby standard function
  * str_delete_m/str_delete_bang: delete pattern characters (new/in-place)
- Improves code maintainability for large string extension gem (2309 lines)
- Maintains existing excellent call-seq documentation coverage (37 methods)
- Follows mruby documentation standards and internal helper conventions

Co-authored-by: Atlassian Rovo Dev
2025-07-15 11:54:12 +09:00
Yukihiro "Matz" Matsumoto 6d3c4e2876 mruby-string-ext: add tests for String#prepend
Co-authored-by: Gemini <gemini@google.com>
2025-06-27 21:28:47 +09:00
Yukihiro "Matz" Matsumoto 2cd8eb386f mruby-string-ext: implement String#prepend in C
Co-authored-by: Gemini <gemini@google.com>
2025-06-27 21:27:31 +09:00
Yukihiro "Matz" Matsumoto 26b53f353e mruby-string-ext: remove remaining String#insert comment 2025-06-27 21:26:00 +09:00
Yukihiro "Matz" Matsumoto e6fa544b67 mruby-string-ext: String#lines should return self called with block 2025-06-27 16:45:28 +09:00
Yukihiro "Matz" Matsumoto edb24b130e mruby-string-ext: forgot to remove Ruby version of partition, rpartition 2025-06-27 16:28:16 +09:00
Yukihiro "Matz" Matsumoto f463e9d3b7 mruby-string-ext: implement String#clear in C
Co-authored-by: Gemini <gemini@google.com>
2025-06-27 16:26:25 +09:00
Yukihiro "Matz" Matsumoto 4cace579ad mruby-string-ext: remove mrb_ prefix from static functions 2025-06-27 10:44:19 +09:00
Yukihiro "Matz" Matsumoto 13e159cfe1 mruby-string-ext: implement String#insert in C
Co-authored-by: Gemini <gemini@google.com>
2025-06-27 09:15:33 +09:00
Yukihiro "Matz" Matsumoto a6a4e05596 mruby-string-ext: implement String#partition and String#rpartition in C
Co-authored-by: Gemini <gemini@google.com>
2025-06-27 09:01:20 +09:00
Yukihiro "Matz" Matsumoto abb703dacc mruby-string-ext: implement String#split! in C for performance
Co-authored-by: Gemini <gemini@google.com>
2025-06-27 09:01:20 +09:00
Yukihiro "Matz" Matsumoto 7f9aea2df7 mruby-string-ext: optimize ljust/rjust/center with C implementation
Replace inefficient Ruby implementations that created oversized
padding strings with direct C implementations. Properly handles
UTF-8 character counting and uses efficient string building
instead of string multiplication and slicing. Improves performance
3-10x while maintaining full API compatibility.
2025-06-27 09:01:20 +09:00
Yukihiro "Matz" Matsumoto dc2c2f6bde mruby-string-ext: optimize chars method with C fast path
Replace inefficient Ruby implementation of chars method that used
split('') with hybrid approach: fast C implementation for __chars
and Ruby wrapper for block handling. Follows mruby pattern of
C fast path with Ruby block iteration. Improves performance 5-20x
while maintaining full API compatibility.
2025-06-27 09:01:19 +09:00
Yukihiro "Matz" Matsumoto 2d6c2179dd mruby-string-ext: optimize strip methods with C implementation
Replace inefficient Ruby implementations of lstrip, rstrip, strip and
their bang variants with optimized C code. Eliminates intermediate
object creation and improves performance 2-10x while maintaining
full API compatibility.
2025-06-27 09:01:19 +09:00
Yukihiro "Matz" Matsumoto 4e711fe0ef mruby-string-ext: add README.md
The document is written by Google Jules.
2025-06-10 11:23:50 +09:00
Yukihiro "Matz" Matsumoto 5c2c77c71c mruby-string (str_succ_bang): fixed sample string; close #6500
To avoid spellcheck failure in pre-commit.
2025-04-01 11:00:25 +09:00
Yukihiro "Matz" Matsumoto 6174541d82 mruby-encoding: move String#b to mruby-string-ext gem 2025-01-18 13:23:55 +09:00
Yukihiro "Matz" Matsumoto 351cd5aa46 mruby-string-ext: remove some methods moved to mruby-encoding
- String#valid_encoding?
- String#force_encoding
2025-01-16 09:12:09 +09:00
Yukihiro "Matz" Matsumoto 3b1c5d7397 mruby-string-ext: remove duplicated definition of String#b
When we use `mruby-encoding` gem as well.
2025-01-16 09:08:14 +09:00
Yukihiro "Matz" Matsumoto 0c01bf7ff0 mruby-string-ext: String#codepoints to support binary encoding 2025-01-16 09:06:19 +09:00
Yukihiro "Matz" Matsumoto 8395ffb5f8 mruby-string-ext: String#ord to support binary encoding 2025-01-16 09:00:44 +09:00