Skip to content

Commit 14bcf69

Browse files
committed
Deprecate Encoding#replicate
* See [Feature #18949].
1 parent f641179 commit 14bcf69

File tree

5 files changed

+71
-53
lines changed

5 files changed

+71
-53
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Note that each entry is kept to a minimum, see links for details.
9696

9797
Note: We're only listing outstanding class updates.
9898
99+
* Encoding
100+
* Encoding#replicate has been deprecated and will be removed in 3.3. [[Feature #18949]]
101+
99102
* Enumerator
100103
* Enumerator.product has been added. Enumerator::Product is the implementation. [[Feature #18685]]
101104
@@ -292,3 +295,4 @@ The following deprecated APIs are removed.
292295
[Feature #18788]: https://bugs.ruby-lang.org/issues/18788
293296
[Feature #18809]: https://bugs.ruby-lang.org/issues/18809
294297
[Feature #18481]: https://bugs.ruby-lang.org/issues/18481
298+
[Feature #18949]: https://bugs.ruby-lang.org/issues/18949

common.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5418,6 +5418,7 @@ encoding.$(OBJEXT): $(top_srcdir)/internal/class.h
54185418
encoding.$(OBJEXT): $(top_srcdir)/internal/compilers.h
54195419
encoding.$(OBJEXT): $(top_srcdir)/internal/enc.h
54205420
encoding.$(OBJEXT): $(top_srcdir)/internal/encoding.h
5421+
encoding.$(OBJEXT): $(top_srcdir)/internal/error.h
54215422
encoding.$(OBJEXT): $(top_srcdir)/internal/gc.h
54225423
encoding.$(OBJEXT): $(top_srcdir)/internal/inits.h
54235424
encoding.$(OBJEXT): $(top_srcdir)/internal/load.h

encoding.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "internal.h"
1818
#include "internal/enc.h"
1919
#include "internal/encoding.h"
20+
#include "internal/error.h"
2021
#include "internal/inits.h"
2122
#include "internal/load.h"
2223
#include "internal/object.h"
@@ -569,7 +570,10 @@ rb_enc_replicate(const char *name, rb_encoding *encoding)
569570
static VALUE
570571
enc_replicate_m(VALUE encoding, VALUE name)
571572
{
572-
int idx = rb_enc_replicate(name_for_encoding(&name), rb_to_encoding(encoding));
573+
int idx;
574+
rb_warn_deprecated_to_remove("3.3", "Encoding#replicate", "the original encoding");
575+
576+
idx = rb_enc_replicate(name_for_encoding(&name), rb_to_encoding(encoding));
573577
RB_GC_GUARD(name);
574578
return rb_enc_from_encoding_index(idx);
575579
}

spec/ruby/core/encoding/replicate_spec.rb

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,74 @@
22
require_relative '../../spec_helper'
33

44
describe "Encoding#replicate" do
5-
before :all do
6-
@i = 0
7-
end
5+
ruby_version_is ""..."3.3" do
6+
before :all do
7+
@i = 0
8+
end
89

9-
before :each do
10-
@i += 1
11-
@prefix = "RS#{@i}"
12-
end
10+
before :each do
11+
@i += 1
12+
@prefix = "RS#{@i}"
13+
end
1314

14-
it "returns a replica of ASCII" do
15-
name = @prefix + '-ASCII'
16-
e = Encoding::ASCII.replicate(name)
17-
e.name.should == name
18-
Encoding.find(name).should == e
15+
it "returns a replica of ASCII" do
16+
name = @prefix + '-ASCII'
17+
e = suppress_warning { Encoding::ASCII.replicate(name) }
18+
e.name.should == name
19+
Encoding.find(name).should == e
1920

20-
"a".force_encoding(e).valid_encoding?.should be_true
21-
"\x80".force_encoding(e).valid_encoding?.should be_false
22-
end
21+
"a".force_encoding(e).valid_encoding?.should be_true
22+
"\x80".force_encoding(e).valid_encoding?.should be_false
23+
end
2324

24-
it "returns a replica of UTF-8" do
25-
name = @prefix + 'UTF-8'
26-
e = Encoding::UTF_8.replicate(name)
27-
e.name.should == name
28-
Encoding.find(name).should == e
25+
it "returns a replica of UTF-8" do
26+
name = @prefix + 'UTF-8'
27+
e = suppress_warning { Encoding::UTF_8.replicate(name) }
28+
e.name.should == name
29+
Encoding.find(name).should == e
2930

30-
"a".force_encoding(e).valid_encoding?.should be_true
31-
"\u3042".force_encoding(e).valid_encoding?.should be_true
32-
"\x80".force_encoding(e).valid_encoding?.should be_false
33-
end
31+
"a".force_encoding(e).valid_encoding?.should be_true
32+
"\u3042".force_encoding(e).valid_encoding?.should be_true
33+
"\x80".force_encoding(e).valid_encoding?.should be_false
34+
end
3435

35-
it "returns a replica of UTF-16BE" do
36-
name = @prefix + 'UTF-16-BE'
37-
e = Encoding::UTF_16BE.replicate(name)
38-
e.name.should == name
39-
Encoding.find(name).should == e
36+
it "returns a replica of UTF-16BE" do
37+
name = @prefix + 'UTF-16-BE'
38+
e = suppress_warning { Encoding::UTF_16BE.replicate(name) }
39+
e.name.should == name
40+
Encoding.find(name).should == e
4041

41-
"a".force_encoding(e).valid_encoding?.should be_false
42-
"\x30\x42".force_encoding(e).valid_encoding?.should be_true
43-
"\x80".force_encoding(e).valid_encoding?.should be_false
44-
end
42+
"a".force_encoding(e).valid_encoding?.should be_false
43+
"\x30\x42".force_encoding(e).valid_encoding?.should be_true
44+
"\x80".force_encoding(e).valid_encoding?.should be_false
45+
end
4546

46-
it "returns a replica of ISO-2022-JP" do
47-
name = @prefix + 'ISO-2022-JP'
48-
e = Encoding::ISO_2022_JP.replicate(name)
49-
Encoding.find(name).should == e
47+
it "returns a replica of ISO-2022-JP" do
48+
name = @prefix + 'ISO-2022-JP'
49+
e = suppress_warning { Encoding::ISO_2022_JP.replicate(name) }
50+
Encoding.find(name).should == e
5051

51-
e.name.should == name
52-
e.dummy?.should be_true
53-
end
52+
e.name.should == name
53+
e.dummy?.should be_true
54+
end
5455

55-
# NOTE: it's unclear of the value of this (for the complexity cost of it),
56-
# but it is the current CRuby behavior.
57-
it "can be associated with a String" do
58-
name = @prefix + '-US-ASCII'
59-
e = Encoding::US_ASCII.replicate(name)
60-
e.name.should == name
61-
Encoding.find(name).should == e
56+
# NOTE: it's unclear of the value of this (for the complexity cost of it),
57+
# but it is the current CRuby behavior.
58+
it "can be associated with a String" do
59+
name = @prefix + '-US-ASCII'
60+
e = suppress_warning { Encoding::US_ASCII.replicate(name) }
61+
e.name.should == name
62+
Encoding.find(name).should == e
63+
64+
s = "abc".force_encoding(e)
65+
s.encoding.should == e
66+
s.encoding.name.should == name
67+
end
68+
end
6269

63-
s = "abc".force_encoding(e)
64-
s.encoding.should == e
65-
s.encoding.name.should == name
70+
ruby_version_is "3.3" do
71+
it "has been removed" do
72+
Encoding::US_ASCII.should_not.respond_to?(:replicate, true)
73+
end
6674
end
6775
end

test/ruby/test_encoding.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def test_find
5757

5858
def test_replicate
5959
assert_separately([], "#{<<~'END;'}")
60+
Warning[:deprecated] = false
6061
assert_instance_of(Encoding, Encoding::UTF_8.replicate("UTF-8-ANOTHER#{Time.now.to_f}"))
6162
assert_instance_of(Encoding, Encoding::ISO_2022_JP.replicate("ISO-2022-JP-ANOTHER#{Time.now.to_f}"))
6263
bug3127 = '[ruby-dev:40954]'
@@ -69,7 +70,7 @@ def test_extra_encoding
6970
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
7071
begin;
7172
200.times {|i|
72-
Encoding::UTF_8.replicate("dummy#{i}")
73+
EnvUtil.suppress_warning { Encoding::UTF_8.replicate("dummy#{i}") }
7374
}
7475
e = Encoding.list.last
7576
format = "%d".force_encoding(e)
@@ -82,7 +83,7 @@ def test_extra_encoding
8283
8384
name = "A" * 64
8485
Encoding.list.each do |enc|
85-
assert_raise(ArgumentError) {enc.replicate(name)}
86+
assert_raise(ArgumentError) { EnvUtil.suppress_warning { enc.replicate(name) } }
8687
name.succ!
8788
end
8889
end;

0 commit comments

Comments
 (0)