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(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35987 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7057facef3
commit
f64e7c834f
@ -1,3 +1,8 @@
|
|||||||
|
Sat Jun 9 23:36:15 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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@fsij.org>
|
Sat Jun 9 22:27:05 2012 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* process.c (retry_fork): rewrite a complex "for" statement by
|
* process.c (retry_fork): rewrite a complex "for" statement by
|
||||||
|
@ -187,6 +187,7 @@ size_t rb_strftime(char *s, size_t maxsize, const char *format, rb_encoding *enc
|
|||||||
|
|
||||||
/* string.c */
|
/* string.c */
|
||||||
int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p);
|
int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p);
|
||||||
|
int rb_str_symname_p(VALUE);
|
||||||
|
|
||||||
/* struct.c */
|
/* struct.c */
|
||||||
VALUE rb_struct_init_copy(VALUE copy, VALUE s);
|
VALUE rb_struct_init_copy(VALUE copy, VALUE s);
|
||||||
|
34
string.c
34
string.c
@ -7450,6 +7450,25 @@ sym_printable(const char *s, const char *send, rb_encoding *enc)
|
|||||||
return TRUE;
|
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:
|
* call-seq:
|
||||||
* sym.inspect -> string
|
* sym.inspect -> string
|
||||||
@ -7463,20 +7482,13 @@ static VALUE
|
|||||||
sym_inspect(VALUE sym)
|
sym_inspect(VALUE sym)
|
||||||
{
|
{
|
||||||
VALUE str;
|
VALUE str;
|
||||||
ID id = SYM2ID(sym);
|
|
||||||
rb_encoding *enc;
|
|
||||||
const char *ptr;
|
const char *ptr;
|
||||||
long len;
|
long len;
|
||||||
|
ID id = SYM2ID(sym);
|
||||||
char *dest;
|
char *dest;
|
||||||
rb_encoding *resenc = rb_default_internal_encoding();
|
|
||||||
|
|
||||||
if (resenc == NULL) resenc = rb_default_external_encoding();
|
|
||||||
sym = rb_id2str(id);
|
sym = rb_id2str(id);
|
||||||
enc = STR_ENC_GET(sym);
|
if (!rb_str_symname_p(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)) {
|
|
||||||
str = rb_str_inspect(sym);
|
str = rb_str_inspect(sym);
|
||||||
len = RSTRING_LEN(str);
|
len = RSTRING_LEN(str);
|
||||||
rb_str_resize(str, len + 1);
|
rb_str_resize(str, len + 1);
|
||||||
@ -7485,7 +7497,9 @@ sym_inspect(VALUE sym)
|
|||||||
dest[0] = ':';
|
dest[0] = ':';
|
||||||
}
|
}
|
||||||
else {
|
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);
|
str = rb_enc_str_new(0, len + 1, enc);
|
||||||
dest = RSTRING_PTR(str);
|
dest = RSTRING_PTR(str);
|
||||||
dest[0] = ':';
|
dest[0] = ':';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user