Commit Graph

44 Commits

Author SHA1 Message Date
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
Yukihiro "Matz" Matsumoto 798ec3aff4 UTF-8 string support in core
define MRB_UTF8_STRING (in mrbconf.h) to enable UTF-8 support.
2015-09-24 02:37:33 +09:00
INOUE Yasuyuki 0d055559f1 Use #nil? instead of == nil. 2015-08-22 19:57:11 +09:00
Jun Hiroe e998094297 Add String#upto 2015-01-13 16:32:30 +09:00
takahashim 1b606025ed add String#ljust into mruby-string-ext 2015-01-12 22:16:29 +09:00
Jun Hiroe 45a442a9d7 Add String#insert 2014-12-13 15:31:22 +09:00
TOMITA Masahiro 0a0afa7c6a Fix String#slice! raise TypeError or return invalid value. 2014-11-23 21:26:50 +09:00
Akito Mochizuki ec04fa7e8d Modify return value of String#slice! if idx == self.size 2014-06-19 17:34:17 +09:00
Jun Hiroe 00fa8097f9 Implement String#slice_bang 2014-06-15 21:11:54 +09:00
ksss f1e15b0236 String#clear: use String#replace to simple 2014-06-11 14:46:52 +00:00
Yukihiro "Matz" Matsumoto 896c131231 Merge pull request #2346 from suzukaze/add-string-comment2
Fix indent in String#casecmp comments
2014-06-03 15:44:13 +09:00
Jun Hiroe 85ed19ca4d Fix indent in String#casecmp comments 2014-06-02 22:03:29 +09:00
Jun Hiroe cbe73e93f7 Add comments in String#lstrip, rstrip, strip, lstring_bang, rstrip_bang and strip_bang 2014-06-02 22:00:05 +09:00
ksss 617fa70211 String#casecmp should be call to_str
when non String object sent
And should be raise TypeError
when can not responsed to `to_str`
2014-04-20 13:25:07 +09:00
Tomoyuki Sahara 120a02c15d add String#partition and String#rpartition. 2013-12-27 15:30:20 +09:00
h2so5 79e7bbc8e8 Add String#swapcase,swapcase!,concat,casecmp,start_with,end_with 2013-04-19 21:01:37 +09:00
Tomoyuki Sahara 11edea3880 add "strip" family to String. 2013-03-08 09:45:04 +09:00