summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-07-13 04:38:01 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-07-13 04:38:01 +0000
commit35c6e6af5ba2c405a91dc6b9e4af833d0979ed22 (patch)
tree7f54dda0bcda0a58eb5229def03ce3015c98a2b5
parent71d408a6c3298b229c0f1d2ff7159ca410a4c612 (diff)
str2cstr
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@266 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog4
-rw-r--r--array.c36
-rw-r--r--dir.c3
-rw-r--r--error.c3
-rw-r--r--eval.c4
-rw-r--r--ext/curses/curses.c12
-rw-r--r--file.c5
-rw-r--r--hash.c11
-rw-r--r--io.c19
-rw-r--r--marshal.c22
-rw-r--r--object.c12
-rw-r--r--process.c6
-rw-r--r--re.c1
-rw-r--r--ruby.h10
-rw-r--r--string.c10
15 files changed, 75 insertions, 83 deletions
diff --git a/ChangeLog b/ChangeLog
index 47718c1..c189489 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Mon Jul 13 12:39:38 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
+
+ * object.c (str2cstr): should not return NULL.
+
Thu Jul 9 17:38:23 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
* experimental release 1.1b9_30.
diff --git a/array.c b/array.c
index c0900a3..6bee8ae 100644
--- a/array.c
+++ b/array.c
@@ -163,20 +163,21 @@ ary_s_new(argc, argv, klass)
NEWOBJ(ary, struct RArray);
OBJSETUP(ary, klass, T_ARRAY);
- rb_scan_args(argc, argv, "01", &size);
ary->len = 0;
ary->ptr = 0;
- if (NIL_P(size)) {
+ if (rb_scan_args(argc, argv, "01", &size) == 0) {
ary->capa = ARY_DEFAULT_SIZE;
}
else {
- ary->capa = NUM2INT(size);
- if (ary->capa < 0) {
+ int capa = NUM2INT(size);
+
+ if (capa < 0) {
ArgError("negative array size");
}
- if (ary->capa*sizeof(VALUE) < 0) {
+ if (capa*sizeof(VALUE) < 0) {
ArgError("array size too big");
}
+ ary->capa = capa;
}
ary->ptr = ALLOC_N(VALUE, ary->capa);
memclear(ary->ptr, ary->capa);
@@ -410,15 +411,13 @@ ary_aref(argc, argv, ary)
if (FIXNUM_P(arg1)) {
return ary_entry(ary, FIX2INT(arg1));
}
- else {
- /* check if idx is Range */
- if (beg_len(arg1, &beg, &len, RARRAY(ary)->len)) {
- return ary_subseq(ary, beg, len);
- }
- }
- if (TYPE(arg1) == T_BIGNUM) {
+ else if (TYPE(arg1) == T_BIGNUM) {
IndexError("index too big");
}
+ else if (beg_len(arg1, &beg, &len, RARRAY(ary)->len)) {
+ /* check if idx is Range */
+ return ary_subseq(ary, beg, len);
+ }
return ary_entry(ary, NUM2INT(arg1));
}
@@ -917,8 +916,8 @@ ary_fill(argc, argv, ary)
int beg, len, end;
VALUE *p, *pend;
- rb_scan_args(argc, argv, "12", &item, &arg1, &arg2);
- if (NIL_P(arg2) && beg_len(arg1, &beg, &len, RARRAY(ary)->len)) {
+ if (rb_scan_args(argc, argv, "12", &item, &arg1, &arg2) == 2 &&
+ beg_len(arg1, &beg, &len, RARRAY(ary)->len)) {
/* beg and len set already */
}
else {
@@ -927,12 +926,7 @@ ary_fill(argc, argv, ary)
beg = RARRAY(ary)->len + beg;
if (beg < 0) beg = 0;
}
- if (!NIL_P(arg2)) {
- len = NUM2INT(arg2);
- }
- else {
- len = RARRAY(ary)->len - beg;
- }
+ len = NIL_P(arg2)?RARRAY(ary)->len - beg:NUM2INT(arg2);
}
end = beg + len;
if (end > RARRAY(ary)->len) {
@@ -941,7 +935,7 @@ ary_fill(argc, argv, ary)
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
}
if (beg > RARRAY(ary)->len) {
- memclear(RARRAY(ary)->ptr+RARRAY(ary)->len, end-RARRAY(ary)->len);
+ memclear(RARRAY(ary)->ptr+RARRAY(ary)->len,end-RARRAY(ary)->len);
}
RARRAY(ary)->len = end;
}
diff --git a/dir.c b/dir.c
index a6c96ce..b92a5f6 100644
--- a/dir.c
+++ b/dir.c
@@ -207,8 +207,7 @@ dir_s_chdir(argc, argv, obj)
char *dist = "";
rb_secure(2);
- rb_scan_args(argc, argv, "01", &path);
- if (!NIL_P(path)) {
+ if (rb_scan_args(argc, argv, "01", &path) == 1) {
Check_SafeStr(path);
dist = RSTRING(path)->ptr;
}
diff --git a/error.c b/error.c
index b8bf252..c316e88 100644
--- a/error.c
+++ b/error.c
@@ -274,8 +274,7 @@ exc_initialize(argc, argv, exc)
{
VALUE mesg;
- rb_scan_args(argc, argv, "01", &mesg);
- if (NIL_P(mesg)) {
+ if (rb_scan_args(argc, argv, "01", &mesg) == 0) {
mesg = str_new(0, 0);
}
else {
diff --git a/eval.c b/eval.c
index 2a174a1..2ed291d 100644
--- a/eval.c
+++ b/eval.c
@@ -3958,11 +3958,11 @@ f_eval(argc, argv, self)
int line = 0;
rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
- if (!NIL_P(vfile)) {
+ if (argc >= 3) {
Check_Type(vfile, T_STRING);
file = RSTRING(vfile)->ptr;
}
- if (!NIL_P(vline)) {
+ if (argc >= 4) {
line = NUM2INT(vline);
}
diff --git a/ext/curses/curses.c b/ext/curses/curses.c
index a2fbe18..69b5636 100644
--- a/ext/curses/curses.c
+++ b/ext/curses/curses.c
@@ -333,10 +333,8 @@ curses_addstr(obj, str)
VALUE obj;
VALUE str;
{
- char *s = STR2CSTR(str);
-
- if (s) {
- addstr(s);
+ if (!NIL_P(str)) {
+ addstr(STR2CSTR(str));
}
return Qnil;
}
@@ -685,13 +683,11 @@ window_addstr(obj, str)
VALUE obj;
VALUE str;
{
- char *s = STR2CSTR(str);
-
- if (s) {
+ if (!NIL_P(str)) {
struct windata *winp;
GetWINDOW(obj, winp);
- waddstr(winp->window, s);
+ waddstr(winp->window, STR2CSTR(str));
}
return Qnil;
}
diff --git a/file.c b/file.c
index 68d3a3c..9232578 100644
--- a/file.c
+++ b/file.c
@@ -1250,9 +1250,10 @@ file_s_basename(argc, argv)
char *name, *p, *ext;
int f;
- rb_scan_args(argc, argv, "11", &fname, &fext);
+ if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) {
+ ext = STR2CSTR(fext);
+ }
name = STR2CSTR(fname);
- if (!NIL_P(fext)) ext = STR2CSTR(fext);
p = strrchr(name, '/');
if (!p) {
if (!NIL_P(fext)) {
diff --git a/hash.c b/hash.c
index 2c4616f..811452b 100644
--- a/hash.c
+++ b/hash.c
@@ -198,8 +198,9 @@ hash_s_new(argc, argv, klass)
hash->status = 0;
hash->tbl = 0; /* avoid GC crashing */
- rb_scan_args(argc, argv, "01", &sz);
- if (NIL_P(sz)) size = 0;
+ if (rb_scan_args(argc, argv, "01", &sz) == 0) {
+ size = 0;
+ }
else size = NUM2INT(sz);
hash->tbl = st_init_table_with_size(&objhash, size);
@@ -848,9 +849,9 @@ f_getenv(obj, name)
int len;
nam = str2cstr(name, &len);
- if (strlen(nam) != len)
- ArgError("Bad environment name");
-
+ if (strlen(nam) != len) {
+ ArgError("Bad environment variable name");
+ }
env = getenv(nam);
if (env) {
if (strcmp(nam, "PATH") == 0 && !env_path_tainted())
diff --git a/io.c b/io.c
index 826c0f0..16e539f 100644
--- a/io.c
+++ b/io.c
@@ -1245,9 +1245,7 @@ io_s_popen(argc, argv, self)
char *mode;
VALUE pname, pmode;
- rb_scan_args(argc, argv, "11", &pname, &pmode);
- Check_SafeStr(pname);
- if (NIL_P(pmode)) {
+ if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
mode = "r";
}
else {
@@ -1257,7 +1255,8 @@ io_s_popen(argc, argv, self)
len = strlen(mode);
if (len == 0 || len > 3)
ArgError("illegal access mode");
- }
+ }
+ Check_SafeStr(pname);
return pipe_open(RSTRING(pname)->ptr, mode);
}
@@ -1634,8 +1633,7 @@ obj_display(argc, argv, self)
{
VALUE out;
- rb_scan_args(argc, argv, "01", &out);
- if (NIL_P(out)) {
+ if (rb_scan_args(argc, argv, "01", &out) == 0) {
out = rb_defout;
}
@@ -1710,9 +1708,7 @@ io_s_new(argc, argv, klass)
VALUE fnum, mode;
char *m = "r";
- rb_scan_args(argc, argv, "11", &fnum, &mode);
-
- if (!NIL_P(mode)) {
+ if (rb_scan_args(argc, argv, "11", &fnum, &mode) == 2) {
Check_SafeStr(mode);
m = RSTRING(mode)->ptr;
}
@@ -1996,8 +1992,7 @@ f_select(argc, argv, obj)
int interrupt = 0;
int pending = 0;
- rb_scan_args(argc, argv, "13", &read, &write, &except, &timeout);
- if (NIL_P(timeout)) {
+ if (rb_scan_args(argc, argv, "13", &read, &write, &except, &timeout)<4) {
tp = NULL;
}
else {
@@ -2576,7 +2571,7 @@ static void
opt_i_set(val)
VALUE val;
{
- if (NIL_P(val)) {
+ if (!RTEST(val)) {
inplace = 0;
return;
}
diff --git a/marshal.c b/marshal.c
index 4234c98..095db75 100644
--- a/marshal.c
+++ b/marshal.c
@@ -812,21 +812,21 @@ marshal_load(argc, argv)
struct load_arg arg;
rb_scan_args(argc, argv, "11", &port, &proc);
- if (TYPE(port) == T_STRING) {
+ if (obj_is_kind_of(port, cIO)) {
+ io_binmode(port);
+ GetOpenFile(port, fptr);
+ io_readable(fptr);
+ arg.fp = fptr->f;
+ }
+ else if (rb_respond_to(port, rb_intern("to_str"))) {
+ int len;
+
arg.fp = 0;
- arg.ptr = RSTRING(port)->ptr;
+ arg.ptr = str2cstr(port, &len);
arg.end = arg.ptr + RSTRING(port)->len;
}
else {
- if (obj_is_kind_of(port, cIO)) {
- io_binmode(port);
- GetOpenFile(port, fptr);
- io_readable(fptr);
- arg.fp = fptr->f;
- }
- else {
- TypeError("instance of IO needed");
- }
+ TypeError("instance of IO needed");
}
major = r_byte(&arg);
diff --git a/object.c b/object.c
index 6bfa103..3dc1243 100644
--- a/object.c
+++ b/object.c
@@ -547,8 +547,9 @@ class_s_new(argc, argv)
{
VALUE super, klass;
- rb_scan_args(argc, argv, "01", &super);
- if (NIL_P(super)) super = cObject;
+ if (rb_scan_args(argc, argv, "01", &super) == 0) {
+ super = cObject;
+ }
Check_Type(super, T_CLASS);
if (FL_TEST(super, FL_SINGLETON)) {
TypeError("can't make subclass of virtual class");
@@ -770,7 +771,11 @@ fail_to_type(arg)
struct arg_to *arg;
{
TypeError("failed to convert %s into %s",
- rb_class2name(CLASS_OF(arg->val)), arg->s);
+ NIL_P(arg->val) ? "nil" :
+ arg->val == TRUE ? "true" :
+ arg->val == FALSE ? "false" :
+ rb_class2name(CLASS_OF(arg->val)),
+ arg->s);
}
VALUE
@@ -838,7 +843,6 @@ str2cstr(str, len)
VALUE str;
int *len;
{
- if (NIL_P(str)) return NULL;
if (TYPE(str) != T_STRING) {
str = str_to_str(str);
}
diff --git a/process.c b/process.c
index faa5e9d..9d01abe 100644
--- a/process.c
+++ b/process.c
@@ -787,7 +787,7 @@ proc_getpgrp(argc, argv)
int pid;
rb_scan_args(argc, argv, "01", &vpid);
- pid = NUM2INT(vpid);
+ pid = NIL_P(vpid)?0:NUM2INT(vpid);
pgrp = BSD_GETPGRP(pid);
#else
rb_scan_args(argc, argv, "0");
@@ -809,8 +809,8 @@ proc_setpgrp(argc, argv)
rb_scan_args(argc, argv, "02", &pid, &pgrp);
- ipid = NUM2INT(pid);
- ipgrp = NUM2INT(pgrp);
+ ipid = NIL_P(pid)?0:NUM2INT(pid);
+ ipgrp = NIL_P(pgrp)?0:NUM2INT(pgrp);
if (BSD_SETPGRP(ipid, ipgrp) < 0) rb_sys_fail(0);
#else
rb_scan_args(argc, argv, "0");
diff --git a/re.c b/re.c
index d9962cb..d9d89a5 100644
--- a/re.c
+++ b/re.c
@@ -768,7 +768,6 @@ reg_s_new(argc, argv, self)
if (argc == 3) {
char *kcode = STR2CSTR(argv[2]);
- if (!kcode) TypeError("wrong char-code");
switch (kcode[0]) {
case 'n': case 'N':
flag |= 4;
diff --git a/ruby.h b/ruby.h
index 439bb1a..0cfd8f6 100644
--- a/ruby.h
+++ b/ruby.h
@@ -246,21 +246,21 @@ struct RFloat {
struct RString {
struct RBasic basic;
- unsigned int len;
+ int len;
char *ptr;
VALUE orig;
};
struct RArray {
struct RBasic basic;
- unsigned int len, capa;
+ int len, capa;
VALUE *ptr;
};
struct RRegexp {
struct RBasic basic;
struct re_pattern_buffer *ptr;
- unsigned int len;
+ int len;
char *str;
};
@@ -305,14 +305,14 @@ VALUE data_object_alloc _((VALUE,void*,void (*)(),void (*)()));
struct RStruct {
struct RBasic basic;
- unsigned int len;
+ int len;
VALUE *ptr;
};
struct RBignum {
struct RBasic basic;
char sign;
- unsigned int len;
+ int len;
unsigned short *digits;
};
diff --git a/string.c b/string.c
index 013238d..16bf6fd 100644
--- a/string.c
+++ b/string.c
@@ -2005,15 +2005,14 @@ str_split_method(argc, argv, str)
int beg, end, lim, i;
VALUE result, tmp;
- rb_scan_args(argc, argv, "02", &spat, &limit);
- if (!NIL_P(limit)) {
+ if (rb_scan_args(argc, argv, "02", &spat, &limit) == 2) {
lim = NUM2INT(limit);
if (lim == 0) limit = Qnil;
else if (lim == 1) return ary_new3(1, str);
i = 1;
}
- if (NIL_P(spat)) {
+ if (argc == 0) {
if (!NIL_P(FS)) {
spat = FS;
goto fs_set;
@@ -2508,8 +2507,9 @@ str_sum(argc, argv, str)
int bits;
char *p, *pend;
- rb_scan_args(argc, argv, "01", &vbits);
- if (NIL_P(vbits)) bits = 16;
+ if (rb_scan_args(argc, argv, "01", &vbits) == 0) {
+ bits = 16;
+ }
else bits = NUM2INT(vbits);
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;