66 Commits

Author SHA1 Message Date
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 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 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 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 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 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 6c530b8f48 mruby-string-ext (str_append_as_bytes): reimplement in C
To handle strings as byte sequences.
2025-01-12 23:13:27 +09:00
Yukihiro "Matz" Matsumoto 85df487e63 mruby-string-ext (append_as_bytes): new method introduced by CRuby 3.4
Since mruby does not have Encoding support, append_as_bytes works
exactly same as String#concat.
2025-01-01 16:05:00 +09:00
Yukihiro "Matz" Matsumoto 8a538c40c0 mruby-string-ext: implement core part of codepoints in C; close #6061 2023-09-23 11:36:20 +09:00
John Bampton ea8964ef35 ruby: standardize whitespace 2022-10-31 16:25:56 +10:00
Yukihiro "Matz" Matsumoto 11504318b8 string-ext/string.rb: String#prepend to take multiple arguments. 2022-09-20 18:31:04 +09:00
dearblue c4bca7cbb3 Align "wrong number of arguments" messages
Make "N for M" into the form "given N, expected M".

As I worked, I noticed that the `argnum_error()` function had a part to include the method name in the message.
I think this part is no longer needed by https://github.com/mruby/mruby/pull/5394.

  - Before this patch

    ```console
    % bin/mruby -e '[1, 2, 3].each 0'
    trace (most recent call last):
            [1] -e:1
    -e:1:in each: 'each': wrong number of arguments (1 for 0) (ArgumentError)
    ```

  - After this patch

    ```console
    % bin/mruby -e '[1, 2, 3].each 0'
    trace (most recent call last):
            [1] -e:1
    -e:1:in each: wrong number of arguments (given 1, expected 0) (ArgumentError)
    ```
2021-11-28 18:21:29 +09:00
Yukihiro "Matz" Matsumoto 37a7ff228b string-ext/string.c: implement casecmp in C.
* should not raise error for non-string arguments
* avoid allocating case converted string internally
2021-09-01 07:00:54 +09:00
Yukihiro "Matz" Matsumoto b2b0329d29 string.rb: upto to break when the string length is longer than end. 2021-06-29 16:49:16 +09:00
dearblue 2d0b50f6f3 Avoid warnings with ruby -cw
```console
% for rb in `git ls-files '*/mrblib/*.rb' 'mrblib'`; do ruby30 -cw $rb > /dev/null; done
mrbgems/mruby-array-ext/mrblib/array.rb:389: warning: assigned but unused variable - ary
mrbgems/mruby-array-ext/mrblib/array.rb:663: warning: assigned but unused variable - len
mrbgems/mruby-hash-ext/mrblib/hash.rb:119: warning: possibly useless use of a variable in void context
mrbgems/mruby-hash-ext/mrblib/hash.rb:259: warning: assigned but unused variable - keys
mrbgems/mruby-io/mrblib/io.rb:229: warning: literal in condition
mrbgems/mruby-io/mrblib/io.rb:280: warning: literal in condition
mrbgems/mruby-string-ext/mrblib/string.rb:347: warning: assigned but unused variable - len
mrbgems/mruby-toplevel-ext/mrblib/toplevel.rb:2: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
```
2021-06-28 23:21:47 +09:00
Yukihiro "Matz" Matsumoto 3a3761ec69 string.c: remove unnecessary branch in the internal method. 2021-05-13 16:08:25 +09:00
Yukihiro "Matz" Matsumoto 2ad5d4f53d string.c: add a new method String#center. 2021-04-28 21:44:19 +09:00
Yukihiro "Matz" Matsumoto e5e5acefaf string.{c,rb}: fix type of return values from some methods as Ruby3.0
When the receiver is the instance of subclass of `String`.

