diff options
author | Derek Mauro <dmauro@google.com> | 2023-07-11 10:57:18 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-07-11 10:58:27 -0700 |
commit | b2a6c1bca7e177cf033ebe2361e8f2d99fabf9a9 (patch) | |
tree | af39af0b6837d73ec9dc3f0d9df2f729807ec903 /absl/strings/string_view.cc | |
parent | 20cf119df47eb7d1d9e7813d15d01f2ea7dc9bc3 (diff) |
Cleanup `//absl/strings/internal/memutil.h`
`memmatch()` is only used in `string_view.cc`, so move it there.
The moving of `memmatch()` to `string_view.cc` decouples `string_view`
from `memutil`, which will allow us to move `string_view` into its
own target in a followup.
The only other function that is used is `memcasecmp()`, so delete
all other functions.
PiperOrigin-RevId: 547238386
Change-Id: Id6fad47dd24191c8e8f26dd923fffa1007c8db4a
Diffstat (limited to 'absl/strings/string_view.cc')
-rw-r--r-- | absl/strings/string_view.cc | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/absl/strings/string_view.cc b/absl/strings/string_view.cc index e2261625..f20ff530 100644 --- a/absl/strings/string_view.cc +++ b/absl/strings/string_view.cc @@ -21,12 +21,35 @@ #include <cstring> #include <ostream> -#include "absl/strings/internal/memutil.h" - namespace absl { ABSL_NAMESPACE_BEGIN namespace { + +// This is significantly faster for case-sensitive matches with very +// few possible matches. +const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle, + size_t neelen) { + if (0 == neelen) { + return phaystack; // even if haylen is 0 + } + if (haylen < neelen) return nullptr; + + const char* match; + const char* hayend = phaystack + haylen - neelen + 1; + // A static cast is used here to work around the fact that memchr returns + // a void* on Posix-compliant systems and const void* on Windows. + while ( + (match = static_cast<const char*>(memchr( + phaystack, pneedle[0], static_cast<size_t>(hayend - phaystack))))) { + if (memcmp(match, pneedle, neelen) == 0) + return match; + else + phaystack = match + 1; + } + return nullptr; +} + void WritePadding(std::ostream& o, size_t pad) { char fill_buf[32]; memset(fill_buf, o.fill(), sizeof(fill_buf)); @@ -84,8 +107,7 @@ string_view::size_type string_view::find(string_view s, if (empty() && pos == 0 && s.empty()) return 0; return npos; } - const char* result = - strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_); + const char* result = memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_); return result ? static_cast<size_type>(result - ptr_) : npos; } |