Skip to content
0.2.0-dev Download ZIP
118 lines ·3.6 KB Raw
1/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
9 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016 University of Cambridge
11
12-----------------------------------------------------------------------------
13Redistribution and use in source and binary forms, with or without
14modification, are permitted provided that the following conditions are met:
15
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 * Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 * Neither the name of the University of Cambridge nor the names of its
24 contributors may be used to endorse or promote products derived from
25 this software without specific prior written permission.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37POSSIBILITY OF SUCH DAMAGE.
38-----------------------------------------------------------------------------
39*/
40
41
42/* This file contains a function that converts a Unicode character code point
43into a UTF string. The behaviour is different for each code unit width. */
44
45
46#include "pcre2_internal.h"
47
48
49
50/* If SUPPORT_UNICODE is not defined, this function will never be called.
51Supply a dummy function because some compilers do not like empty source
52modules. */
53
54#ifndef SUPPORT_UNICODE
55unsigned int
56PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)
57{
58(void)(cvalue);
59(void)(buffer);
60return 0;
61}
62#else /* SUPPORT_UNICODE */
63
64
65/*************************************************
66* Convert code point to UTF *
67*************************************************/
68
69/*
70Arguments:
71 cvalue the character value
72 buffer pointer to buffer for result
73
74Returns: number of code units placed in the buffer
75*/
76
77unsigned int
78PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)
79{
80/* Convert to UTF-8 */
81
82#if PCRE2_CODE_UNIT_WIDTH == 8
83unsigned int i;
84
85for (i = 0; i < PRIV(utf8_table1_size); i++)
86 if ((int)cvalue <= PRIV(utf8_table1)[i]) break;
87buffer += i;
88for (unsigned int j = i; j != 0; j--)
89 {
90 *buffer-- = 0x80 | (cvalue & 0x3f);
91 cvalue >>= 6;
92 }
93*buffer = (PCRE2_UCHAR)(PRIV(utf8_table2)[i] | (int)cvalue);
94return i + 1;
95
96/* Convert to UTF-16 */
97
98#elif PCRE2_CODE_UNIT_WIDTH == 16
99if (cvalue <= 0xffff)
100 {
101 *buffer = (PCRE2_UCHAR)cvalue;
102 return 1;
103 }
104cvalue -= 0x10000;
105*buffer++ = 0xd800 | (cvalue >> 10);
106*buffer = 0xdc00 | (cvalue & 0x3ff);
107return 2;
108
109/* Convert to UTF-32 */
110
111#else
112*buffer = (PCRE2_UCHAR)cvalue;
113return 1;
114#endif
115}
116#endif /* SUPPORT_UNICODE */
117
118/* End of pcre2_ord2utf.c */