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

ext: use rb_sprintf() and rb_vsprintf() with PRIsVALUE

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44572 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-01-12 08:11:36 +00:00
parent 4240685237
commit 9bd672f668
8 changed files with 48 additions and 59 deletions

View file

@ -391,9 +391,8 @@ rb_dlptr_inspect(VALUE self)
char str[1024]; char str[1024];
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data); TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
snprintf(str, 1023, "#<%s:%p ptr=%p size=%ld free=%p>", return rb_sprintf("#<%"PRIsVALUE":%p ptr=%p size=%ld free=%p>",
rb_class2name(CLASS_OF(self)), data, data->ptr, data->size, data->free); CLASS_OF(self), data, data->ptr, data->size, data->free);
return rb_str_new2(str);
} }
/* /*

View file

@ -427,12 +427,10 @@ static VALUE
rb_fiddle_ptr_inspect(VALUE self) rb_fiddle_ptr_inspect(VALUE self)
{ {
struct ptr_data *data; struct ptr_data *data;
char str[1024];
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data); TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
snprintf(str, 1023, "#<%s:%p ptr=%p size=%ld free=%p>", return rb_sprintf("#<%"PRIsVALUE":%p ptr=%p size=%ld free=%p>",
rb_class2name(CLASS_OF(self)), data, data->ptr, data->size, data->free); CLASS_OF(self), data, data->ptr, data->size, data->free);
return rb_str_new2(str);
} }
/* /*

View file

@ -294,10 +294,9 @@ ossl_to_der_if_possible(VALUE obj)
static VALUE static VALUE
ossl_make_error(VALUE exc, const char *fmt, va_list args) ossl_make_error(VALUE exc, const char *fmt, va_list args)
{ {
char buf[BUFSIZ]; VALUE str = Qnil;
const char *msg; const char *msg;
long e; long e;
int len = 0;
#ifdef HAVE_ERR_PEEK_LAST_ERROR #ifdef HAVE_ERR_PEEK_LAST_ERROR
e = ERR_peek_last_error(); e = ERR_peek_last_error();
@ -305,14 +304,19 @@ ossl_make_error(VALUE exc, const char *fmt, va_list args)
e = ERR_peek_error(); e = ERR_peek_error();
#endif #endif
if (fmt) { if (fmt) {
len = vsnprintf(buf, BUFSIZ, fmt, args); str = rb_vsprintf(fmt, args);
} }
if (len < BUFSIZ && e) { if (e) {
if (dOSSL == Qtrue) /* FULL INFO */ if (dOSSL == Qtrue) /* FULL INFO */
msg = ERR_error_string(e, NULL); msg = ERR_error_string(e, NULL);
else else
msg = ERR_reason_error_string(e); msg = ERR_reason_error_string(e);
len += snprintf(buf+len, BUFSIZ-len, "%s%s", (len ? ": " : ""), msg); if (NIL_P(str)) {
str = rb_str_new_cstr(msg);
}
else {
rb_str_cat2(rb_str_cat2(str, ": "), msg);
}
} }
if (dOSSL == Qtrue){ /* show all errors on the stack */ if (dOSSL == Qtrue){ /* show all errors on the stack */
while ((e = ERR_get_error()) != 0){ while ((e = ERR_get_error()) != 0){
@ -321,8 +325,8 @@ ossl_make_error(VALUE exc, const char *fmt, va_list args)
} }
ERR_clear_error(); ERR_clear_error();
if(len > BUFSIZ) len = rb_long2int(strlen(buf)); if (NIL_P(str)) str = rb_str_new(0, 0);
return rb_exc_new(exc, buf, len); return rb_exc_new3(exc, str);
} }
void void

View file

@ -613,7 +613,7 @@ static void
raise_from_check(rb_pid_t pid, int status) raise_from_check(rb_pid_t pid, int status)
{ {
const char *state; const char *state;
char buf[1024]; VALUE msg;
VALUE exc; VALUE exc;
#if defined(WIFSTOPPED) #if defined(WIFSTOPPED)
@ -631,8 +631,8 @@ raise_from_check(rb_pid_t pid, int status)
else { else {
state = "exited"; state = "exited";
} }
snprintf(buf, sizeof(buf), "pty - %s: %ld", state, (long)pid); msg = rb_sprintf("pty - %s: %ld", state, (long)pid);
exc = rb_exc_new2(eChildExited, buf); exc = rb_exc_new_str(eChildExited, msg);
rb_iv_set(exc, "status", rb_last_status_get()); rb_iv_set(exc, "status", rb_last_status_get());
rb_exc_raise(exc); rb_exc_raise(exc);
} }

View file

