diff options
Diffstat (limited to 'regenc.c')
| -rw-r--r-- | regenc.c | 173 |
1 files changed, 124 insertions, 49 deletions
@@ -3,7 +3,7 @@ **********************************************************************/ /*- * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp> - * Copyright (c) 2011 K.Takata <kentkt AT csc DOT jp> + * Copyright (c) 2011-2019 K.Takata <kentkt AT csc DOT jp> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -52,13 +52,28 @@ onigenc_set_default_encoding(OnigEncoding enc) } extern int +onigenc_mbclen(const OnigUChar* p,const OnigUChar* e, OnigEncoding enc) +{ + int ret = ONIGENC_PRECISE_MBC_ENC_LEN(enc, p, e); + if (ONIGENC_MBCLEN_CHARFOUND_P(ret)) { + ret = ONIGENC_MBCLEN_CHARFOUND_LEN(ret); + if (p + ret > e) ret = (int)(e - p); // just for case + return ret; + } + else if (ONIGENC_MBCLEN_NEEDMORE_P(ret)) { + return (int)(e - p); + } + return p < e ? 1 : 0; +} + +extern int onigenc_mbclen_approximate(const OnigUChar* p,const OnigUChar* e, OnigEncoding enc) { - int ret = ONIGENC_PRECISE_MBC_ENC_LEN(enc,p,e); + int ret = ONIGENC_PRECISE_MBC_ENC_LEN(enc, p, e); if (ONIGENC_MBCLEN_CHARFOUND_P(ret)) return ONIGENC_MBCLEN_CHARFOUND_LEN(ret); else if (ONIGENC_MBCLEN_NEEDMORE_P(ret)) - return (int)(e-p)+ONIGENC_MBCLEN_NEEDMORE_LEN(ret); + return (int )(e - p) + ONIGENC_MBCLEN_NEEDMORE_LEN(ret); return 1; } @@ -74,7 +89,7 @@ onigenc_get_right_adjust_char_head(OnigEncoding enc, const UChar* start, const U extern UChar* onigenc_get_right_adjust_char_head_with_prev(OnigEncoding enc, - const UChar* start, const UChar* s, const UChar* end, const UChar** prev) + const UChar* start, const UChar* s, const UChar* end, const UChar** prev) { UChar* p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s, end); @@ -364,12 +379,14 @@ const UChar OnigEncISO_8859_1_ToUpperCaseTable[256] = { }; #endif +#if 0 extern void onigenc_set_default_caseconv_table(const UChar* table ARG_UNUSED) { /* nothing */ /* obsoleted. */ } +#endif extern UChar* onigenc_get_left_adjust_char_head(OnigEncoding enc, const UChar* start, const UChar* s, const UChar* end) @@ -408,8 +425,8 @@ const OnigPairCaseFoldCodes OnigAsciiLowerMap[] = { extern int onigenc_ascii_apply_all_case_fold(OnigCaseFoldType flag ARG_UNUSED, - OnigApplyAllCaseFoldFunc f, void* arg, - OnigEncoding enc ARG_UNUSED) + OnigApplyAllCaseFoldFunc f, void* arg, + OnigEncoding enc ARG_UNUSED) { OnigCodePoint code; int i, r; @@ -429,8 +446,8 @@ onigenc_ascii_apply_all_case_fold(OnigCaseFoldType flag ARG_UNUSED, extern int onigenc_ascii_get_case_fold_codes_by_str(OnigCaseFoldType flag ARG_UNUSED, - const OnigUChar* p, const OnigUChar* end ARG_UNUSED, - OnigCaseFoldCodeItem items[], OnigEncoding enc ARG_UNUSED) + const OnigUChar* p, const OnigUChar* end ARG_UNUSED, + OnigCaseFoldCodeItem items[], OnigEncoding enc ARG_UNUSED) { if (0x41 <= *p && *p <= 0x5a) { items[0].byte_len = 1; @@ -450,7 +467,7 @@ onigenc_ascii_get_case_fold_codes_by_str(OnigCaseFoldType flag ARG_UNUSED, static int ss_apply_all_case_fold(OnigCaseFoldType flag ARG_UNUSED, - OnigApplyAllCaseFoldFunc f, void* arg) + OnigApplyAllCaseFoldFunc f, void* arg) { OnigCodePoint ss[] = { 0x73, 0x73 }; @@ -496,7 +513,7 @@ onigenc_get_case_fold_codes_by_str_with_map(int map_size, items[0].code_len = 1; items[0].code[0] = (OnigCodePoint )(*p + 0x20); if (*p == 0x53 && ess_tsett_flag != 0 && end > p + 1 - && (*(p+1) == 0x53 || *(p+1) == 0x73)) { + && (*(p+1) == 0x53 || *(p+1) == 0x73)) { /* SS */ items[1].byte_len = 2; items[1].code_len = 1; @@ -511,7 +528,7 @@ onigenc_get_case_fold_codes_by_str_with_map(int map_size, items[0].code_len = 1; items[0].code[0] = (OnigCodePoint )(*p - 0x20); if (*p == 0x73 && ess_tsett_flag != 0 && end > p + 1 - && (*(p+1) == 0x73 || *(p+1) == 0x53)) { + && (*(p+1) == 0x73 || *(p+1) == 0x53)) { /* ss */ items[1].byte_len = 2; items[1].code_len = 1; @@ -549,16 +566,16 @@ onigenc_get_case_fold_codes_by_str_with_map(int map_size, for (i = 0; i < map_size; i++) { if (*p == map[i].from) { - items[0].byte_len = 1; - items[0].code_len = 1; - items[0].code[0] = map[i].to; - return 1; + items[0].byte_len = 1; + items[0].code_len = 1; + items[0].code[0] = map[i].to; + return 1; } else if (*p == map[i].to) { - items[0].byte_len = 1; - items[0].code_len = 1; - items[0].code[0] = map[i].from; - return 1; + items[0].byte_len = 1; + items[0].code_len = 1; + items[0].code[0] = map[i].from; + return 1; } } } @@ -569,9 +586,9 @@ onigenc_get_case_fold_codes_by_str_with_map(int map_size, extern int onigenc_not_support_get_ctype_code_range(OnigCtype ctype ARG_UNUSED, - OnigCodePoint* sb_out ARG_UNUSED, - const OnigCodePoint* ranges[] ARG_UNUSED, - OnigEncoding enc) + OnigCodePoint* sb_out ARG_UNUSED, + const OnigCodePoint* ranges[] ARG_UNUSED, + OnigEncoding enc) { return ONIG_NO_SUPPORT_CONFIG; } @@ -588,7 +605,7 @@ onigenc_is_mbc_newline_0x0a(const UChar* p, const UChar* end, OnigEncoding enc A /* for single byte encodings */ extern int onigenc_ascii_mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED, const UChar** p, - const UChar* end, UChar* lower, OnigEncoding enc ARG_UNUSED) + const UChar* end, UChar* lower, OnigEncoding enc ARG_UNUSED) { *lower = ONIGENC_ASCII_CODE_TO_LOWER_CASE(**p); @@ -599,7 +616,7 @@ onigenc_ascii_mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED, const UChar** p, #if 0 extern int onigenc_ascii_is_mbc_ambiguous(OnigCaseFoldType flag ARG_UNUSED, - const UChar** pp, const UChar* end ARG_UNUSED) + const UChar** pp, const UChar* end ARG_UNUSED) { const UChar* p = *pp; @@ -610,54 +627,57 @@ onigenc_ascii_is_mbc_ambiguous(OnigCaseFoldType flag ARG_UNUSED, extern int onigenc_single_byte_mbc_enc_len(const UChar* p ARG_UNUSED, const UChar* e ARG_UNUSED, - OnigEncoding enc ARG_UNUSED) + OnigEncoding enc ARG_UNUSED) { return 1; } extern OnigCodePoint onigenc_single_byte_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED, - OnigEncoding enc ARG_UNUSED) + OnigEncoding enc ARG_UNUSED) { return (OnigCodePoint )(*p); } extern int -onigenc_single_byte_code_to_mbclen(OnigCodePoint code ARG_UNUSED, OnigEncoding enc ARG_UNUSED) +onigenc_single_byte_code_to_mbclen(OnigCodePoint code, OnigEncoding enc ARG_UNUSED) { + if (code > 0xff) + return ONIGERR_INVALID_CODE_POINT_VALUE; return 1; } extern int onigenc_single_byte_code_to_mbc(OnigCodePoint code, UChar *buf, OnigEncoding enc ARG_UNUSED) { - if (code > 0xff) - rb_raise(rb_eRangeError, "%u out of char range", code); + if (code > 0xff) { + return ONIGERR_INVALID_CODE_POINT_VALUE; + } *buf = (UChar )(code & 0xff); return 1; } extern UChar* onigenc_single_byte_left_adjust_char_head(const UChar* start ARG_UNUSED, - const UChar* s, - const UChar* end ARG_UNUSED, - OnigEncoding enc ARG_UNUSED) + const UChar* s, + const UChar* end ARG_UNUSED, + OnigEncoding enc ARG_UNUSED) { return (UChar* )s; } extern int onigenc_always_true_is_allowed_reverse_match(const UChar* s ARG_UNUSED, - const UChar* end ARG_UNUSED, - OnigEncoding enc ARG_UNUSED) + const UChar* end ARG_UNUSED, + OnigEncoding enc ARG_UNUSED) { return TRUE; } extern int onigenc_always_false_is_allowed_reverse_match(const UChar* s ARG_UNUSED, - const UChar* end ARG_UNUSED, - OnigEncoding enc ARG_UNUSED) + const UChar* end ARG_UNUSED, + OnigEncoding enc ARG_UNUSED) { return FALSE; } @@ -693,7 +713,7 @@ onigenc_mbn_mbc_to_code(OnigEncoding enc, const UChar* p, const UChar* end) extern int onigenc_mbn_mbc_case_fold(OnigEncoding enc, OnigCaseFoldType flag ARG_UNUSED, const UChar** pp, const UChar* end ARG_UNUSED, - UChar* lower) + UChar* lower) { int len; const UChar *p = *pp; @@ -824,7 +844,7 @@ onigenc_minimum_property_name_to_ctype(OnigEncoding enc, const UChar* p, const U extern int onigenc_mb2_is_code_ctype(OnigEncoding enc, OnigCodePoint code, - unsigned int ctype) + unsigned int ctype) { if (code < 128) return ONIGENC_IS_ASCII_CODE_CTYPE(code, ctype); @@ -839,7 +859,7 @@ onigenc_mb2_is_code_ctype(OnigEncoding enc, OnigCodePoint code, extern int onigenc_mb4_is_code_ctype(OnigEncoding enc, OnigCodePoint code, - unsigned int ctype) + unsigned int ctype) { if (code < 128) return ONIGENC_IS_ASCII_CODE_CTYPE(code, ctype); @@ -892,6 +912,7 @@ onigenc_with_ascii_strnicmp(OnigEncoding enc, const UChar* p, const UChar* end, return 0; } +#if 0 /* Property management */ static int resize_property_list(int new_size, const OnigCodePoint*** plist, int* psize) @@ -902,13 +923,15 @@ resize_property_list(int new_size, const OnigCodePoint*** plist, int* psize) size = sizeof(OnigCodePoint*) * new_size; if (IS_NULL(list)) { list = (const OnigCodePoint** )xmalloc(size); + if (IS_NULL(list)) return ONIGERR_MEMORY; } else { - list = (const OnigCodePoint** )xrealloc((void* )list, size); + const OnigCodePoint **tmp; + tmp = (const OnigCodePoint** )xrealloc((void* )list, size); + if (IS_NULL(tmp)) return ONIGERR_MEMORY; + list = tmp; } - if (IS_NULL(list)) return ONIGERR_MEMORY; - *plist = list; *psize = new_size; @@ -939,19 +962,71 @@ onigenc_property_list_add_property(UChar* name, const OnigCodePoint* prop, *pnum = *pnum + 1; onig_st_insert_strend(*table, name, name + strlen((char* )name), - (hash_data_type )(*pnum + ONIGENC_MAX_STD_CTYPE)); + (hash_data_type )(*pnum + ONIGENC_MAX_STD_CTYPE)); return 0; } +#endif +#ifdef USE_CASE_MAP_API extern int -onigenc_property_list_init(int (*f)(void)) +onigenc_ascii_only_case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, const OnigUChar* end, + OnigUChar* to, OnigUChar* to_end, const struct OnigEncodingTypeST* enc) { - int r; + OnigCodePoint code; + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + int codepoint_length; + + while (*pp < end && to < to_end) { + codepoint_length = ONIGENC_PRECISE_MBC_ENC_LEN(enc, *pp, end); + if (codepoint_length < 0) + return codepoint_length; /* encoding invalid */ + code = ONIGENC_MBC_TO_CODE(enc, *pp, end); + *pp += codepoint_length; + + if (code >= 'a' && code <= 'z' && (flags & ONIGENC_CASE_UPCASE)) { + flags |= ONIGENC_CASE_MODIFIED; + code -= 'a' - 'A'; + } + else if (code >= 'A' && code <= 'Z' && + (flags & (ONIGENC_CASE_DOWNCASE | ONIGENC_CASE_FOLD))) { + flags |= ONIGENC_CASE_MODIFIED; + code += 'a' - 'A'; + } + to += ONIGENC_CODE_TO_MBC(enc, code, to); + if (flags & ONIGENC_CASE_TITLECASE) /* switch from titlecase to lowercase for capitalize */ + flags ^= (ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE | ONIGENC_CASE_TITLECASE); + } + *flagP = flags; + return (int )(to - to_start); +} - THREAD_ATOMIC_START; +extern int +onigenc_single_byte_ascii_only_case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + const OnigUChar* end, OnigUChar* to, OnigUChar* to_end, + const struct OnigEncodingTypeST* enc) +{ + OnigCodePoint code; + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; - r = f(); + while (*pp < end && to < to_end) { + code = *(*pp)++; - THREAD_ATOMIC_END; - return r; + if (code >= 'a' && code <= 'z' && (flags & ONIGENC_CASE_UPCASE)) { + flags |= ONIGENC_CASE_MODIFIED; + code -= 'a' - 'A'; + } + else if (code >= 'A' && code <= 'Z' && + (flags & (ONIGENC_CASE_DOWNCASE | ONIGENC_CASE_FOLD))) { + flags |= ONIGENC_CASE_MODIFIED; + code += 'a' - 'A'; + } + *to++ = code; + if (flags & ONIGENC_CASE_TITLECASE) /* switch from titlecase to lowercase for capitalize */ + flags ^= (ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE | ONIGENC_CASE_TITLECASE); + } + *flagP = flags; + return (int )(to - to_start); } +#endif |
