pcre16_ord2utf16.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2012 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * 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. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This file contains a private PCRE function that converts an ordinal
  33. character value into a UTF16 string. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "pcre_config.h"
  36. #endif
  37. /* Generate code with 16 bit character support. */
  38. #define COMPILE_PCRE16
  39. #include "pcre_internal.h"
  40. /*************************************************
  41. * Convert character value to UTF-16 *
  42. *************************************************/
  43. /* This function takes an integer value in the range 0 - 0x10ffff
  44. and encodes it as a UTF-16 character in 1 to 2 pcre_uchars.
  45. Arguments:
  46. cvalue the character value
  47. buffer pointer to buffer for result - at least 2 pcre_uchars long
  48. Returns: number of characters placed in the buffer
  49. */
  50. unsigned int
  51. PRIV(ord2utf)(pcre_uint32 cvalue, pcre_uchar *buffer)
  52. {
  53. #ifdef SUPPORT_UTF
  54. if (cvalue <= 0xffff)
  55. {
  56. *buffer = (pcre_uchar)cvalue;
  57. return 1;
  58. }
  59. cvalue -= 0x10000;
  60. *buffer++ = 0xd800 | (cvalue >> 10);
  61. *buffer = 0xdc00 | (cvalue & 0x3ff);
  62. return 2;
  63. #else /* SUPPORT_UTF */
  64. (void)(cvalue); /* Keep compiler happy; this function won't ever be */
  65. (void)(buffer); /* called when SUPPORT_UTF is not defined. */
  66. return 0;
  67. #endif /* SUPPORT_UTF */
  68. }
  69. /* End of pcre16_ord2utf16.c */