Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00087
00088 const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle,
00089 size_t neelen) {
00090 if (0 == neelen) {
00091 return phaystack;
00092 }
00093 if (haylen < neelen) return nullptr;
00094
00095 const char* match;
00096 const char* hayend = phaystack + haylen - neelen + 1;
00097
00098
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 }
00110 }