From 655556823adb6e996a0695de84b4e01338902f83 Mon Sep 17 00:00:00 2001 From: "K.Kosako" Date: Fri, 2 Mar 2018 17:27:28 +0900 Subject: [PATCH] implement encoding initialized list --- src/regcomp.c | 2 ++ src/regenc.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/regenc.h | 1 + 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/regcomp.c b/src/regcomp.c index 8026e79..7c35b43 100644 --- a/src/regcomp.c +++ b/src/regcomp.c @@ -6304,6 +6304,8 @@ onig_end(void) onig_global_callout_names_free(); #endif + onigenc_end(); + onig_inited = 0; return 0; diff --git a/src/regenc.c b/src/regenc.c index 7f572de..7333cdd 100644 --- a/src/regenc.c +++ b/src/regenc.c @@ -31,6 +31,66 @@ OnigEncoding OnigEncDefaultCharEncoding = ONIG_ENCODING_INIT_DEFAULT; +#define INITED_LIST_SIZE 20 + +static int InitedListNum; + +static struct { + OnigEncoding enc; + int inited; +} InitedList[INITED_LIST_SIZE]; + +static int +enc_inited_entry(OnigEncoding enc) +{ + int i; + + for (i = 0; i < InitedListNum; i++) { + if (InitedList[i].enc == enc) { + InitedList[i].inited = 1; + return i; + } + } + + i = InitedListNum; + if (i < INITED_LIST_SIZE - 1) { + InitedList[i].enc = enc; + InitedList[i].inited = 1; + InitedListNum++; + return i; + } + + return -1; +} + +static int +enc_is_inited(OnigEncoding enc) +{ + int i; + + for (i = 0; i < InitedListNum; i++) { + if (InitedList[i].enc == enc) { + return InitedList[i].inited; + } + } + + return 0; +} + +extern int +onigenc_end(void) +{ + int i; + + for (i = 0; i < InitedListNum; i++) { + InitedList[i].enc = 0; + InitedList[i].inited = 0; + } + + InitedListNum = 0; + return ONIG_NORMAL; +} + extern int onigenc_init(void) { @@ -42,16 +102,21 @@ onig_initialize_encoding(OnigEncoding enc) { int r; - if (ONIGENC_IS_ASCII_COMPATIBLE_ENCODING(enc)) { + if (enc != ONIG_ENCODING_ASCII && + ONIGENC_IS_ASCII_COMPATIBLE_ENCODING(enc)) { if (ONIG_ENCODING_ASCII->init != 0 && - ONIG_ENCODING_ASCII->is_initialized() == 0) { + enc_is_inited(ONIG_ENCODING_ASCII) == 0) { r = ONIG_ENCODING_ASCII->init(); if (r != ONIG_NORMAL) return r; + enc_inited_entry(ONIG_ENCODING_ASCII); } } - if (enc->init != 0 && (enc->is_initialized() == 0)) { + if (enc->init != 0 && + enc_is_inited(enc) == 0) { r = (enc->init)(); + if (r == ONIG_NORMAL) + enc_inited_entry(enc); return r; } diff --git a/src/regenc.h b/src/regenc.h index 7978093..4dd89ba 100644 --- a/src/regenc.h +++ b/src/regenc.h @@ -121,6 +121,7 @@ struct PropertyNameCtype { /* for encoding system implementation (internal) */ +extern int onigenc_end(void); extern int onigenc_ascii_apply_all_case_fold P_((OnigCaseFoldType flag, OnigApplyAllCaseFoldFunc f, void* arg)); extern int onigenc_ascii_get_case_fold_codes_by_str P_((OnigCaseFoldType flag, const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[])); extern int onigenc_apply_all_case_fold_with_map P_((int map_size, const OnigPairCaseFoldCodes map[], int ess_tsett_flag, OnigCaseFoldType flag, OnigApplyAllCaseFoldFunc f, void* arg)); -- 2.40.0