memutil.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/internal/memutil.h"
00016 
00017 #include <cstdlib>
00018 
00019 namespace absl {
00020 namespace strings_internal {
00021 
00022 int memcasecmp(const char* s1, const char* s2, size_t len) {
00023   const unsigned char* us1 = reinterpret_cast<const unsigned char*>(s1);
00024   const unsigned char* us2 = reinterpret_cast<const unsigned char*>(s2);
00025 
00026   for (size_t i = 0; i < len; i++) {
00027     const int diff =
00028         int{static_cast<unsigned char>(absl::ascii_tolower(us1[i]))} -
00029         int{static_cast<unsigned char>(absl::ascii_tolower(us2[i]))};
00030     if (diff != 0) return diff;
00031   }
00032   return 0;
00033 }
00034 
00035 char* memdup(const char* s, size_t slen) {
00036   void* copy;
00037   if ((copy = malloc(slen)) == nullptr) return nullptr;
00038   memcpy(copy, s, slen);
00039   return reinterpret_cast<char*>(copy);
00040 }
00041 
00042 char* memrchr(const char* s, int c, size_t slen) {
00043   for (const char* e = s + slen - 1; e >= s; e--) {
00044     if (*e == c) return const_cast<char*>(e);
00045   }
00046   return nullptr;
00047 }
00048 
00049 size_t memspn(const char* s, size_t slen, const char* accept) {
00050   const char* p = s;
00051   const char* spanp;
00052   char c, sc;
00053 
00054 cont:
00055   c = *p++;
00056   if (slen-- == 0) return p - 1 - s;
00057   for (spanp = accept; (sc = *spanp++) != '\0';)
00058     if (sc == c) goto cont;
00059   return p - 1 - s;
00060 }
00061 
00062 size_t memcspn(const char* s, size_t slen, const char* reject) {
00063   const char* p = s;
00064   const char* spanp;
00065   char c, sc;
00066 
00067   while (slen-- != 0) {
00068     c = *p++;
00069     for (spanp = reject; (sc = *spanp++) != '\0';)
00070       if (sc == c) return p - 1 - s;
00071   }
00072   return p - s;
00073 }
00074 
00075 char* mempbrk(const char* s, size_t slen, const char* accept) {
00076   const char* scanp;
00077   int sc;
00078 
00079   for (; slen; ++s, --slen) {
00080     for (scanp = accept; (sc = *scanp++) != '\0';)
00081       if (sc == *s) return const_cast<char*>(s);
00082   }
00083   return nullptr;
00084 }
00085 
00086 // This is significantly faster for case-sensitive matches with very
00087 // few possible matches.  See unit test for benchmarks.
00088 const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle,
00089                      size_t neelen) {
00090   if (0 == neelen) {
00091     return phaystack;  // even if haylen is 0
00092   }
00093   if (haylen < neelen) return nullptr;
00094 
00095   const char* match;
00096   const char* hayend = phaystack + haylen - neelen + 1;
00097   // A static cast is used here to work around the fact that memchr returns
00098   // a void* on Posix-compliant systems and const void* on Windows.
00099   while ((match = static_cast<const char*>(
00100               memchr(phaystack, pneedle[0], hayend - phaystack)))) {
00101     if (memcmp(match, pneedle, neelen) == 0)
00102       return match;
00103     else
00104       phaystack = match + 1;
00105   }
00106   return nullptr;
00107 }
00108 
00109 }  // namespace strings_internal
00110 }  // namespace absl


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