|
50 | 50 | #include "shape.h" |
51 | 51 | #include "vm_sync.h" |
52 | 52 | #include "ruby/internal/attr/nonstring.h" |
53 | | -#include "builtin.h" |
54 | 53 |
|
55 | 54 | #if defined HAVE_CRYPT_R |
56 | 55 | # if defined HAVE_CRYPT_H |
@@ -2136,26 +2135,29 @@ rb_str_init(rb_execution_context_t *ec, VALUE str, VALUE orig, VALUE no_str, VAL |
2136 | 2135 | return str; |
2137 | 2136 | } |
2138 | 2137 |
|
2139 | | -/* String.new fast path: no positional argument and no keywords. |
2140 | | - * Returns the same empty, embedded, ASCII-8BIT string that String's alloc |
2141 | | - * func produces (klass is always rb_cString here; the Ruby-side dispatch |
2142 | | - * routes subclasses through Class#new). */ |
2143 | | -static VALUE |
2144 | | -rb_str_s_new_empty(rb_execution_context_t *ec, VALUE klass) |
2145 | | -{ |
2146 | | - return empty_str_alloc(klass); |
2147 | | -} |
2148 | | - |
2149 | 2138 | /* :nodoc: */ |
2150 | 2139 | static VALUE |
2151 | | -rb_str_s_new(struct rb_execution_context_struct *ec, VALUE klass, VALUE orig, VALUE no_str, VALUE encoding, VALUE no_encoding, VALUE capacity, VALUE no_capacity) |
| 2140 | +rb_str_s_new(int argc, VALUE *argv, VALUE klass) |
2152 | 2141 | { |
| 2142 | + if (klass != rb_cString) { |
| 2143 | + return rb_class_new_instance_pass_kw(argc, argv, klass); |
| 2144 | + } |
| 2145 | + |
| 2146 | + static ID keyword_ids[2]; |
| 2147 | + VALUE orig, opt, encoding = Qnil, capacity = Qnil; |
| 2148 | + VALUE kwargs[2]; |
2153 | 2149 | rb_encoding *enc = NULL; |
2154 | | - int n = 0; |
2155 | 2150 |
|
2156 | | - if (RTEST(no_encoding)) encoding = Qundef; |
2157 | | - if (RTEST(no_capacity)) capacity = Qundef; |
2158 | | - if (!RTEST(no_str)) n = 1; |
| 2151 | + int n = rb_scan_args(argc, argv, "01:", &orig, &opt); |
| 2152 | + if (NIL_P(opt)) { |
| 2153 | + return rb_class_new_instance_pass_kw(argc, argv, klass); |
| 2154 | + } |
| 2155 | + |
| 2156 | + keyword_ids[0] = rb_id_encoding(); |
| 2157 | + CONST_ID(keyword_ids[1], "capacity"); |
| 2158 | + rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs); |
| 2159 | + encoding = kwargs[0]; |
| 2160 | + capacity = kwargs[1]; |
2159 | 2161 |
|
2160 | 2162 | if (n == 1) { |
2161 | 2163 | orig = StringValue(orig); |
@@ -12878,6 +12880,7 @@ Init_String(void) |
12878 | 12880 |
|
12879 | 12881 | rb_include_module(rb_cString, rb_mComparable); |
12880 | 12882 | rb_define_alloc_func(rb_cString, empty_str_alloc); |
| 12883 | + rb_define_singleton_method(rb_cString, "new", rb_str_s_new, -1); |
12881 | 12884 | rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1); |
12882 | 12885 | rb_define_method(rb_cString, "replace", rb_str_replace, 1); |
12883 | 12886 | rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1); |
|
0 commit comments