/source/lib_pcre/pcre/pcre_newline.c

https://github.com/fincs/AutoHotkey_L · C · 174 lines · 76 code · 22 blank · 76 comment · 22 complexity · c662f19d89bec5156aedef7ec6ed8cd4 MD5 · raw file

  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-2009 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 "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. utf8 TRUE if in utf8 mode
  54. Returns: TRUE or FALSE
  55. */
  56. BOOL
  57. _pcre_is_newline(USPTR ptr, int type, USPTR endptr, int *lenptr, BOOL utf8)
  58. {
  59. int c;
  60. #ifdef SUPPORT_UTF8 /* AutoHotkey. */
  61. if (utf8) { GETCHAR(c, ptr); } else c = *ptr;
  62. #else
  63. c = *ptr; /* AutoHotkey. */
  64. #endif /* AutoHotkey. */
  65. if (type == NLTYPE_ANYCRLF) switch(c)
  66. {
  67. case 0x000a: *lenptr = 1; return TRUE; /* LF */
  68. case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
  69. return TRUE; /* CR */
  70. default: return FALSE;
  71. }
  72. /* NLTYPE_ANY */
  73. else switch(c)
  74. {
  75. case 0x000a: /* LF */
  76. case 0x000b: /* VT */
  77. case 0x000c: *lenptr = 1; return TRUE; /* FF */
  78. case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
  79. return TRUE; /* CR */
  80. #ifdef SUPPORT_UTF8 /* AutoHotkey. */
  81. case 0x0085: *lenptr = utf8? 2 : 1; return TRUE; /* NEL */
  82. #else
  83. case 0x0085: *lenptr = 1; return TRUE; /* NEL */ /* AutoHotkey. */
  84. #endif /* AutoHotkey. */
  85. case 0x2028: /* LS */
  86. case 0x2029: *lenptr = 3; return TRUE; /* PS */
  87. default: return FALSE;
  88. }
  89. }
  90. /*************************************************
  91. * Check for newline at previous position *
  92. *************************************************/
  93. /* It is guaranteed that the initial value of ptr is greater than the start of
  94. the string that is being processed.
  95. Arguments:
  96. ptr pointer to possible newline
  97. type the newline type
  98. startptr pointer to the start of the string
  99. lenptr where to return the length
  100. utf8 TRUE if in utf8 mode
  101. Returns: TRUE or FALSE
  102. */
  103. BOOL
  104. _pcre_was_newline(USPTR ptr, int type, USPTR startptr, int *lenptr, BOOL utf8)
  105. {
  106. int c;
  107. ptr--;
  108. #ifdef SUPPORT_UTF8
  109. if (utf8)
  110. {
  111. BACKCHAR(ptr);
  112. GETCHAR(c, ptr);
  113. }
  114. else c = *ptr;
  115. #else /* no UTF-8 support */
  116. c = *ptr;
  117. #endif /* SUPPORT_UTF8 */
  118. if (type == NLTYPE_ANYCRLF) switch(c)
  119. {
  120. case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
  121. return TRUE; /* LF */
  122. case 0x000d: *lenptr = 1; return TRUE; /* CR */
  123. default: return FALSE;
  124. }
  125. else switch(c)
  126. {
  127. case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
  128. return TRUE; /* LF */
  129. case 0x000b: /* VT */
  130. case 0x000c: /* FF */
  131. case 0x000d: *lenptr = 1; return TRUE; /* CR */
  132. #ifdef SUPPORT_UTF8 /* AutoHotkey. */
  133. case 0x0085: *lenptr = utf8? 2 : 1; return TRUE; /* NEL */
  134. #else
  135. case 0x0085: *lenptr = 1; return TRUE; /* NEL */ /* AutoHotkey. */
  136. #endif /* AutoHotkey. */
  137. case 0x2028: /* LS */
  138. case 0x2029: *lenptr = 3; return TRUE; /* PS */
  139. default: return FALSE;
  140. }
  141. }
  142. /* End of pcre_newline.c */