/[pcre]/code/trunk/pcre32_utf32_utils.c
ViewVC logotype

Contents of /code/trunk/pcre32_utf32_utils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1055 - (show annotations)
Tue Oct 16 15:53:30 2012 UTC (7 years ago) by chpe
File MIME type: text/plain
File size: 5046 byte(s)
pcre32: Add 32-bit library

Create libpcre32 that operates on 32-bit characters (UTF-32).

This turned out to be surprisingly simple after the UTF-16 support
was introduced; mostly just extra ifdefs and adjusting and adding
some tests.
1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
4
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
9 Copyright (c) 1997-2012 University of Cambridge
10
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
17
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21
22 * Neither the name of the University of Cambridge nor the names of its
23 contributors may be used to endorse or promote products derived from
24 this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37 -----------------------------------------------------------------------------
38 */
39
40
41 /* This module contains a function for converting any UTF-32 character
42 strings to host byte order. */
43
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 /* Generate code with 32 bit character support. */
50 #define COMPILE_PCRE32
51
52 #include "pcre_internal.h"
53
54 static pcre_uint32
55 swap_uint32(pcre_uint32 value)
56 {
57 return ((value & 0x000000ff) << 24) |
58 ((value & 0x0000ff00) << 8) |
59 ((value & 0x00ff0000) >> 8) |
60 (value >> 24);
61 }
62
63 /*************************************************
64 * Convert any UTF-32 string to host byte order *
65 *************************************************/
66
67 /* This function takes an UTF-32 string and converts
68 it to host byte order. The length can be explicitly set,
69 or automatically detected for zero terminated strings.
70 BOMs can be kept or discarded during the conversion.
71 Conversion can be done in place (output == input).
72
73 Arguments:
74 output the output buffer, its size must be greater
75 or equal than the input string
76 input any UTF-32 string
77 length the number of 32-bit units in the input string
78 can be less than zero for zero terminated strings
79 host_byte_order
80 A non-zero value means the input is in host byte
81 order, which can be dynamically changed by BOMs later.
82 Initially it contains the starting byte order and returns
83 with the last byte order so it can be used for stream
84 processing. It can be NULL, which set the host byte
85 order mode by default.
86 keep_boms for a non-zero value, the BOM (0xfeff) characters
87 are copied as well
88
89 Returns: the number of 32-bit units placed into the output buffer,
90 including the zero-terminator
91 */
92
93 int
94 pcre32_utf32_to_host_byte_order(PCRE_UCHAR32 *output, PCRE_SPTR32 input,
95 int length, int *host_byte_order, int keep_boms)
96 {
97 #ifdef SUPPORT_UTF
98 /* This function converts any UTF-32 string to host byte order and optionally
99 removes any Byte Order Marks (BOMS). Returns with the remainig length. */
100 int host_bo = host_byte_order != NULL ? *host_byte_order : 1;
101 pcre_uchar *optr = (pcre_uchar *)output;
102 const pcre_uchar *iptr = (const pcre_uchar *)input;
103 const pcre_uchar *end;
104 /* The c variable must be unsigned. */
105 register pcre_uchar c;
106
107 if (length < 0)
108 length = STRLEN_UC(iptr) + 1;
109 end = iptr + length;
110
111 while (iptr < end)
112 {
113 c = *iptr++;
114 if (c == 0x0000feffu || c == 0xfffe0000u)
115 {
116 /* Detecting the byte order of the machine is unnecessary, it is
117 enough to know that the UTF-32 string has the same byte order or not. */
118 host_bo = c == 0x0000feffu;
119 if (keep_boms != 0)
120 *optr++ = 0x0000feffu;
121 else
122 length--;
123 }
124 else
125 *optr++ = host_bo ? c : swap_uint32(c);
126 }
127 if (host_byte_order != NULL)
128 *host_byte_order = host_bo;
129
130 #else /* SUPPORT_UTF */
131 (void)(output); /* Keep picky compilers happy */
132 (void)(input);
133 (void)(keep_boms);
134 #endif /* SUPPORT_UTF */
135 return length;
136 }
137
138 /* End of pcre32_utf32_utils.c */

  ViewVC Help
Powered by ViewVC 1.1.5