summaryrefslogtreecommitdiff
path: root/src/regenc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regenc.c')
-rw-r--r--src/regenc.c100
1 files changed, 97 insertions, 3 deletions
diff --git a/src/regenc.c b/src/regenc.c
index 7ded5a8..21f3536 100644
--- a/src/regenc.c
+++ b/src/regenc.c
@@ -2,7 +2,7 @@
regenc.c - Oniguruma (regular expression library)
**********************************************************************/
/*-
- * Copyright (c) 2002-2017 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
+ * Copyright (c) 2002-2018 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -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)
{
@@ -40,8 +100,23 @@ onigenc_init(void)
extern int
onig_initialize_encoding(OnigEncoding enc)
{
- if (enc->init != 0 && (enc->is_initialized() == 0)) {
- int r = (enc->init)();
+ int r;
+
+ if (enc != ONIG_ENCODING_ASCII &&
+ ONIGENC_IS_ASCII_COMPATIBLE_ENCODING(enc)) {
+ OnigEncoding ascii = ONIG_ENCODING_ASCII;
+ if (ascii->init != 0 && enc_is_inited(ascii) == 0) {
+ r = ascii->init();
+ if (r != ONIG_NORMAL) return r;
+ enc_inited_entry(ascii);
+ }
+ }
+
+ if (enc->init != 0 &&
+ enc_is_inited(enc) == 0) {
+ r = (enc->init)();
+ if (r == ONIG_NORMAL)
+ enc_inited_entry(enc);
return r;
}
@@ -62,6 +137,25 @@ onigenc_set_default_encoding(OnigEncoding enc)
}
extern UChar*
+onigenc_strdup(OnigEncoding enc, const UChar* s, const UChar* end)
+{
+ int slen, term_len, i;
+ UChar *r;
+
+ slen = (int )(end - s);
+ term_len = ONIGENC_MBC_MINLEN(enc);
+
+ r = (UChar* )xmalloc(slen + term_len);
+ CHECK_NULL_RETURN(r);
+ xmemcpy(r, s, slen);
+
+ for (i = 0; i < term_len; i++)
+ r[slen + i] = (UChar )0;
+
+ return r;
+}
+
+extern UChar*
onigenc_get_right_adjust_char_head(OnigEncoding enc, const UChar* start, const UChar* s)
{
UChar* p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s);