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 
16 
17 #include <cstdlib>
18 
19 namespace absl {
20 namespace strings_internal {
21 
22 int memcasecmp(const char* s1, const char* s2, size_t len) {
23  const unsigned char* us1 = reinterpret_cast<const unsigned char*>(s1);
24  const unsigned char* us2 = reinterpret_cast<const unsigned char*>(s2);
25 
26  for (size_t i = 0; i < len; i++) {
27  const int diff =
28  int{static_cast<unsigned char>(absl::ascii_tolower(us1[i]))} -
29  int{static_cast<unsigned char>(absl::ascii_tolower(us2[i]))};
30  if (diff != 0) return diff;
31  }
32  return 0;
33 }
34 
35 char* memdup(const char* s, size_t slen) {
36  void* copy;
37  if ((copy = malloc(slen)) == nullptr) return nullptr;
38  memcpy(copy, s, slen);
39  return reinterpret_cast<char*>(copy);
40 }
41 
42 char* memrchr(const char* s, int c, size_t slen) {
43  for (const char* e = s + slen - 1; e >= s; e--) {
44  if (*e == c) return const_cast<char*>(e);
45  }
46  return nullptr;
47 }
48 
49 size_t memspn(const char* s, size_t slen, const char* accept) {
50  const char* p = s;
51  const char* spanp;
52  char c, sc;
53 
54 cont:
55  c = *p++;
56  if (slen-- == 0) return p - 1 - s;
57  for (spanp = accept; (sc = *spanp++) != '\0';)
58  if (sc == c) goto cont;
59  return p - 1 - s;
60 }
61 
62 size_t memcspn(const char* s, size_t slen, const char* reject) {
63  const char* p = s;
64  const char* spanp;
65  char c, sc;
66 
67  while (slen-- != 0) {
68  c = *p++;
69  for (spanp = reject; (sc = *spanp++) != '\0';)
70  if (sc == c) return p - 1 - s;
71  }
72  return p - s;
73 }
74 
75 char* mempbrk(const char* s, size_t slen, const char* accept) {
76  const char* scanp;
77  int sc;
78 
79  for (; slen; ++s, --slen) {
80  for (scanp = accept; (sc = *scanp++) != '\0';)
81  if (sc == *s) return const_cast<char*>(s);
82  }
83  return nullptr;
84 }
85 
86 // This is significantly faster for case-sensitive matches with very
87 // few possible matches. See unit test for benchmarks.
88 const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle,
89  size_t neelen) {
90  if (0 == neelen) {
91  return phaystack; // even if haylen is 0
92  }
93  if (haylen < neelen) return nullptr;
94 
95  const char* match;
96  const char* hayend = phaystack + haylen - neelen + 1;
97  // A static cast is used here to work around the fact that memchr returns
98  // a void* on Posix-compliant systems and const void* on Windows.
99  while ((match = static_cast<const char*>(
100  memchr(phaystack, pneedle[0], hayend - phaystack)))) {
101  if (memcmp(match, pneedle, neelen) == 0)
102  return match;
103  else
104  phaystack = match + 1;
105  }
106  return nullptr;
107 }
108 
109 } // namespace strings_internal
110 } // namespace absl
size_t memspn(const char *s, size_t slen, const char *accept)
Definition: memutil.cc:49
Definition: algorithm.h:29
char * memrchr(const char *s, int c, size_t slen)
Definition: memutil.cc:42
char * memdup(const char *s, size_t slen)
Definition: memutil.cc:35
char * mempbrk(const char *s, size_t slen, const char *accept)
Definition: memutil.cc:75
size_t memcspn(const char *s, size_t slen, const char *reject)
Definition: memutil.cc:62
const char * memmatch(const char *phaystack, size_t haylen, const char *pneedle, size_t neelen)
Definition: memutil.cc:88
char ascii_tolower(unsigned char c)
Definition: ascii.h:161
int memcasecmp(const char *s1, const char *s2, size_t len)
Definition: memutil.cc:22


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:57