pcre2_extuni.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. Original API code Copyright (c) 1997-2012 University of Cambridge
  8. New API code Copyright (c) 2016-2018 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. /* This module contains an internal function that is used to match a Unicode
  34. extended grapheme sequence. It is used by both pcre2_match() and
  35. pcre2_def_match(). However, it is called only when Unicode support is being
  36. compiled. Nevertheless, we provide a dummy function when there is no Unicode
  37. support, because some compilers do not like functionless source files. */
  38. #ifdef HAVE_CONFIG_H
  39. #include "config.h"
  40. #endif
  41. #include "pcre2_internal.h"
  42. /* Dummy function */
  43. #ifndef SUPPORT_UNICODE
  44. PCRE2_SPTR
  45. PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,
  46. PCRE2_SPTR end_subject, BOOL utf, int *xcount)
  47. {
  48. (void)c;
  49. (void)eptr;
  50. (void)start_subject;
  51. (void)end_subject;
  52. (void)utf;
  53. (void)xcount;
  54. return NULL;
  55. }
  56. #else
  57. /*************************************************
  58. * Match an extended grapheme sequence *
  59. *************************************************/
  60. /*
  61. Arguments:
  62. c the first character
  63. eptr pointer to next character
  64. start_subject pointer to start of subject
  65. end_subject pointer to end of subject
  66. utf TRUE if in UTF mode
  67. xcount pointer to count of additional characters,
  68. or NULL if count not needed
  69. Returns: pointer after the end of the sequence
  70. */
  71. PCRE2_SPTR
  72. PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,
  73. PCRE2_SPTR end_subject, BOOL utf, int *xcount)
  74. {
  75. int lgb = UCD_GRAPHBREAK(c);
  76. while (eptr < end_subject)
  77. {
  78. int rgb;
  79. int len = 1;
  80. if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
  81. rgb = UCD_GRAPHBREAK(c);
  82. if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
  83. /* Not breaking between Regional Indicators is allowed only if there
  84. are an even number of preceding RIs. */
  85. if (lgb == ucp_gbRegionalIndicator && rgb == ucp_gbRegionalIndicator)
  86. {
  87. int ricount = 0;
  88. PCRE2_SPTR bptr = eptr - 1;
  89. if (utf) BACKCHAR(bptr);
  90. /* bptr is pointing to the left-hand character */
  91. while (bptr > start_subject)
  92. {
  93. bptr--;
  94. if (utf)
  95. {
  96. BACKCHAR(bptr);
  97. GETCHAR(c, bptr);
  98. }
  99. else
  100. c = *bptr;
  101. if (UCD_GRAPHBREAK(c) != ucp_gbRegionalIndicator) break;
  102. ricount++;
  103. }
  104. if ((ricount & 1) != 0) break; /* Grapheme break required */
  105. }
  106. /* If Extend follows E_Base[_GAZ] do not update lgb; this allows
  107. any number of Extend before a following E_Modifier. */
  108. if (rgb != ucp_gbExtend ||
  109. (lgb != ucp_gbE_Base && lgb != ucp_gbE_Base_GAZ))
  110. lgb = rgb;
  111. eptr += len;
  112. if (xcount != NULL) *xcount += 1;
  113. }
  114. return eptr;
  115. }
  116. #endif /* SUPPORT_UNICODE */
  117. /* End of pcre2_extuni.c */