Back to index

php5  5.3.10
euc_tw.c
Go to the documentation of this file.
00001 /**********************************************************************
00002   euc_tw.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_EUCTW[] = {
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, 4, 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 euctw_mbc_enc_len(const UChar* p)
00053 {
00054   return EncLen_EUCTW[*p];
00055 }
00056 
00057 static OnigCodePoint
00058 euctw_mbc_to_code(const UChar* p, const UChar* end)
00059 {
00060   return onigenc_mbn_mbc_to_code(ONIG_ENCODING_EUC_TW, p, end);
00061 }
00062 
00063 static int
00064 euctw_code_to_mbc(OnigCodePoint code, UChar *buf)
00065 {
00066   return onigenc_mb4_code_to_mbc(ONIG_ENCODING_EUC_TW, code, buf);
00067 }
00068 
00069 static int
00070 euctw_mbc_to_normalize(OnigAmbigType flag, const UChar** pp, const UChar* end,
00071                        UChar* lower)
00072 {
00073   return onigenc_mbn_mbc_to_normalize(ONIG_ENCODING_EUC_TW, flag,
00074                                       pp, end, lower);
00075 }
00076 
00077 static int
00078 euctw_is_mbc_ambiguous(OnigAmbigType flag, const UChar** pp, const UChar* end)
00079 {
00080   return onigenc_mbn_is_mbc_ambiguous(ONIG_ENCODING_EUC_TW, flag, pp, end);
00081 }
00082 
00083 static int
00084 euctw_is_code_ctype(OnigCodePoint code, unsigned int ctype)
00085 {
00086   return onigenc_mb4_is_code_ctype(ONIG_ENCODING_EUC_TW, code, ctype);
00087 }
00088 
00089 #define euctw_islead(c)    (((c) < 0xa1 && (c) != 0x8e) || (c) == 0xff)
00090 
00091 static UChar*
00092 euctw_left_adjust_char_head(const UChar* start, const UChar* s)
00093 {
00094   /* Assumed in this encoding,
00095      mb-trail bytes don't mix with single bytes.
00096   */
00097   const UChar *p;
00098   int len;
00099 
00100   if (s <= start) return (UChar* )s;
00101   p = s;
00102 
00103   while (!euctw_islead(*p) && p > start) p--;
00104   len = enc_len(ONIG_ENCODING_EUC_TW, p);
00105   if (p + len > s) return (UChar* )p;
00106   p += len;
00107   return (UChar* )(p + ((s - p) & ~1));
00108 }
00109 
00110 static int
00111 euctw_is_allowed_reverse_match(const UChar* s, const UChar* end)
00112 {
00113   const UChar c = *s;
00114   if (c <= 0x7e) return TRUE;
00115   else           return FALSE;
00116 }
00117 
00118 OnigEncodingType OnigEncodingEUC_TW = {
00119   euctw_mbc_enc_len,
00120   "EUC-TW",   /* name */
00121   4,          /* max enc length */
00122   1,          /* min enc length */
00123   ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE,
00124   {
00125       (OnigCodePoint )'\\'                       /* esc */
00126     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.'  */
00127     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*'  */
00128     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
00129     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
00130     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
00131   },
00132   onigenc_is_mbc_newline_0x0a,
00133   euctw_mbc_to_code,
00134   onigenc_mb4_code_to_mbclen,
00135   euctw_code_to_mbc,
00136   euctw_mbc_to_normalize,
00137   euctw_is_mbc_ambiguous,
00138   onigenc_ascii_get_all_pair_ambig_codes,
00139   onigenc_nothing_get_all_comp_ambig_codes,
00140   euctw_is_code_ctype,
00141   onigenc_not_support_get_ctype_code_range,
00142   euctw_left_adjust_char_head,
00143   euctw_is_allowed_reverse_match
00144 };