ImmutableString: Less fields

This commit is contained in:
Tatsuhiro Tsujikawa 2016-01-17 21:31:30 +09:00
parent 3d5f5b6a28
commit 09de332028
2 changed files with 32 additions and 20 deletions

View File

@ -151,7 +151,7 @@ void test_shrpx_config_parse_log_format(void) {
CU_ASSERT("$" == res[0].value); CU_ASSERT("$" == res[0].value);
CU_ASSERT(SHRPX_LOGF_REMOTE_ADDR == res[1].type); CU_ASSERT(SHRPX_LOGF_REMOTE_ADDR == res[1].type);
CU_ASSERT(nullptr == res[1].value.c_str()); CU_ASSERT("" == res[1].value);
} }
void test_shrpx_config_read_tls_ticket_key_file(void) { void test_shrpx_config_read_tls_ticket_key_file(void) {

View File

@ -230,46 +230,50 @@ public:
ImmutableString() : len(0), base("") {} ImmutableString() : len(0), base("") {}
ImmutableString(const char *s, size_t slen) ImmutableString(const char *s, size_t slen)
: len(slen), holder(strcopy(s, len)), base(holder.get()) {} : len(slen), base(copystr(s, len)) {}
ImmutableString(const char *s) ImmutableString(const char *s) : len(strlen(s)), base(copystr(s, len)) {}
: len(strlen(s)), holder(strcopy(s, len)), base(holder.get()) {}
ImmutableString(std::unique_ptr<char[]> s) ImmutableString(std::unique_ptr<char[]> s)
: len(strlen(s.get())), holder(std::move(s)), base(holder.get()) {} : len(strlen(s.get())), base(len == 0 ? "" : s.release()) {}
ImmutableString(std::unique_ptr<char[]> s, size_t slen) ImmutableString(std::unique_ptr<char[]> s, size_t slen)
: len(slen), holder(std::move(s)), base(holder.get()) {} : len(slen), base(len == 0 ? "" : s.release()) {}
ImmutableString(const std::string &s) ImmutableString(const std::string &s)
: len(s.size()), holder(strcopy(s)), base(holder.get()) {} : len(s.size()), base(copystr(s.c_str(), s.size())) {}
template <typename InputIt> template <typename InputIt>
ImmutableString(InputIt first, InputIt last) ImmutableString(InputIt first, InputIt last)
: len(std::distance(first, last)), holder(strcopy(first, last)), : len(std::distance(first, last)), base(copystr(first, len)) {}
base(holder.get()) {}
ImmutableString(const ImmutableString &other) ImmutableString(const ImmutableString &other)
: len(other.len), holder(strcopy(other.holder, other.len)), : len(other.len), base(copystr(other.base, other.len)) {}
base(holder.get()) {} ImmutableString(ImmutableString &&other) noexcept : len(other.len),
ImmutableString(ImmutableString &&other) noexcept base(other.base) {
: len(other.len),
holder(std::move(other.holder)),
base(holder.get()) {
other.len = 0; other.len = 0;
other.base = ""; other.base = "";
} }
~ImmutableString() {
if (len) {
delete[] base;
}
}
ImmutableString &operator=(const ImmutableString &other) { ImmutableString &operator=(const ImmutableString &other) {
if (this == &other) { if (this == &other) {
return *this; return *this;
} }
if (len) {
delete[] base;
}
len = other.len; len = other.len;
holder = strcopy(other.holder, other.len); base = copystr(other.base, other.len);
base = holder.get();
return *this; return *this;
} }
ImmutableString &operator=(ImmutableString &&other) noexcept { ImmutableString &operator=(ImmutableString &&other) noexcept {
if (this == &other) { if (this == &other) {
return *this; return *this;
} }
if (len) {
delete[] base;
}
len = other.len; len = other.len;
holder = std::move(other.holder); base = other.base;
base = holder.get();
other.len = 0; other.len = 0;
other.base = ""; other.base = "";
return *this; return *this;
@ -285,8 +289,16 @@ public:
const_reference operator[](size_type pos) const { return *(base + pos); } const_reference operator[](size_type pos) const { return *(base + pos); }
private: private:
const char *copystr(const char *s, size_t slen) {
if (slen == 0) {
return "";
}
auto res = new char[slen + 1];
*std::copy_n(s, slen, res) = '\0';
return res;
}
size_type len; size_type len;
std::unique_ptr<char[]> holder;
const char *base; const char *base;
}; };