diff options
Diffstat (limited to 'encoding.c')
-rw-r--r-- | encoding.c | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/encoding.c b/encoding.c index 93c3eca48c..ff62a3f083 100644 --- a/encoding.c +++ b/encoding.c @@ -43,8 +43,8 @@ enc_new(rb_encoding *encoding) return enc; } -static VALUE -enc_from_encoding(rb_encoding *enc) +VALUE +rb_enc_from_encoding(rb_encoding *enc) { return enc_initialized_p(enc) ? ENC_FROM_ENCODING(enc) : enc_new(enc); } @@ -384,7 +384,7 @@ rb_obj_encoding(VALUE obj) if (!enc) { rb_raise(rb_eTypeError, "unknown encoding"); } - return enc_from_encoding(enc); + return rb_enc_from_encoding(enc); } @@ -482,7 +482,7 @@ enc_list(VALUE klass) for (i = 0; i < enc_table_size; ++i) { rb_encoding *enc = enc_table[i].enc; if (enc) { - rb_ary_push(ary, enc_from_encoding(enc)); + rb_ary_push(ary, rb_enc_from_encoding(enc)); } } return ary; @@ -495,7 +495,7 @@ enc_find(VALUE klass, VALUE enc) if (idx < 0) { rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc)); } - return enc_from_encoding(rb_enc_from_index(idx)); + return rb_enc_from_encoding(rb_enc_from_index(idx)); } /* :nodoc: */ @@ -513,6 +513,34 @@ enc_load(VALUE klass, VALUE str) return enc_find(klass, str); } +static VALUE rb_primary_encoding; + +VALUE +rb_get_primary_encoding(void) +{ + return rb_primary_encoding; +} + +static VALUE +get_primary_encoding(VALUE klass) +{ + return rb_get_primary_encoding(); +} + +void +rb_set_primary_encoding(VALUE encoding) +{ + rb_to_encoding(encoding); + rb_primary_encoding = encoding; +} + +static VALUE +set_primary_encoding(VALUE klass, VALUE enc) +{ + rb_set_primary_encoding(enc); + return rb_primary_encoding; +} + void Init_Encoding(void) { @@ -526,4 +554,8 @@ Init_Encoding(void) rb_define_method(rb_cEncoding, "_dump", enc_dump, -1); rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1); + + rb_primary_encoding = rb_enc_from_encoding(rb_enc_from_index(0)); + rb_define_singleton_method(rb_cEncoding, "primary_encoding", get_primary_encoding, 0); + rb_define_singleton_method(rb_cEncoding, "primary_encoding=", set_primary_encoding, 1); } |