* compile.c (insn_data_to_s_detail), file.c (rb_stat_inspect),

iseq.c (ruby_iseq_disasm_insn, ruby_iseq_disasm),
  process.c (pst_message), re.c (match_inspect): use rb_str_catf.

* dir.c (dir_inspect), iseq.c (iseq_inspect, insn_operand_intern): use
  rb_sprintf.

* error.c (rb_name_error, rb_raise, rb_loaderror, rb_fatal): use
  rb_vsprintf.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2008-07-22 08:53:34 +00:00
parent ee560dc68f
commit 0acca9a826
8 changed files with 68 additions and 90 deletions

View File

@ -1,3 +1,15 @@
Tue Jul 22 17:53:32 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* compile.c (insn_data_to_s_detail), file.c (rb_stat_inspect),
iseq.c (ruby_iseq_disasm_insn, ruby_iseq_disasm),
process.c (pst_message), re.c (match_inspect): use rb_str_catf.
* dir.c (dir_inspect), iseq.c (iseq_inspect, insn_operand_intern): use
rb_sprintf.
* error.c (rb_name_error, rb_raise, rb_loaderror, rb_fatal): use
rb_vsprintf.
Tue Jul 22 17:20:25 2008 NAKAMURA Usaku <usa@ruby-lang.org> Tue Jul 22 17:20:25 2008 NAKAMURA Usaku <usa@ruby-lang.org>
* win32/win32.c (init_func): new function to get API's address which * win32/win32.c (init_func): new function to get API's address which

View File

@ -4679,10 +4679,8 @@ insn_data_to_s_detail(INSN *iobj)
switch (type) { switch (type) {
case TS_OFFSET: /* label(destination position) */ case TS_OFFSET: /* label(destination position) */
{ {
char buff[0x100];
LABEL *lobj = (LABEL *)OPERAND_AT(iobj, j); LABEL *lobj = (LABEL *)OPERAND_AT(iobj, j);
snprintf(buff, sizeof(buff), "<L%03d>", lobj->label_no); rb_str_catf(str, "<L%03d>", lobj->label_no);
rb_str_concat(str, rb_str_new2(buff));
break; break;
} }
break; break;

5
dir.c
View File

@ -477,10 +477,7 @@ dir_inspect(VALUE dir)
Data_Get_Struct(dir, struct dir_data, dirp); Data_Get_Struct(dir, struct dir_data, dirp);
if (dirp->path) { if (dirp->path) {
const char *c = rb_obj_classname(dir); const char *c = rb_obj_classname(dir);
int len = strlen(c) + strlen(dirp->path) + 4; return rb_sprintf("#<%s:%s>", c, dirp->path);
VALUE s = rb_str_new(0, len);
snprintf(RSTRING_PTR(s), len+1, "#<%s:%s>", c, dirp->path);
return s;
} }
return rb_funcall(dir, rb_intern("to_s"), 0, 0); return rb_funcall(dir, rb_intern("to_s"), 0, 0);
} }

24
error.c
View File

@ -621,13 +621,11 @@ rb_name_error(ID id, const char *fmt, ...)
{ {
VALUE exc, argv[2]; VALUE exc, argv[2];
va_list args; va_list args;
char buf[BUFSIZ];
va_start(args, fmt); va_start(args, fmt);
vsnprintf(buf, BUFSIZ, fmt, args); argv[0] = rb_vsprintf(fmt, args);
va_end(args); va_end(args);
argv[0] = rb_str_new2(buf);
argv[1] = ID2SYM(id); argv[1] = ID2SYM(id);
exc = rb_class_new_instance(2, argv, rb_eNameError); exc = rb_class_new_instance(2, argv, rb_eNameError);
rb_exc_raise(exc); rb_exc_raise(exc);
@ -1074,24 +1072,24 @@ void
rb_raise(VALUE exc, const char *fmt, ...) rb_raise(VALUE exc, const char *fmt, ...)
{ {
va_list args; va_list args;
char buf[BUFSIZ]; VALUE mesg;
va_start(args,fmt); va_start(args, fmt);
vsnprintf(buf, BUFSIZ, fmt, args); mesg = rb_vsprintf(fmt, args);
va_end(args); va_end(args);
rb_exc_raise(rb_exc_new2(exc, buf)); rb_exc_raise(rb_exc_new3(exc, mesg));
} }
void void
rb_loaderror(const char *fmt, ...) rb_loaderror(const char *fmt, ...)
{ {
va_list args; va_list args;
char buf[BUFSIZ]; VALUE mesg;
va_start(args, fmt); va_start(args, fmt);
vsnprintf(buf, BUFSIZ, fmt, args); mesg = rb_vsprintf(fmt, args);
va_end(args); va_end(args);
rb_exc_raise(rb_exc_new2(rb_eLoadError, buf)); rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg));
} }
void void
@ -1106,13 +1104,13 @@ void
rb_fatal(const char *fmt, ...) rb_fatal(const char *fmt, ...)
{ {
va_list args; va_list args;
char buf[BUFSIZ]; VALUE mesg;
va_start(args, fmt); va_start(args, fmt);
vsnprintf(buf, BUFSIZ, fmt, args); mesg = rb_vsprintf(fmt, args);
va_end(args); va_end(args);
rb_exc_fatal(rb_exc_new2(rb_eFatal, buf)); rb_exc_fatal(rb_exc_new3(rb_eFatal, mesg));
} }
void void

