Path: blob/master/thirdparty/pcre2/src/pcre2_ord2utf.c
9898 views
/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Original API code Copyright (c) 1997-2012 University of Cambridge9New API code Copyright (c) 2016 University of Cambridge1011-----------------------------------------------------------------------------12Redistribution and use in source and binary forms, with or without13modification, are permitted provided that the following conditions are met:1415* Redistributions of source code must retain the above copyright notice,16this list of conditions and the following disclaimer.1718* Redistributions in binary form must reproduce the above copyright19notice, this list of conditions and the following disclaimer in the20documentation and/or other materials provided with the distribution.2122* Neither the name of the University of Cambridge nor the names of its23contributors may be used to endorse or promote products derived from24this software without specific prior written permission.2526THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36POSSIBILITY OF SUCH DAMAGE.37-----------------------------------------------------------------------------38*/394041/* This file contains a function that converts a Unicode character code point42into a UTF string. The behaviour is different for each code unit width. */434445#ifdef HAVE_CONFIG_H46#include "config.h"47#endif4849#include "pcre2_internal.h"505152/* If SUPPORT_UNICODE is not defined, this function will never be called.53Supply a dummy function because some compilers do not like empty source54modules. */5556#ifndef SUPPORT_UNICODE57unsigned int58PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)59{60(void)(cvalue);61(void)(buffer);62return 0;63}64#else /* SUPPORT_UNICODE */656667/*************************************************68* Convert code point to UTF *69*************************************************/7071/*72Arguments:73cvalue the character value74buffer pointer to buffer for result7576Returns: number of code units placed in the buffer77*/7879unsigned int80PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)81{82/* Convert to UTF-8 */8384#if PCRE2_CODE_UNIT_WIDTH == 885int i, j;86for (i = 0; i < PRIV(utf8_table1_size); i++)87if ((int)cvalue <= PRIV(utf8_table1)[i]) break;88buffer += i;89for (j = i; j > 0; j--)90{91*buffer-- = 0x80 | (cvalue & 0x3f);92cvalue >>= 6;93}94*buffer = PRIV(utf8_table2)[i] | cvalue;95return i + 1;9697/* Convert to UTF-16 */9899#elif PCRE2_CODE_UNIT_WIDTH == 16100if (cvalue <= 0xffff)101{102*buffer = (PCRE2_UCHAR)cvalue;103return 1;104}105cvalue -= 0x10000;106*buffer++ = 0xd800 | (cvalue >> 10);107*buffer = 0xdc00 | (cvalue & 0x3ff);108return 2;109110/* Convert to UTF-32 */111112#else113*buffer = (PCRE2_UCHAR)cvalue;114return 1;115#endif116}117#endif /* SUPPORT_UNICODE */118119/* End of pcre2_ord2utf.c */120121122