abseil-cpp/absl/strings/internal/memutil.cc
Go to the documentation of this file.
1 // 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 
19 namespace absl {
21 namespace strings_internal {
22 
23 int 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 
36 char* 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 
43 char* 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 
50 size_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 
55 cont:
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 
63 size_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 
76 char* 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.
89 const 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
112 } // namespace absl
absl::ascii_tolower
char ascii_tolower(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:163
absl::strings_internal::memcasecmp
int memcasecmp(const char *s1, const char *s2, size_t len)
Definition: abseil-cpp/absl/strings/internal/memutil.cc:23
match
unsigned char match[65280+2]
Definition: bloaty/third_party/zlib/examples/gun.c:165
copy
static int copy(grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:145
absl::FormatConversionChar::s
@ s
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::strings_internal::memrchr
char * memrchr(const char *s, int c, size_t slen)
Definition: abseil-cpp/absl/strings/internal/memutil.cc:43
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
absl::FormatConversionChar::e
@ e
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
bm_diff.diff
diff
Definition: bm_diff.py:274
absl::strings_internal::memdup
char * memdup(const char *s, size_t slen)
Definition: abseil-cpp/absl/strings/internal/memutil.cc:36
absl::strings_internal::mempbrk
char * mempbrk(const char *s, size_t slen, const char *accept)
Definition: abseil-cpp/absl/strings/internal/memutil.cc:76
absl::strings_internal::memmatch
const char * memmatch(const char *phaystack, size_t haylen, const char *pneedle, size_t neelen)
Definition: abseil-cpp/absl/strings/internal/memutil.cc:89
absl::strings_internal::memcspn
size_t memcspn(const char *s, size_t slen, const char *reject)
Definition: abseil-cpp/absl/strings/internal/memutil.cc:63
reject
bool reject
Definition: abseil-cpp/absl/random/internal/fast_uniform_bits_test.cc:65
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
absl::strings_internal::memspn
size_t memspn(const char *s, size_t slen, const char *accept)
Definition: abseil-cpp/absl/strings/internal/memutil.cc:50
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:24