void Encoding::string_reverse(uint8_t* start, uint8_t* end, OnigEncodingType* enc) { ssize_t len = end - start; if(len <= 0) return; uint8_t stack_buf[STACK_BUF_SZ]; uint8_t* malloc_buf = 0; uint8_t* buf = stack_buf; if(len > STACK_BUF_SZ) { malloc_buf = (uint8_t*)malloc(len); buf = malloc_buf; } uint8_t* p = buf + len; uint8_t* s = start; while(s < end) { int clen = mbclen(s, end, enc); p -= clen; memcpy(p, s, clen); s += clen; } memcpy(start, buf, len); if(malloc_buf) free(malloc_buf); }
native_int Encoding::find_character_byte_index(const uint8_t* start, const uint8_t* end, native_int index, OnigEncodingType* enc) { uint8_t* p = (uint8_t*) start; while(p < end && index--) { p += mbclen(p, end, enc); } if(p > end) p = (uint8_t*) end; return p - start; }
native_int Encoding::find_byte_character_index(const uint8_t* start, const uint8_t* end, native_int index, OnigEncodingType* enc) { uint8_t* p = (uint8_t*) start; native_int char_index = 0; while(p < end && index > 0) { native_int char_len = mbclen(p, end, enc); p += char_len; index -= char_len; char_index++; } return char_index; }