[前][次][番号順一覧][スレッド一覧]

ruby-changes:32493

From: nobu <ko1@a...>
Date: Sun, 12 Jan 2014 17:11:53 +0900 (JST)
Subject: [ruby-changes:32493] nobu:r44572 (trunk): ext: use rb_sprintf() and rb_vsprintf() with PRIsVALUE

nobu	2014-01-12 17:11:36 +0900 (Sun, 12 Jan 2014)

  New Revision: 44572

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44572

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

  Modified files:
    trunk/ext/dl/cptr.c
    trunk/ext/fiddle/pointer.c
    trunk/ext/openssl/ossl.c
    trunk/ext/pty/pty.c
    trunk/ext/strscan/strscan.c
    trunk/ext/tk/tcltklib.c
    trunk/ext/win32ole/win32ole.c
    trunk/ext/zlib/zlib.c
Index: ext/dl/cptr.c
===================================================================
--- ext/dl/cptr.c	(revision 44571)
+++ ext/dl/cptr.c	(revision 44572)
@@ -391,9 +391,8 @@ rb_dlptr_inspect(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/dl/cptr.c#L391
     char str[1024];
 
     TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
-    snprintf(str, 1023, "#<%s:%p ptr=%p size=%ld free=%p>",
-	     rb_class2name(CLASS_OF(self)), data, data->ptr, data->size, data->free);
-    return rb_str_new2(str);
+    return rb_sprintf("#<%"PRIsVALUE":%p ptr=%p size=%ld free=%p>",
+		      CLASS_OF(self), data, data->ptr, data->size, data->free);
 }
 
 /*
Index: ext/zlib/zlib.c
===================================================================
--- ext/zlib/zlib.c	(revision 44571)
+++ ext/zlib/zlib.c	(revision 44572)
@@ -337,11 +337,8 @@ raise_zlib_error(int err, const char *ms https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L337
 	rb_sys_fail(msg);
 	/* no return */
       default:
-      {
-	  char buf[BUFSIZ];
-	  snprintf(buf, BUFSIZ, "unknown zlib error %d: %s", err, msg);
-	  exc = rb_exc_new2(cZError, buf);
-      }
+	exc = rb_exc_new_str(cZError,
+			     rb_sprintf("unknown zlib error %d: %s", err, msg));
     }
 
     rb_exc_raise(exc);
Index: ext/win32ole/win32ole.c
===================================================================
--- ext/win32ole/win32ole.c	(revision 44571)
+++ ext/win32ole/win32ole.c	(revision 44572)
@@ -1209,19 +1209,18 @@ static void https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L1209
 ole_raise(HRESULT hr, VALUE ecs, const char *fmt, ...)
 {
     va_list args;
-    char buf[BUFSIZ];
+    VALUE msg;
     VALUE err_msg;
     va_init_list(args, fmt);
-    vsnprintf(buf, BUFSIZ, fmt, args);
+    msg = rb_vsprintf(fmt, args);
     va_end(args);
 
     err_msg = ole_hresult2msg(hr);
     if(err_msg != Qnil) {
-        rb_raise(ecs, "%s\n%s", buf, StringValuePtr(err_msg));
-    }
-    else {
-        rb_raise(ecs, "%s", buf);
+	rb_str_cat2(msg, "\n");
+	rb_str_append(msg, err_msg);
     }
+    rb_exc_raise(rb_exc_new_str(ecs, msg));
 }
 
 void
