From 6848c4e14584e55859018b30390589c418b93358 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Mon, 7 Mar 2016 17:10:57 -0800 Subject: wip. prior to cq refactoring --- include/grpc++/impl/codegen/string_ref.h | 74 ++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 18 deletions(-) (limited to 'include/grpc++/impl/codegen/string_ref.h') diff --git a/include/grpc++/impl/codegen/string_ref.h b/include/grpc++/impl/codegen/string_ref.h index e3af37e0c2..29d85d92dc 100644 --- a/include/grpc++/impl/codegen/string_ref.h +++ b/include/grpc++/impl/codegen/string_ref.h @@ -34,8 +34,12 @@ #ifndef GRPCXX_IMPL_CODEGEN_STRING_REF_H #define GRPCXX_IMPL_CODEGEN_STRING_REF_H -#include +#include + +#include #include +#include +#include #include @@ -56,14 +60,19 @@ class string_ref { typedef std::reverse_iterator const_reverse_iterator; // constants - const static size_t npos; + const static size_t npos = size_t(-1); // construct/copy. string_ref() : data_(nullptr), length_(0) {} string_ref(const string_ref& other) : data_(other.data_), length_(other.length_) {} - string_ref& operator=(const string_ref& rhs); - string_ref(const char* s); + string_ref& operator=(const string_ref& rhs) { + data_ = rhs.data_; + length_ = rhs.length_; + return *this; + } + + string_ref(const char* s) : data_(s), length_(strlen(s)) {} string_ref(const char* s, size_t l) : data_(s), length_(l) {} string_ref(const grpc::string& s) : data_(s.data()), length_(s.length()) {} @@ -95,13 +104,40 @@ class string_ref { const char* data() const { return data_; } // string operations - int compare(string_ref x) const; - bool starts_with(string_ref x) const; - bool ends_with(string_ref x) const; - size_t find(string_ref s) const; - size_t find(char c) const; + int compare(string_ref x) const { + size_t min_size = length_ < x.length_ ? length_ : x.length_; + int r = memcmp(data_, x.data_, min_size); + if (r < 0) return -1; + if (r > 0) return 1; + if (length_ < x.length_) return -1; + if (length_ > x.length_) return 1; + return 0; + } + + bool starts_with(string_ref x) const { + return length_ >= x.length_ && (memcmp(data_, x.data_, x.length_) == 0); + } - string_ref substr(size_t pos, size_t n = npos) const; + bool ends_with(string_ref x) const { + return length_ >= x.length_ && + (memcmp(data_ + (length_ - x.length_), x.data_, x.length_) == 0); + } + + size_t find(string_ref s) const { + auto it = std::search(cbegin(), cend(), s.cbegin(), s.cend()); + return it == cend() ? npos : std::distance(cbegin(), it); + } + + size_t find(char c) const { + auto it = std::find(cbegin(), cend(), c); + return it == cend() ? npos : std::distance(cbegin(), it); + } + + string_ref substr(size_t pos, size_t n = npos) const { + if (pos > length_) pos = length_; + if (n > (length_ - pos)) n = length_ - pos; + return string_ref(data_ + pos, n); + } private: const char* data_; @@ -109,14 +145,16 @@ class string_ref { }; // Comparison operators -bool operator==(string_ref x, string_ref y); -bool operator!=(string_ref x, string_ref y); -bool operator<(string_ref x, string_ref y); -bool operator>(string_ref x, string_ref y); -bool operator<=(string_ref x, string_ref y); -bool operator>=(string_ref x, string_ref y); - -std::ostream& operator<<(std::ostream& stream, const string_ref& string); +inline bool operator==(string_ref x, string_ref y) { return x.compare(y) == 0; } +inline bool operator!=(string_ref x, string_ref y) { return x.compare(y) != 0; } +inline bool operator<(string_ref x, string_ref y) { return x.compare(y) < 0; } +inline bool operator<=(string_ref x, string_ref y) { return x.compare(y) <= 0; } +inline bool operator>(string_ref x, string_ref y) { return x.compare(y) > 0; } +inline bool operator>=(string_ref x, string_ref y) { return x.compare(y) >= 0; } + +inline std::ostream& operator<<(std::ostream& out, const string_ref& string) { + return out << grpc::string(string.begin(), string.end()); +} } // namespace grpc -- cgit v1.2.3