From caae1e38d0d239f4f7088a64526fe1d2f6587999 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Thu, 26 Sep 2013 23:55:29 -0700 Subject: Split string header into pieces --- src/strings/vstring.tcc | 143 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 src/strings/vstring.tcc (limited to 'src/strings/vstring.tcc') diff --git a/src/strings/vstring.tcc b/src/strings/vstring.tcc new file mode 100644 index 0000000..674b21d --- /dev/null +++ b/src/strings/vstring.tcc @@ -0,0 +1,143 @@ +// strings/vstring.tcc - Inline functions for vstring.hpp +// +// Copyright © 2013 Ben Longbons +// +// This file is part of The Mana World (Athena server) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include + +#include "../common/utils2.hpp" + +#include "fstring.hpp" +#include "tstring.hpp" +#include "sstring.hpp" +#include "zstring.hpp" +#include "xstring.hpp" + +namespace strings +{ + template + VString::VString(XString x) : _data(), _special() + { + if (x.size() > n) + // we're hoping this doesn't happen + // hopefully there will be few enough users of this class + x = x.xslice_h(n); + char *e = std::copy(x.begin(), x.end(), std::begin(_data)); + _special = std::end(_data) - e; + assert (_special == n - x.size()); // 0 when it needs to be + } + // poor man's delegated constructors + // needed for gcc 4.6 compatibility + template + VString::VString(FString f) + { + *this = XString(f); + } + template + VString::VString(TString t) + { + *this = XString(t); + } + template + VString::VString(SString s) + { + *this = XString(s); + } + template + VString::VString(ZString z) + { + *this = XString(z); + } + template + template + VString::VString(VString v) + { + static_assert(m < n, "can only grow"); + *this = XString(v); + } + template + template + VString::VString(const char (&s)[m]) + { + static_assert(m <= n + 1, "string would truncate"); + *this = XString(s); + } + template + VString::VString(decltype(really_construct_from_a_pointer) e, const char *s) + { + *this = XString(e, s, nullptr); + } + template + VString::VString(char c) + { + *this = XString(&c, &c + 1, nullptr); + } + template + VString::VString() + { + *this = XString(); + } + template + VString::VString(XPair p) + { + *this = XString(p); + } + + // hopefully this is obvious + template + typename VString::iterator VString::begin() const + { + return std::begin(_data); + } + template + typename VString::iterator VString::end() const + { + return std::end(_data) - _special; + } + template + const FString *VString::base() const + { + return nullptr; + } + template + const char *VString::c_str() const + { + return &*begin(); + } + + // cxxstdio helpers + // I think the conversion will happen automatically. TODO test this. + // Nope, it doesn't, since there's a template + // Actually, it might now. + template + inline + const char *decay_for_printf(const VString& vs) + { + return vs.c_str(); + } + + template + inline + int do_vprint(VString& out, const char *fmt, va_list ap) + { + char buffer[len + 1]; + vsnprintf(buffer, len + 1, fmt, ap); + + out = const_(buffer); + return len; + } +} // namespace strings -- cgit v1.2.3-60-g2f50