Index: ext/pty/pty.c
===================================================================
--- ext/pty/pty.c	(revision 44571)
+++ ext/pty/pty.c	(revision 44572)
@@ -613,7 +613,7 @@ static void https://github.com/ruby/ruby/blob/trunk/ext/pty/pty.c#L613
 raise_from_check(rb_pid_t pid, int status)
 {
     const char *state;
-    char buf[1024];
+    VALUE msg;
     VALUE exc;
 
 #if defined(WIFSTOPPED)
@@ -631,8 +631,8 @@ raise_from_check(rb_pid_t pid, int statu https://github.com/ruby/ruby/blob/trunk/ext/pty/pty.c#L631
     else {
 	state = "exited";
     }
-    snprintf(buf, sizeof(buf), "pty - %s: %ld", state, (long)pid);
-    exc = rb_exc_new2(eChildExited, buf);
+    msg = rb_sprintf("pty - %s: %ld", state, (long)pid);
+    exc = rb_exc_new_str(eChildExited, msg);
     rb_iv_set(exc, "status", rb_last_status_get());
     rb_exc_raise(exc);
 }
Index: ext/tk/tcltklib.c
===================================================================
--- ext/tk/tcltklib.c	(revision 44571)
+++ ext/tk/tcltklib.c	(revision 44572)
@@ -848,15 +848,14 @@ create_ip_exc(interp, exc, fmt, va_alist https://github.com/ruby/ruby/blob/trunk/ext/tk/tcltklib.c#L848
 #endif
 {
     va_list args;
-    char buf[BUFSIZ];
+    VALUE msg;
     VALUE einfo;
     struct tcltkip *ptr = get_ip(interp);
 
     va_init_list(args,fmt);
-    vsnprintf(buf, BUFSIZ, fmt, args);
-    buf[BUFSIZ - 1] = '\0';
+    msg = rb_vsprintf(fmt, 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);
     if (ptr) {
         Tcl_ResetResult(ptr->ip);
Index: ext/openssl/ossl.c
===================================================================
--- ext/openssl/ossl.c	(revision 44571)
+++ ext/openssl/ossl.c	(revision 44572)
@@ -294,10 +294,9 @@ ossl_to_der_if_possible(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl.c#L294
 static VALUE
 ossl_make_error(VALUE exc, const char *fmt, va_list args)
 {
-    char buf[BUFSIZ];
+    VALUE str = Qnil;
     const char *msg;
     long e;
-    int len = 0;
 
 #ifdef HAVE_ERR_PEEK_LAST_ERROR
     e = ERR_peek_last_error();
@@ -305,14 +304,19 @@ ossl_make_error(VALUE exc, const char *f https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl.c#L304
     e = ERR_peek_error();
 #endif
     if (fmt) {
-	len = vsnprintf(buf, BUFSIZ, fmt, args);
+	str = rb_vsprintf(fmt, args);
     }
-    if (len < BUFSIZ && e) {
+    if (e) {
 	if (dOSSL == Qtrue) /* FULL INFO */
 	    msg = ERR_error_string(e, NULL);
 	else
 	    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 */
 	while ((e = ERR_get_error()) != 0){
@@ -321,8 +325,8 @@ ossl_make_error(VALUE exc, const char *f https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl.c#L325
     }
     ERR_clear_error();
 
-    if(len > BUFSIZ) len = rb_long2int(strlen(buf));
-    return rb_exc_new(exc, buf, len);
+    if (NIL_P(str)) str = rb_str_new(0, 0);
+    return rb_exc_new3(exc, str);
 }
 
 void
Index: ext/fiddle/pointer.c
===================================================================
--- ext/fiddle/pointer.c	(revision 44571)
+++ ext/fiddle/pointer.c	(revision 44572)
@@ -427,12 +427,10 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/fiddle/pointer.c#L427
 rb_fiddle_ptr_inspect(VALUE self)
 {
     struct ptr_data *data;
-    char str[1024];
 
     TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
-    snprintf(str, 1023, "#<%s:%p ptr=%p size=%ld free=%p>",
-	     rb_class2name(CLASS_OF(self)), data, data->ptr, data->size, data->free);
-    return rb_str_new2(str);
+    return rb_sprintf("#<%"PRIsVALUE":%p ptr=%p size=%ld free=%p>",
+		      CLASS_OF(self), data, data->ptr, data->size, data->free);
 }
 
 /*
Index: ext/strscan/strscan.c
===================================================================
--- ext/strscan/strscan.c	(revision 44571)
+++ ext/strscan/strscan.c	(revision 44572)
@@ -1155,37 +1155,32 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L1155
 strscan_inspect(VALUE self)
 {
     struct strscanner *p;
-    char buf[BUFSIZE];
-    long len;
     VALUE a, b;
 
     p = check_strscan(self);
     if (NIL_P(p->str)) {
-        len = snprintf(buf, BUFSIZE, "#<%s (uninitialized)>",
-                       rb_class2name(CLASS_OF(self)));
-        return infect(rb_str_new(buf, len), p);
+	a = rb_sprintf("#<%"PRIsVALUE" (uninitialized)>", CLASS_OF(self));
+	return infect(a, p);
     }
     if (EOS_P(p)) {
-        len = snprintf(buf, BUFSIZE, "#<%s fin>",
-                       rb_class2name(CLASS_OF(self)));
-        return infect(rb_str_new(buf, len), p);
+	a = rb_sprintf("#<%"PRIsVALUE" fin>", CLASS_OF(self));
+	return infect(a, p);
     }
     if (p->curr == 0) {
-        b = inspect2(p);
-        len = snprintf(buf, BUFSIZE, "#<%s %ld/%ld @ %s>",
-                       rb_class2name(CLASS_OF(self)),
-                       p->curr, S_LEN(p),
-                       RSTRING_PTR(b));
-        return infect(rb_str_new(buf, len), p);
+	b = inspect2(p);
+	a = rb_sprintf("#<%"PRIsVALUE" %ld/%ld @ %"PRIsVALUE">",
+		       CLASS_OF(self),
+		       p->curr, S_LEN(p),
+		       b);
+	return infect(a, p);
     }
     a = inspect1(p);
     b = inspect2(p);
-    len = snprintf(buf, BUFSIZE, "#<%s %ld/%ld %s @ %s>",
-                   rb_class2name(CLASS_OF(self)),
-                   p->curr, S_LEN(p),
-                   RSTRING_PTR(a),
-                   RSTRING_PTR(b));
-    return infect(rb_str_new(buf, len), p);
+    a = rb_sprintf("#<%"PRIsVALUE" %ld/%ld %"PRIsVALUE" @ %"PRIsVALUE">",
+		   CLASS_OF(self),
+		   p->curr, S_LEN(p),
+		   a, b);
+    return infect(a, p);
 }
 
 static VALUE
@@ -1210,21 +1205,19 @@ inspect1(struct strscanner *p) https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L1205
 static VALUE
 inspect2(struct strscanner *p)
 {
-    char buf[BUFSIZE];
-    char *bp = buf;
+    VALUE str;
     long len;
 
     if (EOS_P(p)) return rb_str_new2("");
     len = S_LEN(p) - p->curr;
     if (len > INSPECT_LENGTH) {
-        len = INSPECT_LENGTH;
-        memcpy(bp, CURPTR(p), len); bp += len;
-        strcpy(bp, "..."); bp += 3;
+	str = rb_str_new(CURPTR(p), INSPECT_LENGTH);
+	rb_str_cat2(str, "...");
     }
     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);
 }
 
 /* =======================================================================

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]