Permalink
Browse files

Add spec for rb_enc_nth

  • Loading branch information...
1 parent dc177cb commit 64e395ff522b82092a436a4bfb8b2f1ca15dd0fb @dbussink dbussink committed Sep 28, 2012
@@ -350,5 +350,12 @@
@s.rb_enc_to_index("FTU-81").should == 0
end
end
+
+ describe "rb_enc_nth" do
+ it "returns the byte index of the given character index" do
+ @s.rb_enc_nth("hüllo", 3).should == 4
+ end
+ end
+
end
end
@@ -213,6 +213,15 @@ static VALUE encoding_spec_rb_to_encoding_index(VALUE self, VALUE obj) {
}
#endif
+#ifdef HAVE_RB_ENC_NTH
+static VALUE encoding_spec_rb_enc_nth(VALUE self, VALUE str, VALUE index) {
+ char* start = RSTRING_PTR(str);
+ char* end = start + RSTRING_LEN(str);
+ char* ptr = rb_enc_nth(start, end, FIX2LONG(index), rb_enc_get(str));
+ return LONG2NUM(ptr - start);
+}
+#endif
+
void Init_encoding_spec() {
VALUE cls;
cls = rb_define_class("CApiEncodingSpecs", rb_cObject);
@@ -341,6 +350,10 @@ void Init_encoding_spec() {
#ifdef HAVE_RB_TO_ENCODING_INDEX
rb_define_method(cls, "rb_to_encoding_index", encoding_spec_rb_to_encoding_index, 1);
#endif
+
+#ifdef HAVE_RB_ENC_NTH
+ rb_define_method(cls, "rb_enc_nth", encoding_spec_rb_enc_nth, 2);
+#endif
}
#ifdef __cplusplus
@@ -210,6 +210,7 @@
#define HAVE_RB_TO_ENCODING 1
#define HAVE_RB_TO_ENCODING_INDEX 1
+#define HAVE_RB_ENC_NTH 1
#define HAVE_RB_EENCCOMPATERROR 1

0 comments on commit 64e395f

Please sign in to comment.