53 Commits

Author SHA1 Message Date
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
Tomoyuki Sahara 3b76aae4f0 fix ISO reference number of String#=~. 2014-01-10 12:01:24 +09:00
Yukihiro "Matz" Matsumoto 4d92d2e1aa string type check based on #to_str to encourage duck typing; #1616 2013-12-24 07:02:37 +09:00
h2so5 e137d1423d verify the argument of String#=~ 2013-12-24 00:09:34 +09:00
Yukihiro "Matz" Matsumoto 8d7e716158 String =~ and match to work with pluggable Regexp; close #1398 2013-07-23 20:22:15 +09:00
Yukihiro Matz Matsumoto b7da7cfee0 adopt String#gsub to fixed split behavior 2013-02-18 18:47:58 +09:00
Daniel Bovensiepen e8c0b61c0d String#gsub fix with last character 2013-02-17 12:13:46 +00:00
Yuichiro MASUI 0ee2c71b03 Added String#sub/sub! and String#gsub/gsub! 2013-02-12 16:10:35 +09:00
skandhas 8a43ec75a0 delete needless output in String#[]= 2012-11-02 14:58:21 +08:00
Yukihiro Matsumoto 57910ca535 String#each_byte to use String#bytes; close #508 2012-11-01 06:13:02 +09:00