summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-09-20 16:46:21 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-09-20 16:46:21 +0000
commitb21b9a46cfadc785f0001b2b34edc0392d79161b (patch)
treef153f287b62d6e9aa5e915a4fb5e0931de8c975a /string.c
parentb1ce6fe804638ec34c5078ee152f84b59c366bee (diff)
* string.c (str_alloc): defaults to null_str instead of NULL.
[ruby-dev:31774] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@13473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c46
1 files changed, 35 insertions, 11 deletions
diff --git a/string.c b/string.c
index cc27550ebe..108a2af128 100644
--- a/string.c
+++ b/string.c
@@ -59,13 +59,17 @@ str_frozen_check(s)
}
}
+static VALUE str_alloc0 _((VALUE, int));
static VALUE str_alloc _((VALUE));
+static VALUE str_alloc1 _((VALUE));
+
static VALUE
-str_alloc(klass)
+str_alloc0(klass, flags)
VALUE klass;
+ int flags;
{
NEWOBJ(str, struct RString);
- OBJSETUP(str, klass, T_STRING);
+ OBJSETUP(str, klass, flags);
str->ptr = 0;
str->len = 0;
@@ -74,6 +78,25 @@ str_alloc(klass)
return (VALUE)str;
}
+static const char null_str[] = "";
+#define null_str (char *)null_str
+
+static VALUE
+str_alloc(klass)
+ VALUE klass;
+{
+ VALUE str = str_alloc0(klass, T_STRING | ELTS_SHARED);
+ RSTRING(str)->ptr = null_str;
+ return str;
+}
+
+static VALUE
+str_alloc1(klass)
+ VALUE klass;
+{
+ return str_alloc0(klass, T_STRING);
+}
+
static VALUE
str_new(klass, ptr, len)
VALUE klass;
@@ -86,7 +109,7 @@ str_new(klass, ptr, len)
rb_raise(rb_eArgError, "negative string size (or size too big)");
}
- str = str_alloc(klass);
+ str = str_alloc1(klass);
RSTRING(str)->len = len;
RSTRING(str)->aux.capa = len;
RSTRING(str)->ptr = ALLOC_N(char,len+1);
@@ -145,7 +168,6 @@ str_new3(klass, str)
RSTRING(str2)->len = RSTRING(str)->len;
RSTRING(str2)->ptr = RSTRING(str)->ptr;
RSTRING(str2)->aux.shared = str;
- FL_SET(str2, ELTS_SHARED);
return str2;
}
@@ -164,7 +186,7 @@ static VALUE
str_new4(klass, str)
VALUE klass, str;
{
- VALUE str2 = str_alloc(klass);
+ VALUE str2 = str_alloc1(klass);
RSTRING(str2)->len = RSTRING(str)->len;
RSTRING(str2)->ptr = RSTRING(str)->ptr;
@@ -223,7 +245,7 @@ VALUE
rb_str_buf_new(capa)
long capa;
{
- VALUE str = str_alloc(rb_cString);
+ VALUE str = str_alloc1(rb_cString);
if (capa < STR_BUF_MIN_SIZE) {
capa = STR_BUF_MIN_SIZE;
@@ -257,13 +279,14 @@ rb_str_to_str(str)
return rb_convert_type(str, T_STRING, "String", "to_str");
}
+static int str_independent _((VALUE));
+
static void
rb_str_shared_replace(str, str2)
VALUE str, str2;
{
if (str == str2) return;
- rb_str_modify(str);
- if (!FL_TEST(str, ELTS_SHARED)) free(RSTRING(str)->ptr);
+ if (str_independent(str)) xfree(RSTRING(str)->ptr);
if (NIL_P(str2)) {
RSTRING(str)->ptr = 0;
RSTRING(str)->len = 0;
@@ -538,8 +561,6 @@ rb_str_associated(str)
return Qfalse;
}
-static char *null_str = "";
-
VALUE
rb_string_value(ptr)
volatile VALUE *ptr;
@@ -2265,8 +2286,11 @@ rb_str_replace(str, str2)
FL_UNSET(str, STR_ASSOC);
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
}
+ else if (!RSTRING(str2)->len) {
+ FL_SET(str, ELTS_SHARED);
+ RSTRING(str)->ptr = null_str;
+ }
else {
- rb_str_modify(str);
rb_str_resize(str, RSTRING(str2)->len);
memcpy(RSTRING(str)->ptr, RSTRING(str2)->ptr, RSTRING(str2)->len);
if (FL_TEST(str2, STR_ASSOC)) {