Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the newer TypedData extension API #72

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*.gem
*.rbc
*.so
.bundle
*.bundle
.config
.yardoc
Gemfile.lock
Expand Down
16 changes: 12 additions & 4 deletions ext/unf_ext/unf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
extern "C" {
VALUE unf_allocate(VALUE klass);
VALUE unf_initialize(VALUE self);
void unf_delete(UNF::Normalizer* ptr);
void unf_delete(void* ptr);
VALUE unf_normalize(VALUE self, VALUE source, VALUE normalization_form);

ID FORM_NFD;
Expand All @@ -30,10 +30,17 @@ extern "C" {
FORM_NFKC= rb_intern("nfkc");
}

static const rb_data_type_t unf_normalizer_data_type = {
.wrap_struct_name = "UNF::Normalizer",
.function = {
.dfree = unf_delete
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};

VALUE unf_allocate(VALUE klass) {
UNF::Normalizer* ptr;
VALUE obj = Data_Make_Struct(klass, UNF::Normalizer, NULL, unf_delete, ptr);
VALUE obj = TypedData_Make_Struct(klass, UNF::Normalizer, &unf_normalizer_data_type, ptr);
new ((void*)ptr) UNF::Normalizer;
return obj;
}
Expand All @@ -42,14 +49,15 @@ extern "C" {
return self;
}

void unf_delete(UNF::Normalizer* ptr) {
void unf_delete(void *data) {
UNF::Normalizer* ptr = (UNF::Normalizer*)data;
ptr->~Normalizer();
ruby_xfree(ptr);
}

VALUE unf_normalize(VALUE self, VALUE source, VALUE normalization_form) {
UNF::Normalizer* ptr;
Data_Get_Struct(self, UNF::Normalizer, ptr);
TypedData_Get_Struct(self, UNF::Normalizer, &unf_normalizer_data_type, ptr);

const char* src = StringValueCStr(source);
const char* rlt;
Expand Down