ruby-changes:23936
From: nobu <ko1@a...>
Date: Sat, 9 Jun 2012 23:36:35 +0900 (JST)
Subject: [ruby-changes:23936] nobu:r35987 (trunk): string.c: rb_str_symname_p
nobu 2012-06-09 23:36:17 +0900 (Sat, 09 Jun 2012) New Revision: 35987 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35987 Log: string.c: rb_str_symname_p * string.c (rb_str_symname_p): new function that checks if the string is valid as a symbol name. split from sym_inspect(). Modified files: trunk/ChangeLog trunk/internal.h trunk/string.c Index: ChangeLog =================================================================== --- ChangeLog (revision 35986) +++ ChangeLog (revision 35987) @@ -1,3 +1,8 @@ +Sat Jun 9 23:36:15 2012 Nobuyoshi Nakada <nobu@r...> + + * string.c (rb_str_symname_p): new function that checks if the string + is valid as a symbol name. split from sym_inspect(). + Sat Jun 9 22:27:05 2012 Tanaka Akira <akr@f...> * process.c (retry_fork): rewrite a complex "for" statement by Index: string.c =================================================================== --- string.c (revision 35986) +++ string.c (revision 35987) @@ -7450,6 +7450,25 @@ return TRUE; } +int +rb_str_symname_p(VALUE sym) +{ + rb_encoding *enc; + const char *ptr; + long len; + rb_encoding *resenc = rb_default_internal_encoding(); + + if (resenc == NULL) resenc = rb_default_external_encoding(); + enc = STR_ENC_GET(sym); + ptr = RSTRING_PTR(sym); + len = RSTRING_LEN(sym); + if ((resenc != enc && !rb_str_is_ascii_only_p(sym)) || len != (long)strlen(ptr) || + !rb_enc_symname_p(ptr, enc) || !sym_printable(ptr, ptr + len, enc)) { + return FALSE; + } + return TRUE; +} + /* * call-seq: * sym.inspect -> string @@ -7463,20 +7482,13 @@ sym_inspect(VALUE sym) { VALUE str; - ID id = SYM2ID(sym); - rb_encoding *enc; const char *ptr; long len; + ID id = SYM2ID(sym); char *dest; - rb_encoding *resenc = rb_default_internal_encoding(); - if (resenc == NULL) resenc = rb_default_external_encoding(); sym = rb_id2str(id); - enc = STR_ENC_GET(sym); - ptr = RSTRING_PTR(sym); - len = RSTRING_LEN(sym); - if ((resenc != enc && !rb_str_is_ascii_only_p(sym)) || len != (long)strlen(ptr) || - !rb_enc_symname_p(ptr, enc) || !sym_printable(ptr, ptr + len, enc)) { + if (!rb_str_symname_p(sym)) { str = rb_str_inspect(sym); len = RSTRING_LEN(str); rb_str_resize(str, len + 1); @@ -7485,7 +7497,9 @@ dest[0] = ':'; } else { - char *dest; + rb_encoding *enc = STR_ENC_GET(sym); + ptr = RSTRING_PTR(sym); + len = RSTRING_LEN(sym); str = rb_enc_str_new(0, len + 1, enc); dest = RSTRING_PTR(str); dest[0] = ':'; Index: internal.h =================================================================== --- internal.h (revision 35986) +++ internal.h (revision 35987) @@ -187,6 +187,7 @@ /* string.c */ int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p); +int rb_str_symname_p(VALUE); /* struct.c */ VALUE rb_struct_init_copy(VALUE copy, VALUE s); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/