Skip to content

Commit 841921f

Browse files
committed
Revert "implement String.new in Ruby"
This reverts commit fef48f0.
1 parent 8a64e7a commit 841921f

2 files changed

Lines changed: 19 additions & 47 deletions

File tree

string.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
#include "shape.h"
5151
#include "vm_sync.h"
5252
#include "ruby/internal/attr/nonstring.h"
53-
#include "builtin.h"
5453

5554
#if defined HAVE_CRYPT_R
5655
# 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
21362135
return str;
21372136
}
21382137

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-
21492138
/* :nodoc: */
21502139
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)
21522141
{
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];
21532149
rb_encoding *enc = NULL;
2154-
int n = 0;
21552150

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];
21592161

21602162
if (n == 1) {
21612163
orig = StringValue(orig);
@@ -12878,6 +12880,7 @@ Init_String(void)
1287812880

1287912881
rb_include_module(rb_cString, rb_mComparable);
1288012882
rb_define_alloc_func(rb_cString, empty_str_alloc);
12883+
rb_define_singleton_method(rb_cString, "new", rb_str_s_new, -1);
1288112884
rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1);
1288212885
rb_define_method(rb_cString, "replace", rb_str_replace, 1);
1288312886
rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);

string.rb

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,9 @@ def ascii_only?
2323
Primitive.cexpr! 'rb_str_is_ascii_only_p(self)'
2424
end
2525

26-
# :nodoc:
27-
def self._new(orig = (no_str = true; nil),
28-
encoding: (no_encoding = true; nil),
29-
capacity: (no_capacity = true; nil))
30-
31-
Primitive.rb_str_s_new(orig, no_str, encoding, no_encoding, capacity, no_capacity)
32-
end
33-
private_class_method :_new
34-
3526
def initialize(orig = (no_str = true; nil),
3627
encoding: (no_encoding = true; nil),
3728
capacity: (no_capacity = true; nil))
3829
Primitive.rb_str_init(orig, no_str, encoding, no_encoding, capacity, no_capacity)
3930
end
40-
41-
# call-seq:
42-
# String.new(string = ''.encode(Encoding::ASCII_8BIT), **options) -> new_string
43-
#
44-
# :include: doc/string/new.rdoc
45-
#
46-
def self.new(...)
47-
# If the receiver isn't a String, jbb
48-
if Primitive.mandatory_only?
49-
if String.equal?(self)
50-
Primitive.rb_str_s_new_empty
51-
else
52-
super
53-
end
54-
else
55-
if String.equal?(self)
56-
_new(...)
57-
else
58-
super
59-
end
60-
end
61-
end
6231
end

0 commit comments

Comments
 (0)