blob: 2519c6881e3563000968afc8c22948f86117b234 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2017 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "absl/strings/internal/memutil.h"
16
17#include <cstdlib>
18
19namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080020ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070021namespace strings_internal {
22
23int memcasecmp(const char* s1, const char* s2, size_t len) {
24 const unsigned char* us1 = reinterpret_cast<const unsigned char*>(s1);
25 const unsigned char* us2 = reinterpret_cast<const unsigned char*>(s2);
26
27 for (size_t i = 0; i < len; i++) {
28 const int diff =
29 int{static_cast<unsigned char>(absl::ascii_tolower(us1[i]))} -
30 int{static_cast<unsigned char>(absl::ascii_tolower(us2[i]))};
31 if (diff != 0) return diff;
32 }
33 return 0;
34}
35
36char* memdup(const char* s, size_t slen) {
37 void* copy;
38 if ((copy = malloc(slen)) == nullptr) return nullptr;
39 memcpy(copy, s, slen);
40 return reinterpret_cast<char*>(copy);
41}
42
43char* memrchr(const char* s, int c, size_t slen) {
44 for (const char* e = s + slen - 1; e >= s; e--) {
45 if (*e == c) return const_cast<char*>(e);
46 }
47 return nullptr;
48}
49
50size_t memspn(const char* s, size_t slen, const char* accept) {
51 const char* p = s;
52 const char* spanp;
53 char c, sc;
54
55cont:
56 c = *p++;
57 if (slen-- == 0) return p - 1 - s;
58 for (spanp = accept; (sc = *spanp++) != '\0';)
59 if (sc == c) goto cont;
60 return p - 1 - s;
61}
62
63size_t memcspn(const char* s, size_t slen, const char* reject) {
64 const char* p = s;
65 const char* spanp;
66 char c, sc;
67
68 while (slen-- != 0) {
69 c = *p++;
70 for (spanp = reject; (sc = *spanp++) != '\0';)
71 if (sc == c) return p - 1 - s;
72 }
73 return p - s;
74}
75
76char* mempbrk(const char* s, size_t slen, const char* accept) {
77 const char* scanp;
78 int sc;
79
80 for (; slen; ++s, --slen) {
81 for (scanp = accept; (sc = *scanp++) != '\0';)
82 if (sc == *s) return const_cast<char*>(s);
83 }
84 return nullptr;
85}
86
87// This is significantly faster for case-sensitive matches with very
88// few possible matches. See unit test for benchmarks.
89const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle,
90 size_t neelen) {
91 if (0 == neelen) {
92 return phaystack; // even if haylen is 0
93 }
94 if (haylen < neelen) return nullptr;
95
96 const char* match;
97 const char* hayend = phaystack + haylen - neelen + 1;
98 // A static cast is used here to work around the fact that memchr returns
99 // a void* on Posix-compliant systems and const void* on Windows.
100 while ((match = static_cast<const char*>(
101 memchr(phaystack, pneedle[0], hayend - phaystack)))) {
102 if (memcmp(match, pneedle, neelen) == 0)
103 return match;
104 else
105 phaystack = match + 1;
106 }
107 return nullptr;
108}
109
110} // namespace strings_internal
Austin Schuhb4691e92020-12-31 12:37:18 -0800111ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700112} // namespace absl