10
file.c
View File

@ -687,16 +687,10 @@ rb_stat_inspect(VALUE self)
rb_str_buf_cat2(str, "="); rb_str_buf_cat2(str, "=");
v = (*member[i].func)(self); v = (*member[i].func)(self);
if (i == 2) { /* mode */ if (i == 2) { /* mode */
char buf[32]; rb_str_catf(str, "0%lo", NUM2ULONG(v));
sprintf(buf, "0%lo", NUM2ULONG(v));
rb_str_buf_cat2(str, buf);
} }
else if (i == 0 || i == 6) { /* dev/rdev */ else if (i == 0 || i == 6) { /* dev/rdev */
char buf[32]; rb_str_catf(str, "0x%lx", NUM2ULONG(v));
sprintf(buf, "0x%lx", NUM2ULONG(v));
rb_str_buf_cat2(str, buf);
} }
else { else {
rb_str_append(str, rb_inspect(v)); rb_str_append(str, rb_inspect(v));

83
iseq.c
View File

@ -545,13 +545,10 @@ iseq_eval(VALUE self)
static VALUE static VALUE
iseq_inspect(VALUE self) iseq_inspect(VALUE self)
{ {
char buff[0x100];
rb_iseq_t *iseq = iseq_check(self); rb_iseq_t *iseq = iseq_check(self);
snprintf(buff, sizeof(buff), "<ISeq:%s@%s>", return rb_sprintf("<ISeq:%s@%s>",
RSTRING_PTR(iseq->name), RSTRING_PTR(iseq->filename)); RSTRING_PTR(iseq->name), RSTRING_PTR(iseq->filename));
return rb_str_new2(buff);
} }
VALUE iseq_data_to_ary(rb_iseq_t *iseq); VALUE iseq_data_to_ary(rb_iseq_t *iseq);
@ -622,17 +619,14 @@ insn_operand_intern(rb_iseq_t *iseq,
const char *types = insn_op_types(insn); const char *types = insn_op_types(insn);
char type = types[op_no]; char type = types[op_no];
VALUE ret; VALUE ret;
char buff[0x100];
switch (type) { switch (type) {
case TS_OFFSET: /* LONG */ case TS_OFFSET: /* LONG */
snprintf(buff, sizeof(buff), "%ld", pos + len + op); ret = rb_sprintf("%ld", pos + len + op);
ret = rb_str_new2(buff);
break; break;
case TS_NUM: /* ULONG */ case TS_NUM: /* ULONG */
snprintf(buff, sizeof(buff), "%lu", op); ret = rb_sprintf("%lu", op);
ret = rb_str_new2(buff);
break; break;
case TS_LINDEX: case TS_LINDEX:
@ -728,23 +722,19 @@ ruby_iseq_disasm_insn(VALUE ret, VALUE *iseq, int pos,
{ {
int insn = iseq[pos]; int insn = iseq[pos];
int len = insn_len(insn); int len = insn_len(insn);
int i, j; int j;
const char *types = insn_op_types(insn); const char *types = insn_op_types(insn);
VALUE str = rb_str_new(0, 0); VALUE str = rb_str_new(0, 0);
char buff[0x100]; const char *insn_name_buff;
char insn_name_buff[0x100];
strcpy(insn_name_buff, insn_name(insn)); insn_name_buff = insn_name(insn);
if (0) { if (1) {
for (i = 0; insn_name_buff[i]; i++) { rb_str_catf(str, "%04d %-16s ", pos, insn_name_buff);
if (insn_name_buff[i] == '_') { }
insn_name_buff[i] = 0; else {
} rb_str_catf(str, "%04d %-16.*s ", pos,
} strcspn(insn_name_buff, "_"), insn_name_buff);
} }
snprintf(buff, sizeof(buff), "%04d %-16s ", pos, insn_name_buff);
rb_str_cat2(str, buff);
for (j = 0; types[j]; j++) { for (j = 0; types[j]; j++) {
const char *types = insn_op_types(insn); const char *types = insn_op_types(insn);
@ -762,17 +752,18 @@ ruby_iseq_disasm_insn(VALUE ret, VALUE *iseq, int pos,
int line_no = find_line_no(iseqdat, pos); int line_no = find_line_no(iseqdat, pos);
int prev = find_prev_line_no(iseqdat, pos); int prev = find_prev_line_no(iseqdat, pos);
if (line_no && line_no != prev) { if (line_no && line_no != prev) {
snprintf(buff, sizeof(buff), "%-70s(%4d)", RSTRING_PTR(str), long slen = RSTRING_LEN(str);
line_no); slen = (slen > 70) ? 0 : (70 - slen);
str = rb_str_new2(buff); str = rb_str_catf(str, "%*s(%4d)", (int)slen, "", line_no);
} }
} }
else { else {
/* for debug */ /* for debug */
struct iseq_insn_info_entry *entry = get_insn_info(iseqdat, pos); struct iseq_insn_info_entry *entry = get_insn_info(iseqdat, pos);
snprintf(buff, sizeof(buff), "%-60s(line: %d, sp: %d)", long slen = RSTRING_LEN(str);
RSTRING_PTR(str), entry->line_no, entry->sp); slen = (slen > 60) ? 0 : (60 - slen);
str = rb_str_new2(buff); str = rb_str_catf(str, "%*s(line: %d, sp: %d)",
(int)slen, "", entry->line_no, entry->sp);
} }
if (ret) { if (ret) {
@ -817,7 +808,6 @@ ruby_iseq_disasm(VALUE self)
unsigned long size; unsigned long size;
int i; int i;
ID *tbl; ID *tbl;
char buff[0x200];
enum {header_minlen = 72}; enum {header_minlen = 72};
rb_secure(1); rb_secure(1);
@ -840,11 +830,10 @@ ruby_iseq_disasm(VALUE self)
} }
for (i = 0; i < iseqdat->catch_table_size; i++) { for (i = 0; i < iseqdat->catch_table_size; i++) {
struct iseq_catch_table_entry *entry = &iseqdat->catch_table[i]; struct iseq_catch_table_entry *entry = &iseqdat->catch_table[i];
sprintf(buff, rb_str_catf(str,
"| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n", "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
catch_type((int)entry->type), (int)entry->start, catch_type((int)entry->type), (int)entry->start,
(int)entry->end, (int)entry->sp, (int)entry->cont); (int)entry->end, (int)entry->sp, (int)entry->cont);
rb_str_cat2(str, buff);
if (entry->iseq) { if (entry->iseq) {
rb_str_concat(str, ruby_iseq_disasm(entry->iseq)); rb_str_concat(str, ruby_iseq_disasm(entry->iseq));
} }
@ -858,14 +847,13 @@ ruby_iseq_disasm(VALUE self)
tbl = iseqdat->local_table; tbl = iseqdat->local_table;
if (tbl) { if (tbl) {
snprintf(buff, sizeof(buff), rb_str_catf(str,
"local table (size: %d, argc: %d " "local table (size: %d, argc: %d "
"[opts: %d, rest: %d, post: %d, block: %d] s%d)\n", "[opts: %d, rest: %d, post: %d, block: %d] s%d)\n",
iseqdat->local_size, iseqdat->argc, iseqdat->local_size, iseqdat->argc,
iseqdat->arg_opts, iseqdat->arg_rest, iseqdat->arg_opts, iseqdat->arg_rest,
iseqdat->arg_post_len, iseqdat->arg_block, iseqdat->arg_post_len, iseqdat->arg_block,
iseqdat->arg_simple); iseqdat->arg_simple);
rb_str_cat2(str, buff);
for (i = 0; i < iseqdat->local_table_size; i++) { for (i = 0; i < iseqdat->local_table_size; i++) {
const char *name = rb_id2name(tbl[i]); const char *name = rb_id2name(tbl[i]);
@ -893,10 +881,7 @@ ruby_iseq_disasm(VALUE self)
snprintf(info, sizeof(info), "%s%s%s%s", name ? name : "?", snprintf(info, sizeof(info), "%s%s%s%s", name ? name : "?",
*argi ? "<" : "", argi, *argi ? ">" : ""); *argi ? "<" : "", argi, *argi ? ">" : "");
snprintf(buff, sizeof(buff), "[%2d] %-11s", rb_str_catf(str, "[%2d] %-11s", iseqdat->local_size - i, info);
iseqdat->local_size - i, info);
rb_str_cat2(str, buff);
} }
rb_str_cat2(str, "\n"); rb_str_cat2(str, "\n");
} }
@ -954,9 +939,9 @@ static VALUE
register_label(struct st_table *table, int idx) register_label(struct st_table *table, int idx)
{ {
VALUE sym; VALUE sym;
char buff[0x20]; char buff[8 + (sizeof(idx) * CHAR_BIT * 32 / 100)];
snprintf(buff, 0x20, "label_%u", idx); snprintf(buff, sizeof(buff), "label_%u", idx);
sym = ID2SYM(rb_intern(buff)); sym = ID2SYM(rb_intern(buff));
st_insert(table, idx, sym); st_insert(table, idx, sym);
return sym; return sym;

View File

@ -278,33 +278,29 @@ static void
pst_message(VALUE str, rb_pid_t pid, int status) pst_message(VALUE str, rb_pid_t pid, int status)
{ {
char buf[256]; char buf[256];
snprintf(buf, sizeof(buf), "pid %ld", (long)pid); rb_str_catf(str, "pid %ld", (long)pid);
rb_str_cat2(str, buf);
if (WIFSTOPPED(status)) { if (WIFSTOPPED(status)) {
int stopsig = WSTOPSIG(status); int stopsig = WSTOPSIG(status);
const char *signame = ruby_signal_name(stopsig); const char *signame = ruby_signal_name(stopsig);
if (signame) { if (signame) {
snprintf(buf, sizeof(buf), " stopped SIG%s (signal %d)", signame, stopsig); rb_str_catf(str, " stopped SIG%s (signal %d)", signame, stopsig);
} }
else { else {
snprintf(buf, sizeof(buf), " stopped signal %d", stopsig); rb_str_catf(str, " stopped signal %d", stopsig);
} }
rb_str_cat2(str, buf);
} }
if (WIFSIGNALED(status)) { if (WIFSIGNALED(status)) {
int termsig = WTERMSIG(status); int termsig = WTERMSIG(status);
const char *signame = ruby_signal_name(termsig); const char *signame = ruby_signal_name(termsig);
if (signame) { if (signame) {
snprintf(buf, sizeof(buf), " SIG%s (signal %d)", signame, termsig); rb_str_catf(str, " SIG%s (signal %d)", signame, termsig);
} }
else { else {
snprintf(buf, sizeof(buf), " signal %d", termsig); rb_str_catf(str, " signal %d", termsig);
} }
rb_str_cat2(str, buf);
} }
if (WIFEXITED(status)) { if (WIFEXITED(status)) {
snprintf(buf, sizeof(buf), " exit %d", WEXITSTATUS(status)); rb_str_catf(str, " exit %d", WEXITSTATUS(status));
rb_str_cat2(str, buf);
} }
#ifdef WCOREDUMP #ifdef WCOREDUMP
if (WCOREDUMP(status)) { if (WCOREDUMP(status)) {

4
re.c
View File

@ -1806,9 +1806,7 @@ match_inspect(VALUE match)
if (names[i].name) if (names[i].name)
rb_str_buf_cat(str, (const char *)names[i].name, names[i].len); rb_str_buf_cat(str, (const char *)names[i].name, names[i].len);
else { else {
char buf[sizeof(i)*3+1]; rb_str_catf(str, "%d", i);
snprintf(buf, sizeof(buf), "%d", i);
rb_str_buf_cat2(str, buf);
} }
rb_str_buf_cat2(str, ":"); rb_str_buf_cat2(str, ":");
} }