diff options
| author | Ryan Lopopolo <[email protected]> | 2019-06-22 12:07:21 +0100 |
|---|---|---|
| committer | Ryan Lopopolo <[email protected]> | 2019-06-22 12:31:01 +0100 |
| commit | 0f516bbe1dd61759faf2609970bd5cd855931221 (patch) | |
| tree | 68c725199ff2c04d40789ce9b2609f67ab60fe36 /src/numeric.c | |
| parent | f94551ad40c12370770c8e743ba8b39abc430547 (diff) | |
| download | mruby-0f516bbe1dd61759faf2609970bd5cd855931221.tar.gz mruby-0f516bbe1dd61759faf2609970bd5cd855931221.zip | |
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") { }';
```
Diffstat (limited to 'src/numeric.c')
0 files changed, 0 insertions, 0 deletions
