1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

pack.c: refine warning

* pack.c (unknown_directive): refine warning message at unknown
  directive in unpack too, and quote unprintable characters.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65748 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-11-16 00:25:54 +00:00
parent 6a6d03791b
commit 027cf467a4
2 changed files with 30 additions and 12 deletions

35
pack.c
View file

@ -127,6 +127,27 @@ str_associated(VALUE str)
return rb_ivar_lookup(str, id_associated, Qfalse);
}
static void
unknown_directive(const char *mode, char type, VALUE fmt)
{
VALUE f;
char unknown[5];
if (ISPRINT(type)) {
unknown[0] = type;
unknown[1] = '\0';
}
else {
snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff);
}
f = rb_str_quote_unprintable(fmt);
if (f != fmt) {
fmt = rb_str_subseq(f, 1, RSTRING_LEN(f) - 2);
}
rb_warning("unknown %s directive '%s' in '%"PRIsVALUE"'",
mode, unknown, fmt);
}
/*
* call-seq:
* arr.pack( aTemplateString ) -> aBinaryString
@ -849,16 +870,7 @@ pack_pack(int argc, VALUE *argv, VALUE ary)
break;
default: {
char unknown[5];
if (ISPRINT(type)) {
unknown[0] = type;
unknown[1] = '\0';
}
else {
snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff);
}
rb_warning("unknown pack directive '%s' in '% "PRIsVALUE"'",
unknown, fmt);
unknown_directive("pack", type, fmt);
break;
}
}
@ -1748,8 +1760,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode)
break;
default:
rb_warning("unknown unpack directive '%c' in '%s'",
type, RSTRING_PTR(fmt));
unknown_directive("unpack", type, fmt);
break;
}
}

View file

@ -802,6 +802,13 @@ EXPECTED
assert_warning(/\A(.* in '\u{3042}'\n)+\z/) {
[].pack("\u{3042}")
}
assert_warning(/\A.* in '.*U'\Z/) {
assert_equal "\000", [0].pack("\0U")
}
assert_warning(/\A.* in '.*U'\Z/) {
"\000".unpack("\0U")
}
end
def test_pack_resize