diff --git a/octa/format.hh b/octa/format.hh index 09462a9..6ea8fe2 100644 --- a/octa/format.hh +++ b/octa/format.hh @@ -31,8 +31,9 @@ namespace detail { /* OctaSTD string */ - template - static inline octa::Size write_val(R &writer, const octa::String &val) { + template + static inline octa::Size + write_val(R &writer, const octa::AnyString &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 -octa::Size formatted_write(R writer, octa::Size &fmtn, const octa::String &fmt, +template +octa::Size formatted_write(R writer, octa::Size &fmtn, + const octa::AnyString &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 -octa::Size formatted_write(R writer, const octa::String &fmt, +template +octa::Size formatted_write(R writer, const octa::AnyString &fmt, const A &...args) { octa::Size fmtn = 0; return formatted_write(writer, fmtn, fmt.data(), args...); diff --git a/octa/io.hh b/octa/io.hh index 2ea5d3f..7c7cc0d 100644 --- a/octa/io.hh +++ b/octa/io.hh @@ -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 + FileStream(const octa::AnyString &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 + bool open(const octa::AnyString &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 +static inline void write(const octa::AnyString &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 +static inline void writeln(const octa::AnyString &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 -static inline void writef(const octa::String &fmt, const A &...args) { +template +static inline void writef(const octa::AnyString &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 -static inline void writefln(const octa::String &fmt, const A &...args) { +template +static inline void writefln(const octa::AnyString &fmt, + const A &...args) { writef(fmt, args...); putc('\n', ::stdout); } diff --git a/octa/string.hh b/octa/string.hh index c8f0ffa..b41c1f6 100644 --- a/octa/string.hh +++ b/octa/string.hh @@ -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 &s): p_beg(s.data()), + + template + StringRangeBase(const StringBase &s): p_beg(s.data()), p_end(s.data() + s.size()) {} template &s) { + + template + StringRangeBase &operator=(const StringBase &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; using StringRange = StringRangeBase; using ConstStringRange = StringRangeBase; -static inline bool operator==(const String &lhs, const String &rhs) { +template using AnyString = StringBase; + +template +static inline bool operator==(const StringBase &lhs, + const StringBase &rhs) { return !lhs.compare(rhs); } -static inline bool operator==(const String &lhs, const char *rhs) { +template +static inline bool operator==(const StringBase &lhs, + const char *rhs) { return !lhs.compare(rhs); } -static inline bool operator==(const char *lhs, const String &rhs) { +template +static inline bool operator==(const char *lhs, + const StringBase &rhs) { return !rhs.compare(lhs); } -static inline bool operator!=(const String &lhs, const String &rhs) { +template +static inline bool operator!=(const StringBase &lhs, + const StringBase &rhs) { return !!lhs.compare(rhs); } -static inline bool operator!=(const String &lhs, const char *rhs) { +template +static inline bool operator!=(const StringBase &lhs, + const char *rhs) { return !!lhs.compare(rhs); } -static inline bool operator!=(const char *lhs, const String &rhs) { +template +static inline bool operator!=(const char *lhs, + const StringBase &rhs) { return !!rhs.compare(lhs); } -template -String concat(const T &v, const String &sep, F func) { - String ret; +template +AnyString concat(const T &v, const S &sep, F func) { + AnyString 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 -String concat(const T &v, const String &sep = " ") { - String ret; +template +AnyString concat(const T &v, const S &sep = " ") { + AnyString 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 -String concat(std::initializer_list v, const String &sep, F func) { +template +AnyString concat(std::initializer_list v, const S &sep, F func) { return concat(octa::iter(v), sep, func); } -template -String concat(std::initializer_list v, const String &sep = " ") { +template +AnyString concat(std::initializer_list v, const S &sep = " ") { return concat(octa::iter(v), sep); }