xref: /aosp_15_r20/external/abseil-cpp/absl/strings/internal/memutil.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #include "absl/strings/internal/memutil.h"
16*9356374aSAndroid Build Coastguard Worker 
17*9356374aSAndroid Build Coastguard Worker #include <cstdlib>
18*9356374aSAndroid Build Coastguard Worker 
19*9356374aSAndroid Build Coastguard Worker #include "absl/strings/ascii.h"
20*9356374aSAndroid Build Coastguard Worker 
21*9356374aSAndroid Build Coastguard Worker namespace absl {
22*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
23*9356374aSAndroid Build Coastguard Worker namespace strings_internal {
24*9356374aSAndroid Build Coastguard Worker 
memcasecmp(const char * s1,const char * s2,size_t len)25*9356374aSAndroid Build Coastguard Worker int memcasecmp(const char* s1, const char* s2, size_t len) {
26*9356374aSAndroid Build Coastguard Worker   const unsigned char* us1 = reinterpret_cast<const unsigned char*>(s1);
27*9356374aSAndroid Build Coastguard Worker   const unsigned char* us2 = reinterpret_cast<const unsigned char*>(s2);
28*9356374aSAndroid Build Coastguard Worker 
29*9356374aSAndroid Build Coastguard Worker   for (size_t i = 0; i < len; i++) {
30*9356374aSAndroid Build Coastguard Worker     unsigned char c1 = us1[i];
31*9356374aSAndroid Build Coastguard Worker     unsigned char c2 = us2[i];
32*9356374aSAndroid Build Coastguard Worker     // If bytes are the same, they will be the same when converted to lower.
33*9356374aSAndroid Build Coastguard Worker     // So we only need to convert if bytes are not equal.
34*9356374aSAndroid Build Coastguard Worker     // NOTE(b/308193381): We do not use `absl::ascii_tolower` here in order
35*9356374aSAndroid Build Coastguard Worker     // to avoid its lookup table and improve performance.
36*9356374aSAndroid Build Coastguard Worker     if (c1 != c2) {
37*9356374aSAndroid Build Coastguard Worker       c1 = c1 >= 'A' && c1 <= 'Z' ? c1 - 'A' + 'a' : c1;
38*9356374aSAndroid Build Coastguard Worker       c2 = c2 >= 'A' && c2 <= 'Z' ? c2 - 'A' + 'a' : c2;
39*9356374aSAndroid Build Coastguard Worker       const int diff = int{c1} - int{c2};
40*9356374aSAndroid Build Coastguard Worker       if (diff != 0) return diff;
41*9356374aSAndroid Build Coastguard Worker     }
42*9356374aSAndroid Build Coastguard Worker   }
43*9356374aSAndroid Build Coastguard Worker   return 0;
44*9356374aSAndroid Build Coastguard Worker }
45*9356374aSAndroid Build Coastguard Worker 
46*9356374aSAndroid Build Coastguard Worker }  // namespace strings_internal
47*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
48*9356374aSAndroid Build Coastguard Worker }  // namespace absl
49