abseil-cpp/absl/strings/internal/memutil.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 // These routines provide mem versions of standard C string routines,
18 // such as strpbrk. They function exactly the same as the str versions,
19 // so if you wonder what they are, replace the word "mem" by
20 // "str" and check out the man page. I could return void*, as the
21 // strutil.h mem*() routines tend to do, but I return char* instead
22 // since this is by far the most common way these functions are called.
23 //
24 // The difference between the mem and str versions is the mem version
25 // takes a pointer and a length, rather than a '\0'-terminated string.
26 // The memcase* routines defined here assume the locale is "C"
27 // (they use absl::ascii_tolower instead of tolower).
28 //
29 // These routines are based on the BSD library.
30 //
31 // Here's a list of routines from string.h, and their mem analogues.
32 // Functions in lowercase are defined in string.h; those in UPPERCASE
33 // are defined here:
34 //
35 // strlen --
36 // strcat strncat MEMCAT
37 // strcpy strncpy memcpy
38 // -- memccpy (very cool function, btw)
39 // -- memmove
40 // -- memset
41 // strcmp strncmp memcmp
42 // strcasecmp strncasecmp MEMCASECMP
43 // strchr memchr
44 // strcoll --
45 // strxfrm --
46 // strdup strndup MEMDUP
47 // strrchr MEMRCHR
48 // strspn MEMSPN
49 // strcspn MEMCSPN
50 // strpbrk MEMPBRK
51 // strstr MEMSTR MEMMEM
52 // (g)strcasestr MEMCASESTR MEMCASEMEM
53 // strtok --
54 // strprefix MEMPREFIX (strprefix is from strutil.h)
55 // strcaseprefix MEMCASEPREFIX (strcaseprefix is from strutil.h)
56 // strsuffix MEMSUFFIX (strsuffix is from strutil.h)
57 // strcasesuffix MEMCASESUFFIX (strcasesuffix is from strutil.h)
58 // -- MEMIS
59 // -- MEMCASEIS
60 // strcount MEMCOUNT (strcount is from strutil.h)
61 
62 #ifndef ABSL_STRINGS_INTERNAL_MEMUTIL_H_
63 #define ABSL_STRINGS_INTERNAL_MEMUTIL_H_
64 
65 #include <cstddef>
66 #include <cstring>
67 
68 #include "absl/base/port.h" // disable some warnings on Windows
69 #include "absl/strings/ascii.h" // for absl::ascii_tolower
70 
71 namespace absl {
73 namespace strings_internal {
74 
75 inline char* memcat(char* dest, size_t destlen, const char* src,
76  size_t srclen) {
77  return reinterpret_cast<char*>(memcpy(dest + destlen, src, srclen));
78 }
79 
80 int memcasecmp(const char* s1, const char* s2, size_t len);
81 char* memdup(const char* s, size_t slen);
82 char* memrchr(const char* s, int c, size_t slen);
83 size_t memspn(const char* s, size_t slen, const char* accept);
84 size_t memcspn(const char* s, size_t slen, const char* reject);
85 char* mempbrk(const char* s, size_t slen, const char* accept);
86 
87 // This is for internal use only. Don't call this directly
88 template <bool case_sensitive>
89 const char* int_memmatch(const char* haystack, size_t haylen,
90  const char* needle, size_t neelen) {
91  if (0 == neelen) {
92  return haystack; // even if haylen is 0
93  }
94  const char* hayend = haystack + haylen;
95  const char* needlestart = needle;
96  const char* needleend = needlestart + neelen;
97 
98  for (; haystack < hayend; ++haystack) {
99  char hay = case_sensitive
100  ? *haystack
101  : absl::ascii_tolower(static_cast<unsigned char>(*haystack));
102  char nee = case_sensitive
103  ? *needle
104  : absl::ascii_tolower(static_cast<unsigned char>(*needle));
105  if (hay == nee) {
106  if (++needle == needleend) {
107  return haystack + 1 - neelen;
108  }
109  } else if (needle != needlestart) {
110  // must back up haystack in case a prefix matched (find "aab" in "aaab")
111  haystack -= needle - needlestart; // for loop will advance one more
112  needle = needlestart;
113  }
114  }
115  return nullptr;
116 }
117 
118 // These are the guys you can call directly
119 inline const char* memstr(const char* phaystack, size_t haylen,
120  const char* pneedle) {
121  return int_memmatch<true>(phaystack, haylen, pneedle, strlen(pneedle));
122 }
123 
124 inline const char* memcasestr(const char* phaystack, size_t haylen,
125  const char* pneedle) {
126  return int_memmatch<false>(phaystack, haylen, pneedle, strlen(pneedle));
127 }
128 
129 inline const char* memmem(const char* phaystack, size_t haylen,
130  const char* pneedle, size_t needlelen) {
131  return int_memmatch<true>(phaystack, haylen, pneedle, needlelen);
132 }
133 
134 inline const char* memcasemem(const char* phaystack, size_t haylen,
135  const char* pneedle, size_t needlelen) {
136  return int_memmatch<false>(phaystack, haylen, pneedle, needlelen);
137 }
138 
139 // This is significantly faster for case-sensitive matches with very
140 // few possible matches. See unit test for benchmarks.
141 const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle,
142  size_t neelen);
143 
144 } // namespace strings_internal
146 } // namespace absl
147 
148 #endif // ABSL_STRINGS_INTERNAL_MEMUTIL_H_
absl::ascii_tolower
char ascii_tolower(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:163
absl::strings_internal::memmem
const char * memmem(const char *phaystack, size_t haylen, const char *pneedle, size_t needlelen)
Definition: abseil-cpp/absl/strings/internal/memutil.h:129
absl::strings_internal::memcasecmp
int memcasecmp(const char *s1, const char *s2, size_t len)
Definition: abseil-cpp/absl/strings/internal/memutil.cc:23
absl::strings_internal::memcasestr
const char * memcasestr(const char *phaystack, size_t haylen, const char *pneedle)
Definition: abseil-cpp/absl/strings/internal/memutil.h:124
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
absl::strings_internal::memcasemem
const char * memcasemem(const char *phaystack, size_t haylen, const char *pneedle, size_t needlelen)
Definition: abseil-cpp/absl/strings/internal/memutil.h:134
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::strings_internal::int_memmatch
const char * int_memmatch(const char *haystack, size_t haylen, const char *needle, size_t neelen)
Definition: abseil-cpp/absl/strings/internal/memutil.h:89
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
tests.qps.qps_worker.dest
dest
Definition: qps_worker.py:45
absl::strings_internal::memstr
const char * memstr(const char *phaystack, size_t haylen, const char *pneedle)
Definition: abseil-cpp/absl/strings/internal/memutil.h:119
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::strings_internal::memcat
char * memcat(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: abseil-cpp/absl/strings/internal/memutil.h:75
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


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