Back to index

php5  5.3.10
big5.c
Go to the documentation of this file.
00001 /**********************************************************************
00002   big5.c -  Oniguruma (regular expression library)
00003 **********************************************************************/
00004 /*-
00005  * Copyright (c) 2002-2005  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 "regenc.h"
00031 
00032 static const int EncLen_BIG5[] = {
00033   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00034   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00035   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00036   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00037   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00038   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00039   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00040   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00041   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00042   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00043   1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00044   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00045   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00046   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00047   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00048   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
00049 };
00050 
00051 static int
00052 big5_mbc_enc_len(const UChar* p)
00053 {
00054   return EncLen_BIG5[*p];
00055 }
00056 
00057 static OnigCodePoint
00058 big5_mbc_to_code(const UChar* p, const UChar* end)
00059 {
00060   return onigenc_mbn_mbc_to_code(ONIG_ENCODING_BIG5, p, end);
00061 }
00062 
00063 static int
00064 big5_code_to_mbc(OnigCodePoint code, UChar *buf)
00065 {
00066   return onigenc_mb2_code_to_mbc(ONIG_ENCODING_BIG5, code, buf);
00067 }
00068 
00069 static int
00070 big5_mbc_to_normalize(OnigAmbigType flag, const UChar** pp, const UChar* end,
00071                       UChar* lower)
00072 {
00073   return onigenc_mbn_mbc_to_normalize(ONIG_ENCODING_BIG5, flag,
00074                                       pp, end, lower);
00075 }
00076 
00077 static int
00078 big5_is_mbc_ambiguous(OnigAmbigType flag, const UChar** pp, const UChar* end)
00079 {
00080   return onigenc_mbn_is_mbc_ambiguous(ONIG_ENCODING_BIG5, flag, pp, end);
00081 }
00082 
00083 static int
00084 big5_is_code_ctype(OnigCodePoint code, unsigned int ctype)
00085 {
00086   return onigenc_mb2_is_code_ctype(ONIG_ENCODING_BIG5, code, ctype);
00087 }
00088 
00089 static const char BIG5_CAN_BE_TRAIL_TABLE[256] = {
00090   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00091   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00092   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00093   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00094   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00095   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00096   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00097   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
00098   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00099   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00100   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00101   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00102   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00103   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00104   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00105   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
00106 };
00107 
00108 #define BIG5_ISMB_FIRST(byte)  (EncLen_BIG5[byte] > 1)
00109 #define BIG5_ISMB_TRAIL(byte)  BIG5_CAN_BE_TRAIL_TABLE[(byte)]
00110 
00111 static UChar*
00112 big5_left_adjust_char_head(const UChar* start, const UChar* s)
00113 {
00114   const UChar *p;
00115   int len;
00116 
00117   if (s <= start) return (UChar* )s;
00118   p = s;
00119 
00120   if (BIG5_ISMB_TRAIL(*p)) {
00121     while (p > start) {
00122       if (! BIG5_ISMB_FIRST(*--p)) {
00123        p++;
00124        break;
00125       }
00126     } 
00127   }
00128   len = enc_len(ONIG_ENCODING_BIG5, p);
00129   if (p + len > s) return (UChar* )p;
00130   p += len;
00131   return (UChar* )(p + ((s - p) & ~1));
00132 }
00133 
00134 static int
00135 big5_is_allowed_reverse_match(const UChar* s, const UChar* end)
00136 {
00137   const UChar c = *s;
00138 
00139   return (BIG5_ISMB_TRAIL(c) ? FALSE : TRUE);
00140 }
00141 
00142 OnigEncodingType OnigEncodingBIG5 = {
00143   big5_mbc_enc_len,
00144   "Big5",     /* name */
00145   2,          /* max enc length */
00146   1,          /* min enc length */
00147   ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE,
00148   {
00149       (OnigCodePoint )'\\'                       /* esc */
00150     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.'  */
00151     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*'  */
00152     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
00153     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
00154     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
00155   },
00156   onigenc_is_mbc_newline_0x0a,
00157   big5_mbc_to_code,
00158   onigenc_mb2_code_to_mbclen,
00159   big5_code_to_mbc,
00160   big5_mbc_to_normalize,
00161   big5_is_mbc_ambiguous,
00162   onigenc_ascii_get_all_pair_ambig_codes,
00163   onigenc_nothing_get_all_comp_ambig_codes,
00164   big5_is_code_ctype,
00165   onigenc_not_support_get_ctype_code_range,
00166   big5_left_adjust_char_head,
00167   big5_is_allowed_reverse_match
00168 };