pcre_string_utils.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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-2014 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 comparing and finding the length
  33. of strings for different data item sizes. */
  34. #include "pcre_config.h"
  35. #include "pcre_internal.h"
  36. #ifndef COMPILE_PCRE8
  37. /*************************************************
  38. * Compare string utilities *
  39. *************************************************/
  40. /* The following two functions compares two strings. Basically a strcmp
  41. for non 8 bit characters.
  42. Arguments:
  43. str1 first string
  44. str2 second string
  45. Returns: 0 if both string are equal (like strcmp), 1 otherwise
  46. */
  47. int
  48. PRIV(strcmp_uc_uc)(const pcre_uchar *str1, const pcre_uchar *str2)
  49. {
  50. pcre_uchar c1;
  51. pcre_uchar c2;
  52. while (*str1 != '\0' || *str2 != '\0')
  53. {
  54. c1 = *str1++;
  55. c2 = *str2++;
  56. if (c1 != c2)
  57. return ((c1 > c2) << 1) - 1;
  58. }
  59. /* Both length and characters must be equal. */
  60. return 0;
  61. }
  62. #ifdef COMPILE_PCRE32
  63. int
  64. PRIV(strcmp_uc_uc_utf)(const pcre_uchar *str1, const pcre_uchar *str2)
  65. {
  66. pcre_uchar c1;
  67. pcre_uchar c2;
  68. while (*str1 != '\0' || *str2 != '\0')
  69. {
  70. c1 = UCHAR21INC(str1);
  71. c2 = UCHAR21INC(str2);
  72. if (c1 != c2)
  73. return ((c1 > c2) << 1) - 1;
  74. }
  75. /* Both length and characters must be equal. */
  76. return 0;
  77. }
  78. #endif /* COMPILE_PCRE32 */
  79. int
  80. PRIV(strcmp_uc_c8)(const pcre_uchar *str1, const char *str2)
  81. {
  82. const pcre_uint8 *ustr2 = (pcre_uint8 *)str2;
  83. pcre_uchar c1;
  84. pcre_uchar c2;
  85. while (*str1 != '\0' || *ustr2 != '\0')
  86. {
  87. c1 = *str1++;
  88. c2 = (pcre_uchar)*ustr2++;
  89. if (c1 != c2)
  90. return ((c1 > c2) << 1) - 1;
  91. }
  92. /* Both length and characters must be equal. */
  93. return 0;
  94. }
  95. #ifdef COMPILE_PCRE32
  96. int
  97. PRIV(strcmp_uc_c8_utf)(const pcre_uchar *str1, const char *str2)
  98. {
  99. const pcre_uint8 *ustr2 = (pcre_uint8 *)str2;
  100. pcre_uchar c1;
  101. pcre_uchar c2;
  102. while (*str1 != '\0' || *ustr2 != '\0')
  103. {
  104. c1 = UCHAR21INC(str1);
  105. c2 = (pcre_uchar)*ustr2++;
  106. if (c1 != c2)
  107. return ((c1 > c2) << 1) - 1;
  108. }
  109. /* Both length and characters must be equal. */
  110. return 0;
  111. }
  112. #endif /* COMPILE_PCRE32 */
  113. /* The following two functions compares two, fixed length
  114. strings. Basically an strncmp for non 8 bit characters.
  115. Arguments:
  116. str1 first string
  117. str2 second string
  118. num size of the string
  119. Returns: 0 if both string are equal (like strcmp), 1 otherwise
  120. */
  121. int
  122. PRIV(strncmp_uc_uc)(const pcre_uchar *str1, const pcre_uchar *str2, unsigned int num)
  123. {
  124. pcre_uchar c1;
  125. pcre_uchar c2;
  126. while (num-- > 0)
  127. {
  128. c1 = *str1++;
  129. c2 = *str2++;
  130. if (c1 != c2)
  131. return ((c1 > c2) << 1) - 1;
  132. }
  133. /* Both length and characters must be equal. */
  134. return 0;
  135. }
  136. int
  137. PRIV(strncmp_uc_c8)(const pcre_uchar *str1, const char *str2, unsigned int num)
  138. {
  139. const pcre_uint8 *ustr2 = (pcre_uint8 *)str2;
  140. pcre_uchar c1;
  141. pcre_uchar c2;
  142. while (num-- > 0)
  143. {
  144. c1 = *str1++;
  145. c2 = (pcre_uchar)*ustr2++;
  146. if (c1 != c2)
  147. return ((c1 > c2) << 1) - 1;
  148. }
  149. /* Both length and characters must be equal. */
  150. return 0;
  151. }
  152. /* The following function returns with the length of
  153. a zero terminated string. Basically an strlen for non 8 bit characters.
  154. Arguments:
  155. str string
  156. Returns: length of the string
  157. */
  158. unsigned int
  159. PRIV(strlen_uc)(const pcre_uchar *str)
  160. {
  161. unsigned int len = 0;
  162. while (*str++ != 0)
  163. len++;
  164. return len;
  165. }
  166. #endif /* !COMPILE_PCRE8 */
  167. /* End of pcre_string_utils.c */