From 30026d99bf33dbb8723b0b4da2659166cf1b522f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 10 Jul 2024 10:44:45 -0700 Subject: [PATCH] Change data wrapping to use TypedData The old Data_* API is deprecated, so this changes us to use the TypedData APIs. Also cleans up some compiler warnings --- ext/charlock_holmes/common.h | 8 ++-- ext/charlock_holmes/converter.c | 4 +- ext/charlock_holmes/encoding_detector.c | 49 ++++++++++++++----------- ext/charlock_holmes/ext.c | 2 +- ext/charlock_holmes/transliterator.cpp | 2 +- 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/ext/charlock_holmes/common.h b/ext/charlock_holmes/common.h index ca091b1..2b211c8 100644 --- a/ext/charlock_holmes/common.h +++ b/ext/charlock_holmes/common.h @@ -44,10 +44,10 @@ extern "C" { #endif -extern void Init_charlock_holmes(); -extern void _init_charlock_encoding_detector(); -extern void _init_charlock_converter(); -extern void _init_charlock_transliterator(); +extern void Init_charlock_holmes(void); +extern void _init_charlock_encoding_detector(void); +extern void _init_charlock_converter(void); +extern void _init_charlock_transliterator(void); #ifdef __cplusplus } diff --git a/ext/charlock_holmes/converter.c b/ext/charlock_holmes/converter.c index 0542ef9..5127cfd 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); @@ -50,7 +50,7 @@ static VALUE rb_converter_convert(VALUE self, VALUE rb_txt, VALUE rb_src_enc, VA return rb_out; } -void _init_charlock_converter() { +void _init_charlock_converter(void) { rb_cConverter = rb_define_class_under(rb_mCharlockHolmes, "Converter", rb_cObject); rb_define_singleton_method(rb_cConverter, "convert", rb_converter_convert, 3); diff --git a/ext/charlock_holmes/encoding_detector.c b/ext/charlock_holmes/encoding_detector.c index 5be2465..580d6e0 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; @@ -47,7 +66,7 @@ static VALUE rb_encdec_buildmatch(const UCharsetMatch *match) return rb_match; } -static VALUE rb_encdec_binarymatch() { +static VALUE rb_encdec_binarymatch(void) { VALUE rb_match; rb_match = rb_hash_new(); @@ -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)) { @@ -363,7 +370,7 @@ static VALUE rb_encdec__alloc(VALUE klass) return obj; } -void _init_charlock_encoding_detector() +void _init_charlock_encoding_detector(void) { rb_cEncodingDetector = rb_define_class_under(rb_mCharlockHolmes, "EncodingDetector", rb_cObject); rb_define_alloc_func(rb_cEncodingDetector, rb_encdec__alloc); diff --git a/ext/charlock_holmes/ext.c b/ext/charlock_holmes/ext.c index 7c9539a..ee37fd2 100644 --- a/ext/charlock_holmes/ext.c +++ b/ext/charlock_holmes/ext.c @@ -2,7 +2,7 @@ VALUE rb_mCharlockHolmes; -void Init_charlock_holmes() { +void Init_charlock_holmes(void) { rb_mCharlockHolmes = rb_define_module("CharlockHolmes"); _init_charlock_encoding_detector(); diff --git a/ext/charlock_holmes/transliterator.cpp b/ext/charlock_holmes/transliterator.cpp index e79422e..d464ed3 100644 --- a/ext/charlock_holmes/transliterator.cpp +++ b/ext/charlock_holmes/transliterator.cpp @@ -116,7 +116,7 @@ static VALUE rb_transliterator_transliterate(VALUE self, VALUE rb_txt, VALUE rb_ return rb_out; } -void _init_charlock_transliterator() { +void _init_charlock_transliterator(void) { #ifdef HAVE_RUBY_ENCODING_H rb_eEncodingCompatibilityError = rb_const_get(rb_cEncoding, rb_intern("CompatibilityError")); #endif