pcre_ucp_searchfuncs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-2008 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 module contains code for searching the table of Unicode character
  33. properties. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "config.h"
  36. #endif
  37. #include "pcre_internal.h"
  38. #include "ucp.h" /* Category definitions */
  39. #include "ucpinternal.h" /* Internal table details */
  40. #include "ucptable.h" /* The table itself */
  41. /* Table to translate from particular type value to the general value. */
  42. static const int ucp_gentype[] = {
  43. ucp_C, ucp_C, ucp_C, ucp_C, ucp_C, /* Cc, Cf, Cn, Co, Cs */
  44. ucp_L, ucp_L, ucp_L, ucp_L, ucp_L, /* Ll, Lu, Lm, Lo, Lt */
  45. ucp_M, ucp_M, ucp_M, /* Mc, Me, Mn */
  46. ucp_N, ucp_N, ucp_N, /* Nd, Nl, No */
  47. ucp_P, ucp_P, ucp_P, ucp_P, ucp_P, /* Pc, Pd, Pe, Pf, Pi */
  48. ucp_P, ucp_P, /* Ps, Po */
  49. ucp_S, ucp_S, ucp_S, ucp_S, /* Sc, Sk, Sm, So */
  50. ucp_Z, ucp_Z, ucp_Z /* Zl, Zp, Zs */
  51. };
  52. /*************************************************
  53. * Search table and return type *
  54. *************************************************/
  55. /* Three values are returned: the category is ucp_C, ucp_L, etc. The detailed
  56. character type is ucp_Lu, ucp_Nd, etc. The script is ucp_Latin, etc.
  57. Arguments:
  58. c the character value
  59. type_ptr the detailed character type is returned here
  60. script_ptr the script is returned here
  61. Returns: the character type category
  62. */
  63. int
  64. _pcre_ucp_findprop(const unsigned int c, int *type_ptr, int *script_ptr)
  65. {
  66. int bot = 0;
  67. int top = sizeof(ucp_table)/sizeof(cnode);
  68. int mid;
  69. /* The table is searched using a binary chop. You might think that using
  70. intermediate variables to hold some of the common expressions would speed
  71. things up, but tests with gcc 3.4.4 on Linux showed that, on the contrary, it
  72. makes things a lot slower. */
  73. for (;;)
  74. {
  75. if (top <= bot)
  76. {
  77. *type_ptr = ucp_Cn;
  78. *script_ptr = ucp_Common;
  79. return ucp_C;
  80. }
  81. mid = (bot + top) >> 1;
  82. if (c == (ucp_table[mid].f0 & f0_charmask)) break;
  83. if (c < (ucp_table[mid].f0 & f0_charmask)) top = mid;
  84. else
  85. {
  86. if ((ucp_table[mid].f0 & f0_rangeflag) != 0 &&
  87. c <= (ucp_table[mid].f0 & f0_charmask) +
  88. (ucp_table[mid].f1 & f1_rangemask)) break;
  89. bot = mid + 1;
  90. }
  91. }
  92. /* Found an entry in the table. Set the script and detailed type values, and
  93. return the general type. */
  94. *script_ptr = (ucp_table[mid].f0 & f0_scriptmask) >> f0_scriptshift;
  95. *type_ptr = (ucp_table[mid].f1 & f1_typemask) >> f1_typeshift;
  96. return ucp_gentype[*type_ptr];
  97. }
  98. /*************************************************
  99. * Search table and return other case *
  100. *************************************************/
  101. /* If the given character is a letter, and there is another case for the
  102. letter, return the other case. Otherwise, return -1.
  103. Arguments:
  104. c the character value
  105. Returns: the other case or NOTACHAR if none
  106. */
  107. unsigned int
  108. _pcre_ucp_othercase(const unsigned int c)
  109. {
  110. int bot = 0;
  111. int top = sizeof(ucp_table)/sizeof(cnode);
  112. int mid, offset;
  113. /* The table is searched using a binary chop. You might think that using
  114. intermediate variables to hold some of the common expressions would speed
  115. things up, but tests with gcc 3.4.4 on Linux showed that, on the contrary, it
  116. makes things a lot slower. */
  117. for (;;)
  118. {
  119. if (top <= bot) return -1;
  120. mid = (bot + top) >> 1;
  121. if (c == (ucp_table[mid].f0 & f0_charmask)) break;
  122. if (c < (ucp_table[mid].f0 & f0_charmask)) top = mid;
  123. else
  124. {
  125. if ((ucp_table[mid].f0 & f0_rangeflag) != 0 &&
  126. c <= (ucp_table[mid].f0 & f0_charmask) +
  127. (ucp_table[mid].f1 & f1_rangemask)) break;
  128. bot = mid + 1;
  129. }
  130. }
  131. /* Found an entry in the table. Return NOTACHAR for a range entry. Otherwise
  132. return the other case if there is one, else NOTACHAR. */
  133. if ((ucp_table[mid].f0 & f0_rangeflag) != 0) return NOTACHAR;
  134. offset = ucp_table[mid].f1 & f1_casemask;
  135. if ((offset & f1_caseneg) != 0) offset |= f1_caseneg;
  136. return (offset == 0)? NOTACHAR : c + offset;
  137. }
  138. /* End of pcre_ucp_searchfuncs.c */