pcre_string_utils.c 5.3 KB

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