diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/ext/-test-/string/coderange.c |
Fresh start
Diffstat (limited to 'jni/ruby/ext/-test-/string/coderange.c')
-rw-r--r-- | jni/ruby/ext/-test-/string/coderange.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/jni/ruby/ext/-test-/string/coderange.c b/jni/ruby/ext/-test-/string/coderange.c new file mode 100644 index 0000000..b93172d --- /dev/null +++ b/jni/ruby/ext/-test-/string/coderange.c @@ -0,0 +1,47 @@ +#include "ruby/ruby.h" +#include "ruby/encoding.h" + +static VALUE sym_7bit, sym_valid, sym_unknown, sym_broken; + +static VALUE +coderange_int2sym(int coderange) +{ + switch (coderange) { + case ENC_CODERANGE_7BIT: + return sym_7bit; + case ENC_CODERANGE_VALID: + return sym_valid; + case ENC_CODERANGE_UNKNOWN: + return sym_unknown; + case ENC_CODERANGE_BROKEN: + return sym_broken; + } + rb_bug("wrong condition of coderange"); + UNREACHABLE; +} + +/* return coderange without scan */ +static VALUE +str_coderange(VALUE str) +{ + return coderange_int2sym(ENC_CODERANGE(str)); +} + +/* scan coderange and return the result */ +static VALUE +str_coderange_scan(VALUE str) +{ + ENC_CODERANGE_SET(str, ENC_CODERANGE_UNKNOWN); + return coderange_int2sym(rb_enc_str_coderange(str)); +} + +void +Init_coderange(VALUE klass) +{ + sym_7bit = ID2SYM(rb_intern("7bit")); + sym_valid = ID2SYM(rb_intern("valid")); + sym_unknown = ID2SYM(rb_intern("unknown")); + sym_broken = ID2SYM(rb_intern("broken")); + rb_define_method(klass, "coderange", str_coderange, 0); + rb_define_method(klass, "coderange_scan", str_coderange_scan, 0); +} |