1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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:
nobu 2012-06-09 14:36:17 +00:00
parent 7057facef3
commit f64e7c834f
3 changed files with 30 additions and 10 deletions

View file

@ -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>
* process.c (retry_fork): rewrite a complex "for" statement by

View file

@ -187,6 +187,7 @@ size_t rb_strftime(char *s, size_t maxsize, const char *format, rb_encoding *enc
/* 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);

View file

@ -7450,6 +7450,25 @@ sym_printable(const char *s, const char *send, rb_encoding *enc)
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 @@ static VALUE
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 @@ sym_inspect(VALUE sym)
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] = ':';