pcre2_tables.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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-2021 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 some fixed tables that are used by more than one of the
  34. PCRE2 code modules. The tables are also #included by the pcre2test program,
  35. which uses macros to change their names from _pcre2_xxx to xxxx, thereby
  36. avoiding name clashes with the library. In this case, PCRE2_PCRE2TEST is
  37. defined. */
  38. #ifndef PCRE2_PCRE2TEST /* We're compiling the library */
  39. #ifdef HAVE_CONFIG_H
  40. #include "config.h"
  41. #endif
  42. #include "pcre2_internal.h"
  43. #endif /* PCRE2_PCRE2TEST */
  44. /* Table of sizes for the fixed-length opcodes. It's defined in a macro so that
  45. the definition is next to the definition of the opcodes in pcre2_internal.h.
  46. This is mode-dependent, so it is skipped when this file is included by
  47. pcre2test. */
  48. #ifndef PCRE2_PCRE2TEST
  49. const uint8_t PRIV(OP_lengths)[] = { OP_LENGTHS };
  50. #endif
  51. /* Tables of horizontal and vertical whitespace characters, suitable for
  52. adding to classes. */
  53. const uint32_t PRIV(hspace_list)[] = { HSPACE_LIST };
  54. const uint32_t PRIV(vspace_list)[] = { VSPACE_LIST };
  55. /* These tables are the pairs of delimiters that are valid for callout string
  56. arguments. For each starting delimiter there must be a matching ending
  57. delimiter, which in fact is different only for bracket-like delimiters. */
  58. const uint32_t PRIV(callout_start_delims)[] = {
  59. CHAR_GRAVE_ACCENT, CHAR_APOSTROPHE, CHAR_QUOTATION_MARK,
  60. CHAR_CIRCUMFLEX_ACCENT, CHAR_PERCENT_SIGN, CHAR_NUMBER_SIGN,
  61. CHAR_DOLLAR_SIGN, CHAR_LEFT_CURLY_BRACKET, 0 };
  62. const uint32_t PRIV(callout_end_delims[]) = {
  63. CHAR_GRAVE_ACCENT, CHAR_APOSTROPHE, CHAR_QUOTATION_MARK,
  64. CHAR_CIRCUMFLEX_ACCENT, CHAR_PERCENT_SIGN, CHAR_NUMBER_SIGN,
  65. CHAR_DOLLAR_SIGN, CHAR_RIGHT_CURLY_BRACKET, 0 };
  66. /*************************************************
  67. * Tables for UTF-8 support *
  68. *************************************************/
  69. /* These tables are required by pcre2test in 16- or 32-bit mode, as well
  70. as for the library in 8-bit mode, because pcre2test uses UTF-8 internally for
  71. handling wide characters. */
  72. #if defined PCRE2_PCRE2TEST || \
  73. (defined SUPPORT_UNICODE && \
  74. defined PCRE2_CODE_UNIT_WIDTH && \
  75. PCRE2_CODE_UNIT_WIDTH == 8)
  76. /* These are the breakpoints for different numbers of bytes in a UTF-8
  77. character. */
  78. const int PRIV(utf8_table1)[] =
  79. { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff};
  80. const int PRIV(utf8_table1_size) = sizeof(PRIV(utf8_table1)) / sizeof(int);
  81. /* These are the indicator bits and the mask for the data bits to set in the
  82. first byte of a character, indexed by the number of additional bytes. */
  83. const int PRIV(utf8_table2)[] = { 0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc};
  84. const int PRIV(utf8_table3)[] = { 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01};
  85. /* Table of the number of extra bytes, indexed by the first byte masked with
  86. 0x3f. The highest number for a valid UTF-8 first byte is in fact 0x3d. */
  87. const uint8_t PRIV(utf8_table4)[] = {
  88. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  89. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  90. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  91. 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 };
  92. #endif /* UTF-8 support needed */
  93. /* Tables concerned with Unicode properties are relevant only when Unicode
  94. support is enabled. See also the pcre2_ucptables.c file, which is generated by
  95. a Python script from Unicode data files. */
  96. #ifdef SUPPORT_UNICODE
  97. /* Table to translate from particular type value to the general value. */
  98. const uint32_t PRIV(ucp_gentype)[] = {
  99. ucp_C, ucp_C, ucp_C, ucp_C, ucp_C, /* Cc, Cf, Cn, Co, Cs */
  100. ucp_L, ucp_L, ucp_L, ucp_L, ucp_L, /* Ll, Lu, Lm, Lo, Lt */
  101. ucp_M, ucp_M, ucp_M, /* Mc, Me, Mn */
  102. ucp_N, ucp_N, ucp_N, /* Nd, Nl, No */
  103. ucp_P, ucp_P, ucp_P, ucp_P, ucp_P, /* Pc, Pd, Pe, Pf, Pi */
  104. ucp_P, ucp_P, /* Ps, Po */
  105. ucp_S, ucp_S, ucp_S, ucp_S, /* Sc, Sk, Sm, So */
  106. ucp_Z, ucp_Z, ucp_Z /* Zl, Zp, Zs */
  107. };
  108. /* This table encodes the rules for finding the end of an extended grapheme
  109. cluster. Every code point has a grapheme break property which is one of the
  110. ucp_gbXX values defined in pcre2_ucp.h. These changed between Unicode versions
  111. 10 and 11. The 2-dimensional table is indexed by the properties of two adjacent
  112. code points. The left property selects a word from the table, and the right
  113. property selects a bit from that word like this:
  114. PRIV(ucp_gbtable)[left-property] & (1u << right-property)
  115. The value is non-zero if a grapheme break is NOT permitted between the relevant
  116. two code points. The breaking rules are as follows:
  117. 1. Break at the start and end of text (pretty obviously).
  118. 2. Do not break between a CR and LF; otherwise, break before and after
  119. controls.
  120. 3. Do not break Hangul syllable sequences, the rules for which are:
  121. L may be followed by L, V, LV or LVT
  122. LV or V may be followed by V or T
  123. LVT or T may be followed by T
  124. 4. Do not break before extending characters or zero-width-joiner (ZWJ).
  125. The following rules are only for extended grapheme clusters (but that's what we
  126. are implementing).
  127. 5. Do not break before SpacingMarks.
  128. 6. Do not break after Prepend characters.
  129. 7. Do not break within emoji modifier sequences or emoji zwj sequences. That
  130. is, do not break between characters with the Extended_Pictographic property.
  131. Extend and ZWJ characters are allowed between the characters; this cannot be
  132. represented in this table, the code has to deal with it.
  133. 8. Do not break within emoji flag sequences. That is, do not break between
  134. regional indicator (RI) symbols if there are an odd number of RI characters
  135. before the break point. This table encodes "join RI characters"; the code
  136. has to deal with checking for previous adjoining RIs.
  137. 9. Otherwise, break everywhere.
  138. */
  139. #define ESZ (1<<ucp_gbExtend)|(1<<ucp_gbSpacingMark)|(1<<ucp_gbZWJ)
  140. const uint32_t PRIV(ucp_gbtable)[] = {
  141. (1u<<ucp_gbLF), /* 0 CR */
  142. 0, /* 1 LF */
  143. 0, /* 2 Control */
  144. ESZ, /* 3 Extend */
  145. ESZ|(1u<<ucp_gbPrepend)| /* 4 Prepend */
  146. (1u<<ucp_gbL)|(1u<<ucp_gbV)|(1u<<ucp_gbT)|
  147. (1u<<ucp_gbLV)|(1u<<ucp_gbLVT)|(1u<<ucp_gbOther)|
  148. (1u<<ucp_gbRegional_Indicator),
  149. ESZ, /* 5 SpacingMark */
  150. ESZ|(1u<<ucp_gbL)|(1u<<ucp_gbV)|(1u<<ucp_gbLV)| /* 6 L */
  151. (1u<<ucp_gbLVT),
  152. ESZ|(1u<<ucp_gbV)|(1u<<ucp_gbT), /* 7 V */
  153. ESZ|(1u<<ucp_gbT), /* 8 T */
  154. ESZ|(1u<<ucp_gbV)|(1u<<ucp_gbT), /* 9 LV */
  155. ESZ|(1u<<ucp_gbT), /* 10 LVT */
  156. (1u<<ucp_gbRegional_Indicator), /* 11 Regional Indicator */
  157. ESZ, /* 12 Other */
  158. ESZ, /* 13 ZWJ */
  159. ESZ|(1u<<ucp_gbExtended_Pictographic) /* 14 Extended Pictographic */
  160. };
  161. #undef ESZ
  162. #ifdef SUPPORT_JIT
  163. /* This table reverses PRIV(ucp_gentype). We can save the cost
  164. of a memory load. */
  165. const int PRIV(ucp_typerange)[] = {
  166. ucp_Cc, ucp_Cs,
  167. ucp_Ll, ucp_Lu,
  168. ucp_Mc, ucp_Mn,
  169. ucp_Nd, ucp_No,
  170. ucp_Pc, ucp_Ps,
  171. ucp_Sc, ucp_So,
  172. ucp_Zl, ucp_Zs,
  173. };
  174. #endif /* SUPPORT_JIT */
  175. /* Finally, include the tables that are auto-generated from the Unicode data
  176. files. */
  177. #include "pcre2_ucptables.c"
  178. #endif /* SUPPORT_UNICODE */
  179. /* End of pcre2_tables.c */