aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-22 17:10:00 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-22 17:10:00 +0000
commit0d2c2bdcc13b565b3acecfc906e7d8ed7df693a2 (patch)
tree1685c7f9a014271bcaa5fff7bbf2c2782aae33bc
parent6fdda02efae859b6ddef9568315f34e65fdec789 (diff)
downloadruby-0d2c2bdcc13b565b3acecfc906e7d8ed7df693a2.tar.gz
string.c: $; name in error message
* string.c (rb_str_split_m): show $; name in error message when it is a wrong object. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55986 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--string.c25
-rw-r--r--test/ruby/test_string.rb9
3 files changed, 32 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 0c567cbce9..9389de5270 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Aug 23 02:09:57 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * string.c (rb_str_split_m): show $; name in error message when it
+ is a wrong object.
+
Mon Aug 22 16:29:52 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/csv.rb (CSV#shift): store partial quoted strings in an array
diff --git a/string.c b/string.c
index bc504ec3ed..f74cbaafd1 100644
--- a/string.c
+++ b/string.c
@@ -7003,6 +7003,16 @@ rb_str_count(int argc, VALUE *argv, VALUE str)
return INT2NUM(i);
}
+static VALUE
+rb_fs_check(VALUE val)
+{
+ if (!NIL_P(val) && !RB_TYPE_P(val, T_STRING) && !RB_TYPE_P(val, T_REGEXP)) {
+ val = rb_check_string_type(val);
+ if (NIL_P(val)) return 0;
+ }
+ return val;
+}
+
static const char isspacetable[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -7094,11 +7104,17 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
}
enc = STR_ENC_GET(str);
- if (NIL_P(spat) && NIL_P(spat = rb_fs)) {
+ split_type = regexp;
+ if (!NIL_P(spat)) {
+ spat = get_pat_quoted(spat, 0);
+ }
+ else if (NIL_P(spat = rb_fs)) {
split_type = awk;
}
- else {
- spat = get_pat_quoted(spat, 0);
+ else if (!(spat = rb_fs_check(spat))) {
+ rb_raise(rb_eTypeError, "value of $; must be String or Regexp");
+ }
+ if (split_type != awk) {
if (BUILTIN_TYPE(spat) == T_STRING) {
rb_encoding *enc2 = STR_ENC_GET(spat);
@@ -7122,9 +7138,6 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
}
}
}
- else {
- split_type = regexp;
- }
}
result = rb_ary_new();
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 790fa21536..e5aa2251a7 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -1365,7 +1365,7 @@ CODE
end
def test_split
- assert_nil($;)
+ fs, $; = $;, nil
assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split)
assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split(S(" ")))
@@ -1389,6 +1389,13 @@ CODE
assert_equal([], "".split(//, 1))
assert_equal("[2, 3]", [1,2,3].slice!(1,10000).inspect, "moved from btest/knownbug")
+
+ $; = []
+ assert_raise_with_message(TypeError, /\$;/) {
+ "".split
+ }
+ ensure
+ $; = fs
end
def test_split_encoding