Skip to content

Commit

Permalink
Change data wrapping to use TypedData
Browse files Browse the repository at this point in the history
The old Data_* API is deprecated, so this changes us to use the
TypedData APIs. Also cleans up some compiler warnings
  • Loading branch information
tenderlove committed Jul 10, 2024
1 parent dcbf192 commit 38a1db8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ext/charlock_holmes/converter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
45 changes: 26 additions & 19 deletions ext/charlock_holmes/encoding_detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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));
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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;

Expand Down Expand Up @@ -334,26 +353,14 @@ 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;
UErrorCode status = U_ZERO_ERROR;
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)) {
Expand Down

0 comments on commit 38a1db8

Please sign in to comment.