diff --git a/ChangeLog b/ChangeLog index b8dffe35a0..cd49e10372 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Sep 3 22:03:49 2013 Nobuyoshi Nakada + + * string.c (rb_enc_str_new_cstr): new function to create a string from + the C-string pointer with the specified encoding. + Tue Sep 3 21:41:37 2013 Akira Matsuda * eval.c (Init_eval): Make Module#include and Module#prepend public diff --git a/README.EXT b/README.EXT index dc3136c414..09354a149b 100644 --- a/README.EXT +++ b/README.EXT @@ -218,6 +218,7 @@ rb_str_vcatf(VALUE str, const char* format, va_list ap) :: rb_str_cat2(str, rb_vsprintf(format, ap)), respectively. rb_enc_str_new(const char *ptr, long len, rb_encoding *enc) :: +rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc) :: Creates a new Ruby string with the specified encoding. diff --git a/README.EXT.ja b/README.EXT.ja index cd7fda5a4f..d4d1e61980 100644 --- a/README.EXT.ja +++ b/README.EXT.ja @@ -245,6 +245,7 @@ rb_str_vcatf(VALUE str, const char* format, va_list ap) rb_str_cat2(str, rb_vsprintf(format, ap)) と同等である. rb_enc_str_new(const char *ptr, long len, rb_encoding *enc) +rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc) 指定されたエンコーディングでRubyの文字列を生成する. diff --git a/include/ruby/encoding.h b/include/ruby/encoding.h index ebd9cfd262..e0dc66dec7 100644 --- a/include/ruby/encoding.h +++ b/include/ruby/encoding.h @@ -89,6 +89,7 @@ VALUE rb_enc_associate(VALUE, rb_encoding*); void rb_enc_copy(VALUE dst, VALUE src); VALUE rb_enc_str_new(const char*, long, rb_encoding*); +VALUE rb_enc_str_new_cstr(const char*, rb_encoding*); VALUE rb_enc_reg_new(const char*, long, rb_encoding*, int); PRINTF_ARGS(VALUE rb_enc_sprintf(rb_encoding *, const char*, ...), 2, 3); VALUE rb_enc_vsprintf(rb_encoding *, const char*, va_list); @@ -103,6 +104,15 @@ VALUE rb_str_export_to_enc(VALUE, rb_encoding *); VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to); VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts); +#if defined(__GNUC__) && !defined(__PCC__) +#define rb_enc_str_new_cstr(str, enc) __extension__ ( \ +{ \ + (__builtin_constant_p(str)) ? \ + rb_enc_str_new((str), (long)strlen(str), (enc)) : \ + rb_enc_str_new_cstr((str), (enc)); \ +}) +#endif + PRINTF_ARGS(NORETURN(void rb_enc_raise(rb_encoding *, VALUE, const char*, ...)), 3, 4); /* index -> rb_encoding */ diff --git a/string.c b/string.c index 7f8707ae5c..70cedf204b 100644 --- a/string.c +++ b/string.c @@ -34,6 +34,7 @@ #undef rb_str_new_cstr #undef rb_tainted_str_new_cstr #undef rb_usascii_str_new_cstr +#undef rb_enc_str_new_cstr #undef rb_external_str_new_cstr #undef rb_locale_str_new_cstr #undef rb_str_dup_frozen @@ -473,6 +474,18 @@ rb_usascii_str_new_cstr(const char *ptr) return str; } +VALUE +rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc) +{ + if (!ptr) { + rb_raise(rb_eArgError, "NULL pointer given"); + } + if (rb_enc_mbminlen(enc) != 1) { + rb_raise(rb_eArgError, "wchar encoding given"); + } + return rb_enc_str_new(ptr, strlen(ptr), enc); +} + VALUE rb_tainted_str_new(const char *ptr, long len) {