62 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 89f22158d3 string.rb (each_line): may truncate string by mistake; fix #5727 2022-06-24 13:25:45 +09:00
Yukihiro "Matz" Matsumoto 038aa6beb2 strinc.rb (sub, gsub): use byte index instead of char index. 2022-05-19 10:14:59 +09:00
Yukihiro "Matz" Matsumoto ce141b1332 string.rb (each_line): should not use [] reference.
Use `byteslice` instead.
2022-05-19 10:14:59 +09:00
Yukihiro "Matz" Matsumoto f8b9464118 string.rb (each_line): use byteindex for performance; fix #4522
Character-wise String#index method calculate UTF-8 character width
numerous times if the string is long.
2022-05-17 07:18:44 +09:00
dearblue 76d3ea8458 Improvements to String#each_byte
Each byte value is now retrieved inside the loop.
2022-04-16 22:23:34 +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 3622f2c4b5 string.c: implement __sub_replace() in C.
To reduce number of string allocation.
2021-09-01 07:00:55 +09:00
Yukihiro "Matz" Matsumoto 082882da69 string.rb: avoid internal __to_str calls.
`__to_str` was a mere type check method despite its name.
2021-09-01 07:00:55 +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 6bbdb97e75 Merge pull request #4527 from lopopolo/string-each-line-paragraph-mode
Add paragraph mode to String#each_line in mrblib
2019-09-12 21:19:42 +09:00
dearblue 0d452073f4 Replace String#[]= method by C implements
The purpose is to eliminate string objects that are temporarily created during processing.
2019-06-29 14:41:43 +09:00
Ryan Lopopolo 46c972f746 Unify loops to minimize bytecode size 2019-06-25 00:01:51 +02:00
Ryan Lopopolo 6b92acf361 Use explicit block parameter 2019-06-23 03:40:39 +01:00
Ryan Lopopolo 8ea45b862a Optimize String#each_line 2019-06-23 03:29:47 +01:00
Ryan Lopopolo a932b8c96a Speed up base case by 2x
Make non-paragraph mode twice as fast. Performance is within a factor of 2
of the original implementation.
2019-06-22 12:55:02 +01:00
Ryan Lopopolo 0f516bbe1d Add paragraph mode to String#each_line in mrblib
mruby/mruby#4511 demonstrated an infinite loop in `String#each_line` when
given an empty string separator. In MRI, an empty separator places
String#each_line in paragraph mode, where the String is separated on
successive runs of newlines. In paragraph mode, the String
`"abc\n\n\ndef\nxyz"` is split into `["abc\n\n\n", "def\nxyz"]`.

This commit makes the String#each_line implementation as close to
ruby/spec compliant as possible given the limitations of mruby core.
With this patch, the following specs fail for `String#each_line`:

- uses `$/` as the separator when none is given (can be fixed by
  aliasing and redefining the method to use $/ as the default value
  of separator in mruby-io)
- when no block is given returned Enumerator size should return nil
  (`Enumerator#size` is not supported on mruby)
- tries to convert the separator to a string using to_str (`String#to_str`
  is not implemented on mruby)

This patch has similar memory consumption compared to the prior
implementation and is takes 4x the time the prior implementation takes to
execute:

```console
/usr/bin/time -l ./bin/mruby -e '("aaa\n\nbbbbb\n\n\n\n\ncccc" * 100000).each_line("\n") { }';
```
2019-06-22 12:31:01 +01:00
Yukihiro "Matz" Matsumoto 780342eddb Add Enumerator support to String#each_byte.
`String#each_byte` is not defined in ISO Ruby but it is implemented in
the core mruby because it's useful.
2019-05-15 14:59:48 +09:00
Yukihiro "Matz" Matsumoto fd37bc53de Remove String#=~ and String#match that requires Regexp. 2019-05-15 09:55:21 +09:00
KOBAYASHI Shuji 270131253f Remove duplicated String#each_char 2019-04-27 12:50:02 +09:00
KOBAYASHI Shuji cdb458ed4e Commented out String#scan because it is not implemented yet 2019-04-21 20:34:39 +09:00
KOBAYASHI Shuji 4a8b88f775 Add type check (conversion) in String#[]=
Before this patch:

  'a'[0] = 1        #=> 1
  'a'[:a] = '1'     #=> ArgumentError
  'a'[:a, 0] = '1'  #=> ArgumentError
  'a'[0, :a] = '1'  #=> ArgumentError
  'a'[0, 1] = 1     #=> 1

