xref: /PHP-5.6/ext/mbstring/oniguruma/reggnu.c (revision fe92d64a)
1 /**********************************************************************
2   reggnu.c -  Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2008  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include "regint.h"
31 
32 #ifndef ONIGGNU_H
33 #include "oniggnu.h"
34 #endif
35 
36 extern void
re_free_registers(OnigRegion * r)37 re_free_registers(OnigRegion* r)
38 {
39   /* 0: don't free self */
40   onig_region_free(r, 0);
41 }
42 
43 extern int
re_adjust_startpos(regex_t * reg,const char * string,int size,int startpos,int range)44 re_adjust_startpos(regex_t* reg, const char* string, int size,
45 		   int startpos, int range)
46 {
47   if (startpos > 0 && ONIGENC_MBC_MAXLEN(reg->enc) != 1 && startpos < size) {
48     UChar *p;
49     UChar *s = (UChar* )string + startpos;
50 
51     if (range > 0) {
52       p = onigenc_get_right_adjust_char_head(reg->enc, (UChar* )string, s);
53     }
54     else {
55       p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg->enc, (UChar* )string, s);
56     }
57     return p - (UChar* )string;
58   }
59 
60   return startpos;
61 }
62 
63 extern int
re_match(regex_t * reg,const char * str,int size,int pos,struct re_registers * regs)64 re_match(regex_t* reg, const char* str, int size, int pos,
65 	 struct re_registers* regs)
66 {
67   return onig_match(reg, (UChar* )str, (UChar* )(str + size),
68 		    (UChar* )(str + pos), regs, ONIG_OPTION_NONE);
69 }
70 
71 extern int
re_search(regex_t * bufp,const char * string,int size,int startpos,int range,struct re_registers * regs)72 re_search(regex_t* bufp, const char* string, int size, int startpos, int range,
73 	  struct re_registers* regs)
74 {
75   return onig_search(bufp, (UChar* )string, (UChar* )(string + size),
76 		     (UChar* )(string + startpos),
77 		     (UChar* )(string + startpos + range),
78 		     regs, ONIG_OPTION_NONE);
79 }
80 
81 extern int
re_compile_pattern(const char * pattern,int size,regex_t * reg,char * ebuf)82 re_compile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
83 {
84   int r;
85   OnigErrorInfo einfo;
86 
87   r = onig_compile(reg, (UChar* )pattern, (UChar* )(pattern + size), &einfo);
88   if (r != ONIG_NORMAL) {
89     if (IS_NOT_NULL(ebuf))
90       (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
91   }
92 
93   return r;
94 }
95 
96 #ifdef USE_RECOMPILE_API
97 extern int
re_recompile_pattern(const char * pattern,int size,regex_t * reg,char * ebuf)98 re_recompile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
99 {
100   int r;
101   OnigErrorInfo einfo;
102   OnigEncoding enc;
103 
104   /* I think encoding and options should be arguments of this function.
105      But this is adapted to present re.c. (2002/11/29)
106    */
107   enc = OnigEncDefaultCharEncoding;
108 
109   r = onig_recompile(reg, (UChar* )pattern, (UChar* )(pattern + size),
110 		     reg->options, enc, OnigDefaultSyntax, &einfo);
111   if (r != ONIG_NORMAL) {
112     if (IS_NOT_NULL(ebuf))
113       (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
114   }
115   return r;
116 }
117 #endif
118 
119 extern void
re_free_pattern(regex_t * reg)120 re_free_pattern(regex_t* reg)
121 {
122   onig_free(reg);
123 }
124 
125 extern int
re_alloc_pattern(regex_t ** reg)126 re_alloc_pattern(regex_t** reg)
127 {
128   *reg = (regex_t* )xmalloc(sizeof(regex_t));
129   if (IS_NULL(*reg)) return ONIGERR_MEMORY;
130 
131   return onig_reg_init(*reg, ONIG_OPTION_DEFAULT,
132 		       ONIGENC_CASE_FOLD_DEFAULT,
133 		       OnigEncDefaultCharEncoding,
134 		       OnigDefaultSyntax);
135 }
136 
137 extern void
re_set_casetable(const char * table)138 re_set_casetable(const char* table)
139 {
140   onigenc_set_default_caseconv_table((UChar* )table);
141 }
142 
143 extern void
re_mbcinit(int mb_code)144 re_mbcinit(int mb_code)
145 {
146   OnigEncoding enc;
147 
148   switch (mb_code) {
149   case RE_MBCTYPE_ASCII:
150     enc = ONIG_ENCODING_ASCII;
151     break;
152   case RE_MBCTYPE_EUC:
153     enc = ONIG_ENCODING_EUC_JP;
154     break;
155   case RE_MBCTYPE_SJIS:
156     enc = ONIG_ENCODING_SJIS;
157     break;
158   case RE_MBCTYPE_UTF8:
159     enc = ONIG_ENCODING_UTF8;
160     break;
161   default:
162     return ;
163     break;
164   }
165 
166   onigenc_set_default_encoding(enc);
167 }
168