~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/mbstring/oniguruma/enc/utf32_le.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
  utf32_le.c -  Oniguruma (regular expression library)
 
3
**********************************************************************/
 
4
/*-
 
5
 * Copyright (c) 2002-2005  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 "regenc.h"
 
31
 
 
32
static int
 
33
utf32le_mbc_enc_len(const UChar* p)
 
34
{
 
35
  return 4;
 
36
}
 
37
 
 
38
static int
 
39
utf32le_is_mbc_newline(const UChar* p, const UChar* end)
 
40
{
 
41
  if (p + 3 < end) {
 
42
    if (*p == 0x0a && *(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0)
 
43
      return 1;
 
44
  }
 
45
  return 0;
 
46
}
 
47
 
 
48
static OnigCodePoint
 
49
utf32le_mbc_to_code(const UChar* p, const UChar* end)
 
50
{
 
51
  return (OnigCodePoint )(((p[3] * 256 + p[2]) * 256 + p[1]) * 256 + p[0]);
 
52
}
 
53
 
 
54
static int
 
55
utf32le_code_to_mbclen(OnigCodePoint code)
 
56
{
 
57
  return 4;
 
58
}
 
59
 
 
60
static int
 
61
utf32le_code_to_mbc(OnigCodePoint code, UChar *buf)
 
62
{
 
63
  UChar* p = buf;
 
64
 
 
65
  *p++ = (UChar ) (code & 0xff);
 
66
  *p++ = (UChar )((code & 0xff00)     >> 8);
 
67
  *p++ = (UChar )((code & 0xff0000)   >>16);
 
68
  *p++ = (UChar )((code & 0xff000000) >>24);
 
69
  return 4;
 
70
}
 
71
 
 
72
static int
 
73
utf32le_mbc_to_normalize(OnigAmbigType flag, const UChar** pp, const UChar* end,
 
74
                         UChar* lower)
 
75
{
 
76
  const UChar* p = *pp;
 
77
 
 
78
  if (*(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0) {
 
79
    if (end > p + 7 &&
 
80
        (flag & ONIGENC_AMBIGUOUS_MATCH_COMPOUND) != 0 &&
 
81
        ((*p == 's' && *(p+4) == 's') ||
 
82
         ((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
 
83
          (*p == 'S' && *(p+4) == 'S'))) &&
 
84
        *(p+5) == 0 && *(p+6) == 0 && *(p+7) == 0) {
 
85
      *lower++ = 0xdf;
 
86
      *lower++ = '\0';
 
87
      *lower++ = '\0';
 
88
      *lower   = '\0';
 
89
      (*pp) += 8;
 
90
      return 4;
 
91
    }
 
92
 
 
93
    if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
 
94
         ONIGENC_IS_MBC_ASCII(p)) ||
 
95
        ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
 
96
         !ONIGENC_IS_MBC_ASCII(p))) {
 
97
      *lower++ = ONIGENC_ISO_8859_1_TO_LOWER_CASE(*p);
 
98
    }
 
99
    else {
 
100
      *lower++ = *p;
 
101
    }
 
102
    *lower++ = '\0';
 
103
    *lower++ = '\0';
 
104
    *lower   = '\0';
 
105
 
 
106
    (*pp) += 4;
 
107
    return 4;  /* return byte length of converted char to lower */
 
108
  }
 
109
  else {
 
110
    int len = 4;
 
111
    if (lower != p) {
 
112
      int i;
 
113
      for (i = 0; i < len; i++) {
 
114
        *lower++ = *p++;
 
115
      }
 
116
    }
 
117
    (*pp) += len;
 
118
    return len; /* return byte length of converted char to lower */
 
119
  }
 
120
}
 
121
 
 
122
static int
 
123
utf32le_is_mbc_ambiguous(OnigAmbigType flag, const UChar** pp, const UChar* end)
 
124
{
 
125
  const UChar* p = *pp;
 
126
 
 
127
  (*pp) += 4;
 
128
 
 
129
  if (*(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0) {
 
130
    int c, v;
 
131
 
 
132
    if ((flag & ONIGENC_AMBIGUOUS_MATCH_COMPOUND) != 0) {
 
133
      if (end > p + 7 &&
 
134
          ((*p == 's' && *(p+4) == 's') ||
 
135
           ((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
 
136
            (*p == 'S' && *(p+4) == 'S'))) &&
 
137
          *(p+5) == 0 && *(p+6) == 0 && *(p+7) == 0) {
 
138
        (*pp) += 4;
 
139
        return TRUE;
 
140
      }
 
141
      else if (*p == 0xdf) {
 
142
        return TRUE;
 
143
      }
 
144
    }
 
145
 
 
146
    if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
 
147
         ONIGENC_IS_MBC_ASCII(p)) ||
 
148
        ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
 
149
         !ONIGENC_IS_MBC_ASCII(p))) {
 
150
      c = *p;
 
151
      v = ONIGENC_IS_UNICODE_ISO_8859_1_CTYPE(c,
 
152
                       (ONIGENC_CTYPE_UPPER | ONIGENC_CTYPE_LOWER));
 
153
      if ((v | ONIGENC_CTYPE_LOWER) != 0) {
 
154
        /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
 
155
        if (c >= 0xaa && c <= 0xba)
 
156
          return FALSE;
 
157
        else
 
158
          return TRUE;
 
159
      }
 
160
      return (v != 0 ? TRUE : FALSE);
 
161
    }
 
162
  }
 
163
 
 
164
  return FALSE;
 
165
}
 
166
 
 
167
static UChar*
 
168
utf32le_left_adjust_char_head(const UChar* start, const UChar* s)
 
169
{
 
170
  int rem;
 
171
 
 
172
  if (s <= start) return (UChar* )s;
 
173
 
 
174
  rem = (s - start) % 4;
 
175
  return (UChar* )(s - rem);
 
176
}
 
177
 
 
178
OnigEncodingType OnigEncodingUTF32_LE = {
 
179
  utf32le_mbc_enc_len,
 
180
  "UTF-32LE",   /* name */
 
181
  4,            /* max byte length */
 
182
  4,            /* min byte length */
 
183
  (ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE |
 
184
   ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE |
 
185
   ONIGENC_AMBIGUOUS_MATCH_COMPOUND),
 
186
  {
 
187
      (OnigCodePoint )'\\'                       /* esc */
 
188
    , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.'  */
 
189
    , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*'  */
 
190
    , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
 
191
    , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
 
192
    , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
 
193
  },
 
194
  utf32le_is_mbc_newline,
 
195
  utf32le_mbc_to_code,
 
196
  utf32le_code_to_mbclen,
 
197
  utf32le_code_to_mbc,
 
198
  utf32le_mbc_to_normalize,
 
199
  utf32le_is_mbc_ambiguous,
 
200
  onigenc_iso_8859_1_get_all_pair_ambig_codes,
 
201
  onigenc_ess_tsett_get_all_comp_ambig_codes,
 
202
  onigenc_unicode_is_code_ctype,
 
203
  onigenc_unicode_get_ctype_code_range,
 
204
  utf32le_left_adjust_char_head,
 
205
  onigenc_always_false_is_allowed_reverse_match
 
206
};