pcre_newline.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 module contains internal functions for testing newlines when more than
  33. one kind of newline is to be recognized. When a newline is found, its length is
  34. returned. In principle, we could implement several newline "types", each
  35. referring to a different set of newline characters. At present, PCRE supports
  36. only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF,
  37. and NLTYPE_ANY. The full list of Unicode newline characters is taken from
  38. http://unicode.org/unicode/reports/tr18/. */
  39. #ifdef HAVE_CONFIG_H
  40. #include "pcre_config.h"
  41. #endif
  42. #include "pcre_internal.h"
  43. /*************************************************
  44. * Check for newline at given position *
  45. *************************************************/
  46. /* It is guaranteed that the initial value of ptr is less than the end of the
  47. string that is being processed.
  48. Arguments:
  49. ptr pointer to possible newline
  50. type the newline type
  51. endptr pointer to the end of the string
  52. lenptr where to return the length
  53. utf TRUE if in utf mode
  54. Returns: TRUE or FALSE
  55. */
  56. BOOL
  57. PRIV(is_newline)(PCRE_PUCHAR ptr, int type, PCRE_PUCHAR endptr, int *lenptr,
  58. BOOL utf)
  59. {
  60. pcre_uint32 c;
  61. (void)utf;
  62. #ifdef SUPPORT_UTF
  63. if (utf)
  64. {
  65. GETCHAR(c, ptr);
  66. }
  67. else
  68. #endif /* SUPPORT_UTF */
  69. c = *ptr;
  70. /* Note that this function is called only for ANY or ANYCRLF. */
  71. if (type == NLTYPE_ANYCRLF) switch(c)
  72. {
  73. case CHAR_LF: *lenptr = 1; return TRUE;
  74. case CHAR_CR: *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
  75. return TRUE;
  76. default: return FALSE;
  77. }
  78. /* NLTYPE_ANY */
  79. else switch(c)
  80. {
  81. #ifdef EBCDIC
  82. case CHAR_NEL:
  83. #endif
  84. case CHAR_LF:
  85. case CHAR_VT:
  86. case CHAR_FF: *lenptr = 1; return TRUE;
  87. case CHAR_CR:
  88. *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
  89. return TRUE;
  90. #ifndef EBCDIC
  91. #ifdef COMPILE_PCRE8
  92. case CHAR_NEL: *lenptr = utf? 2 : 1; return TRUE;
  93. case 0x2028: /* LS */
  94. case 0x2029: *lenptr = 3; return TRUE; /* PS */
  95. #else /* COMPILE_PCRE16 || COMPILE_PCRE32 */
  96. case CHAR_NEL:
  97. case 0x2028: /* LS */
  98. case 0x2029: *lenptr = 1; return TRUE; /* PS */
  99. #endif /* COMPILE_PCRE8 */
  100. #endif /* Not EBCDIC */
  101. default: return FALSE;
  102. }
  103. }
  104. /*************************************************
  105. * Check for newline at previous position *
  106. *************************************************/
  107. /* It is guaranteed that the initial value of ptr is greater than the start of
  108. the string that is being processed.
  109. Arguments:
  110. ptr pointer to possible newline
  111. type the newline type
  112. startptr pointer to the start of the string
  113. lenptr where to return the length
  114. utf TRUE if in utf mode
  115. Returns: TRUE or FALSE
  116. */
  117. BOOL
  118. PRIV(was_newline)(PCRE_PUCHAR ptr, int type, PCRE_PUCHAR startptr, int *lenptr,
  119. BOOL utf)
  120. {
  121. pcre_uint32 c;
  122. (void)utf;
  123. ptr--;
  124. #ifdef SUPPORT_UTF
  125. if (utf)
  126. {
  127. BACKCHAR(ptr);
  128. GETCHAR(c, ptr);
  129. }
  130. else
  131. #endif /* SUPPORT_UTF */
  132. c = *ptr;
  133. /* Note that this function is called only for ANY or ANYCRLF. */
  134. if (type == NLTYPE_ANYCRLF) switch(c)
  135. {
  136. case CHAR_LF:
  137. *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
  138. return TRUE;
  139. case CHAR_CR: *lenptr = 1; return TRUE;
  140. default: return FALSE;
  141. }
  142. /* NLTYPE_ANY */
  143. else switch(c)
  144. {
  145. case CHAR_LF:
  146. *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
  147. return TRUE;
  148. #ifdef EBCDIC
  149. case CHAR_NEL:
  150. #endif
  151. case CHAR_VT:
  152. case CHAR_FF:
  153. case CHAR_CR: *lenptr = 1; return TRUE;
  154. #ifndef EBCDIC
  155. #ifdef COMPILE_PCRE8
  156. case CHAR_NEL: *lenptr = utf? 2 : 1; return TRUE;
  157. case 0x2028: /* LS */
  158. case 0x2029: *lenptr = 3; return TRUE; /* PS */
  159. #else /* COMPILE_PCRE16 || COMPILE_PCRE32 */
  160. case CHAR_NEL:
  161. case 0x2028: /* LS */
  162. case 0x2029: *lenptr = 1; return TRUE; /* PS */
  163. #endif /* COMPILE_PCRE8 */
  164. #endif /* NotEBCDIC */
  165. default: return FALSE;
  166. }
  167. }
  168. /* End of pcre_newline.c */