More descriptive aliases of rb_str_new[2-5] (Re: [request]C APIの拡張)

[email protected]$G$9!#e(B

At Mon, 21 Jul 2008 16:31:02 +0900,
Nobuyoshi N. wrote in [ruby-dev:35614]:

print()e$B$He(Bprintf()e$B$N0c$$$,e([email protected]$1$J$N$G$=$l$G==J,$+$H;W$C$F$7$^$$e(B
e$B$^$7$?!#L>[email protected]$o$j$O$"$j$^$;$s!#e(BRubye$B$N47=,$K=>e(B
e$B$&$J$i$P!$e(Brb_str_new6()e$B$H$$$&$3$H$K$J$k$G$7$g$&$+!#e(B

e$B$b$&e(Bnew+e$B?t;z$O$d$a$^$7$g$&$h!#$9$G$K$"$k$b$N$O$H$b$+$/!":#$5$iA}e(B
e$B$d$9$3$H$O$J$$$G$7$g$&!#e(B

e$BJLL>$rIU$1$F$_$^$7$?!#e(B

rb_str_new2 -> rb_str_new_cstr
rb_str_new3 -> rb_str_new_shared
rb_str_new4 -> rb_str_new_frozen
rb_str_new5 -> rb_str_new_with_class

e$B$I$s$J$b$s$G$7$g$&$+!#e(B

Index: include/ruby/intern.h

include/ruby/intern.h (revision 18139)
+++ include/ruby/intern.h (working copy)
@@ -527,35 +527,49 @@ VALUE rb_str_format(int, const VALUE ,
/
string.c /
VALUE rb_str_new(const char
, long);
+VALUE rb_str_new_cstr(const char*);
VALUE rb_str_new2(const char*);
+VALUE rb_str_new_shared(VALUE);
VALUE rb_str_new3(VALUE);
+VALUE rb_str_new_frozen(VALUE);
VALUE rb_str_new4(VALUE);
+VALUE rb_str_new_with_class(VALUE, const char*, long);
VALUE rb_str_new5(VALUE, const char*, long);
VALUE rb_tainted_str_new(const char*, long);
+VALUE rb_tainted_str_new_cstr(const char*);
VALUE rb_tainted_str_new2(const char*);
VALUE rb_str_buf_new(long);
+VALUE rb_str_buf_new_cstr(const char*);
VALUE rb_str_buf_new2(const char*);
VALUE rb_str_tmp_new(long);
VALUE rb_usascii_str_new(const char*, long);
+VALUE rb_usascii_str_new_cstr(const char*);
VALUE rb_usascii_str_new2(const char*);
#if defined GNUC
-#define rb_str_new2(str) extension (
+#define rb_str_new_cstr(str) extension (
{ \

  • (__builtin_constant_p(str)) ? \
  • (__builtin_constant_p(str)) ?
    rb_str_new(str, strlen(str)) : \
  • rb_str_new2(str); \
  • rb_str_new_cstr(str);
    })
    -#define rb_tainted_str_new2(str) extension (
    +#define rb_tainted_str_new_cstr(str) extension (
    {
    (__builtin_constant_p(str)) ?
    rb_tainted_str_new(str, strlen(str)) : \
  • rb_tainted_str_new2(str); \
  • rb_tainted_str_new_cstr(str);
    })
    -#define rb_usascii_str_new2(str) extension (
    +#define rb_usascii_str_new_cstr(str) extension (
    {
    (__builtin_constant_p(str)) ?
    rb_usascii_str_new(str, strlen(str)) : \
  • rb_usascii_str_new2(str); \
  • rb_usascii_str_new_cstr(str);
    })
    #endif
    +#define rb_str_new2 rb_str_new_cstr
    +#define rb_str_new3 rb_str_new_shared
    +#define rb_str_new4 rb_str_new_frozen
    +#define rb_str_new5 rb_str_new_with_class
    +#define rb_tainted_str_new2 rb_tainted_str_new_cstr
    +#define rb_str_buf_new2 rb_str_buf_new_cstr
    +#define rb_usascii_str_new2 rb_usascii_str_new_cstr
    void rb_str_free(VALUE);
    void rb_str_shared_replace(VALUE, VALUE);
    @@ -570,4 +584,5 @@ VALUE rb_str_locktmp(VALUE);
    VALUE rb_str_unlocktmp(VALUE);
    VALUE rb_str_dup_frozen(VALUE);
    +#define rb_str_dup_frozen rb_str_new_frozen
    VALUE rb_str_plus(VALUE, VALUE);
    VALUE rb_str_times(VALUE, VALUE);
    Index: string.c
    ===================================================================
    string.c (revision 18139)
    +++ string.c (working copy)
    @@ -26,11 +26,27 @@
    #endif

+#undef rb_str_new_cstr
+#undef rb_tainted_str_new_cstr
+#undef rb_usascii_str_new_cstr
#undef rb_str_new2
+#undef rb_str_new3
+#undef rb_str_new4
+#undef rb_str_new5
#undef rb_tainted_str_new2
+#undef rb_str_buf_new2
#undef rb_usascii_str_new2
+#undef rb_str_dup_frozen

VALUE rb_cString;
VALUE rb_cSymbol;

+#ifdef GNUC
+#define alias_func(old_prot, new_name, args)
+VALUE old_prot attribute((alias(#new_name)));
+#else
+#define alias_func(old_prot, new_name, args)
+VALUE old_prot {return new_name args;}
+#endif
+
#define STR_TMPLOCK FL_USER7
#define STR_NOEMBED FL_USER1
@@ -400,5 +416,5 @@ rb_enc_str_new(const char *ptr, long len

VALUE
-rb_str_new2(const char *ptr)
+rb_str_new_cstr(const char *ptr)
{
if (!ptr) {
@@ -408,6 +424,9 @@ rb_str_new2(const char *ptr)
}

+alias_func(rb_str_new2(const char *ptr), rb_str_new_cstr, (ptr))
+#define rb_str_new2 rb_str_new_cstr
+
VALUE
-rb_usascii_str_new2(const char *ptr)
+rb_usascii_str_new_cstr(const char *ptr)
{
VALUE str = rb_str_new2(ptr);
@@ -416,4 +435,7 @@ rb_usascii_str_new2(const char *ptr)
}

+alias_func(rb_usascii_str_new2(const char *ptr),
rb_usascii_str_new_cstr, (ptr))
+#define rb_usascii_str_new2 rb_usascii_str_new_cstr
+
VALUE
rb_tainted_str_new(const char *ptr, long len)
@@ -426,5 +448,5 @@ rb_tainted_str_new(const char *ptr, long

VALUE
-rb_tainted_str_new2(const char *ptr)
+rb_tainted_str_new_cstr(const char *ptr)
{
VALUE str = rb_str_new2(ptr);
@@ -434,4 +456,7 @@ rb_tainted_str_new2(const char *ptr)
}

+alias_func(rb_tainted_str_new2(const char *ptr),
rb_tainted_str_new_cstr, (ptr))
+#define rb_tainted_str_new2 rb_tainted_str_new_cstr
+
static VALUE
str_replace_shared(VALUE str2, VALUE str)
@@ -467,5 +492,5 @@ str_new3(VALUE klass, VALUE str)

VALUE
-rb_str_new3(VALUE str)
+rb_str_new_shared(VALUE str)
{
VALUE str2 = str_new3(rb_obj_class(str), str);
@@ -475,4 +500,7 @@ rb_str_new3(VALUE str)
}

+alias_func(rb_str_new3(VALUE str), rb_str_new_shared, (str))
+#define rb_str_new3 rb_str_new_shared
+
static VALUE
str_new4(VALUE klass, VALUE str)
@@ -498,5 +526,5 @@ str_new4(VALUE klass, VALUE str)

VALUE
-rb_str_new4(VALUE orig)
+rb_str_new_frozen(VALUE orig)
{
VALUE klass, str;
@@ -535,10 +563,17 @@ rb_str_new4(VALUE orig)
}

+alias_func(rb_str_new4(VALUE orig), rb_str_new_frozen, (orig))
+#define rb_str_new4 rb_str_new_frozen
+
VALUE
-rb_str_new5(VALUE obj, const char *ptr, long len)
+rb_str_new_with_class(VALUE obj, const char *ptr, long len)
{
return str_new(rb_obj_class(obj), ptr, len);
}

+alias_func(rb_str_new5(VALUE obj, const char *ptr, long len),

  • rb_str_new_with_class, (obj, ptr, len))
    

+#define rb_str_new5 rb_str_new_with_class
+
#define STR_BUF_MIN_SIZE 128

@@ -560,5 +595,5 @@ rb_str_buf_new(long capa)

VALUE
-rb_str_buf_new2(const char *ptr)
+rb_str_buf_new_cstr(const char *ptr)
{
VALUE str;
@@ -571,4 +606,7 @@ rb_str_buf_new2(const char *ptr)
}

+alias_func(rb_str_buf_new2(const char *ptr), rb_str_buf_new_cstr,
(ptr))
+#define rb_str_buf_new2 rb_str_buf_new_cstr
+
VALUE
rb_str_tmp_new(long len)
@@ -1311,19 +1349,6 @@ rb_str_freeze(VALUE str)
}

-VALUE
-rb_str_dup_frozen(VALUE str)
-{

  • if (STR_SHARED_P(str) && RSTRING(str)->as.heap.aux.shared) {
  • VALUE shared = RSTRING(str)->as.heap.aux.shared;
  • if (RSTRING_LEN(shared) == RSTRING_LEN(str)) {
  •  OBJ_FREEZE(shared);
    
  •  return shared;
    
  • }
  • }
  • if (OBJ_FROZEN(str)) return str;
  • str = rb_str_dup(str);
  • OBJ_FREEZE(str);
  • return str;
    -}
    +alias_func(rb_str_dup_frozen(VALUE str), rb_str_new_frozen, (str))
    +#define rb_str_dup_frozen rb_str_new_frozen

VALUE

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs