pcre2_newline.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 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 internal functions for testing newlines when more than
  34. one kind of newline is to be recognized. When a newline is found, its length is
  35. returned. In principle, we could implement several newline "types", each
  36. referring to a different set of newline characters. At present, PCRE2 supports
  37. only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF,
  38. and NLTYPE_ANY. The full list of Unicode newline characters is taken from
  39. http://unicode.org/unicode/reports/tr18/. */
  40. #ifdef HAVE_CONFIG_H
  41. #include "config.h"
  42. #endif
  43. #include "pcre2_internal.h"
  44. /*************************************************
  45. * Check for newline at given position *
  46. *************************************************/
  47. /* This function is called only via the IS_NEWLINE macro, which does so only
  48. when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed
  49. newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the code unit
  50. pointed to by ptr is less than the end of the string.
  51. Arguments:
  52. ptr pointer to possible newline
  53. type the newline type
  54. endptr pointer to the end of the string
  55. lenptr where to return the length
  56. utf TRUE if in utf mode
  57. Returns: TRUE or FALSE
  58. */
  59. BOOL
  60. PRIV(is_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR endptr,
  61. uint32_t *lenptr, BOOL utf)
  62. {
  63. uint32_t c;
  64. #ifdef SUPPORT_UNICODE
  65. if (utf) { GETCHAR(c, ptr); } else c = *ptr;
  66. #else
  67. (void)utf;
  68. c = *ptr;
  69. #endif /* SUPPORT_UNICODE */
  70. if (type == NLTYPE_ANYCRLF) switch(c)
  71. {
  72. case CHAR_LF:
  73. *lenptr = 1;
  74. return TRUE;
  75. case CHAR_CR:
  76. *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
  77. return TRUE;
  78. default:
  79. return FALSE;
  80. }
  81. /* NLTYPE_ANY */
  82. else switch(c)
  83. {
  84. #ifdef EBCDIC
  85. case CHAR_NEL:
  86. #endif
  87. case CHAR_LF:
  88. case CHAR_VT:
  89. case CHAR_FF:
  90. *lenptr = 1;
  91. return TRUE;
  92. case CHAR_CR:
  93. *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
  94. return TRUE;
  95. #ifndef EBCDIC
  96. #if PCRE2_CODE_UNIT_WIDTH == 8
  97. case CHAR_NEL:
  98. *lenptr = utf? 2 : 1;
  99. return TRUE;
  100. case 0x2028: /* LS */
  101. case 0x2029: /* PS */
  102. *lenptr = 3;
  103. return TRUE;
  104. #else /* 16-bit or 32-bit code units */
  105. case CHAR_NEL:
  106. case 0x2028: /* LS */
  107. case 0x2029: /* PS */
  108. *lenptr = 1;
  109. return TRUE;
  110. #endif
  111. #endif /* Not EBCDIC */
  112. default:
  113. return FALSE;
  114. }
  115. }
  116. /*************************************************
  117. * Check for newline at previous position *
  118. *************************************************/
  119. /* This function is called only via the WAS_NEWLINE macro, which does so only
  120. when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed
  121. newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the initial
  122. value of ptr is greater than the start of the string that is being processed.
  123. Arguments:
  124. ptr pointer to possible newline
  125. type the newline type
  126. startptr pointer to the start of the string
  127. lenptr where to return the length
  128. utf TRUE if in utf mode
  129. Returns: TRUE or FALSE
  130. */
  131. BOOL
  132. PRIV(was_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR startptr,
  133. uint32_t *lenptr, BOOL utf)
  134. {
  135. uint32_t c;
  136. ptr--;
  137. #ifdef SUPPORT_UNICODE
  138. if (utf)
  139. {
  140. BACKCHAR(ptr);
  141. GETCHAR(c, ptr);
  142. }
  143. else c = *ptr;
  144. #else
  145. (void)utf;
  146. c = *ptr;
  147. #endif /* SUPPORT_UNICODE */
  148. if (type == NLTYPE_ANYCRLF) switch(c)
  149. {
  150. case CHAR_LF:
  151. *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
  152. return TRUE;
  153. case CHAR_CR:
  154. *lenptr = 1;
  155. return TRUE;
  156. default:
  157. return FALSE;
  158. }
  159. /* NLTYPE_ANY */
  160. else switch(c)
  161. {
  162. case CHAR_LF:
  163. *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
  164. return TRUE;
  165. #ifdef EBCDIC
  166. case CHAR_NEL:
  167. #endif
  168. case CHAR_VT:
  169. case CHAR_FF:
  170. case CHAR_CR:
  171. *lenptr = 1;
  172. return TRUE;
  173. #ifndef EBCDIC
  174. #if PCRE2_CODE_UNIT_WIDTH == 8
  175. case CHAR_NEL:
  176. *lenptr = utf? 2 : 1;
  177. return TRUE;
  178. case 0x2028: /* LS */
  179. case 0x2029: /* PS */
  180. *lenptr = 3;
  181. return TRUE;
  182. #else /* 16-bit or 32-bit code units */
  183. case CHAR_NEL:
  184. case 0x2028: /* LS */
  185. case 0x2029: /* PS */
  186. *lenptr = 1;
  187. return TRUE;
  188. #endif
  189. #endif /* Not EBCDIC */
  190. default:
  191. return FALSE;
  192. }
  193. }
  194. /* End of pcre2_newline.c */