X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FRange.h;h=0b63f45770efa5f29ec579b48d23c86d37bdd962;hb=73f2085759f2dc872bf23f90d8bd354445433c01;hp=4a06491684549db077af179fb49270e4223375f8;hpb=8cb615a27594078056b2e4ec2350660e594f5a89;p=folly.git diff --git a/folly/Range.h b/folly/Range.h index 4a064916..0b63f457 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -1085,6 +1085,10 @@ size_t rfind(const Range& haystack, // specialization for StringPiece template <> inline size_t qfind(const Range& haystack, const char& needle) { + // memchr expects a not-null pointer, early return if the range is empty. + if (haystack.empty()) { + return std::string::npos; + } auto pos = static_cast( ::memchr(haystack.data(), needle, haystack.size())); return pos == nullptr ? std::string::npos : pos - haystack.data(); @@ -1092,6 +1096,10 @@ inline size_t qfind(const Range& haystack, const char& needle) { template <> inline size_t rfind(const Range& haystack, const char& needle) { + // memchr expects a not-null pointer, early return if the range is empty. + if (haystack.empty()) { + return std::string::npos; + } auto pos = static_cast( ::memrchr(haystack.data(), needle, haystack.size())); return pos == nullptr ? std::string::npos : pos - haystack.data(); @@ -1101,6 +1109,10 @@ inline size_t rfind(const Range& haystack, const char& needle) { template <> inline size_t qfind(const Range& haystack, const unsigned char& needle) { + // memchr expects a not-null pointer, early return if the range is empty. + if (haystack.empty()) { + return std::string::npos; + } auto pos = static_cast( ::memchr(haystack.data(), needle, haystack.size())); return pos == nullptr ? std::string::npos : pos - haystack.data(); @@ -1109,6 +1121,10 @@ inline size_t qfind(const Range& haystack, template <> inline size_t rfind(const Range& haystack, const unsigned char& needle) { + // memchr expects a not-null pointer, early return if the range is empty. + if (haystack.empty()) { + return std::string::npos; + } auto pos = static_cast( ::memrchr(haystack.data(), needle, haystack.size())); return pos == nullptr ? std::string::npos : pos - haystack.data(); @@ -1146,6 +1162,14 @@ struct hasher, } }; +/** + * Ubiquitous helper template for knowing what's a string + */ +template struct IsSomeString { + enum { value = std::is_same::value + || std::is_same::value }; +}; + } // !namespace folly #pragma GCC diagnostic pop