/src/libgit2/deps/pcre2/pcre2_string_utils.c
Line | Count | Source |
1 | | /************************************************* |
2 | | * Perl-Compatible Regular Expressions * |
3 | | *************************************************/ |
4 | | |
5 | | /* PCRE is a library of functions to support regular expressions whose syntax |
6 | | and semantics are as close as possible to those of the Perl 5 language. |
7 | | |
8 | | Written by Philip Hazel |
9 | | Original API code Copyright (c) 1997-2012 University of Cambridge |
10 | | New API code Copyright (c) 2018-2021 University of Cambridge |
11 | | |
12 | | ----------------------------------------------------------------------------- |
13 | | Redistribution and use in source and binary forms, with or without |
14 | | modification, are permitted provided that the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the University of Cambridge nor the names of its |
24 | | contributors may be used to endorse or promote products derived from |
25 | | this software without specific prior written permission. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
28 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
29 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
30 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
31 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
32 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
33 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
34 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
35 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
36 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
37 | | POSSIBILITY OF SUCH DAMAGE. |
38 | | ----------------------------------------------------------------------------- |
39 | | */ |
40 | | |
41 | | |
42 | | /* This module contains internal functions for comparing and finding the length |
43 | | of strings. These are used instead of strcmp() etc because the standard |
44 | | functions work only on 8-bit data. */ |
45 | | |
46 | | |
47 | | #include "pcre2_internal.h" |
48 | | |
49 | | |
50 | | |
51 | | /************************************************* |
52 | | * Compare two zero-terminated PCRE2 strings * |
53 | | *************************************************/ |
54 | | |
55 | | /* |
56 | | Arguments: |
57 | | str1 first string |
58 | | str2 second string |
59 | | |
60 | | Returns: 0, 1, or -1 |
61 | | */ |
62 | | |
63 | | int |
64 | | PRIV(strcmp)(PCRE2_SPTR str1, PCRE2_SPTR str2) |
65 | 0 | { |
66 | 0 | PCRE2_UCHAR c1, c2; |
67 | 0 | while (*str1 != '\0' || *str2 != '\0') |
68 | 0 | { |
69 | 0 | c1 = *str1++; |
70 | 0 | c2 = *str2++; |
71 | 0 | if (c1 != c2) return ((c1 > c2) << 1) - 1; |
72 | 0 | } |
73 | 0 | return 0; |
74 | 0 | } |
75 | | |
76 | | |
77 | | /************************************************* |
78 | | * Compare zero-terminated PCRE2 & 8-bit strings * |
79 | | *************************************************/ |
80 | | |
81 | | /* As the 8-bit string is almost always a literal, its type is specified as |
82 | | const char *. |
83 | | |
84 | | Arguments: |
85 | | str1 first string |
86 | | str2 second string |
87 | | |
88 | | Returns: 0, 1, or -1 |
89 | | */ |
90 | | |
91 | | int |
92 | | PRIV(strcmp_c8)(PCRE2_SPTR str1, const char *str2) |
93 | 358k | { |
94 | 358k | PCRE2_UCHAR c1, c2; |
95 | 526k | while (*str1 != '\0' || *str2 != '\0') |
96 | 483k | { |
97 | 483k | c1 = *str1++; |
98 | 483k | c2 = *str2++; |
99 | 483k | if (c1 != c2) return ((c1 > c2) << 1) - 1; |
100 | 483k | } |
101 | 43.0k | return 0; |
102 | 358k | } |
103 | | |
104 | | |
105 | | /************************************************* |
106 | | * Compare two PCRE2 strings, given a length * |
107 | | *************************************************/ |
108 | | |
109 | | /* |
110 | | Arguments: |
111 | | str1 first string |
112 | | str2 second string |
113 | | len the length |
114 | | |
115 | | Returns: 0, 1, or -1 |
116 | | */ |
117 | | |
118 | | int |
119 | | PRIV(strncmp)(PCRE2_SPTR str1, PCRE2_SPTR str2, size_t len) |
120 | 129k | { |
121 | 129k | PCRE2_UCHAR c1, c2; |
122 | 362k | for (; len > 0; len--) |
123 | 249k | { |
124 | 249k | c1 = *str1++; |
125 | 249k | c2 = *str2++; |
126 | 249k | if (c1 != c2) return ((c1 > c2) << 1) - 1; |
127 | 249k | } |
128 | 113k | return 0; |
129 | 129k | } |
130 | | |
131 | | |
132 | | /************************************************* |
133 | | * Compare PCRE2 string to 8-bit string by length * |
134 | | *************************************************/ |
135 | | |
136 | | /* As the 8-bit string is almost always a literal, its type is specified as |
137 | | const char *. |
138 | | |
139 | | Arguments: |
140 | | str1 first string |
141 | | str2 second string |
142 | | len the length |
143 | | |
144 | | Returns: 0, 1, or -1 |
145 | | */ |
146 | | |
147 | | int |
148 | | PRIV(strncmp_c8)(PCRE2_SPTR str1, const char *str2, size_t len) |
149 | 377k | { |
150 | 377k | PCRE2_UCHAR c1, c2; |
151 | 640k | for (; len > 0; len--) |
152 | 598k | { |
153 | 598k | c1 = *str1++; |
154 | 598k | c2 = *str2++; |
155 | 598k | if (c1 != c2) return ((c1 > c2) << 1) - 1; |
156 | 598k | } |
157 | 42.0k | return 0; |
158 | 377k | } |
159 | | |
160 | | |
161 | | /************************************************* |
162 | | * Find the length of a PCRE2 string * |
163 | | *************************************************/ |
164 | | |
165 | | /* |
166 | | Argument: the string |
167 | | Returns: the length |
168 | | */ |
169 | | |
170 | | PCRE2_SIZE |
171 | | PRIV(strlen)(PCRE2_SPTR str) |
172 | 31.1k | { |
173 | 31.1k | PCRE2_SIZE c = 0; |
174 | 51.1M | while (*str++ != 0) c++; |
175 | 31.1k | return c; |
176 | 31.1k | } |
177 | | |
178 | | |
179 | | /************************************************* |
180 | | * Copy 8-bit 0-terminated string to PCRE2 string * |
181 | | *************************************************/ |
182 | | |
183 | | /* Arguments: |
184 | | str1 buffer to receive the string |
185 | | str2 8-bit string to be copied |
186 | | |
187 | | Returns: the number of code units used (excluding trailing zero) |
188 | | */ |
189 | | |
190 | | PCRE2_SIZE |
191 | | PRIV(strcpy_c8)(PCRE2_UCHAR *str1, const char *str2) |
192 | 0 | { |
193 | 0 | PCRE2_UCHAR *t = str1; |
194 | 0 | while (*str2 != 0) *t++ = *str2++; |
195 | 0 | *t = 0; |
196 | 0 | return t - str1; |
197 | 0 | } |
198 | | |
199 | | /* End of pcre2_string_utils.c */ |