libostd/ostd/string.hh

859 lines
23 KiB
C++
Raw Normal View History

2015-05-27 22:43:13 +02:00
/* String for OctaSTD.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
2015-07-13 21:08:55 +02:00
#ifndef OSTD_STRING_HH
#define OSTD_STRING_HH
2015-05-27 22:43:13 +02:00
2015-05-27 23:15:24 +02:00
#include <stdio.h>
2015-05-27 22:43:13 +02:00
#include <stddef.h>
2015-07-13 21:08:55 +02:00
#include "ostd/utility.hh"
#include "ostd/range.hh"
#include "ostd/vector.hh"
2015-07-20 03:15:12 +02:00
#include "ostd/functional.hh"
#include "ostd/type_traits.hh"
2015-05-27 22:43:13 +02:00
2015-07-13 21:07:14 +02:00
namespace ostd {
static constexpr Size npos = -1;
2015-05-27 22:43:13 +02:00
template<typename T, typename A = Allocator<T>> class StringBase;
template<typename T>
struct StringRangeBase: InputRange<
StringRangeBase<T>, FiniteRandomAccessRangeTag, T
> {
StringRangeBase() = delete;
StringRangeBase(T *beg, T *end): p_beg(beg), p_end(end) {}
StringRangeBase(T *beg, Size n): p_beg(beg), p_end(beg + n) {}
/* TODO: traits for utf-16/utf-32 string lengths, for now assume char */
template<typename U>
StringRangeBase(U beg, EnableIf<
IsConvertible<U, T *>::value && !IsArray<U>::value, bool
> = true): p_beg(beg), p_end((T *)beg + strlen(beg)) {}
template<typename U, Size N>
StringRangeBase(U (&beg)[N], EnableIf<
IsConvertible<U *, T *>::value, bool
> = true): p_beg(beg),
p_end(beg + N - (beg[N - 1] == '\0')) {}
template<typename U, typename A>
StringRangeBase(const StringBase<U, A> &s, EnableIf<
IsConvertible<U *, T *>::value, bool
> = true): p_beg(s.data()),
p_end(s.data() + s.size()) {}
template<typename U, typename = EnableIf<
IsConvertible<U *, T *>::value
>> StringRangeBase(const StringRangeBase<U> &v):
p_beg(&v[0]), p_end(&v[v.size()]) {}
2015-06-17 03:00:39 +02:00
StringRangeBase &operator=(const StringRangeBase &v) {
p_beg = v.p_beg; p_end = v.p_end; return *this;
}
template<typename A>
StringRangeBase &operator=(const StringBase<T, A> &s) {
p_beg = s.data(); p_end = s.data() + s.size(); return *this;
}
/* TODO: traits for utf-16/utf-32 string lengths, for now assume char */
StringRangeBase &operator=(T *s) {
p_beg = s; p_end = s + strlen(s); return *this;
}
bool empty() const { return p_beg == p_end; }
bool pop_front() {
if (p_beg == p_end) return false;
++p_beg;
return true;
}
bool push_front() { --p_beg; return true; }
Size pop_front_n(Size n) {
Size olen = p_end - p_beg;
p_beg += n;
if (p_beg > p_end) {
p_beg = p_end;
return olen;
}
return n;
}
Size push_front_n(Size n) { p_beg -= n; return true; }
T &front() const { return *p_beg; }
bool equals_front(const StringRangeBase &range) const {
return p_beg == range.p_beg;
}
Ptrdiff distance_front(const StringRangeBase &range) const {
return range.p_beg - p_beg;
}
bool pop_back() {
if (p_end == p_beg) return false;
--p_end;
return true;
}
bool push_back() { ++p_end; return true; }
Size pop_back_n(Size n) {
Size olen = p_end - p_beg;
p_end -= n;
if (p_end < p_beg) {
p_end = p_beg;
return olen;
}
return n;
}
Size push_back_n(Size n) { p_end += n; return true; }
T &back() const { return *(p_end - 1); }
bool equals_back(const StringRangeBase &range) const {
return p_end == range.p_end;
}
Ptrdiff distance_back(const StringRangeBase &range) const {
return range.p_end - p_end;
}
Size size() const { return p_end - p_beg; }
StringRangeBase slice(Size start, Size end) const {
return StringRangeBase(p_beg + start, p_beg + end);
}
T &operator[](Size i) const { return p_beg[i]; }
2015-07-01 22:12:45 +02:00
bool put(T v) {
if (empty()) return false;
*(p_beg++) = v;
2015-07-01 22:12:45 +02:00
return true;
}
/* non-range methods */
T *data() { return p_beg; }
const T *data() const { return p_beg; }
Size to_hash() const {
2015-07-20 03:15:12 +02:00
return detail::mem_hash(data(), size());
}
private:
T *p_beg, *p_end;
};
template<typename T, typename A>
2015-06-04 00:07:57 +02:00
class StringBase {
using StrPair = detail::CompressedPair<AllocatorPointer<A>, A>;
2015-05-27 22:43:13 +02:00
2015-07-13 21:07:14 +02:00
ostd::Size p_len, p_cap;
StrPair p_buf;
template<typename R>
void ctor_from_range(R &range, EnableIf<
IsFiniteRandomAccessRange<R>::value &&
IsSame<T, RemoveCv<RangeValue<R>>>::value, bool
> = true) {
if (range.empty()) return;
RangeSize<R> l = range.size();
reserve(l);
p_len = l;
range.copy(p_buf.first(), l);
p_buf.first()[l] = '\0';
}
template<typename R>
void ctor_from_range(R &range, EnableIf<
!IsFiniteRandomAccessRange<R>::value ||
!IsSame<T, RemoveCv<RangeValue<R>>>::value, bool
> = true) {
if (range.empty()) return;
Size i = 0;
for (; !range.empty(); range.pop_front()) {
reserve(i + 1);
allocator_construct(p_buf.second(), &p_buf.first()[i],
range.front());
++i;
p_len = i;
}
p_buf.first()[p_len] = '\0';
2015-06-04 00:07:57 +02:00
}
2015-05-27 22:43:13 +02:00
2015-06-04 00:07:57 +02:00
public:
2015-07-13 21:07:14 +02:00
using Size = ostd::Size;
using Difference = Ptrdiff;
2015-06-08 01:55:08 +02:00
using Value = T;
using Reference = T &;
using ConstReference = const T &;
using Pointer = AllocatorPointer<A>;
using ConstPointer = AllocatorConstPointer<A>;
using Range = StringRangeBase<T>;
using ConstRange = StringRangeBase<const T>;
2015-06-08 01:55:08 +02:00
using Allocator = A;
2015-06-04 00:07:57 +02:00
StringBase(const A &a = A()): p_len(0), p_cap(0),
p_buf((Pointer)&p_len, a) {}
explicit StringBase(Size n, T val = T(), const A &al = A()):
StringBase(al) {
if (!n) return;
p_buf.first() = allocator_allocate(p_buf.second(), n + 1);
p_len = p_cap = n;
Pointer cur = p_buf.first(), last = p_buf.first() + n;
while (cur != last) *cur++ = val;
*cur = '\0';
}
StringBase(const StringBase &s): p_len(0), p_cap(0),
p_buf((Pointer)&p_len, allocator_container_copy(s.p_buf.second())) {
if (!s.p_len) return;
reserve(s.p_len);
p_len = s.p_len;
memcpy(p_buf.first(), s.p_buf.first(), (p_len + 1) * sizeof(T));
}
StringBase(const StringBase &s, const A &a): p_len(0), p_cap(0),
p_buf((Pointer)&p_len, a) {
if (!s.p_len) return;
reserve(s.p_len);
p_len = s.p_len;
memcpy(p_buf.first(), s.p_buf.first(), (p_len + 1) * sizeof(T));
}
StringBase(StringBase &&s): p_len(s.p_len), p_cap(s.p_cap),
p_buf(s.p_buf.first(), move(s.p_buf.second())) {
s.p_len = s.p_cap = 0;
s.p_buf.first() = (Pointer)&s.p_len;
}
StringBase(StringBase &&s, const A &a): p_len(0), p_cap(0),
p_buf((Pointer)&p_len, a) {
if (!s.p_len) return;
if (a != s.p_buf.second()) {
reserve(s.p_cap);
p_len = s.p_len;
memcpy(p_buf.first(), s.p_buf.first(), (p_len + 1) * sizeof(T));
return;
}
p_buf.first() = s.p_buf.first();
p_len = s.p_len;
p_cap = s.p_cap;
s.p_len = s.p_cap = 0;
s.p_buf.first() = &s.p_cap;
}
2015-06-04 00:07:57 +02:00
StringBase(const StringBase &s, Size pos, Size len = npos,
const A &a = A()): StringBase(a) {
Size end = (len == npos) ? s.size() : (pos + len);
Size nch = (end - pos);
reserve(nch);
memcpy(p_buf.first(), s.p_buf.first() + pos, nch);
p_len += nch;
p_buf.first()[p_len] = '\0';
2015-06-04 00:07:57 +02:00
}
2015-05-29 02:02:19 +02:00
2015-06-04 00:07:57 +02:00
/* 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;
}
2015-05-28 02:26:48 +02:00
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';
}
template<typename R, typename = EnableIf<
IsInputRange<R>::value &&
IsConvertible<RangeReference<R>, Value>::value
>> StringBase(R range, const A &a = A()): StringBase(a) {
ctor_from_range(range);
2015-06-04 00:07:57 +02:00
}
2015-05-27 22:43:13 +02:00
void clear() {
p_len = 0;
*p_buf.first() = '\0';
}
2015-05-27 22:43:13 +02:00
2015-06-10 01:16:25 +02:00
StringBase &operator=(const StringBase &v) {
if (this == &v) return *this;
clear();
if (AllocatorPropagateOnContainerCopyAssignment<A>::value) {
if ((p_buf.second() != v.p_buf.second()) && p_cap) {
allocator_deallocate(p_buf.second(), p_buf.first(), p_cap);
p_cap = 0;
p_buf.first() = &p_len;
}
p_buf.second() = v.p_buf.second();
}
reserve(v.p_cap);
p_len = v.p_len;
if (p_len) {
memcpy(p_buf.first(), v.p_buf.first(), p_len);
p_buf.first()[p_len] = '\0';
} else p_buf.first() = &p_len;
2015-06-04 00:07:57 +02:00
return *this;
}
2015-06-10 01:16:25 +02:00
StringBase &operator=(StringBase &&v) {
clear();
if (p_cap) allocator_deallocate(p_buf.second(), p_buf.first(), p_cap);
if (AllocatorPropagateOnContainerMoveAssignment<A>::value)
p_buf.second() = v.p_buf.second();
p_len = v.p_len;
p_cap = v.p_cap;
p_buf.~StrPair();
new (&p_buf) StrPair(v.disown(), move(v.p_buf.second()));
2015-07-18 18:01:11 +02:00
if (!p_cap) p_buf.first() = (Pointer)&p_len;
2015-06-04 00:07:57 +02:00
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';
2015-06-04 00:07:57 +02:00
return *this;
}
template<typename R, typename = EnableIf<
IsInputRange<R>::value &&
IsConvertible<RangeReference<R>, Value>::value
>> StringBase &operator=(const R &r) {
clear();
ctor_from_range(r);
return *this;
}
2015-05-27 22:43:13 +02:00
void resize(Size n, T v = T()) {
if (!n) {
clear();
return;
}
Size l = p_len;
reserve(n);
p_len = n;
for (Size i = l; i < p_len; ++i) {
p_buf.first()[i] = T(v);
}
p_buf.first()[l] = '\0';
2015-06-04 00:07:57 +02:00
}
2015-05-27 22:43:13 +02:00
void reserve(Size n) {
if (n <= p_cap) return;
Size oc = p_cap;
if (!oc) {
p_cap = max(n, Size(8));
} else {
while (p_cap < n) p_cap *= 2;
}
Pointer tmp = allocator_allocate(p_buf.second(), p_cap + 1);
if (oc > 0) {
memcpy(tmp, p_buf.first(), (p_len + 1) * sizeof(T));
allocator_deallocate(p_buf.second(), p_buf.first(), oc + 1);
}
tmp[p_len] = '\0';
p_buf.first() = tmp;
2015-06-04 00:07:57 +02:00
}
2015-05-27 22:43:13 +02:00
T &operator[](Size i) { return p_buf[i]; }
const T &operator[](Size i) const { return p_buf[i]; }
2015-05-27 22:43:13 +02:00
T &at(Size i) { return p_buf[i]; }
const T &at(Size i) const { return p_buf[i]; }
2015-05-27 22:43:13 +02:00
2015-06-04 23:57:06 +02:00
T &front() { return p_buf[0]; }
const T &front() const { return p_buf[0]; };
2015-05-27 22:43:13 +02:00
2015-06-04 23:57:06 +02:00
T &back() { return p_buf[size() - 1]; }
const T &back() const { return p_buf[size() - 1]; }
2015-05-27 23:15:24 +02:00
Value *data() { return p_buf.first(); }
const Value *data() const { return p_buf.first(); }
2015-05-29 02:02:19 +02:00
Size size() const {
return p_len;
2015-06-04 00:07:57 +02:00
}
2015-05-29 02:02:19 +02:00
Size capacity() const {
return p_cap;
2015-06-04 00:07:57 +02:00
}
2015-05-29 02:02:19 +02:00
Size length() const {
/* TODO: unicode */
return size();
}
2015-06-04 00:07:57 +02:00
bool empty() const { return (size() == 0); }
2015-05-29 02:02:19 +02:00
2015-07-18 18:01:11 +02:00
Value *disown() {
Pointer r = p_buf.first();
p_buf.first() = nullptr;
p_len = p_cap = 0;
return (Value *)r;
}
2015-06-04 23:57:06 +02:00
void push(T v) {
reserve(p_len + 1);
p_buf.first()[p_len++] = v;
p_buf.first()[p_len] = '\0';
2015-06-04 00:07:57 +02:00
}
2015-05-29 02:02:19 +02:00
2015-06-10 01:16:25 +02:00
StringBase &append(const StringBase &s) {
reserve(p_len + s.p_len);
if (!s.p_len) return *this;
memcpy(p_buf.first() + p_len, s.p_buf.first(), s.p_len);
p_len += s.p_len;
p_buf.first()[p_len] = '\0';
2015-06-04 00:07:57 +02:00
return *this;
}
2015-05-27 22:43:13 +02:00
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';
2015-06-04 00:07:57 +02:00
return *this;
}
2015-05-27 22:43:13 +02:00
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';
2015-06-04 00:07:57 +02:00
return *this;
}
2015-05-27 22:43:13 +02:00
StringBase &append(Size n, T c) {
if (!n) return;
reserve(p_len + n);
for (Size i = 0; i < n; ++n) p_buf.first()[p_len + i] = c;
p_len += n;
p_buf.first()[p_len] = '\0';
2015-06-04 00:07:57 +02:00
return *this;
}
2015-05-27 23:05:51 +02:00
2015-06-04 23:57:06 +02:00
template<typename R>
2015-06-10 01:16:25 +02:00
StringBase &append_range(R range) {
Size nadd = 0;
for (; !range.empty(); range.pop_front()) {
reserve(p_len + nadd + 1);
p_buf.first()[p_len + nadd++] = range.front();
}
p_len += nadd;
p_buf.first()[p_len] = '\0';
2015-06-04 00:07:57 +02:00
return *this;
}
2015-06-10 01:16:25 +02:00
StringBase &operator+=(const StringBase &s) {
2015-06-04 00:07:57 +02:00
return append(s);
}
StringBase &operator+=(const Value *s) {
2015-06-04 00:07:57 +02:00
return append(s);
}
2015-06-10 01:16:25 +02:00
StringBase &operator+=(T c) {
reserve(p_len + 1);
p_buf.first()[p_len++] = c;
p_buf.first()[p_len] = '\0';
2015-06-04 00:07:57 +02:00
return *this;
}
2015-06-14 03:36:20 +02:00
int compare(const StringBase &s) const {
return strcmp(p_buf.first(), s.data());
2015-06-14 03:36:20 +02:00
}
int compare(const Value *p) const {
return strcmp(p_buf.first(), p);
2015-06-14 03:36:20 +02:00
}
2015-06-26 22:01:16 +02:00
Range iter() {
return Range(p_buf.first(), size());
2015-06-04 00:07:57 +02:00
}
2015-06-26 22:01:16 +02:00
ConstRange iter() const {
return ConstRange(p_buf.first(), size());
2015-05-27 23:05:51 +02:00
}
2015-06-26 22:01:16 +02:00
ConstRange citer() const {
return ConstRange(p_buf.dfirst(), size());
2015-06-09 22:18:43 +02:00
}
Range iter_cap() {
return Range(p_buf.first(), capacity());
}
2015-06-04 00:07:57 +02:00
void swap(StringBase &v) {
detail::swap_adl(p_len, v.p_len);
detail::swap_adl(p_cap, v.p_cap);
detail::swap_adl(p_buf.first(), v.p_buf.first());
if (AllocatorPropagateOnContainerSwap<A>::value)
detail::swap_adl(p_buf.second(), v.p_buf.second());
2015-05-28 20:58:05 +02:00
}
Size to_hash() const {
2015-06-26 22:01:16 +02:00
return iter().to_hash();
}
2015-06-12 21:13:27 +02:00
A get_allocator() const {
return p_buf.second();
2015-06-12 21:13:27 +02:00
}
2015-06-04 00:07:57 +02:00
};
2015-06-08 01:55:08 +02:00
using String = StringBase<char>;
using StringRange = StringRangeBase<char>;
using ConstStringRange = StringRangeBase<const char>;
2015-06-04 00:07:57 +02:00
template<typename A> using AnyString = StringBase<char, A>;
template<typename T, typename A>
inline bool operator==(const StringBase<T, A> &lhs,
const StringBase<T, A> &rhs) {
2015-06-14 03:36:20 +02:00
return !lhs.compare(rhs);
}
template<typename T, typename A>
inline bool operator==(const StringBase<T, A> &lhs, const char *rhs) {
2015-06-14 03:36:20 +02:00
return !lhs.compare(rhs);
}
template<typename T, typename A>
inline bool operator==(const char *lhs, const StringBase<T, A> &rhs) {
2015-06-14 03:36:20 +02:00
return !rhs.compare(lhs);
}
template<typename T, typename A>
inline bool operator!=(const StringBase<T, A> &lhs,
const StringBase<T, A> &rhs) {
return !(lhs == rhs);
2015-06-14 03:36:20 +02:00
}
template<typename T, typename A>
inline bool operator!=(const StringBase<T, A> &lhs, const char *rhs) {
return !(lhs == rhs);
2015-06-14 03:36:20 +02:00
}
template<typename T, typename A>
inline bool operator!=(const char *lhs, const StringBase<T, A> &rhs) {
return !(rhs == lhs);
}
template<typename T, typename A>
inline bool operator<(const StringBase<T, A> &lhs,
const StringBase<T, A> &rhs) {
return lhs.compare(rhs) < 0;
}
template<typename T, typename A>
inline bool operator<(const StringBase<T, A> &lhs, const char *rhs) {
return lhs.compare(rhs) < 0;
}
template<typename T, typename A>
inline bool operator<(const char *lhs, const StringBase<T, A> &rhs) {
return rhs.compare(lhs) > 0;
}
template<typename T, typename A>
inline bool operator>(const StringBase<T, A> &lhs,
const StringBase<T, A> &rhs) {
return rhs < lhs;
}
template<typename T, typename A>
inline bool operator>(const StringBase<T, A> &lhs, const char *rhs) {
return rhs < lhs;
}
template<typename T, typename A>
inline bool operator>(const char *lhs, const StringBase<T, A> &rhs) {
return rhs < lhs;
}
template<typename T, typename A>
inline bool operator<=(const StringBase<T, A> &lhs,
const StringBase<T, A> &rhs) {
return !(rhs < lhs);
}
template<typename T, typename A>
inline bool operator<=(const StringBase<T, A> &lhs, const char *rhs) {
return !(rhs < lhs);
}
template<typename T, typename A>
inline bool operator<=(const char *lhs, const StringBase<T, A> &rhs) {
return !(rhs < lhs);
}
template<typename T, typename A>
inline bool operator>=(const StringBase<T, A> &lhs,
const StringBase<T, A> &rhs) {
return !(lhs < rhs);
}
template<typename T, typename A>
inline bool operator>=(const StringBase<T, A> &lhs, const char *rhs) {
return !(lhs < rhs);
}
template<typename T, typename A>
inline bool operator>=(const char *lhs, const StringBase<T, A> &rhs) {
return !(lhs < rhs);
2015-06-14 03:36:20 +02:00
}
2015-07-18 02:02:13 +02:00
/* string literals */
inline namespace literals { inline namespace string_literals {
String operator "" _s(const char *str, Size len) {
return String(str, len);
}
} }
2015-07-12 16:55:41 +02:00
template<typename A, typename T, typename F, typename S = const char *>
AnyString<A> concat(AllocatorArg, const A &alloc, const T &v, const S &sep,
F func) {
AnyString<A> ret(alloc);
2015-07-13 21:07:14 +02:00
auto range = ostd::iter(v);
if (range.empty()) return ret;
2015-06-04 00:07:57 +02:00
for (;;) {
ret += func(range.front());
range.pop_front();
if (range.empty()) break;
ret += sep;
}
return ret;
2015-06-04 00:07:57 +02:00
}
2015-05-28 20:58:05 +02:00
2015-07-12 16:55:41 +02:00
template<typename A, typename T, typename S = const char *>
AnyString<A> concat(AllocatorArg, const A &alloc, const T &v,
const S &sep = " ") {
AnyString<A> ret(alloc);
2015-07-13 21:07:14 +02:00
auto range = ostd::iter(v);
if (range.empty()) return ret;
2015-06-04 00:07:57 +02:00
for (;;) {
ret += range.front();
range.pop_front();
if (range.empty()) break;
ret += sep;
2015-05-28 20:58:05 +02:00
}
return ret;
2015-06-04 00:07:57 +02:00
}
2015-07-12 16:55:41 +02:00
template<typename T, typename F, typename S = const char *>
String concat(const T &v, const S &sep, F func) {
return concat(allocator_arg, typename String::Allocator(), v, sep, func);
}
template<typename T, typename S = const char *>
String concat(const T &v, const S &sep = " ") {
return concat(allocator_arg, typename String::Allocator(), v, sep);
}
template<typename A, typename T, typename F, typename S = const char *>
AnyString<A> concat(AllocatorArg, const A &alloc,
std::initializer_list<T> v, const S &sep, F func) {
2015-07-13 21:07:14 +02:00
return concat(allocator_arg, alloc, ostd::iter(v), sep, func);
2015-07-12 16:55:41 +02:00
}
template<typename A, typename T, typename S = const char *>
AnyString<A> concat(AllocatorArg, const A &alloc,
std::initializer_list<T> v, const S &sep = " ") {
2015-07-13 21:07:14 +02:00
return concat(allocator_arg, alloc, ostd::iter(v), sep);
2015-07-12 16:55:41 +02:00
}
template<typename T, typename F, typename S = const char *>
String concat(std::initializer_list<T> v, const S &sep, F func) {
2015-07-13 21:07:14 +02:00
return concat(ostd::iter(v), sep, func);
2015-06-04 00:07:57 +02:00
}
2015-05-28 20:58:05 +02:00
2015-07-12 16:55:41 +02:00
template<typename T, typename S = const char *>
String concat(std::initializer_list<T> v, const S &sep = " ") {
2015-07-13 21:07:14 +02:00
return concat(ostd::iter(v), sep);
2015-06-04 00:07:57 +02:00
}
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-07-11 20:54:51 +02:00
auto test_tostring(int) ->
decltype(IsSame<decltype(declval<T>().to_string()), String>());
template<typename>
False test_tostring(...);
template<typename T>
2015-07-11 20:54:51 +02:00
using ToStringTest = decltype(test_tostring<T>(0));
template<typename T>
2015-07-13 21:07:14 +02:00
True test_iterable(decltype(ostd::iter(declval<T>())) *);
template<typename> static False test_iterable(...);
template<typename T>
using IterableTest = decltype(test_iterable<T>(0));
2015-06-04 00:07:57 +02:00
}
2015-05-28 03:38:52 +02:00
template<typename T, typename = void>
struct ToString;
template<typename T>
struct ToString<T, EnableIf<detail::IterableTest<T>::value>> {
2015-07-11 21:21:49 +02:00
using Argument = RemoveCv<RemoveReference<T>>;
2015-06-08 01:55:08 +02:00
using Result = String;
2015-05-28 03:38:52 +02:00
String operator()(const T &v) const {
2015-06-04 00:07:57 +02:00
String ret("{");
2015-07-13 21:07:14 +02:00
ret += concat(ostd::iter(v), ", ", ToString<
RemoveConst<RemoveReference<
2015-07-13 21:07:14 +02:00
RangeReference<decltype(ostd::iter(v))>
>>
2015-07-04 15:52:02 +02:00
>());
2015-06-04 00:07:57 +02:00
ret += "}";
return ret;
2015-06-04 00:07:57 +02:00
}
};
2015-05-28 20:58:05 +02:00
template<typename T>
struct ToString<T, EnableIf<detail::ToStringTest<T>::value>> {
2015-07-11 21:21:49 +02:00
using Argument = RemoveCv<RemoveReference<T>>;
using Result = String;
2015-05-28 03:38:52 +02:00
String operator()(const T &v) const {
return v.to_string();
2015-06-04 00:07:57 +02:00
}
};
2015-05-27 23:15:24 +02:00
2015-06-04 00:07:57 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
void str_printf(String &s, const char *fmt, T v) {
2015-06-04 00:07:57 +02:00
char buf[256];
int n = snprintf(buf, sizeof(buf), fmt, v);
s.clear();
s.reserve(n);
2015-06-04 00:07:57 +02:00
if (n >= (int)sizeof(buf))
snprintf(s.data(), n + 1, fmt, v);
2015-06-04 00:07:57 +02:00
else if (n > 0)
memcpy(s.data(), buf, n + 1);
2015-05-28 02:44:21 +02:00
else {
s.clear();
2015-05-28 02:44:21 +02:00
}
*((Size *)&s) = n;
2015-05-28 02:26:48 +02:00
}
2015-06-04 00:07:57 +02:00
}
2015-05-28 02:26:48 +02:00
2015-06-04 00:07:57 +02:00
template<> struct ToString<bool> {
2015-06-08 01:55:08 +02:00
using Argument = bool;
using Result = String;
2015-06-04 00:07:57 +02:00
String operator()(bool b) {
return b ? "true" : "false";
}
};
template<> struct ToString<char> {
2015-06-08 01:55:08 +02:00
using Argument = char;
using Result = String;
2015-06-04 00:07:57 +02:00
String operator()(char c) {
String ret;
ret.push(c);
return ret;
2015-06-04 00:07:57 +02:00
}
};
2015-07-13 21:08:55 +02:00
#define OSTD_TOSTR_NUM(T, fmt) \
2015-06-04 23:57:06 +02:00
template<> struct ToString<T> { \
2015-06-08 01:55:08 +02:00
using Argument = T; \
using Result = String; \
2015-06-04 23:57:06 +02:00
String operator()(T v) { \
2015-06-04 00:07:57 +02:00
String ret; \
detail::str_printf(ret, fmt, v); \
return ret; \
2015-06-04 00:07:57 +02:00
} \
};
2015-07-13 21:08:55 +02:00
OSTD_TOSTR_NUM(sbyte, "%d")
OSTD_TOSTR_NUM(int, "%d")
OSTD_TOSTR_NUM(int &, "%d")
OSTD_TOSTR_NUM(long, "%ld")
OSTD_TOSTR_NUM(float, "%f")
OSTD_TOSTR_NUM(double, "%f")
OSTD_TOSTR_NUM(byte, "%u")
OSTD_TOSTR_NUM(uint, "%u")
OSTD_TOSTR_NUM(ulong, "%lu")
OSTD_TOSTR_NUM(llong, "%lld")
OSTD_TOSTR_NUM(ullong, "%llu")
OSTD_TOSTR_NUM(ldouble, "%Lf")
#undef OSTD_TOSTR_NUM
2015-06-04 00:07:57 +02:00
2015-06-04 23:57:06 +02:00
template<typename T> struct ToString<T *> {
2015-06-08 01:55:08 +02:00
using Argument = T *;
using Result = String;
2015-06-04 00:07:57 +02:00
String operator()(Argument v) {
String ret;
detail::str_printf(ret, "%p", v);
return ret;
2015-05-28 20:58:05 +02:00
}
2015-06-04 00:07:57 +02:00
};
2015-05-28 20:58:05 +02:00
2015-07-10 02:17:08 +02:00
template<> struct ToString<const char *> {
using Argument = const char *;
using Result = String;
String operator()(const char *s) {
return String(s);
}
};
2015-07-11 21:18:46 +02:00
template<> struct ToString<char *> {
using Argument = char *;
using Result = String;
String operator()(char *s) {
return String(s);
}
};
2015-06-04 00:07:57 +02:00
template<> struct ToString<String> {
using Argument = String;
2015-06-08 01:55:08 +02:00
using Result = String;
String operator()(const Argument &s) {
2015-06-04 00:07:57 +02:00
return s;
}
};
template<> struct ToString<StringRange> {
using Argument = StringRange;
using Result = String;
String operator()(const Argument &s) {
return String(s);
}
};
template<> struct ToString<ConstStringRange> {
using Argument = ConstStringRange;
using Result = String;
String operator()(const Argument &s) {
return String(s);
}
};
template<typename T, typename U> struct ToString<Pair<T, U>> {
using Argument = Pair<T, U>;
2015-06-08 01:55:08 +02:00
using Result = String;
String operator()(const Argument &v) {
2015-06-04 00:07:57 +02:00
String ret("{");
2015-07-11 21:18:46 +02:00
ret += ToString<RemoveReference<RemoveCv<T>>>()(v.first);
2015-06-04 00:07:57 +02:00
ret += ", ";
2015-07-11 21:18:46 +02:00
ret += ToString<RemoveReference<RemoveCv<U>>>()(v.second);
2015-06-04 00:07:57 +02:00
ret += "}";
return ret;
2015-05-28 02:26:48 +02:00
}
2015-06-04 00:07:57 +02:00
};
template<typename T>
typename ToString<T>::Result to_string(const T &v) {
2015-07-11 21:18:46 +02:00
return ToString<RemoveReference<RemoveCv<T>>>()(v);
2015-05-27 22:43:13 +02:00
}
2015-06-04 23:57:06 +02:00
template<typename T>
String to_string(std::initializer_list<T> init) {
2015-07-11 22:54:58 +02:00
return to_string(iter(init));
2015-06-04 00:07:57 +02:00
}
2015-07-13 21:07:14 +02:00
} /* namespace ostd */
2015-06-04 00:07:57 +02:00
2015-05-27 22:43:13 +02:00
#endif