cleaned up constructors/assign ops for string and vector

master
Daniel Kolesa 2015-07-22 20:51:12 +01:00
parent b9edc0c568
commit 161ab5be85
2 changed files with 24 additions and 27 deletions

View File

@ -262,19 +262,12 @@ public:
}
/* TODO: traits for utf-16/utf-32 string lengths, for now assume char */
StringBase(const Value *v, const A &a = A()): StringBase(a) {
Size len = strlen(v);
if (!len) return;
reserve(len);
memcpy(p_buf.first(), v, len + 1);
p_len = len;
}
StringBase(const Value *v, Size n, const A &a = A()): StringBase(a) {
if (!n) return;
reserve(n);
memcpy(p_buf.first(), v, n);
p_buf.first()[n] = '\0';
StringBase(ConstRange v, const A &a = A()): StringBase(a) {
if (!v.size()) return;
reserve(v.size());
memcpy(p_buf.first(), &v[0], v.size());
p_buf.first()[v.size()] = '\0';
p_len = v.size();
}
template<typename R, typename = EnableIf<
@ -284,6 +277,10 @@ public:
ctor_from_range(range);
}
~StringBase() {
allocator_deallocate(p_buf.second(), p_buf.first(), p_cap + 1);
}
void clear() {
p_len = 0;
*p_buf.first() = '\0';
@ -320,11 +317,11 @@ public:
if (!p_cap) p_buf.first() = (Pointer)&p_len;
return *this;
}
StringBase &operator=(const Value *v) {
Size len = strlen(v);
reserve(len);
if (len) memcpy(p_buf.first(), v, len);
p_buf.first()[len] = '\0';
StringBase &operator=(ConstRange v) {
reserve(v.size());
if (v.size()) memcpy(p_buf.first(), &v[0], v.size());
p_buf.first()[v.size()] = '\0';
p_len = v.size();
return *this;
}
template<typename R, typename = EnableIf<
@ -608,7 +605,7 @@ inline bool operator>=(const char *lhs, const StringBase<T, A> &rhs) {
inline namespace literals { inline namespace string_literals {
String operator "" _s(const char *str, Size len) {
return String(str, len);
return String(ConstCharRange(str, len));
}
ConstCharRange operator "" _S(const char *str, Size len) {
@ -748,7 +745,7 @@ template<> struct ToString<bool> {
using Argument = bool;
using Result = String;
String operator()(bool b) {
return b ? "true" : "false";
return String(b ? "true" : "false");
}
};

View File

@ -138,19 +138,19 @@ public:
v.p_len = v.p_cap = 0;
}
Vector(const Value *buf, Size n, const A &a = A()): Vector(a) {
reserve(n);
Vector(ConstRange r, const A &a = A()): Vector(a) {
reserve(r.size());
if (IsPod<T>()) {
memcpy(p_buf.first(), buf, n * sizeof(T));
memcpy(p_buf.first(), &r[0], r.size() * sizeof(T));
} else {
for (Size i = 0; i < n; ++i)
allocator_construct(p_buf.second(), &p_buf.first()[i], buf[i]);
for (Size i = 0; i < r.size(); ++i)
allocator_construct(p_buf.second(), &p_buf.first()[i], r[i]);
}
p_len = n;
p_len = r.size();
}
Vector(InitializerList<T> v, const A &a = A()):
Vector(v.begin(), v.size(), a) {}
Vector(ConstRange(v.begin(), v.size()), a) {}
template<typename R, typename = EnableIf<
IsInputRange<R>::value &&