blob: 9ad05358086c840501c31fcc2fd77ebdb54cf88e [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001//
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
71namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080072ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070073namespace strings_internal {
74
75inline 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
80int memcasecmp(const char* s1, const char* s2, size_t len);
81char* memdup(const char* s, size_t slen);
82char* memrchr(const char* s, int c, size_t slen);
83size_t memspn(const char* s, size_t slen, const char* accept);
84size_t memcspn(const char* s, size_t slen, const char* reject);
85char* mempbrk(const char* s, size_t slen, const char* accept);
86
87// This is for internal use only. Don't call this directly
88template <bool case_sensitive>
89const 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
119inline 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
124inline 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
129inline 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
134inline 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.
141const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle,
142 size_t neelen);
143
144} // namespace strings_internal
Austin Schuhb4691e92020-12-31 12:37:18 -0800145ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700146} // namespace absl
147
148#endif // ABSL_STRINGS_INTERNAL_MEMUTIL_H_