@ -1155,37 +1155,32 @@ static VALUE
strscan_inspect(VALUE self) strscan_inspect(VALUE self)
{ {
struct strscanner *p; struct strscanner *p;
char buf[BUFSIZE];
long len;
VALUE a, b; VALUE a, b;
p = check_strscan(self); p = check_strscan(self);
if (NIL_P(p->str)) { if (NIL_P(p->str)) {
len = snprintf(buf, BUFSIZE, "#<%s (uninitialized)>", a = rb_sprintf("#<%"PRIsVALUE" (uninitialized)>", CLASS_OF(self));
rb_class2name(CLASS_OF(self))); return infect(a, p);
return infect(rb_str_new(buf, len), p);
} }
if (EOS_P(p)) { if (EOS_P(p)) {
len = snprintf(buf, BUFSIZE, "#<%s fin>", a = rb_sprintf("#<%"PRIsVALUE" fin>", CLASS_OF(self));
rb_class2name(CLASS_OF(self))); return infect(a, p);
return infect(rb_str_new(buf, len), p);
} }
if (p->curr == 0) { if (p->curr == 0) {
b = inspect2(p); b = inspect2(p);
len = snprintf(buf, BUFSIZE, "#<%s %ld/%ld @ %s>", a = rb_sprintf("#<%"PRIsVALUE" %ld/%ld @ %"PRIsVALUE">",
rb_class2name(CLASS_OF(self)), CLASS_OF(self),
p->curr, S_LEN(p), p->curr, S_LEN(p),
RSTRING_PTR(b)); b);
return infect(rb_str_new(buf, len), p); return infect(a, p);
} }
a = inspect1(p); a = inspect1(p);
b = inspect2(p); b = inspect2(p);
len = snprintf(buf, BUFSIZE, "#<%s %ld/%ld %s @ %s>", a = rb_sprintf("#<%"PRIsVALUE" %ld/%ld %"PRIsVALUE" @ %"PRIsVALUE">",
rb_class2name(CLASS_OF(self)), CLASS_OF(self),
p->curr, S_LEN(p), p->curr, S_LEN(p),
RSTRING_PTR(a), a, b);
RSTRING_PTR(b)); return infect(a, p);
return infect(rb_str_new(buf, len), p);
} }
static VALUE static VALUE
@ -1210,21 +1205,19 @@ inspect1(struct strscanner *p)
static VALUE static VALUE
inspect2(struct strscanner *p) inspect2(struct strscanner *p)
{ {
char buf[BUFSIZE]; VALUE str;
char *bp = buf;
long len; long len;
if (EOS_P(p)) return rb_str_new2(""); if (EOS_P(p)) return rb_str_new2("");
len = S_LEN(p) - p->curr; len = S_LEN(p) - p->curr;
if (len > INSPECT_LENGTH) { if (len > INSPECT_LENGTH) {
len = INSPECT_LENGTH; str = rb_str_new(CURPTR(p), INSPECT_LENGTH);
memcpy(bp, CURPTR(p), len); bp += len; rb_str_cat2(str, "...");
strcpy(bp, "..."); bp += 3;
} }
else { else {
memcpy(bp, CURPTR(p), len); bp += len; str = rb_str_new(CURPTR(p), len);
} }
return rb_str_dump(rb_str_new(buf, bp - buf)); return rb_str_dump(str);
} }
/* ======================================================================= /* =======================================================================

View file

@ -848,15 +848,14 @@ create_ip_exc(interp, exc, fmt, va_alist)
#endif #endif
{ {
va_list args; va_list args;
char buf[BUFSIZ]; VALUE msg;
VALUE einfo; VALUE einfo;
struct tcltkip *ptr = get_ip(interp); struct tcltkip *ptr = get_ip(interp);
va_init_list(args,fmt); va_init_list(args,fmt);
vsnprintf(buf, BUFSIZ, fmt, args); msg = rb_vsprintf(fmt, args);
buf[BUFSIZ - 1] = '\0';
va_end(args); va_end(args);
einfo = rb_exc_new2(exc, buf); einfo = rb_exc_new_str(exc, msg);
rb_ivar_set(einfo, ID_at_interp, interp); rb_ivar_set(einfo, ID_at_interp, interp);
if (ptr) { if (ptr) {
Tcl_ResetResult(ptr->ip); Tcl_ResetResult(ptr->ip);

View file

@ -1209,19 +1209,18 @@ static void
ole_raise(HRESULT hr, VALUE ecs, const char *fmt, ...) ole_raise(HRESULT hr, VALUE ecs, const char *fmt, ...)
{ {
va_list args; va_list args;
char buf[BUFSIZ]; VALUE msg;
VALUE err_msg; VALUE err_msg;
va_init_list(args, fmt); va_init_list(args, fmt);
vsnprintf(buf, BUFSIZ, fmt, args); msg = rb_vsprintf(fmt, args);
va_end(args); va_end(args);
err_msg = ole_hresult2msg(hr); err_msg = ole_hresult2msg(hr);
if(err_msg != Qnil) { if(err_msg != Qnil) {
rb_raise(ecs, "%s\n%s", buf, StringValuePtr(err_msg)); rb_str_cat2(msg, "\n");
} rb_str_append(msg, err_msg);
else {
rb_raise(ecs, "%s", buf);
} }
rb_exc_raise(rb_exc_new_str(ecs, msg));
} }
void void

View file

@ -337,11 +337,8 @@ raise_zlib_error(int err, const char *msg)
rb_sys_fail(msg); rb_sys_fail(msg);
/* no return */ /* no return */
default: default:
{ exc = rb_exc_new_str(cZError,
char buf[BUFSIZ]; rb_sprintf("unknown zlib error %d: %s", err, msg));
snprintf(buf, BUFSIZ, "unknown zlib error %d: %s", err, msg);
exc = rb_exc_new2(cZError, buf);
}
} }
rb_exc_raise(exc); rb_exc_raise(exc);