xref: /PHP-5.4/ext/mbstring/oniguruma/reggnu.c (revision 7aab46a2)
1 /**********************************************************************
2   reggnu.c -  Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2006  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 != 0) {
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 != 0) {
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   return onig_alloc_init(reg, ONIG_OPTION_DEFAULT,
129                          ONIGENC_AMBIGUOUS_MATCH_DEFAULT,
130                          OnigEncDefaultCharEncoding,
131 			 OnigDefaultSyntax);
132 }
133 
134 extern void
re_set_casetable(const char * table)135 re_set_casetable(const char* table)
136 {
137   onigenc_set_default_caseconv_table((UChar* )table);
138 }
139 
140 extern void
141 #ifdef ONIG_RUBY_M17N
re_mbcinit(OnigEncoding enc)142 re_mbcinit(OnigEncoding enc)
143 #else
144 re_mbcinit(int mb_code)
145 #endif
146 {
147 #ifdef ONIG_RUBY_M17N
148 
149   onigenc_set_default_encoding(enc);
150 
151 #else
152 
153   OnigEncoding enc;
154 
155   switch (mb_code) {
156   case RE_MBCTYPE_ASCII:
157     enc = ONIG_ENCODING_ASCII;
158     break;
159   case RE_MBCTYPE_EUC:
160     enc = ONIG_ENCODING_EUC_JP;
161     break;
162   case RE_MBCTYPE_SJIS:
163     enc = ONIG_ENCODING_SJIS;
164     break;
165   case RE_MBCTYPE_UTF8:
166     enc = ONIG_ENCODING_UTF8;
167     break;
168   default:
169     return ;
170     break;
171   }
172 
173   onigenc_set_default_encoding(enc);
174 #endif
175 }
176