157 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 49ae2a69f2 Add mrb_get_arg1() that retrieves single (and only) argument.
`mrb_get_arg1()` raises `ArgumentError` if the method does not receive one
argument.

And replaces all `mrb_get_args(mrb, "o", &arg)` by the new function.
2020-06-20 12:49:46 +09:00
Yukihiro "Matz" Matsumoto 106f4c4e9d Add proper casts to silence VC warnings. 2020-06-05 16:04:30 +09:00
KOBAYASHI Shuji 7047c52d7b Use mrb_str_cat_str instead of mrb_str_concat if possible 2019-10-20 16:26:10 +09:00
KOBAYASHI Shuji 7ce5d33947 Integrate mrb_str_inspect and mrb_str_dump 2019-10-10 19:50:48 +09:00
KOBAYASHI Shuji f252ab0770 Use mrb_define_method instead of mrb_define_alias 2019-09-21 16:51:10 +09:00
KOBAYASHI Shuji 0bedd900b9 Remove mrb_get_args(mrb, ""); ref 30f37872 2019-09-18 16:07:21 +09:00
Yukihiro "Matz" Matsumoto 57d7fe94a9 Add a macro mrb_frozen_p that points to MRB_FROZEN_P. 2019-09-14 23:21:44 +09:00
Yukihiro "Matz" Matsumoto 04b098d000 Move tests related to getbyte, setbyte, byteslice` to core. 2019-09-11 18:49:08 +09:00
Yukihiro "Matz" Matsumoto ec3aeede20 Move String#{getbyte,setbyte,byteslice} to the core; #4696
Unlike CRuby, there's no way to process strings byte-wise by core
methods because there's no per string encoding in mruby, so that
we moved 3 byte-wise operation methods from `mruby-string-ext` gem.
2019-09-11 18:47:39 +09:00
KOBAYASHI Shuji fab781cf8b Drop test dependency from mruby-string-ext to mruby-enumerator 2019-09-11 10:35:52 +09:00
dearblue 05a873c25c Simplify get arguments
- `mrb_str_index_m()` and `mrb_str_rindex()`
  Make `mrb_get_args()` called only once from called twice.
- `mrb_str_byteslice()`
  Replace `goto` with `if ~ else`.
2019-08-18 15:38:25 +09:00
KOBAYASHI Shuji 334afb167c Use new specifiers/modifiers of mrb_vfromat()
The binary sizes (gems are only `mruby-bin-mruby`) are reduced slightly in
my environment than before the introduction of new specifiers/modifiers
(5116789a) with this change.

  ------------+-------------------+-------------------+--------
   BINARY     | BEFORE (5116789a) |   AFTER (This PR) |  RATIO
  ------------+-------------------+-------------------+--------
   mruby      |      593416 bytes |      593208 bytes | -0.04%
   libmruby.a |      769048 bytes |      767264 bytes | -0.23%
  ------------+-------------------+-------------------+--------

BTW, I accidentally changed `tasks/toolchains/visualcpp.rake` at #4613,
so I put it back.
2019-08-05 13:18:50 +09:00
Yukihiro "Matz" Matsumoto 5779464ec3 Add return to silence a warning; ref #4593 2019-07-24 10: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 c980fe2792 Integrate Integral#chr (Fixnum#chr) to mruby-string-ext
Because they're defined in both `mruby-string-ext` and `mruby-numeric-ext`
(they seem more natural to define in N, but `mruby-string-ext` depends on
`Integral#chr`).
2019-07-21 22:59:41 +09:00
KOBAYASHI Shuji 8ab846b5b9 Refine String#chr test and separate Fixnum#chr test 2019-07-19 21:48:06 +09:00
Ryan Lopopolo eab07daf92 Add Range#max and Range#min tests from Ruby Spec 2019-07-09 01:41:10 -07:00
Ryan Lopopolo 469c6261c6 Add tests for String Ranges
Range#each depends on String#upto which is implemented in mruby-string-ext
which is why these tests live there.
2019-07-08 22:36:24 -07:00
KOBAYASHI Shuji bc3176da63 Use __ENCODING__ in tests
It cannot be used for `String#size` test if judging whether or not `MRB_UTF8_STRING` is defined by result of `String#size`.
2019-06-28 19:26:29 +09:00
KOBAYASHI Shuji 75df13a973 Fix String#byteslice with MRB_UTF8_STRING and some edge cases
Example:

  $ bin/mruby -e '
    p "あa".byteslice(1)
    p "bar".byteslice(3)
    p "bar".byteslice(4..0)
  '

  Before this patch:

    "a"
    ""
    RangeError (4..0 out of range)

  After this patch (same as Ruby):

    "\x81"
    nil
    nil
