Separate Slice constructors for debug purposes.

GitOrigin-RevId: f7ef57f55d59a50ce920549432c3a10d612228eb
This commit is contained in:
levlam 2018-06-30 15:55:58 +03:00
parent d30d227289
commit b38ec4ceb4
2 changed files with 39 additions and 17 deletions

View File

@ -8,10 +8,11 @@
#include "td/utils/common.h" #include "td/utils/common.h"
#include <cstring> #include <cstddef>
#include <type_traits> #include <type_traits>
namespace td { namespace td {
class Slice; class Slice;
class MutableSlice { class MutableSlice {
@ -26,9 +27,7 @@ class MutableSlice {
MutableSlice(unsigned char *s, size_t len); MutableSlice(unsigned char *s, size_t len);
MutableSlice(string &s); MutableSlice(string &s);
template <class T> template <class T>
explicit MutableSlice(T s, std::enable_if_t<std::is_same<char *, T>::value, private_tag> = {}) explicit MutableSlice(T s, std::enable_if_t<std::is_same<char *, T>::value, private_tag> = {});
: MutableSlice(s, std::strlen(s)) {
}
MutableSlice(char *s, char *t); MutableSlice(char *s, char *t);
MutableSlice(unsigned char *s, unsigned char *t); MutableSlice(unsigned char *s, unsigned char *t);
template <size_t N> template <size_t N>
@ -74,13 +73,9 @@ class Slice {
Slice(const unsigned char *s, size_t len); Slice(const unsigned char *s, size_t len);
Slice(const string &s); Slice(const string &s);
template <class T> template <class T>
explicit Slice(T s, std::enable_if_t<std::is_same<char *, std::remove_const_t<T>>::value, private_tag> = {}) explicit Slice(T s, std::enable_if_t<std::is_same<char *, std::remove_const_t<T>>::value, private_tag> = {});
: Slice(s, std::strlen(s)) {
}
template <class T> template <class T>
explicit Slice(T s, std::enable_if_t<std::is_same<const char *, std::remove_const_t<T>>::value, private_tag> = {}) explicit Slice(T s, std::enable_if_t<std::is_same<const char *, std::remove_const_t<T>>::value, private_tag> = {});
: Slice(s, std::strlen(s)) {
}
Slice(const char *s, const char *t); Slice(const char *s, const char *t);
Slice(const unsigned char *s, const unsigned char *t); Slice(const unsigned char *s, const unsigned char *t);

View File

@ -13,8 +13,9 @@
#include <cstring> #include <cstring>
namespace td { namespace td {
/*** MutableSlice ***/ /*** MutableSlice ***/
inline MutableSlice::MutableSlice() : MutableSlice(const_cast<char *>(""), static_cast<size_t>(0)) { inline MutableSlice::MutableSlice() : s_(const_cast<char *>("")), len_(0) {
} }
inline MutableSlice::MutableSlice(char *s, size_t len) : s_(s), len_(len) { inline MutableSlice::MutableSlice(char *s, size_t len) : s_(s), len_(len) {
@ -25,7 +26,17 @@ inline MutableSlice::MutableSlice(unsigned char *s, size_t len) : s_(reinterpret
CHECK(s_ != nullptr); CHECK(s_ != nullptr);
} }
inline MutableSlice::MutableSlice(string &s) : MutableSlice(&s[0], s.size()) { inline MutableSlice::MutableSlice(string &s) : MutableSlice() {
if (!s.empty()) {
s_ = &s[0];
len_ = s.size();
}
}
template <class T>
inline MutableSlice::MutableSlice(T s, std::enable_if_t<std::is_same<char *, T>::value, private_tag>) : s_(s) {
CHECK(s_ != nullptr);
len_ = std::strlen(s_);
} }
inline MutableSlice::MutableSlice(char *s, char *t) : MutableSlice(s, t - s) { inline MutableSlice::MutableSlice(char *s, char *t) : MutableSlice(s, t - s) {
@ -131,10 +142,10 @@ inline char &MutableSlice::operator[](size_t i) {
} }
/*** Slice ***/ /*** Slice ***/
inline Slice::Slice() : Slice("", static_cast<size_t>(0)) { inline Slice::Slice() : s_(""), len_(0) {
} }
inline Slice::Slice(const MutableSlice &other) : Slice(other.begin(), other.size()) { inline Slice::Slice(const MutableSlice &other) : s_(other.begin()), len_(other.size()) {
} }
inline Slice::Slice(const char *s, size_t len) : s_(s), len_(len) { inline Slice::Slice(const char *s, size_t len) : s_(s), len_(len) {
@ -145,13 +156,29 @@ inline Slice::Slice(const unsigned char *s, size_t len) : s_(reinterpret_cast<co
CHECK(s_ != nullptr); CHECK(s_ != nullptr);
} }
inline Slice::Slice(const string &s) : Slice(s.c_str(), s.size()) { inline Slice::Slice(const string &s) : s_(s.c_str()), len_(s.size()) {
} }
inline Slice::Slice(const char *s, const char *t) : Slice(s, t - s) { template <class T>
inline Slice::Slice(T s, std::enable_if_t<std::is_same<char *, std::remove_const_t<T>>::value, private_tag>) : s_(s) {
CHECK(s_ != nullptr);
len_ = std::strlen(s_);
} }
inline Slice::Slice(const unsigned char *s, const unsigned char *t) : Slice(s, t - s) { template <class T>
inline Slice::Slice(T s, std::enable_if_t<std::is_same<const char *, std::remove_const_t<T>>::value, private_tag>)
: s_(s) {
CHECK(s_ != nullptr);
len_ = std::strlen(s_);
}
inline Slice::Slice(const char *s, const char *t) : s_(s), len_(t - s) {
CHECK(s_ != nullptr);
}
inline Slice::Slice(const unsigned char *s, const unsigned char *t)
: s_(reinterpret_cast<const char *>(s)), len_(t - s) {
CHECK(s_ != nullptr);
} }
inline size_t Slice::size() const { inline size_t Slice::size() const {