Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement rb_enc_nth

  • Loading branch information...
commit 7569ff387d1f6373f4ff469ff52faecd70be2fad 1 parent 64e395f
Dirkjan Bussink dbussink authored
Showing with 13 additions and 0 deletions.
  1. +1 −0  vm/capi/19/include/ruby/encoding.h
  2. +12 −0 vm/capi/encoding.cpp
1  vm/capi/19/include/ruby/encoding.h
View
@@ -90,6 +90,7 @@ VALUE rb_enc_from_encoding(rb_encoding *enc);
int rb_define_dummy_encoding(const char *);
int rb_enc_find_index(const char *name);
int rb_enc_to_index(rb_encoding* enc);
+char* rb_enc_nth(const char *p, const char *e, long nth, rb_encoding *enc);
int rb_enc_dummy_p(rb_encoding *enc);
VALUE rb_enc_associate(VALUE, rb_encoding*);
12 vm/capi/encoding.cpp
View
@@ -308,6 +308,18 @@ extern "C" {
return n;
}
+ char* rb_enc_nth(const char* p, const char* e, long nth, rb_encoding* enc) {
+ if (rb_enc_mbmaxlen(enc) == 1) {
+ p += nth;
+ } else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
+ p += nth * rb_enc_mbmaxlen(enc);
+ } else {
+ p += Encoding::find_character_byte_index((uint8_t*)p, (uint8_t*)e, nth, enc);
+ }
+ if (p > e) p = e;
+ return (char*)p;
+ }
+
#define ctype_test(c, ctype) (rb_isascii(c) && ONIGENC_IS_ASCII_CODE_CTYPE((c), ctype))
int rb_isalnum(int c) {
Please sign in to comment.
Something went wrong with that request. Please try again.