Back to index

php5  5.3.10
utf32_le.c
Go to the documentation of this file.
00001 /**********************************************************************
00002   utf32_le.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 "regenc.h"
00031 
00032 static int
00033 utf32le_mbc_enc_len(const UChar* p)
00034 {
00035   return 4;
00036 }
00037 
00038 static int
00039 utf32le_is_mbc_newline(const UChar* p, const UChar* end)
00040 {
00041   if (p + 3 < end) {
00042     if (*p == 0x0a && *(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0)
00043       return 1;
00044 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
00045     if ((*p == 0x0d || *p == 0x85) && *(p+1) == 0x00
00046        && (p+2) == 0x00 && *(p+3) == 0x00)
00047       return 1;
00048     if (*(p+1) == 0x20 && (*p == 0x29 || *p == 0x28)
00049        && *(p+2) == 0x00 && *(p+3) == 0x00)
00050       return 1;
00051 #endif
00052   }
00053   return 0;
00054 }
00055 
00056 static OnigCodePoint
00057 utf32le_mbc_to_code(const UChar* p, const UChar* end)
00058 {
00059   return (OnigCodePoint )(((p[3] * 256 + p[2]) * 256 + p[1]) * 256 + p[0]);
00060 }
00061 
00062 static int
00063 utf32le_code_to_mbclen(OnigCodePoint code)
00064 {
00065   return 4;
00066 }
00067 
00068 static int
00069 utf32le_code_to_mbc(OnigCodePoint code, UChar *buf)
00070 {
00071   UChar* p = buf;
00072 
00073   *p++ = (UChar ) (code & 0xff);
00074   *p++ = (UChar )((code & 0xff00)     >> 8);
00075   *p++ = (UChar )((code & 0xff0000)   >>16);
00076   *p++ = (UChar )((code & 0xff000000) >>24);
00077   return 4;
00078 }
00079 
00080 static int
00081 utf32le_mbc_to_normalize(OnigAmbigType flag, const UChar** pp, const UChar* end,
00082                          UChar* lower)
00083 {
00084   const UChar* p = *pp;
00085 
00086   if (*(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0) {
00087     if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
00088         ONIGENC_IS_MBC_ASCII(p)) ||
00089        ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
00090         !ONIGENC_IS_MBC_ASCII(p))) {
00091       *lower++ = ONIGENC_ISO_8859_1_TO_LOWER_CASE(*p);
00092     }
00093     else {
00094       *lower++ = *p;
00095     }
00096     *lower++ = '\0';
00097     *lower++ = '\0';
00098     *lower   = '\0';
00099 
00100     (*pp) += 4;
00101     return 4;  /* return byte length of converted char to lower */
00102   }
00103   else {
00104     int len = 4;
00105     if (lower != p) {
00106       int i;
00107       for (i = 0; i < len; i++) {
00108        *lower++ = *p++;
00109       }
00110     }
00111     (*pp) += len;
00112     return len; /* return byte length of converted char to lower */
00113   }
00114 }
00115 
00116 static int
00117 utf32le_is_mbc_ambiguous(OnigAmbigType flag, const UChar** pp, const UChar* end)
00118 {
00119   const UChar* p = *pp;
00120 
00121   (*pp) += 4;
00122 
00123   if (*(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0) {
00124     int c, v;
00125 
00126     if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
00127         ONIGENC_IS_MBC_ASCII(p)) ||
00128        ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
00129         !ONIGENC_IS_MBC_ASCII(p))) {
00130       c = *p;
00131       v = ONIGENC_IS_UNICODE_ISO_8859_1_CTYPE(c,
00132                        (ONIGENC_CTYPE_UPPER | ONIGENC_CTYPE_LOWER));
00133       if ((v | ONIGENC_CTYPE_LOWER) != 0) {
00134         /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
00135         if (c >= 0xaa && c <= 0xba)
00136           return FALSE;
00137         else
00138           return TRUE;
00139       }
00140       return (v != 0 ? TRUE : FALSE);
00141     }
00142   }
00143 
00144   return FALSE;
00145 }
00146 
00147 static UChar*
00148 utf32le_left_adjust_char_head(const UChar* start, const UChar* s)
00149 {
00150   int rem;
00151 
00152   if (s <= start) return (UChar* )s;
00153 
00154   rem = (s - start) % 4;
00155   return (UChar* )(s - rem);
00156 }
00157 
00158 OnigEncodingType OnigEncodingUTF32_LE = {
00159   utf32le_mbc_enc_len,
00160   "UTF-32LE",   /* name */
00161   4,            /* max byte length */
00162   4,            /* min byte length */
00163   (ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE |
00164    ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE ),
00165   {
00166       (OnigCodePoint )'\\'                       /* esc */
00167     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.'  */
00168     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*'  */
00169     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
00170     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
00171     , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
00172   },
00173   utf32le_is_mbc_newline,
00174   utf32le_mbc_to_code,
00175   utf32le_code_to_mbclen,
00176   utf32le_code_to_mbc,
00177   utf32le_mbc_to_normalize,
00178   utf32le_is_mbc_ambiguous,
00179   onigenc_iso_8859_1_get_all_pair_ambig_codes,
00180   onigenc_ess_tsett_get_all_comp_ambig_codes,
00181   onigenc_unicode_is_code_ctype,
00182   onigenc_unicode_get_ctype_code_range,
00183   utf32le_left_adjust_char_head,
00184   onigenc_always_false_is_allowed_reverse_match
00185 };