Skip to content

Commit

Permalink
string.c: rb_enc_str_new_cstr
Browse files Browse the repository at this point in the history
* string.c (rb_enc_str_new_cstr): new function to create a string from
  the C-string pointer with the specified encoding.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42811 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Sep 3, 2013
1 parent ad6731b commit 5669902
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Tue Sep 3 22:03:49 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>

* 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 <ronnie@dio.jp>

* eval.c (Init_eval): Make Module#include and Module#prepend public
Expand Down
1 change: 1 addition & 0 deletions README.EXT
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions README.EXT.ja
Original file line number Diff line number Diff line change
Expand Up @@ -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の文字列を生成する.

Expand Down
10 changes: 10 additions & 0 deletions include/ruby/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */
Expand Down
13 changes: 13 additions & 0 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 5669902

Please sign in to comment.