2019-06-25 23:09:23 +09:00
KOBAYASHI Shuji db3e6f6ac6 Fix typo in String#setbyte error message 2019-06-12 20:45:27 +09:00
dearblue 56e0e1934d Name the return value of mrb_range_beg_len() 2019-05-25 12:12:21 +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
KOBAYASHI Shuji e3b339bec6 Fix missing assertions in mruby-string-ext test 2019-03-12 21:47:33 +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 afca99a40b Remove implicit conversion using to_int method.
The ISO standard does not include implicit type conversion using
`to_int`. This implicit conversion often causes vulnerability.
There will be no more attacks like #4120.

In addition, we have added internal convenience method `__to_int` which
does type check and conversion (from floats).
2018-11-19 11:28:51 +09:00
Yukihiro "Matz" Matsumoto 485955eccc String#{squeeze,delete,count} to use bitmap for matching; ref #4163 2018-11-15 04:51:27 +09:00
Yukihiro "Matz" Matsumoto 4550f4e381 Pattern length may overflow uint16_t; fixed #4163
The issue is reported by `https://hackerone.com/dgaletic`.
2018-11-15 02:03:54 +09:00
Yukihiro "Matz" Matsumoto f2084f300b Add tests for empty patterns for tr and count; #4156 #4157 2018-11-02 08:58:21 +09:00
Yukihiro "Matz" Matsumoto 8e59695625 Empty pattern string for String#tr should remove characters; fix #4157 2018-11-02 08:55:17 +09:00
Yukihiro "Matz" Matsumoto 65b04066a2 Empty pattern string can generate TR_UNINITIALIZED pattern; fix #4156 2018-11-02 08:54:24 +09:00
Yukihiro "Matz" Matsumoto 7ad53273a2 Silence Appveyor's VC compilation warnings. 2018-11-01 22:52:12 +09:00
Yukihiro "Matz" Matsumoto cca5e0977c VS 2017 C does not understand inline struct initialization; ref #4153 2018-10-30 09:01:12 +09:00
take-cheeze 52d55e6069 Keep tr_pattern static 2018-10-29 13:21:36 +09:00
Yukihiro "Matz" Matsumoto e6841abad6 Fixed a String#squeeze bug in handling iso-8859-1 strings; ref #4127 2018-09-27 21:43:35 +09:00
Yukihiro "Matz" Matsumoto 01c2f59e14 Revert "Fix comparisons in str_squeeze."
This reverts commit 7b04fcd092.
The issue was addressed by 9e3cbaa. No longer needed.
2018-09-26 12:35:25 +09:00
Yukihiro "Matz" Matsumoto 9e3cbaaacd Avoid using memmove() for performance; fix #4130 2018-09-26 12:20:37 +09:00
Yukihiro "Matz" Matsumoto fb49a94314 Rename tr_pattern_free() to tr_free_pattern(). 2018-09-26 12:17:38 +09:00
Takeshi Watanabe 67897195de Fix leak in mrb_str_count 2018-09-25 11:48:59 +09:00
Clayton Smith 7b04fcd092 Fix comparisons in str_squeeze. 2018-09-24 18:16:12 -04:00
Yukihiro "Matz" Matsumoto 346f154ece Implement String#delete and #delete!; ref #4086
mruby restriction:
In mruby, `String#delete` only takes single pattern argument.
2018-09-21 00:03:07 +09:00
Yukihiro "Matz" Matsumoto 58f7f2361a Implement String#count; ref #4086
mruby restriction:
In mruby, `String#count` does not take multiple pattern arguments,
but only one pattern.
2018-09-21 00:03:07 +09:00
Yukihiro "Matz" Matsumoto 68523b4ec4 Add String#squeeze and #squeeze!; ref #4086
mruby restriction:
`String#squeeze` can take more than 1 pattern arguments in CRuby,
in that case, the intersection of patterns will be used to match.
But in mruby, it doesn't take multiple patterns.
2018-09-21 00:03:07 +09:00
Yukihiro "Matz" Matsumoto c0ae8a96a1 Add String#tr_s and String#tr_s!; ref #4086 2018-09-21 00:03:07 +09:00
Yukihiro "Matz" Matsumoto 58ba38fe1e Add String#tr and #tr! to mruby-string-ext gem; fix #4086
This patch is based on `mruby/c` implementation by Hirohito Higashi.

We might need to add `#tr_s`, `#squeeze` and `#delete` as well.
Adding them should not be too hard using functions we implemented here.
2018-09-21 00:03:07 +09:00
Yukihiro "Matz" Matsumoto 891839b976 New bytecode implementation of mruby VM. 2018-07-30 22:57:54 +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