memutil.h
Go to the documentation of this file.
00001 //
00002 // Copyright 2017 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 //
00016 
00017 // These routines provide mem versions of standard C string routines,
00018 // such as strpbrk.  They function exactly the same as the str versions,
00019 // so if you wonder what they are, replace the word "mem" by
00020 // "str" and check out the man page.  I could return void*, as the
00021 // strutil.h mem*() routines tend to do, but I return char* instead
00022 // since this is by far the most common way these functions are called.
00023 //
00024 // The difference between the mem and str versions is the mem version
00025 // takes a pointer and a length, rather than a '\0'-terminated string.
00026 // The memcase* routines defined here assume the locale is "C"
00027 // (they use absl::ascii_tolower instead of tolower).
00028 //
00029 // These routines are based on the BSD library.
00030 //
00031 // Here's a list of routines from string.h, and their mem analogues.
00032 // Functions in lowercase are defined in string.h; those in UPPERCASE
00033 // are defined here:
00034 //
00035 // strlen                  --
00036 // strcat strncat          MEMCAT
00037 // strcpy strncpy          memcpy
00038 // --                      memccpy   (very cool function, btw)
00039 // --                      memmove
00040 // --                      memset
00041 // strcmp strncmp          memcmp
00042 // strcasecmp strncasecmp  MEMCASECMP
00043 // strchr                  memchr
00044 // strcoll                 --
00045 // strxfrm                 --
00046 // strdup strndup          MEMDUP
00047 // strrchr                 MEMRCHR
00048 // strspn                  MEMSPN
00049 // strcspn                 MEMCSPN
00050 // strpbrk                 MEMPBRK
00051 // strstr                  MEMSTR MEMMEM
00052 // (g)strcasestr           MEMCASESTR MEMCASEMEM
00053 // strtok                  --
00054 // strprefix               MEMPREFIX      (strprefix is from strutil.h)
00055 // strcaseprefix           MEMCASEPREFIX  (strcaseprefix is from strutil.h)
00056 // strsuffix               MEMSUFFIX      (strsuffix is from strutil.h)
00057 // strcasesuffix           MEMCASESUFFIX  (strcasesuffix is from strutil.h)
00058 // --                      MEMIS
00059 // --                      MEMCASEIS
00060 // strcount                MEMCOUNT       (strcount is from strutil.h)
00061 
00062 #ifndef ABSL_STRINGS_INTERNAL_MEMUTIL_H_
00063 #define ABSL_STRINGS_INTERNAL_MEMUTIL_H_
00064 
00065 #include <cstddef>
00066 #include <cstring>
00067 
00068 #include "absl/base/port.h"  // disable some warnings on Windows
00069 #include "absl/strings/ascii.h"  // for absl::ascii_tolower
00070 
00071 namespace absl {
00072 namespace strings_internal {
00073 
00074 inline char* memcat(char* dest, size_t destlen, const char* src,
00075                     size_t srclen) {
00076   return reinterpret_cast<char*>(memcpy(dest + destlen, src, srclen));
00077 }
00078 
00079 int memcasecmp(const char* s1, const char* s2, size_t len);
00080 char* memdup(const char* s, size_t slen);
00081 char* memrchr(const char* s, int c, size_t slen);
00082 size_t memspn(const char* s, size_t slen, const char* accept);
00083 size_t memcspn(const char* s, size_t slen, const char* reject);
00084 char* mempbrk(const char* s, size_t slen, const char* accept);
00085 
00086 // This is for internal use only.  Don't call this directly
00087 template <bool case_sensitive>
00088 const char* int_memmatch(const char* haystack, size_t haylen,
00089                          const char* needle, size_t neelen) {
00090   if (0 == neelen) {
00091     return haystack;  // even if haylen is 0
00092   }
00093   const char* hayend = haystack + haylen;
00094   const char* needlestart = needle;
00095   const char* needleend = needlestart + neelen;
00096 
00097   for (; haystack < hayend; ++haystack) {
00098     char hay = case_sensitive
00099                    ? *haystack
00100                    : absl::ascii_tolower(static_cast<unsigned char>(*haystack));
00101     char nee = case_sensitive
00102                    ? *needle
00103                    : absl::ascii_tolower(static_cast<unsigned char>(*needle));
00104     if (hay == nee) {
00105       if (++needle == needleend) {
00106         return haystack + 1 - neelen;
00107       }
00108     } else if (needle != needlestart) {
00109       // must back up haystack in case a prefix matched (find "aab" in "aaab")
00110       haystack -= needle - needlestart;  // for loop will advance one more
00111       needle = needlestart;
00112     }
00113   }
00114   return nullptr;
00115 }
00116 
00117 // These are the guys you can call directly
00118 inline const char* memstr(const char* phaystack, size_t haylen,
00119                           const char* pneedle) {
00120   return int_memmatch<true>(phaystack, haylen, pneedle, strlen(pneedle));
00121 }
00122 
00123 inline const char* memcasestr(const char* phaystack, size_t haylen,
00124                               const char* pneedle) {
00125   return int_memmatch<false>(phaystack, haylen, pneedle, strlen(pneedle));
00126 }
00127 
00128 inline const char* memmem(const char* phaystack, size_t haylen,
00129                           const char* pneedle, size_t needlelen) {
00130   return int_memmatch<true>(phaystack, haylen, pneedle, needlelen);
00131 }
00132 
00133 inline const char* memcasemem(const char* phaystack, size_t haylen,
00134                               const char* pneedle, size_t needlelen) {
00135   return int_memmatch<false>(phaystack, haylen, pneedle, needlelen);
00136 }
00137 
00138 // This is significantly faster for case-sensitive matches with very
00139 // few possible matches.  See unit test for benchmarks.
00140 const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle,
00141                      size_t neelen);
00142 
00143 }  // namespace strings_internal
00144 }  // namespace absl
00145 
00146 #endif  // ABSL_STRINGS_INTERNAL_MEMUTIL_H_


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