@@ -45,11 +45,10 @@ static VALUE encode_utf8_string(const char *c_string) {
45
45
return string ;
46
46
}
47
47
48
- /* Encode a C string using the encoding from Ruby string +source+. */
49
- static VALUE encode_source_string (const char * c_string , VALUE source ) {
50
- VALUE string = rb_str_new2 (c_string );
51
- rb_enc_copy (string , source );
52
- return string ;
48
+ static VALUE commonmarker_cstr_adopt (const char * str , rb_encoding * enc ) {
49
+ VALUE ret = rb_enc_str_new_cstr (str , enc );
50
+ cmark_get_default_mem_allocator ()-> free (str );
51
+ return ret ;
53
52
}
54
53
55
54
static void rb_mark_c_struct (void * data ) {
@@ -175,7 +174,7 @@ static VALUE rb_markdown_to_html(VALUE self, VALUE rb_text, VALUE rb_options, VA
175
174
cmark_parser_free (parser );
176
175
cmark_node_free (doc );
177
176
178
- return rb_utf8_str_new_cstr (html );
177
+ return commonmarker_cstr_adopt (html , rb_utf8_encoding () );
179
178
}
180
179
181
180
/*
@@ -204,7 +203,7 @@ static VALUE rb_markdown_to_xml(VALUE self, VALUE rb_text, VALUE rb_options, VAL
204
203
cmark_parser_free (parser );
205
204
cmark_node_free (doc );
206
205
207
- return rb_utf8_str_new_cstr (xml );
206
+ return commonmarker_cstr_adopt (xml , rb_utf8_encoding () );
208
207
}
209
208
210
209
/*
@@ -1178,7 +1177,7 @@ static VALUE rb_html_escape_href(VALUE self, VALUE rb_text) {
1178
1177
if (houdini_escape_href (& buf , (const uint8_t * )RSTRING_PTR (rb_text ),
1179
1178
RSTRING_LEN (rb_text ))) {
1180
1179
result = (char * )cmark_strbuf_detach (& buf );
1181
- return encode_source_string (result , rb_text );
1180
+ return commonmarker_cstr_adopt (result , rb_enc_get ( rb_text ) );
1182
1181
1183
1182
}
1184
1183
@@ -1199,7 +1198,7 @@ static VALUE rb_html_escape_html(VALUE self, VALUE rb_text) {
1199
1198
if (houdini_escape_html0 (& buf , (const uint8_t * )RSTRING_PTR (rb_text ),
1200
1199
RSTRING_LEN (rb_text ), 0 )) {
1201
1200
result = (char * )cmark_strbuf_detach (& buf );
1202
- return encode_source_string (result , rb_text );
1201
+ return commonmarker_cstr_adopt (result , rb_enc_get ( rb_text ) );
1203
1202
}
1204
1203
1205
1204
return rb_text ;
0 commit comments