|
50 | 50 | #include "shape.h" |
51 | 51 | #include "vm_sync.h" |
52 | 52 | #include "ruby/internal/attr/nonstring.h" |
| 53 | +#include "builtin.h" |
53 | 54 |
|
54 | 55 | #if defined HAVE_CRYPT_R |
55 | 56 | # if defined HAVE_CRYPT_H |
@@ -2142,29 +2143,26 @@ rb_str_init(int argc, VALUE *argv, VALUE str) |
2142 | 2143 | return str; |
2143 | 2144 | } |
2144 | 2145 |
|
2145 | | -/* :nodoc: */ |
| 2146 | +/* String.new fast path: no positional argument and no keywords. |
| 2147 | + * Returns the same empty, embedded, ASCII-8BIT string that String's alloc |
| 2148 | + * func produces (klass is always rb_cString here; the Ruby-side dispatch |
| 2149 | + * routes subclasses through Class#new). */ |
2146 | 2150 | static VALUE |
2147 | | -rb_str_s_new(int argc, VALUE *argv, VALUE klass) |
| 2151 | +rb_str_s_new_empty(rb_execution_context_t *ec, VALUE klass) |
2148 | 2152 | { |
2149 | | - if (klass != rb_cString) { |
2150 | | - return rb_class_new_instance_pass_kw(argc, argv, klass); |
2151 | | - } |
| 2153 | + return empty_str_alloc(klass); |
| 2154 | +} |
2152 | 2155 |
|
2153 | | - static ID keyword_ids[2]; |
2154 | | - VALUE orig, opt, encoding = Qnil, capacity = Qnil; |
2155 | | - VALUE kwargs[2]; |
| 2156 | +/* :nodoc: */ |
| 2157 | +static VALUE |
| 2158 | +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) |
| 2159 | +{ |
2156 | 2160 | rb_encoding *enc = NULL; |
| 2161 | + int n = 0; |
2157 | 2162 |
|
2158 | | - int n = rb_scan_args(argc, argv, "01:", &orig, &opt); |
2159 | | - if (NIL_P(opt)) { |
2160 | | - return rb_class_new_instance_pass_kw(argc, argv, klass); |
2161 | | - } |
2162 | | - |
2163 | | - keyword_ids[0] = rb_id_encoding(); |
2164 | | - CONST_ID(keyword_ids[1], "capacity"); |
2165 | | - rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs); |
2166 | | - encoding = kwargs[0]; |
2167 | | - capacity = kwargs[1]; |
| 2163 | + if (RTEST(no_encoding)) encoding = Qundef; |
| 2164 | + if (RTEST(no_capacity)) capacity = Qundef; |
| 2165 | + if (!RTEST(no_str)) n = 1; |
2168 | 2166 |
|
2169 | 2167 | if (n == 1) { |
2170 | 2168 | orig = StringValue(orig); |
@@ -12905,7 +12903,6 @@ Init_String(void) |
12905 | 12903 |
|
12906 | 12904 | rb_include_module(rb_cString, rb_mComparable); |
12907 | 12905 | rb_define_alloc_func(rb_cString, empty_str_alloc); |
12908 | | - rb_define_singleton_method(rb_cString, "new", rb_str_s_new, -1); |
12909 | 12906 | rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1); |
12910 | 12907 | rb_define_method(rb_cString, "initialize", rb_str_init, -1); |
12911 | 12908 | rb_define_method(rb_cString, "replace", rb_str_replace, 1); |
@@ -13107,3 +13104,5 @@ Init_String(void) |
13107 | 13104 | rb_define_method(rb_cSymbol, "encoding", sym_encoding, 0); |
13108 | 13105 | } |
13109 | 13106 |
|
| 13107 | +#include "string.rbinc" |
| 13108 | + |
0 commit comments