Back to index

php5  5.3.10
reggnu.c
Go to the documentation of this file.
00001 /**********************************************************************
00002   reggnu.c -  Oniguruma (regular expression library)
00003 **********************************************************************/
00004 /*-
00005  * Copyright (c) 2002-2006  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include "regint.h"
00031 
00032 #ifndef ONIGGNU_H
00033 #include "oniggnu.h"
00034 #endif
00035 
00036 extern void
00037 re_free_registers(OnigRegion* r)
00038 {
00039   /* 0: don't free self */
00040   onig_region_free(r, 0);
00041 }
00042 
00043 extern int
00044 re_adjust_startpos(regex_t* reg, const char* string, int size,
00045                  int startpos, int range)
00046 {
00047   if (startpos > 0 && ONIGENC_MBC_MAXLEN(reg->enc) != 1 && startpos < size) {
00048     UChar *p;
00049     UChar *s = (UChar* )string + startpos;
00050 
00051     if (range > 0) {
00052       p = onigenc_get_right_adjust_char_head(reg->enc, (UChar* )string, s);
00053     }
00054     else {
00055       p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg->enc, (UChar* )string, s);
00056     }
00057     return p - (UChar* )string;
00058   }
00059 
00060   return startpos;
00061 }
00062 
00063 extern int
00064 re_match(regex_t* reg, const char* str, int size, int pos,
00065         struct re_registers* regs)
00066 {
00067   return onig_match(reg, (UChar* )str, (UChar* )(str + size),
00068                   (UChar* )(str + pos), regs, ONIG_OPTION_NONE);
00069 }
00070 
00071 extern int
00072 re_search(regex_t* bufp, const char* string, int size, int startpos, int range,
00073          struct re_registers* regs)
00074 {
00075   return onig_search(bufp, (UChar* )string, (UChar* )(string + size),
00076                    (UChar* )(string + startpos),
00077                    (UChar* )(string + startpos + range),
00078                    regs, ONIG_OPTION_NONE);
00079 }
00080 
00081 extern int
00082 re_compile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
00083 {
00084   int r;
00085   OnigErrorInfo einfo;
00086 
00087   r = onig_compile(reg, (UChar* )pattern, (UChar* )(pattern + size), &einfo);
00088   if (r != 0) {
00089     if (IS_NOT_NULL(ebuf))
00090       (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
00091   }
00092 
00093   return r;
00094 }
00095 
00096 #ifdef USE_RECOMPILE_API
00097 extern int
00098 re_recompile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
00099 {
00100   int r;
00101   OnigErrorInfo einfo;
00102   OnigEncoding enc;
00103 
00104   /* I think encoding and options should be arguments of this function.
00105      But this is adapted to present re.c. (2002/11/29)
00106    */
00107   enc = OnigEncDefaultCharEncoding;
00108 
00109   r = onig_recompile(reg, (UChar* )pattern, (UChar* )(pattern + size),
00110                    reg->options, enc, OnigDefaultSyntax, &einfo);
00111   if (r != 0) {
00112     if (IS_NOT_NULL(ebuf))
00113       (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
00114   }
00115   return r;
00116 }
00117 #endif
00118 
00119 extern void
00120 re_free_pattern(regex_t* reg)
00121 {
00122   onig_free(reg);
00123 }
00124 
00125 extern int
00126 re_alloc_pattern(regex_t** reg)
00127 {
00128   return onig_alloc_init(reg, ONIG_OPTION_DEFAULT,
00129                          ONIGENC_AMBIGUOUS_MATCH_DEFAULT,
00130                          OnigEncDefaultCharEncoding,
00131                       OnigDefaultSyntax);
00132 }
00133 
00134 extern void
00135 re_set_casetable(const char* table)
00136 {
00137   onigenc_set_default_caseconv_table((UChar* )table);
00138 }
00139 
00140 extern void
00141 #ifdef ONIG_RUBY_M17N
00142 re_mbcinit(OnigEncoding enc)
00143 #else
00144 re_mbcinit(int mb_code)
00145 #endif
00146 {
00147 #ifdef ONIG_RUBY_M17N
00148 
00149   onigenc_set_default_encoding(enc);
00150 
00151 #else
00152 
00153   OnigEncoding enc;
00154 
00155   switch (mb_code) {
00156   case RE_MBCTYPE_ASCII:
00157     enc = ONIG_ENCODING_ASCII;
00158     break;
00159   case RE_MBCTYPE_EUC:
00160     enc = ONIG_ENCODING_EUC_JP;
00161     break;
00162   case RE_MBCTYPE_SJIS:
00163     enc = ONIG_ENCODING_SJIS;
00164     break;
00165   case RE_MBCTYPE_UTF8:
00166     enc = ONIG_ENCODING_UTF8;
00167     break;
00168   default:
00169     return ;
00170     break;
00171   }
00172 
00173   onigenc_set_default_encoding(enc);
00174 #endif
00175 }