static int to_ascii(OnigEncoding enc, UChar *s, UChar *end, UChar buf[], int buf_size, int *is_over) { int len; UChar *p; OnigCodePoint code; if (ONIGENC_MBC_MINLEN(enc) > 1) { p = s; len = 0; while (p < end) { code = ONIGENC_MBC_TO_CODE(enc, p, end); if (code >= 0x80) { if (code > 0xffff && len + 10 <= buf_size) { sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 24)); sprint_byte((char*)(&(buf[len+4])), (unsigned int)(code >> 16)); sprint_byte((char*)(&(buf[len+6])), (unsigned int)(code >> 8)); sprint_byte((char*)(&(buf[len+8])), (unsigned int)code); len += 10; } else if (len + 6 <= buf_size) { sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 8)); sprint_byte((char*)(&(buf[len+4])), (unsigned int)code); len += 6; }
extern int onigenc_str_bytelen_null(OnigEncoding enc, const UChar* s) { UChar* start = (UChar* )s; UChar* p = (UChar* )s; UChar* e; while (1) { if (*p == '\0') { UChar* q; int len = ONIGENC_MBC_MINLEN(enc); if (len == 1) return (int )(p - start); q = p + 1; while (len > 1) { if (*q != '\0') break; q++; len--; } if (len == 1) return (int )(p - start); } e = p + ONIGENC_MBC_MAXLEN(enc); p += ONIGENC_MBC_ENC_LEN(enc, p, e); } }
native_int Encoding::string_character_length(const uint8_t* p, const uint8_t* e, OnigEncodingType* enc) { native_int chars; for(chars = 0; p < e; chars++) { int n = Encoding::precise_mbclen(p, e, enc); if(ONIGENC_MBCLEN_CHARFOUND_P(n)) { p += ONIGENC_MBCLEN_CHARFOUND_LEN(n); } else if(p + ONIGENC_MBC_MINLEN(enc) <= e) { p += ONIGENC_MBC_MINLEN(enc); } else { p = e; } } return chars; }
int Encoding::mbclen(const uint8_t* p, const uint8_t* e, OnigEncodingType* enc) { int n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e); if (ONIGENC_MBCLEN_CHARFOUND_P(n) && ONIGENC_MBCLEN_CHARFOUND_LEN(n) <= e-p) { return ONIGENC_MBCLEN_CHARFOUND_LEN(n); } else { int min = ONIGENC_MBC_MINLEN(enc); return min <= e-p ? min : (int)(e-p); } }
extern int onigenc_strlen_null(OnigEncoding enc, const UChar* s) { int n = 0; UChar* p = (UChar* )s; while (1) { if (*p == '\0') { UChar* q; int len = ONIGENC_MBC_MINLEN(enc); if (len == 1) return n; q = p + 1; while (len > 1) { if (*q != '\0') break; q++; len--; } if (len == 1) return n; } p += ONIGENC_MBC_ENC_LEN(enc, p); n++; } }