make most funcs taking strings allocator independent

master
Daniel Kolesa 2015-07-01 20:09:02 +01:00
parent ea1325543d
commit 1de58ac2b6
3 changed files with 71 additions and 33 deletions

View File

@ -31,8 +31,9 @@ namespace detail {
/* OctaSTD string */
template<typename R>
static inline octa::Size write_val(R &writer, const octa::String &val) {
template<typename R, typename A>
static inline octa::Size
write_val(R &writer, const octa::AnyString<A> &val) {
for (octa::Size i = 0; i < val.size(); ++i) writer.put(val[i]);
return val.size();
}
@ -141,8 +142,9 @@ static inline octa::Size formatted_write(R writer, octa::Size &fmtn,
return written;
}
template<typename R, typename ...A>
octa::Size formatted_write(R writer, octa::Size &fmtn, const octa::String &fmt,
template<typename R, typename AL, typename ...A>
octa::Size formatted_write(R writer, octa::Size &fmtn,
const octa::AnyString<AL> &fmt,
const A &...args) {
return formatted_write(writer, fmtn, fmt.data(), args...);
}
@ -153,8 +155,8 @@ octa::Size formatted_write(R writer, const char *fmt, const A &...args) {
return formatted_write(writer, fmtn, fmt, args...);
}
template<typename R, typename ...A>
octa::Size formatted_write(R writer, const octa::String &fmt,
template<typename R, typename AL, typename ...A>
octa::Size formatted_write(R writer, const octa::AnyString<AL> &fmt,
const A &...args) {
octa::Size fmtn = 0;
return formatted_write(writer, fmtn, fmt.data(), args...);

View File

@ -33,7 +33,12 @@ struct FileStream: Stream {
s.p_owned = false;
}
FileStream(const octa::String &path, StreamMode mode): p_f() {
FileStream(const char *path, StreamMode mode): p_f() {
open(path, mode);
}
template<typename A>
FileStream(const octa::AnyString<A> &path, StreamMode mode): p_f() {
open(path, mode);
}
@ -48,13 +53,18 @@ struct FileStream: Stream {
return *this;
}
bool open(const octa::String &path, StreamMode mode) {
bool open(const char *path, StreamMode mode) {
if (p_f) return false;
p_f = fopen(path.data(), octa::detail::filemodes[octa::Size(mode)]);
p_f = fopen(path, octa::detail::filemodes[octa::Size(mode)]);
p_owned = true;
return is_open();
}
template<typename A>
bool open(const octa::AnyString<A> &path, StreamMode mode) {
return open(path.data(), mode);
}
bool open(FILE *f) {
if (p_f) return false;
p_f = f;
@ -115,7 +125,8 @@ static inline void write(const char *s) {
fputs(s, ::stdout);
}
static inline void write(const octa::String &s) {
template<typename A>
static inline void write(const octa::AnyString<A> &s) {
fwrite(s.data(), 1, s.size(), ::stdout);
}
@ -135,7 +146,8 @@ static inline void writeln(const char *s) {
putc('\n', ::stdout);
}
static inline void writeln(const octa::String &s) {
template<typename A>
static inline void writeln(const octa::AnyString<A> &s) {
octa::write(s);
putc('\n', ::stdout);
}
@ -168,8 +180,9 @@ static inline void writef(const char *fmt, const A &...args) {
fwrite(s.data(), 1, need, ::stdout);
}
template<typename ...A>
static inline void writef(const octa::String &fmt, const A &...args) {
template<typename AL, typename ...A>
static inline void writef(const octa::AnyString<AL> &fmt,
const A &...args) {
writef(fmt.data(), args...);
}
@ -179,8 +192,9 @@ static inline void writefln(const char *fmt, const A &...args) {
putc('\n', ::stdout);
}
template<typename ...A>
static inline void writefln(const octa::String &fmt, const A &...args) {
template<typename AL, typename ...A>
static inline void writefln(const octa::AnyString<AL> &fmt,
const A &...args) {
writef(fmt, args...);
putc('\n', ::stdout);
}

View File

@ -27,7 +27,9 @@ struct StringRangeBase: InputRange<
StringRangeBase(T *beg, octa::Size n): p_beg(beg), p_end(beg + n) {}
/* TODO: traits for utf-16/utf-32 string lengths, for now assume char */
StringRangeBase(T *beg): p_beg(beg), p_end(beg + strlen(beg)) {}
StringRangeBase(const StringBase<T> &s): p_beg(s.data()),
template<typename A>
StringRangeBase(const StringBase<T, A> &s): p_beg(s.data()),
p_end(s.data() + s.size()) {}
template<typename U, typename = octa::EnableIf<
@ -38,7 +40,9 @@ struct StringRangeBase: InputRange<
StringRangeBase &operator=(const StringRangeBase &v) {
p_beg = v.p_beg; p_end = v.p_end; return *this;
}
StringRangeBase &operator=(const StringBase<T> &s) {
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 */
@ -335,29 +339,44 @@ using String = StringBase<char>;
using StringRange = StringRangeBase<char>;
using ConstStringRange = StringRangeBase<const char>;
static inline bool operator==(const String &lhs, const String &rhs) {
template<typename A> using AnyString = StringBase<char, A>;
template<typename T, typename A>
static inline bool operator==(const StringBase<T, A> &lhs,
const StringBase<T, A> &rhs) {
return !lhs.compare(rhs);
}
static inline bool operator==(const String &lhs, const char *rhs) {
template<typename T, typename A>
static inline bool operator==(const StringBase<T, A> &lhs,
const char *rhs) {
return !lhs.compare(rhs);
}
static inline bool operator==(const char *lhs, const String &rhs) {
template<typename T, typename A>
static inline bool operator==(const char *lhs,
const StringBase<T, A> &rhs) {
return !rhs.compare(lhs);
}
static inline bool operator!=(const String &lhs, const String &rhs) {
template<typename T, typename A>
static inline bool operator!=(const StringBase<T, A> &lhs,
const StringBase<T, A> &rhs) {
return !!lhs.compare(rhs);
}
static inline bool operator!=(const String &lhs, const char *rhs) {
template<typename T, typename A>
static inline bool operator!=(const StringBase<T, A> &lhs,
const char *rhs) {
return !!lhs.compare(rhs);
}
static inline bool operator!=(const char *lhs, const String &rhs) {
template<typename T, typename A>
static inline bool operator!=(const char *lhs,
const StringBase<T, A> &rhs) {
return !!rhs.compare(lhs);
}
template<typename T, typename F>
String concat(const T &v, const String &sep, F func) {
String ret;
template<typename T, typename F, typename S = const char *,
typename A = typename String::Allocator>
AnyString<A> concat(const T &v, const S &sep, F func) {
AnyString<A> ret;
auto range = octa::iter(v);
if (range.empty()) return ret;
for (;;) {
@ -369,9 +388,10 @@ String concat(const T &v, const String &sep, F func) {
return ret;
}
template<typename T>
String concat(const T &v, const String &sep = " ") {
String ret;
template<typename T, typename S = const char *,
typename A = typename String::Allocator>
AnyString<A> concat(const T &v, const S &sep = " ") {
AnyString<A> ret;
auto range = octa::iter(v);
if (range.empty()) return ret;
for (;;) {
@ -383,13 +403,15 @@ String concat(const T &v, const String &sep = " ") {
return ret;
}
template<typename T, typename F>
String concat(std::initializer_list<T> v, const String &sep, F func) {
template<typename T, typename F, typename S = const char *,
typename A = typename String::Allocator>
AnyString<A> concat(std::initializer_list<T> v, const S &sep, F func) {
return concat(octa::iter(v), sep, func);
}
template<typename T>
String concat(std::initializer_list<T> v, const String &sep = " ") {
template<typename T, typename S = const char *,
typename A = typename String::Allocator>
AnyString<A> concat(std::initializer_list<T> v, const S &sep = " ") {
return concat(octa::iter(v), sep);
}