Skip to content

Commit c90d291

Browse files
committed
Implement rb_enc_str_asciionly_p
1 parent b3c9fd0 commit c90d291

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

spec/ruby/optional/capi/encoding_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,14 @@
479479
length.should == 4
480480
end
481481
end
482+
483+
describe "rb_enc_str_asciionly_p" do
484+
it "returns true for an ASCII string" do
485+
@s.rb_enc_str_asciionly_p("hello").should be_true
486+
end
487+
488+
it "returns true for a non-ASCII string" do
489+
@s.rb_enc_str_asciionly_p("hüllo").should be_false
490+
end
491+
end
482492
end

spec/ruby/optional/capi/ext/encoding_spec.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ static VALUE encoding_spec_rb_enc_codepoint_len(VALUE self, VALUE str) {
196196
return rb_ary_new3(2, LONG2NUM(codepoint), LONG2NUM(len));
197197
}
198198

199+
static VALUE encoding_spec_rb_enc_str_asciionly_p(VALUE self, VALUE str) {
200+
if (rb_enc_str_asciionly_p(str)) {
201+
return Qtrue;
202+
} else {
203+
return Qfalse;
204+
}
205+
}
206+
199207
void Init_encoding_spec(void) {
200208
VALUE cls = rb_define_class("CApiEncodingSpecs", rb_cObject);
201209
rb_define_method(cls, "ENC_CODERANGE_ASCIIONLY",
@@ -242,6 +250,7 @@ void Init_encoding_spec(void) {
242250
rb_define_method(cls, "rb_to_encoding_index", encoding_spec_rb_to_encoding_index, 1);
243251
rb_define_method(cls, "rb_enc_nth", encoding_spec_rb_enc_nth, 2);
244252
rb_define_method(cls, "rb_enc_codepoint_len", encoding_spec_rb_enc_codepoint_len, 1);
253+
rb_define_method(cls, "rb_enc_str_asciionly_p", encoding_spec_rb_enc_str_asciionly_p, 1);
245254
}
246255

247256
#ifdef __cplusplus

src/main/c/cext/ruby.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3247,7 +3247,7 @@ long rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding
32473247
}
32483248

32493249
int rb_enc_str_asciionly_p(VALUE str) {
3250-
rb_tr_error("rb_enc_str_asciionly_p not implemented");
3250+
return polyglot_as_boolean(RUBY_INVOKE_NO_WRAP(str, "ascii_only?"));
32513251
}
32523252

32533253
int rb_enc_unicode_p(rb_encoding *enc) {

0 commit comments

Comments
 (0)