mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
string.c: enumerator_wantarray
* string.c (enumerator_wantarray): show warnings at method functions for proper method names. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59732 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
71de56621e
commit
adac779218
2 changed files with 75 additions and 99 deletions
135
string.c
135
string.c
|
@ -7568,6 +7568,22 @@ rb_str_split(VALUE str, const char *sep0)
|
|||
return rb_str_split_m(1, &sep, str);
|
||||
}
|
||||
|
||||
static int
|
||||
enumerator_wantarray(const char *method)
|
||||
{
|
||||
if (rb_block_given_p()) {
|
||||
#if STRING_ENUMERATORS_WANTARRAY
|
||||
rb_warn("given block not used");
|
||||
#else
|
||||
rb_warning("passing a block to String#%s is deprecated", method);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define WANTARRAY(m) enumerator_wantarray(m)
|
||||
|
||||
static const char *
|
||||
chomp_newline(const char *p, const char *e, rb_encoding *enc)
|
||||
{
|
||||
|
@ -7602,23 +7618,8 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
|||
chomp = (chomp != Qundef && RTEST(chomp));
|
||||
}
|
||||
|
||||
if (rb_block_given_p()) {
|
||||
if (wantarray) {
|
||||
#if STRING_ENUMERATORS_WANTARRAY
|
||||
rb_warn("given block not used");
|
||||
ary = rb_ary_new();
|
||||
#else
|
||||
rb_warning("passing a block to String#lines is deprecated");
|
||||
wantarray = 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (wantarray)
|
||||
ary = rb_ary_new();
|
||||
else
|
||||
return SIZED_ENUMERATOR(str, argc, argv, 0);
|
||||
}
|
||||
if (wantarray)
|
||||
ary = rb_ary_new();
|
||||
|
||||
if (NIL_P(rs)) {
|
||||
if (wantarray) {
|
||||
|
@ -7780,6 +7781,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray)
|
|||
static VALUE
|
||||
rb_str_each_line(int argc, VALUE *argv, VALUE str)
|
||||
{
|
||||
RETURN_SIZED_ENUMERATOR(str, argc, argv, 0);
|
||||
return rb_str_enumerate_lines(argc, argv, str, 0);
|
||||
}
|
||||
|
||||
|
@ -7798,7 +7800,7 @@ rb_str_each_line(int argc, VALUE *argv, VALUE str)
|
|||
static VALUE
|
||||
rb_str_lines(int argc, VALUE *argv, VALUE str)
|
||||
{
|
||||
return rb_str_enumerate_lines(argc, argv, str, 1);
|
||||
return rb_str_enumerate_lines(argc, argv, str, WANTARRAY("lines"));
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -7811,25 +7813,10 @@ static VALUE
|
|||
rb_str_enumerate_bytes(VALUE str, int wantarray)
|
||||
{
|
||||
long i;
|
||||
VALUE MAYBE_UNUSED(ary);
|
||||
VALUE ary;
|
||||
|
||||
if (rb_block_given_p()) {
|
||||
if (wantarray) {
|
||||
#if STRING_ENUMERATORS_WANTARRAY
|
||||
rb_warn("given block not used");
|
||||
ary = rb_ary_new();
|
||||
#else
|
||||
rb_warning("passing a block to String#bytes is deprecated");
|
||||
wantarray = 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (wantarray)
|
||||
ary = rb_ary_new2(RSTRING_LEN(str));
|
||||
else
|
||||
return SIZED_ENUMERATOR(str, 0, 0, rb_str_each_byte_size);
|
||||
}
|
||||
if (wantarray)
|
||||
ary = rb_ary_new2(RSTRING_LEN(str));
|
||||
|
||||
for (i=0; i<RSTRING_LEN(str); i++) {
|
||||
if (wantarray)
|
||||
|
@ -7861,6 +7848,7 @@ rb_str_enumerate_bytes(VALUE str, int wantarray)
|
|||
static VALUE
|
||||
rb_str_each_byte(VALUE str)
|
||||
{
|
||||
RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_byte_size);
|
||||
return rb_str_enumerate_bytes(str, 0);
|
||||
}
|
||||
|
||||
|
@ -7878,7 +7866,7 @@ rb_str_each_byte(VALUE str)
|
|||
static VALUE
|
||||
rb_str_bytes(VALUE str)
|
||||
{
|
||||
return rb_str_enumerate_bytes(str, 1);
|
||||
return rb_str_enumerate_bytes(str, WANTARRAY("bytes"));
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -7895,30 +7883,15 @@ rb_str_enumerate_chars(VALUE str, int wantarray)
|
|||
long i, len, n;
|
||||
const char *ptr;
|
||||
rb_encoding *enc;
|
||||
VALUE MAYBE_UNUSED(ary);
|
||||
VALUE ary;
|
||||
|
||||
str = rb_str_new_frozen(str);
|
||||
ptr = RSTRING_PTR(str);
|
||||
len = RSTRING_LEN(str);
|
||||
enc = rb_enc_get(str);
|
||||
|
||||
if (rb_block_given_p()) {
|
||||
if (wantarray) {
|
||||
#if STRING_ENUMERATORS_WANTARRAY
|
||||
rb_warn("given block not used");
|
||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
||||
#else
|
||||
rb_warning("passing a block to String#chars is deprecated");
|
||||
wantarray = 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (wantarray)
|
||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
||||
else
|
||||
return SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
|
||||
}
|
||||
if (wantarray)
|
||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
||||
|
||||
if (ENC_CODERANGE_CLEAN_P(ENC_CODERANGE(str))) {
|
||||
for (i = 0; i < len; i += n) {
|
||||
|
@ -7965,6 +7938,7 @@ rb_str_enumerate_chars(VALUE str, int wantarray)
|
|||
static VALUE
|
||||
rb_str_each_char(VALUE str)
|
||||
{
|
||||
RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
|
||||
return rb_str_enumerate_chars(str, 0);
|
||||
}
|
||||
|
||||
|
@ -7982,10 +7956,9 @@ rb_str_each_char(VALUE str)
|
|||
static VALUE
|
||||
rb_str_chars(VALUE str)
|
||||
{
|
||||
return rb_str_enumerate_chars(str, 1);
|
||||
return rb_str_enumerate_chars(str, WANTARRAY("chars"));
|
||||
}
|
||||
|
||||
|
||||
static VALUE
|
||||
rb_str_enumerate_codepoints(VALUE str, int wantarray)
|
||||
{
|
||||
|
@ -7994,7 +7967,7 @@ rb_str_enumerate_codepoints(VALUE str, int wantarray)
|
|||
unsigned int c;
|
||||
const char *ptr, *end;
|
||||
rb_encoding *enc;
|
||||
VALUE MAYBE_UNUSED(ary);
|
||||
VALUE ary = 0;
|
||||
|
||||
if (single_byte_optimizable(str))
|
||||
return rb_str_enumerate_bytes(str, wantarray);
|
||||
|
@ -8004,23 +7977,8 @@ rb_str_enumerate_codepoints(VALUE str, int wantarray)
|
|||
end = RSTRING_END(str);
|
||||
enc = STR_ENC_GET(str);
|
||||
|
||||
if (rb_block_given_p()) {
|
||||
if (wantarray) {
|
||||
#if STRING_ENUMERATORS_WANTARRAY
|
||||
rb_warn("given block not used");
|
||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
||||
#else
|
||||
rb_warning("passing a block to String#codepoints is deprecated");
|
||||
wantarray = 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (wantarray)
|
||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
||||
else
|
||||
return SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
|
||||
}
|
||||
if (wantarray)
|
||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
||||
|
||||
while (ptr < end) {
|
||||
c = rb_enc_codepoint_len(ptr, end, &n, enc);
|
||||
|
@ -8060,6 +8018,7 @@ rb_str_enumerate_codepoints(VALUE str, int wantarray)
|
|||
static VALUE
|
||||
rb_str_each_codepoint(VALUE str)
|
||||
{
|
||||
RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
|
||||
return rb_str_enumerate_codepoints(str, 0);
|
||||
}
|
||||
|
||||
|
@ -8078,7 +8037,7 @@ rb_str_each_codepoint(VALUE str)
|
|||
static VALUE
|
||||
rb_str_codepoints(VALUE str)
|
||||
{
|
||||
return rb_str_enumerate_codepoints(str, 1);
|
||||
return rb_str_enumerate_codepoints(str, WANTARRAY("codepoints"));
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -8090,7 +8049,7 @@ rb_str_enumerate_grapheme_clusters(VALUE str, int wantarray)
|
|||
rb_encoding *enc = rb_enc_from_index(encidx);
|
||||
int unicode_p = rb_enc_unicode_p(enc);
|
||||
const char *ptr, *end;
|
||||
VALUE MAYBE_UNUSED(ary);
|
||||
VALUE ary = 0;
|
||||
|
||||
if (!unicode_p) {
|
||||
return rb_str_enumerate_chars(str, wantarray);
|
||||
|
@ -8115,23 +8074,8 @@ rb_str_enumerate_grapheme_clusters(VALUE str, int wantarray)
|
|||
ptr = RSTRING_PTR(str);
|
||||
end = RSTRING_END(str);
|
||||
|
||||
if (rb_block_given_p()) {
|
||||
if (wantarray) {
|
||||
#if STRING_ENUMERATORS_WANTARRAY
|
||||
rb_warn("given block not used");
|
||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
||||
#else
|
||||
rb_warning("passing a block to String#grapheme_clusters is deprecated");
|
||||
wantarray = 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (wantarray)
|
||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
||||
else
|
||||
return SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
|
||||
}
|
||||
if (wantarray)
|
||||
ary = rb_ary_new_capa(str_strlen(str, enc)); /* str's enc*/
|
||||
|
||||
while (ptr < end) {
|
||||
VALUE grapheme_cluster;
|
||||
|
@ -8173,6 +8117,7 @@ rb_str_enumerate_grapheme_clusters(VALUE str, int wantarray)
|
|||
static VALUE
|
||||
rb_str_each_grapheme_cluster(VALUE str)
|
||||
{
|
||||
RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
|
||||
return rb_str_enumerate_grapheme_clusters(str, 0);
|
||||
}
|
||||
|
||||
|
@ -8190,7 +8135,7 @@ rb_str_each_grapheme_cluster(VALUE str)
|
|||
static VALUE
|
||||
rb_str_grapheme_clusters(VALUE str)
|
||||
{
|
||||
return rb_str_enumerate_grapheme_clusters(str, 1);
|
||||
return rb_str_enumerate_grapheme_clusters(str, WANTARRAY("grapheme_clusters"));
|
||||
}
|
||||
|
||||
static long
|
||||
|
|
|
@ -807,13 +807,20 @@ CODE
|
|||
assert_equal [65, 66, 67], s.bytes {}
|
||||
}
|
||||
else
|
||||
assert_warning(/deprecated/) {
|
||||
warning = /passing a block to String#bytes is deprecated/
|
||||
assert_warning(warning) {
|
||||
res = []
|
||||
assert_equal s.object_id, s.bytes {|x| res << x }.object_id
|
||||
assert_equal(65, res[0])
|
||||
assert_equal(66, res[1])
|
||||
assert_equal(67, res[2])
|
||||
}
|
||||
assert_warning(warning) {
|
||||
s = S("ABC")
|
||||
res = []
|
||||
assert_same s, s.bytes {|x| res << x }
|
||||
assert_equal [65, 66, 67], res
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -844,13 +851,20 @@ CODE
|
|||
assert_equal [0x3042, 0x3044, 0x3046], s.codepoints {}
|
||||
}
|
||||
else
|
||||
assert_warning(/deprecated/) {
|
||||
warning = /passing a block to String#codepoints is deprecated/
|
||||
assert_warning(warning) {
|
||||
res = []
|
||||
assert_equal s.object_id, s.codepoints {|x| res << x }.object_id
|
||||
assert_equal(0x3042, res[0])
|
||||
assert_equal(0x3044, res[1])
|
||||
assert_equal(0x3046, res[2])
|
||||
}
|
||||
assert_warning(warning) {
|
||||
s = S("ABC")
|
||||
res = []
|
||||
assert_same s, s.codepoints {|x| res << x }
|
||||
assert_equal [65, 66, 67], res
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -875,7 +889,8 @@ CODE
|
|||
assert_equal ["A", "B", "C"], s.chars {}
|
||||
}
|
||||
else
|
||||
assert_warning(/deprecated/) {
|
||||
warning = /passing a block to String#chars is deprecated/
|
||||
assert_warning(warning) {
|
||||
res = []
|
||||
assert_equal s.object_id, s.chars {|x| res << x }.object_id
|
||||
assert_equal("A", res[0])
|
||||
|
@ -925,6 +940,22 @@ CODE
|
|||
assert_equal ["\u000A", "\u0308"], "\u{a 308}".grapheme_clusters
|
||||
assert_equal ["\u000D", "\u0308"], "\u{d 308}".grapheme_clusters
|
||||
assert_equal ["a", "b", "c"], "abc".b.grapheme_clusters
|
||||
|
||||
if ENUMERATOR_WANTARRAY
|
||||
assert_warn(/block not used/) {
|
||||
assert_equal ["A", "B", "C"], "ABC".grapheme_clusters {}
|
||||
}
|
||||
else
|
||||
warning = /passing a block to String#grapheme_clusters is deprecated/
|
||||
assert_warning(warning) {
|
||||
s = "ABC".b
|
||||
res = []
|
||||
assert_same s, s.grapheme_clusters {|x| res << x }
|
||||
assert_equal("A", res[0])
|
||||
assert_equal("B", res[1])
|
||||
assert_equal("C", res[2])
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def test_each_line
|
||||
|
@ -1032,7 +1063,7 @@ CODE
|
|||
assert_equal ["hello\n", "world"], s.lines {}
|
||||
}
|
||||
else
|
||||
assert_warning(/deprecated/) {
|
||||
assert_warning(/passing a block to String#lines is deprecated/) {
|
||||
res = []
|
||||
assert_equal s.object_id, s.lines {|x| res << x }.object_id
|
||||
assert_equal(S("hello\n"), res[0])
|
||||
|
|
Loading…
Add table
Reference in a new issue