Skip to content

Commit

Permalink
Merge pull request #181 from brianmario/typed-data
Browse files Browse the repository at this point in the history
Change data wrapping to use TypedData
  • Loading branch information
tenderlove authored Jul 10, 2024
2 parents dcbf192 + 30026d9 commit f9a7985
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
8 changes: 4 additions & 4 deletions ext/charlock_holmes/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -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);
Expand Down
49 changes: 28 additions & 21 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 @@ -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();
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 All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ext/charlock_holmes/ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion ext/charlock_holmes/transliterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f9a7985

Please sign in to comment.