- `String#each_char`
- `String#each_line`
- `String#partition`
2021-04-28 21:41:01 +09:00
Yukihiro "Matz" Matsumoto 6b457d2c00 Merge branch 'work_for_merge' of https://github.com/zubycz/mruby into zubycz-work_for_merge 2020-10-15 18:35:13 +09:00
taiyoslime bec4d05340 Introduce endless range (a part of #5085)
Co-Authored-By: n4o847 <22975590+n4o847@users.noreply.github.com>
Co-Authored-By: smallkirby <ssmallkirby@gmail.com>
2020-10-13 14:09:36 +09:00
n4o847 59af217779 Remove unnecessary assignment in String#upto
Co-authored-by: taiyoslime <t@iyosli.me>
Co-authored-by: smallkirby <ssmallkirby@gmail.com>
2020-10-12 16:50:22 +09:00
KOBAYASHI Shuji e86aa61f20 Add encoding argument to Integral#chr
Currently, `Integral#chr` in mruby changes behavior by `MRB_UTF8_STRING`
setting.

before this patch:

  $ bin/mruby -e 'p 171.chr'  #=> "\xab"  (`MRB_UTF8_STRING` is disabled)
  $ bin/mruby -e 'p 171.chr'  #=> "«"     (`MRB_UTF8_STRING` is enabled)

This behavior is incompatible with Ruby, and a little inconvenient because
it can't be interpreted as ASCII-8BIT with `MRB_UTF8_STRING`, I think.

So add encoding argument according to Ruby.

after this patch:

  $ bin/mruby -e 'p 171.chr'                #=> "\xab"
  $ bin/mruby -e 'p 171.chr("ASCII-8BIT")'  #=> "\xab"
  $ bin/mruby -e 'p 171.chr("UTF-8")'       #=> "«"

Allow only `String` for encoding because mruby doesn't have `Encoding`
class, and `"ASCII-8BIT"` (`"BINARY"`) and `"UTF-8"` (only with
`MRB_UTF8_STRING`) are valid value (default is `"ASCII-8BIT"`).
2019-07-23 20:16:46 +09:00
KOBAYASHI Shuji 270131253f Remove duplicated String#each_char 2019-04-27 12:50:02 +09:00
KOBAYASHI Shuji cd4daf78ec Use FrozenError instead of RuntimeError in String#rstrip! 2019-03-15 22:49:40 +09:00
Yukihiro "Matz" Matsumoto b5d43a16a3 Removed String#try_convert method from mruby-string-ext gem.
Because `try_convert` method rarely used in production. For mruby users,
we have `__to_str` utility method to check string type.
2018-11-19 12:08:28 +09:00
Yukihiro "Matz" Matsumoto ff08856fe3 Remove implicit conversion using to_str method; fix #3854
We have added internal convenience method `__to_str` which
does string type check.

The issue #3854 was fixed but fundamental flaw of lack of stack
depth check along with fibers still remains. Use `MRB_GC_FIXED_ARENA`
for workaround.
2018-11-19 12:05:46 +09:00
Yukihiro "Matz" Matsumoto 2213deeeaa Implement String#upto in Ruby.
Avoid using `mrb_yield` in C code. The function is not recommended.
Because it doesn't work well with fibers.
2018-04-21 22:30:54 +09:00
Yukihiro "Matz" Matsumoto 8b92ab5c7b CRuby2.6 stops deprecating String#lines with a block.
`String#lines` (with a block) is now implemented in Ruby.
2018-04-19 17:33:00 +09:00
Yukihiro "Matz" Matsumoto bbb0882343 Modifying frozen objects will raise FrozenError.
`FrozenError` is a subclass of `RuntimeError` which used to be
raised.  [Ruby2.5]
2017-12-12 18:41:18 +09:00
Yukihiro "Matz" Matsumoto 4122c320bb Add {String,Symbol}#casecmp?; CRuby2.4 2017-10-17 13:26:37 +09:00
ksss 19785f43d1 Reimplement String#upto 2017-06-14 17:30:31 +09:00
Yukihiro "Matz" Matsumoto 4763312fb8 Terminate loop if generated string longer than the last; ref #3489 2017-03-10 10:38:28 +09:00
ksss eba4b1fd76 Check modifiable for String `bang' methods 2017-03-05 23:04:22 +09:00
Tomasz Dabrowski ac9d04f4af String#ljust and String#rjust reimplemented with optimized Ruby 2017-02-10 15:31:33 +01:00
Tomasz Dabrowski 981105b3e6 String#ljust and String#rjust reimplementation (fix #3445)
- String#ljust and String#rjust are now C functions to improve performance
 - infinite loop because of an empty padding argument is now prevented (ArgumentError is raised)
 - extra tests for ljust/rjust added
2017-02-10 12:59:28 +01:00
ksss eeca6ec8f5 Rewrite String#prepend with Ruby
Fix #3357
2017-01-04 14:00:28 +09:00
Yukihiro "Matz" Matsumoto 92be276d76 String#{strip,lstrip,rstrip} may cause OOB access 2016-11-17 18:00:21 +09:00
Nobuyoshi Nakada 6511bfd79a Removed trailing spaces 2016-09-28 12:29:41 +09:00
Kouhei Sutou 6bb0775d76 Reduce needless Array generation in some String methods
Here are some benchmarks:

each_char:

    # /tmp/each_char.rb
    a = "a" * 1000000
    a.each_char do |x|
    end

Without this change:

    % time bin/mruby /tmp/each_char.rb
    bin/mruby /tmp/each_char.rb  1.07s user 0.02s system 99% cpu 1.088 total

With this change:

    % time bin/mruby /tmp/each_char.rb
    bin/mruby /tmp/each_char.rb  0.52s user 0.01s system 99% cpu 0.530 total

2 times faster with this change.

codepoints:

    # /tmp/codepoints.rb
    a = "a" * 1000000
    a.codepoints do |x|
    end

Without this change:

    % time bin/mruby /tmp/codepoints.rb
    bin/mruby /tmp/codepoints.rb  1.16s user 0.05s system 99% cpu 1.216 total

With this change:

    % time bin/mruby /tmp/codepoints.rb
    bin/mruby /tmp/codepoints.rb  0.56s user 0.02s system 99% cpu 0.589 total
2016-07-27 13:58:07 +09:00
ksss bef63a4f91 Support to call without block to String#each_char 2016-07-26 18:46:23 +09:00
ksss d83d9cd164 String#insert should be destructive 2016-07-14 21:33:15 +09:00
Akira Moroo e86bc6bb47 Add String#rjust to mruby-string-ext 2016-01-30 14:25:53 +09:00
takahashim 621487a0cd add {Array|Hash|String}.try_convert 2015-11-24 08:39:39 +09:00