After this patch / Ruby:

  'a'[0] = 1        #=> TypeError
  'a'[:a] = '1'     #=> TypeError
  'a'[:a, 0] = '1'  #=> TypeError
  'a'[0, :a] = '1'  #=> TypeError
  'a'[0, 1] = 1     #=> TypeError
2019-04-19 20:14:23 +09:00
KOBAYASHI Shuji 916045921e Remove duplicated include Comparable in mrblib/string.rb 2019-04-18 19:58:05 +09:00
Yukihiro "Matz" Matsumoto 292bffb32e Avoid infinite loop when no Regexp class is available; fix #4363 2019-04-08 20:27:28 +09:00
Yukihiro "Matz" Matsumoto c022e4643f Avoid assignments from type checking String#__to_str. 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 35be8b2524 Fixed the corner case bug in String#{gsub!,sub!}.
`"a".sub!("a", "a")` should not return `nil`.
2018-08-07 23:03:17 +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 057be5ffb5 use unless instead of if not. 2017-06-21 11:13:33 +09:00
ksss 63c0044b11 Fix result if pattern is empty 2017-03-22 22:19:34 +09:00
ksss 7a74a617ea Callback should yield with pattern every time 2017-03-19 23:04:22 +09:00
ksss 67a6982710 Support to return Enumerator for String#gsub,gsub! 2017-03-19 22:45:14 +09:00
ksss 4ee79a8777 Support to return enumerator when no block given 2017-03-15 16:58:29 +09:00
ksss 673ce237c4 Suuport custom separator 2017-03-15 16:58:28 +09:00
ksss 6eb02a892d Use duplicated object for block args 2017-03-15 16:58:28 +09:00
Yukihiro "Matz" Matsumoto 7590482d48 Merge branch 'master' into string-gsub 2017-03-11 10:22:17 +09:00
ksss b56ad8d122 \1 sequences as empty strings 2017-03-10 22:22:06 +09:00
ksss 4b3e6cf169 Avoid infinity loop when empty string pattern 2017-03-10 13:26:15 +09:00
ksss eba4b1fd76 Check modifiable for String `bang' methods 2017-03-05 23:04:22 +09:00
Yukihiro "Matz" Matsumoto 1685eff2a5 Fixed off-by-one error in String#[]= with Ranges 2016-11-16 10:16:14 +09:00
Yukihiro "Matz" Matsumoto 92f72c7490 make String#[]= to take Ranges as position argument 2016-11-16 02:04:56 +09:00
Yukihiro "Matz" Matsumoto 418eec2c7d remove a comment that no longer be true; 2cb6c27 2015-09-23 00:01:39 +09:00
Yukihiro "Matz" Matsumoto 2cb6c27781 String#index should no longer take integer argument 2015-09-22 19:10:48 +09:00
takahashim f1c23a0f75 support String#[]= with 3 args 2015-09-16 18:50:03 +09:00
go.kikuta 11524f6f67 string.rb: refactor code (remove redundant code) 2015-08-21 14:54:01 +09:00
Jared Breeden 08c12ab7bf Don't crash if pattern not found for sub 2015-07-16 15:33:07 -07:00
Tomoyuki Sahara 5dd2b8e16f gsub/sub supports back references in substitutes. fixes #2816.
This implementation is compatible with CRuby's String#gsub/sub
except \1 ... \9 and \+.  They are useless without Regexp library.
2015-06-08 17:32:14 +09:00
Yukihiro "Matz" Matsumoto b05d736b49 update mrblib/*.rb files to conform (some of) Rubocop checks 2015-05-29 17:01:52 +09:00
Yukihiro "Matz" Matsumoto 8aba34301b String#[]= should support negative position; close #2707 2015-01-19 22:13:34 +09:00
takahashim bcceeba09b fix infinite loop in String#match(arg) when arg is String 2015-01-14 02:18:56 +09:00
Yukihiro "Matz" Matsumoto 4a2c37df93 made mrb_define_class to return existing class, with heavy refactoring 2014-02-08 13:44:31 +09:00