1 /**********************************************************************
2 reggnu.c - Oniguruma (regular expression library)
3 **********************************************************************/
5 * Copyright (c) 2002-2018 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 re_free_registers(OnigRegion
* r
)
36 /* 0: don't free self */
37 onig_region_free(r
, 0);
41 re_adjust_startpos(regex_t
* reg
, const char* string
, int size
,
42 int startpos
, int range
)
44 if (startpos
> 0 && ONIGENC_MBC_MAXLEN(reg
->enc
) != 1 && startpos
< size
) {
46 UChar
*s
= (UChar
* )string
+ startpos
;
49 p
= onigenc_get_right_adjust_char_head(reg
->enc
, (UChar
* )string
, s
);
52 p
= ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg
->enc
, (UChar
* )string
, s
);
54 return (int )(p
- (UChar
* )string
);
61 re_match(regex_t
* reg
, const char* str
, int size
, int pos
,
62 struct re_registers
* regs
)
64 return onig_match(reg
, (UChar
* )str
, (UChar
* )(str
+ size
),
65 (UChar
* )(str
+ pos
), regs
, ONIG_OPTION_NONE
);
69 re_search(regex_t
* bufp
, const char* string
, int size
, int startpos
, int range
,
70 struct re_registers
* regs
)
72 return onig_search(bufp
, (UChar
* )string
, (UChar
* )(string
+ size
),
73 (UChar
* )(string
+ startpos
),
74 (UChar
* )(string
+ startpos
+ range
),
75 regs
, ONIG_OPTION_NONE
);
79 re_compile_pattern(const char* pattern
, int size
, regex_t
* reg
, char* ebuf
)
84 r
= onig_compile(reg
, (UChar
* )pattern
, (UChar
* )(pattern
+ size
), &einfo
);
85 if (r
!= ONIG_NORMAL
) {
86 if (IS_NOT_NULL(ebuf
))
87 (void )onig_error_code_to_str((UChar
* )ebuf
, r
, &einfo
);
94 re_free_pattern(regex_t
* reg
)
100 re_alloc_pattern(regex_t
** reg
)
102 *reg
= (regex_t
* )xmalloc(sizeof(regex_t
));
103 if (IS_NULL(*reg
)) return ONIGERR_MEMORY
;
105 return onig_reg_init(*reg
, ONIG_OPTION_DEFAULT
,
106 ONIGENC_CASE_FOLD_DEFAULT
,
107 OnigEncDefaultCharEncoding
,
112 re_set_casetable(const char* table
)
114 onigenc_set_default_caseconv_table((UChar
* )table
);
118 re_mbcinit(int mb_code
)
123 case RE_MBCTYPE_ASCII
:
124 enc
= ONIG_ENCODING_ASCII
;
128 onig_initialize(&enc
, 1);
130 onigenc_set_default_encoding(enc
);