forked from OctaForge/libostd
better string append() and operator+= (easy ranged append)
This commit is contained in:
parent
6c28e6dd66
commit
841000d4d8
|
@ -167,7 +167,7 @@ inline bool operator==(ConstCharRange lhs, ConstCharRange rhs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator!=(ConstCharRange lhs, ConstCharRange rhs) {
|
inline bool operator!=(ConstCharRange lhs, ConstCharRange rhs) {
|
||||||
return !(lhs == rhs);
|
return lhs.compare(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator<(ConstCharRange lhs, ConstCharRange rhs) {
|
inline bool operator<(ConstCharRange lhs, ConstCharRange rhs) {
|
||||||
|
@ -450,33 +450,11 @@ public:
|
||||||
p_buf.first()[p_len] = '\0';
|
p_buf.first()[p_len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBase &append(const StringBase &s) {
|
StringBase &append(ConstRange r) {
|
||||||
reserve(p_len + s.p_len);
|
if (!r.size()) return *this;
|
||||||
if (!s.p_len) return *this;
|
reserve(p_len + r.size());
|
||||||
memcpy(p_buf.first() + p_len, s.p_buf.first(), s.p_len);
|
memcpy(p_buf.first() + p_len, &r[0], r.size());
|
||||||
p_len += s.p_len;
|
p_len += r.size();
|
||||||
p_buf.first()[p_len] = '\0';
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBase &append(const StringBase &s, Size idx, Size len) {
|
|
||||||
if (!s.p_len) return;
|
|
||||||
Size end = (len == npos) ? s.size() : (idx + len);
|
|
||||||
Size nch = (end - idx);
|
|
||||||
if (!nch) return;
|
|
||||||
reserve(p_len + nch);
|
|
||||||
memcpy(p_buf.first() + p_len, s.p_buf.first() + idx, nch);
|
|
||||||
p_len += nch;
|
|
||||||
p_buf.first()[p_len] = '\0';
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBase &append(const Value *s) {
|
|
||||||
Size len = strlen(s);
|
|
||||||
reserve(p_len + len);
|
|
||||||
if (!len) return *this;
|
|
||||||
memcpy(p_buf.first() + p_len, s, len);
|
|
||||||
p_len += len;
|
|
||||||
p_buf.first()[p_len] = '\0';
|
p_buf.first()[p_len] = '\0';
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -490,8 +468,11 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename R>
|
template<typename R, typename = EnableIf<
|
||||||
StringBase &append_range(R range) {
|
IsInputRange<R>::value &&
|
||||||
|
IsConvertible<RangeReference<R>, Value>::value &&
|
||||||
|
!IsConvertible<R, ConstRange>::value
|
||||||
|
>> StringBase &append(R range) {
|
||||||
Size nadd = 0;
|
Size nadd = 0;
|
||||||
for (; !range.empty(); range.pop_front()) {
|
for (; !range.empty(); range.pop_front()) {
|
||||||
reserve(p_len + nadd + 1);
|
reserve(p_len + nadd + 1);
|
||||||
|
@ -502,11 +483,8 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBase &operator+=(const StringBase &s) {
|
StringBase &operator+=(ConstRange r) {
|
||||||
return append(s);
|
return append(r);
|
||||||
}
|
|
||||||
StringBase &operator+=(const Value *s) {
|
|
||||||
return append(s);
|
|
||||||
}
|
}
|
||||||
StringBase &operator+=(T c) {
|
StringBase &operator+=(T c) {
|
||||||
reserve(p_len + 1);
|
reserve(p_len + 1);
|
||||||
|
@ -514,6 +492,10 @@ public:
|
||||||
p_buf.first()[p_len] = '\0';
|
p_buf.first()[p_len] = '\0';
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
template<typename R>
|
||||||
|
StringBase &operator+=(const R &v) {
|
||||||
|
return append(v);
|
||||||
|
}
|
||||||
|
|
||||||
int compare(ConstRange r) const {
|
int compare(ConstRange r) const {
|
||||||
return iter().compare(r);
|
return iter().compare(r);
|
||||||
|
|
Loading…
Reference in a new issue