diff --git a/ext/charlock_holmes/converter.c b/ext/charlock_holmes/converter.c index 0542ef9..abfbfe0 100644 --- a/ext/charlock_holmes/converter.c +++ b/ext/charlock_holmes/converter.c @@ -20,7 +20,7 @@ static VALUE rb_converter_convert(VALUE self, VALUE rb_txt, VALUE rb_src_enc, VA Check_Type(rb_dst_enc, T_STRING); src_txt = RSTRING_PTR(rb_txt); - src_len = RSTRING_LEN(rb_txt); + src_len = (int32_t)RSTRING_LEN(rb_txt); src_enc = RSTRING_PTR(rb_src_enc); dst_enc = RSTRING_PTR(rb_dst_enc); diff --git a/ext/charlock_holmes/encoding_detector.c b/ext/charlock_holmes/encoding_detector.c index 5be2465..fd57c69 100644 --- a/ext/charlock_holmes/encoding_detector.c +++ b/ext/charlock_holmes/encoding_detector.c @@ -8,6 +8,25 @@ typedef struct { UCharsetDetector *csd; } charlock_detector_t; +static void rb_encdec__free(void *obj) +{ + charlock_detector_t *detector; + + detector = (charlock_detector_t *)obj; + + if (detector->csd) + ucsdet_close(detector->csd); + + free(detector); +} + +static const rb_data_type_t charlock_detector_type = { + "Charlock/Detector", + { 0, rb_encdec__free, 0, }, + 0, 0, + RUBY_TYPED_FREE_IMMEDIATELY, +}; + static VALUE rb_encdec_buildmatch(const UCharsetMatch *match) { UErrorCode status = U_ZERO_ERROR; @@ -167,7 +186,7 @@ static VALUE rb_encdec_detect(int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "11", &rb_str, &rb_enc_hint); Check_Type(rb_str, T_STRING); - Data_Get_Struct(self, charlock_detector_t, detector); + TypedData_Get_Struct(self, charlock_detector_t, &charlock_detector_type, detector); // first lets see if this is binary content if (detect_binary_content(self, rb_str)) { @@ -180,7 +199,7 @@ static VALUE rb_encdec_detect(int argc, VALUE *argv, VALUE self) if (!NIL_P(rb_enc_hint)) { Check_Type(rb_enc_hint, T_STRING); - ucsdet_setDeclaredEncoding(detector->csd, RSTRING_PTR(rb_enc_hint), RSTRING_LEN(rb_enc_hint), &status); + ucsdet_setDeclaredEncoding(detector->csd, RSTRING_PTR(rb_enc_hint), (int32_t)RSTRING_LEN(rb_enc_hint), &status); } return rb_encdec_buildmatch(ucsdet_detect(detector->csd, &status)); @@ -215,7 +234,7 @@ static VALUE rb_encdec_detect_all(int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "11", &rb_str, &rb_enc_hint); Check_Type(rb_str, T_STRING); - Data_Get_Struct(self, charlock_detector_t, detector); + TypedData_Get_Struct(self, charlock_detector_t, &charlock_detector_type, detector); rb_ret = rb_ary_new(); @@ -229,7 +248,7 @@ static VALUE rb_encdec_detect_all(int argc, VALUE *argv, VALUE self) if (!NIL_P(rb_enc_hint)) { Check_Type(rb_enc_hint, T_STRING); - ucsdet_setDeclaredEncoding(detector->csd, RSTRING_PTR(rb_enc_hint), RSTRING_LEN(rb_enc_hint), &status); + ucsdet_setDeclaredEncoding(detector->csd, RSTRING_PTR(rb_enc_hint), (int32_t)RSTRING_LEN(rb_enc_hint), &status); } csm = ucsdet_detectAll(detector->csd, &match_count, &status); @@ -257,7 +276,7 @@ static VALUE rb_get_strip_tags(VALUE self) UBool val; VALUE rb_val; - Data_Get_Struct(self, charlock_detector_t, detector); + TypedData_Get_Struct(self, charlock_detector_t, &charlock_detector_type, detector); val = ucsdet_isInputFilterEnabled(detector->csd); @@ -279,7 +298,7 @@ static VALUE rb_set_strip_tags(VALUE self, VALUE rb_val) charlock_detector_t *detector; UBool val; - Data_Get_Struct(self, charlock_detector_t, detector); + TypedData_Get_Struct(self, charlock_detector_t, &charlock_detector_type, detector); val = rb_val == Qtrue ? 1 : 0; @@ -334,18 +353,6 @@ static VALUE rb_get_supported_encodings(VALUE klass) return rb_encoding_list; } -static void rb_encdec__free(void *obj) -{ - charlock_detector_t *detector; - - detector = (charlock_detector_t *)obj; - - if (detector->csd) - ucsdet_close(detector->csd); - - free(detector); -} - static VALUE rb_encdec__alloc(VALUE klass) { charlock_detector_t *detector; @@ -353,7 +360,7 @@ static VALUE rb_encdec__alloc(VALUE klass) VALUE obj; detector = (charlock_detector_t *) calloc(1, sizeof(charlock_detector_t)); - obj = Data_Wrap_Struct(klass, NULL, rb_encdec__free, (void *)detector); + obj = TypedData_Wrap_Struct(klass, &charlock_detector_type, (void *)detector); detector->csd = ucsdet_open(&status); if (U_FAILURE(status)) {