summaryrefslogtreecommitdiff
path: root/src/strings
diff options
context:
space:
mode:
Diffstat (limited to 'src/strings')
-rw-r--r--src/strings/astring.cpp7
-rw-r--r--src/strings/astring.hpp7
-rw-r--r--src/strings/astring.tcc17
-rw-r--r--src/strings/base_test.cpp22
-rw-r--r--src/strings/fwd.hpp12
-rw-r--r--src/strings/literal.cpp56
-rw-r--r--src/strings/literal.hpp82
-rw-r--r--src/strings/pair.hpp21
-rw-r--r--src/strings/rstring.cpp5
-rw-r--r--src/strings/rstring.hpp7
-rw-r--r--src/strings/rstring.tcc6
-rw-r--r--src/strings/sstring.cpp4
-rw-r--r--src/strings/sstring.hpp5
-rw-r--r--src/strings/sstring.tcc5
-rw-r--r--src/strings/strings2_test.cpp86
-rw-r--r--src/strings/strings_test.cpp60
-rw-r--r--src/strings/tstring.cpp4
-rw-r--r--src/strings/tstring.hpp5
-rw-r--r--src/strings/tstring.tcc4
-rw-r--r--src/strings/vstring.hpp5
-rw-r--r--src/strings/vstring.tcc8
-rw-r--r--src/strings/xstring.cpp3
-rw-r--r--src/strings/xstring.hpp6
-rw-r--r--src/strings/xstring.tcc4
-rw-r--r--src/strings/zstring.cpp5
-rw-r--r--src/strings/zstring.hpp6
-rw-r--r--src/strings/zstring.tcc5
27 files changed, 282 insertions, 175 deletions
diff --git a/src/strings/astring.cpp b/src/strings/astring.cpp
index f1e9030..0906584 100644
--- a/src/strings/astring.cpp
+++ b/src/strings/astring.cpp
@@ -156,6 +156,13 @@ namespace strings
special = 255 - x.size();
}
}
+ AString::AString(LString l)
+ : data{}, special()
+ {
+ new(r_ptr()) RString();
+ special = 255;
+ *this = XString(l);
+ }
AString::iterator AString::begin() const
{
diff --git a/src/strings/astring.hpp b/src/strings/astring.hpp
index aab14cf..b0fb196 100644
--- a/src/strings/astring.hpp
+++ b/src/strings/astring.hpp
@@ -49,12 +49,6 @@ namespace strings
explicit AString(const MString& s);
- template<size_t n>
- AString(char (&s)[n]) = delete;
-
- template<size_t n>
- AString(const char (&s)[n]);
-
template<class It>
AString(It b, It e);
@@ -66,6 +60,7 @@ namespace strings
AString(XString);
template<uint8_t n>
AString(const VString<n>& v);
+ AString(LString s);
iterator begin() const;
iterator end() const;
diff --git a/src/strings/astring.tcc b/src/strings/astring.tcc
index ed07bd9..f9606d8 100644
--- a/src/strings/astring.tcc
+++ b/src/strings/astring.tcc
@@ -21,23 +21,6 @@
namespace strings
{
- template<size_t n>
- AString::AString(const char (&s)[n])
- : data{}, special()
- {
- XPair x = s;
- if (x.size() > 255 || x.size() == 0)
- {
- new(r_ptr()) RString(x);
- special = 255;
- }
- else
- {
- *std::copy(x.begin(), x.end(), data) = '\0';
- special = 255 - x.size();
- }
- }
-
template<class It>
AString::AString(It b, It e)
: data{}, special()
diff --git a/src/strings/base_test.cpp b/src/strings/base_test.cpp
index 52c44dc..524450d 100644
--- a/src/strings/base_test.cpp
+++ b/src/strings/base_test.cpp
@@ -31,18 +31,18 @@ using namespace strings;
struct _test : VString<1> {};
struct _test2 : VString<1> {};
-static_assert(string_comparison_allowed<_test, _test>::value, "tt");
-static_assert(string_comparison_allowed<VString<1>, VString<1>>::value, "vv");
-static_assert(!string_comparison_allowed<_test, XString>::value, "tx");
-static_assert(!string_comparison_allowed<_test, VString<1>>::value, "tv");
-static_assert(!string_comparison_allowed<_test, _test2>::value, "t2");
-static_assert(string_comparison_allowed<VString<1>, XString>::value, "vx");
-static_assert(string_comparison_allowed<XString, XString>::value, "xx");
-static_assert(string_comparison_allowed<XString, RString>::value, "xf");
+static_assert(string_comparison_allowed<_test, _test>::value, "tt"_s);
+static_assert(string_comparison_allowed<VString<1>, VString<1>>::value, "vv"_s);
+static_assert(!string_comparison_allowed<_test, XString>::value, "tx"_s);
+static_assert(!string_comparison_allowed<_test, VString<1>>::value, "tv"_s);
+static_assert(!string_comparison_allowed<_test, _test2>::value, "t2"_s);
+static_assert(string_comparison_allowed<VString<1>, XString>::value, "vx"_s);
+static_assert(string_comparison_allowed<XString, XString>::value, "xx"_s);
+static_assert(string_comparison_allowed<XString, RString>::value, "xf"_s);
TEST(strings, contains)
{
- XString hi = "Hello";
- EXPECT_TRUE(hi.contains_any("Hi"));
- EXPECT_FALSE(hi.contains_any("hi"));
+ XString hi = "Hello"_s;
+ EXPECT_TRUE(hi.contains_any("Hi"_s));
+ EXPECT_FALSE(hi.contains_any("hi"_s));
}
diff --git a/src/strings/fwd.hpp b/src/strings/fwd.hpp
index 4c58e03..0e824c8 100644
--- a/src/strings/fwd.hpp
+++ b/src/strings/fwd.hpp
@@ -21,6 +21,7 @@
# include "../sanity.hpp"
+# include <cstddef>
# include <cstdint>
// It is a common mistake to assume that one string class for everything.
@@ -40,12 +41,18 @@ namespace strings
class XString;
// semi-owning
+ class LString;
+ class FormatString;
template<uint8_t len>
class VString;
// refactor this into a function?
enum _type_that_just_has_a_name_to_fix_linkage
{ really_construct_from_a_pointer };
+
+ LString operator "" _s(const char *, size_t);
+ constexpr
+ FormatString operator "" _fmt(const char *, size_t);
} // namespace strings
using strings::MString;
@@ -57,6 +64,11 @@ using strings::SString;
using strings::ZString;
using strings::XString;
+using strings::LString;
+using strings::FormatString;
using strings::VString;
+using strings::operator "" _s;
+using strings::operator "" _fmt;
+
#endif // TMWA_STRINGS_FWD_HPP
diff --git a/src/strings/literal.cpp b/src/strings/literal.cpp
new file mode 100644
index 0000000..974a8bd
--- /dev/null
+++ b/src/strings/literal.cpp
@@ -0,0 +1,56 @@
+#include "literal.hpp"
+// strings/literal.cpp - A string stored in the readonly data segment.
+//
+// Copyright © 2014 Ben Longbons <b.r.longbons@gmail.com>
+//
+// 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 <http://www.gnu.org/licenses/>.
+
+#include "../poison.hpp"
+
+namespace strings
+{
+ LString::LString(const char *b, const char *e)
+ : _b(b), _e(e)
+ {}
+
+ LString::iterator LString::begin() const
+ {
+ return _b;
+ }
+ LString::iterator LString::end() const
+ {
+ return _e;
+ }
+ const RString *LString::base() const
+ {
+ return nullptr;
+ }
+ const char *LString::c_str() const
+ {
+ return &*begin();
+ }
+
+ const char *decay_for_printf(const LString& zs)
+ {
+ return zs.c_str();
+ }
+
+ __attribute__((format(scanf, 2, 0)))
+ int do_vscan(LString in, const char *fmt, va_list ap)
+ {
+ return vsscanf(in.c_str(), fmt, ap);
+ }
+} // namespace strings
diff --git a/src/strings/literal.hpp b/src/strings/literal.hpp
new file mode 100644
index 0000000..c5b9938
--- /dev/null
+++ b/src/strings/literal.hpp
@@ -0,0 +1,82 @@
+#ifndef TMWA_STRINGS_LITERAL_HPP
+#define TMWA_STRINGS_LITERAL_HPP
+// strings/literal.hpp - A string stored in the readonly data segment.
+//
+// Copyright © 2014 Ben Longbons <b.r.longbons@gmail.com>
+//
+// 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 <http://www.gnu.org/licenses/>.
+
+# include "../sanity.hpp"
+
+# include <cstring>
+
+# include "base.hpp"
+
+namespace strings
+{
+ /// A statically owned string that is guaranteed to be NUL-terminated.
+ /// This is a more permissive lifetime than anybody else has.
+ class LString : public _crtp_string<LString, AString, LPair>
+ {
+ iterator _b, _e;
+ // optional
+ const RString *_base;
+ private:
+ LString(const char *b, const char *e);
+ friend LString operator "" _s(const char *, size_t);
+ public:
+
+ iterator begin() const;
+ iterator end() const;
+ const RString *base() const;
+ const char *c_str() const;
+ };
+
+ class FormatString
+ {
+ const char *_format;
+
+ friend constexpr FormatString operator "" _fmt(const char *, size_t);
+ constexpr explicit
+ FormatString(const char *f) : _format(f) {}
+ public:
+ constexpr
+ const char *format_string() { return _format; }
+ };
+
+
+ // 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.
+ const char *decay_for_printf(const LString& zs);
+
+ __attribute__((format(scanf, 2, 0)))
+ int do_vscan(LString in, const char *fmt, va_list ap);
+
+ inline
+ LString operator "" _s(const char *s, size_t)
+ {
+ return LString(s, s + __builtin_strlen(s));
+ }
+ constexpr
+ FormatString operator "" _fmt(const char *s, size_t)
+ {
+ return FormatString(s);
+ }
+} // namespace strings
+
+#endif // TMWA_STRINGS_LSTRING_HPP
diff --git a/src/strings/pair.hpp b/src/strings/pair.hpp
index a592a91..24537de 100644
--- a/src/strings/pair.hpp
+++ b/src/strings/pair.hpp
@@ -2,7 +2,7 @@
#define TMWA_STRINGS_PAIR_HPP
// strings/pair.hpp - Internal contiguous range.
//
-// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
+// Copyright © 2013-2014 Ben Longbons <b.r.longbons@gmail.com>
//
// This file is part of The Mana World (Athena server)
//
@@ -40,12 +40,6 @@ namespace strings
XPair(const char *b, const char *e)
: _begin(b), _end(e)
{}
- template<size_t n>
- XPair(char (&arr)[n]) = delete;
- template<size_t n>
- XPair(const char (&arr)[n])
- : _begin(arr), _end(arr + strlen(arr))
- {}
const char *begin() const { return _begin; }
const char *end() const { return _end; }
@@ -59,11 +53,14 @@ namespace strings
ZPair(const char *b, const char *e)
: XPair(b, e)
{}
- template<size_t n>
- ZPair(char (&arr)[n]) = delete;
- template<size_t n>
- ZPair(const char (&arr)[n])
- : XPair(arr)
+ };
+ struct LPair : ZPair
+ {
+ typedef LString TailSlice;
+ typedef XString FullSlice;
+
+ LPair(const char *b, const char *e)
+ : ZPair(b, e)
{}
};
} // namespace strings
diff --git a/src/strings/rstring.cpp b/src/strings/rstring.cpp
index c0e231e..671e7b0 100644
--- a/src/strings/rstring.cpp
+++ b/src/strings/rstring.cpp
@@ -125,6 +125,11 @@ namespace strings
else
_assign(x.begin(), x.end());
}
+ RString::RString(LString l)
+ : owned(nullptr)
+ {
+ *this = XString(l);
+ }
RString::iterator RString::begin() const
{
diff --git a/src/strings/rstring.hpp b/src/strings/rstring.hpp
index 7cb19d6..1de4db0 100644
--- a/src/strings/rstring.hpp
+++ b/src/strings/rstring.hpp
@@ -56,12 +56,6 @@ namespace strings
explicit RString(const MString& s);
- template<size_t n>
- RString(char (&s)[n]) = delete;
-
- template<size_t n>
- RString(const char (&s)[n]);
-
template<class It>
RString(It b, It e);
@@ -73,6 +67,7 @@ namespace strings
RString(XString);
template<uint8_t n>
RString(const VString<n>& v);
+ RString(LString s);
iterator begin() const;
iterator end() const;
diff --git a/src/strings/rstring.tcc b/src/strings/rstring.tcc
index 8b4c0c0..6bfc7b0 100644
--- a/src/strings/rstring.tcc
+++ b/src/strings/rstring.tcc
@@ -47,12 +47,6 @@ namespace strings
owned->body[diff] = '\0';
}
- template<size_t n>
- RString::RString(const char (&s)[n])
- {
- _assign(s, s + strlen(s));
- }
-
template<class It>
RString::RString(It b, It e)
{
diff --git a/src/strings/sstring.cpp b/src/strings/sstring.cpp
index 76f0994..8de8655 100644
--- a/src/strings/sstring.cpp
+++ b/src/strings/sstring.cpp
@@ -54,6 +54,10 @@ namespace strings
else
*this = RString(x);
}
+ SString::SString(const LString& l)
+ {
+ *this = XString(l);
+ }
SString::SString(RString r, size_t b, size_t e)
: _s(std::move(r)), _b(b), _e(e)
diff --git a/src/strings/sstring.hpp b/src/strings/sstring.hpp
index 7166e94..0594499 100644
--- a/src/strings/sstring.hpp
+++ b/src/strings/sstring.hpp
@@ -42,10 +42,7 @@ namespace strings
SString(const XString&);
template<uint8_t n>
SString(const VString<n>& v);
- template<size_t n>
- SString(char (&s)[n]) = delete;
- template<size_t n>
- SString(const char (&s)[n]);
+ SString(const LString&);
//template<class It>
//SString(It b, It e) : _s(b, e), _b(0), _e(_s.size()) {}
SString(RString f, size_t b, size_t e);
diff --git a/src/strings/sstring.tcc b/src/strings/sstring.tcc
index 4be33dd..89effbc 100644
--- a/src/strings/sstring.tcc
+++ b/src/strings/sstring.tcc
@@ -25,9 +25,4 @@ namespace strings
SString::SString(const VString<n>& v)
: _s(v), _b(0), _e(_s.size())
{}
-
- template<size_t n>
- SString::SString(const char (&s)[n])
- : _s(s), _b(0), _e(_s.size())
- {}
} // namespace strings
diff --git a/src/strings/strings2_test.cpp b/src/strings/strings2_test.cpp
index e5d5281..8c4a343 100644
--- a/src/strings/strings2_test.cpp
+++ b/src/strings/strings2_test.cpp
@@ -25,23 +25,23 @@
TEST(StringTests, traits2)
{
- ZString print_non = "\t\e";
- ZString print_mix = "n\t";
- RString print_all = "n ";
+ ZString print_non = "\t\e"_s;
+ ZString print_mix = "n\t"_s;
+ RString print_all = "n "_s;
EXPECT_FALSE(print_non.has_print());
EXPECT_TRUE(print_mix.has_print());
EXPECT_TRUE(print_all.has_print());
EXPECT_FALSE(print_non.is_print());
EXPECT_FALSE(print_mix.is_print());
EXPECT_TRUE(print_all.is_print());
- EXPECT_EQ("__", print_non.to_print());
- EXPECT_EQ("n_", print_mix.to_print());
- EXPECT_EQ("n ", print_all.to_print());
+ EXPECT_EQ("__"_s, print_non.to_print());
+ EXPECT_EQ("n_"_s, print_mix.to_print());
+ EXPECT_EQ("n "_s, print_all.to_print());
EXPECT_EQ(print_all.begin(), print_all.to_print().begin());
- ZString graph_non = " \e";
- ZString graph_mix = "n ";
- RString graph_all = "n.";
+ ZString graph_non = " \e"_s;
+ ZString graph_mix = "n "_s;
+ RString graph_all = "n."_s;
EXPECT_FALSE(graph_non.has_graph());
EXPECT_TRUE(graph_mix.has_graph());
EXPECT_TRUE(graph_all.has_graph());
@@ -49,37 +49,37 @@ TEST(StringTests, traits2)
EXPECT_FALSE(graph_mix.is_graph());
EXPECT_TRUE(graph_all.is_graph());
- ZString lower_non = "0A";
- ZString lower_mix = "Oa";
- RString lower_all = "oa";
+ ZString lower_non = "0A"_s;
+ ZString lower_mix = "Oa"_s;
+ RString lower_all = "oa"_s;
EXPECT_FALSE(lower_non.has_lower());
EXPECT_TRUE(lower_mix.has_lower());
EXPECT_TRUE(lower_all.has_lower());
EXPECT_FALSE(lower_non.is_lower());
EXPECT_FALSE(lower_mix.is_lower());
EXPECT_TRUE(lower_all.is_lower());
- EXPECT_EQ("0a", lower_non.to_lower());
- EXPECT_EQ("oa", lower_mix.to_lower());
- EXPECT_EQ("oa", lower_all.to_lower());
+ EXPECT_EQ("0a"_s, lower_non.to_lower());
+ EXPECT_EQ("oa"_s, lower_mix.to_lower());
+ EXPECT_EQ("oa"_s, lower_all.to_lower());
EXPECT_EQ(lower_all.begin(), lower_all.to_lower().begin());
- ZString upper_non = "0a";
- ZString upper_mix = "oA";
- RString upper_all = "OA";
+ ZString upper_non = "0a"_s;
+ ZString upper_mix = "oA"_s;
+ RString upper_all = "OA"_s;
EXPECT_FALSE(upper_non.has_upper());
EXPECT_TRUE(upper_mix.has_upper());
EXPECT_TRUE(upper_all.has_upper());
EXPECT_FALSE(upper_non.is_upper());
EXPECT_FALSE(upper_mix.is_upper());
EXPECT_TRUE(upper_all.is_upper());
- EXPECT_EQ("0A", upper_non.to_upper());
- EXPECT_EQ("OA", upper_mix.to_upper());
- EXPECT_EQ("OA", upper_all.to_upper());
+ EXPECT_EQ("0A"_s, upper_non.to_upper());
+ EXPECT_EQ("OA"_s, upper_mix.to_upper());
+ EXPECT_EQ("OA"_s, upper_all.to_upper());
EXPECT_EQ(upper_all.begin(), upper_all.to_upper().begin());
- ZString alpha_non = " 0";
- ZString alpha_mix = "n ";
- RString alpha_all = "nA";
+ ZString alpha_non = " 0"_s;
+ ZString alpha_mix = "n "_s;
+ RString alpha_all = "nA"_s;
EXPECT_FALSE(alpha_non.has_alpha());
EXPECT_TRUE(alpha_mix.has_alpha());
EXPECT_TRUE(alpha_all.has_alpha());
@@ -87,9 +87,9 @@ TEST(StringTests, traits2)
EXPECT_FALSE(alpha_mix.is_alpha());
EXPECT_TRUE(alpha_all.is_alpha());
- ZString digit2_non = "a9";
- ZString digit2_mix = "20";
- RString digit2_all = "01";
+ ZString digit2_non = "a9"_s;
+ ZString digit2_mix = "20"_s;
+ RString digit2_all = "01"_s;
EXPECT_FALSE(digit2_non.has_digit2());
EXPECT_TRUE(digit2_mix.has_digit2());
EXPECT_TRUE(digit2_all.has_digit2());
@@ -97,9 +97,9 @@ TEST(StringTests, traits2)
EXPECT_FALSE(digit2_mix.is_digit2());
EXPECT_TRUE(digit2_all.is_digit2());
- ZString digit8_non = "a9";
- ZString digit8_mix = "80";
- RString digit8_all = "37";
+ ZString digit8_non = "a9"_s;
+ ZString digit8_mix = "80"_s;
+ RString digit8_all = "37"_s;
EXPECT_FALSE(digit8_non.has_digit8());
EXPECT_TRUE(digit8_mix.has_digit8());
EXPECT_TRUE(digit8_all.has_digit8());
@@ -107,9 +107,9 @@ TEST(StringTests, traits2)
EXPECT_FALSE(digit8_mix.is_digit8());
EXPECT_TRUE(digit8_all.is_digit8());
- ZString digit10_non = "az";
- ZString digit10_mix = "a9";
- RString digit10_all = "42";
+ ZString digit10_non = "az"_s;
+ ZString digit10_mix = "a9"_s;
+ RString digit10_all = "42"_s;
EXPECT_FALSE(digit10_non.has_digit10());
EXPECT_TRUE(digit10_mix.has_digit10());
EXPECT_TRUE(digit10_all.has_digit10());
@@ -117,9 +117,9 @@ TEST(StringTests, traits2)
EXPECT_FALSE(digit10_mix.is_digit10());
EXPECT_TRUE(digit10_all.is_digit10());
- ZString digit16_non = "gz";
- ZString digit16_mix = "ao";
- RString digit16_all = "be";
+ ZString digit16_non = "gz"_s;
+ ZString digit16_mix = "ao"_s;
+ RString digit16_all = "be"_s;
EXPECT_FALSE(digit16_non.has_digit16());
EXPECT_TRUE(digit16_mix.has_digit16());
EXPECT_TRUE(digit16_all.has_digit16());
@@ -127,9 +127,9 @@ TEST(StringTests, traits2)
EXPECT_FALSE(digit16_mix.is_digit16());
EXPECT_TRUE(digit16_all.is_digit16());
- ZString alnum_non = " .";
- ZString alnum_mix = "n ";
- RString alnum_all = "n0";
+ ZString alnum_non = " ."_s;
+ ZString alnum_mix = "n "_s;
+ RString alnum_all = "n0"_s;
EXPECT_FALSE(alnum_non.has_alnum());
EXPECT_TRUE(alnum_mix.has_alnum());
EXPECT_TRUE(alnum_all.has_alnum());
@@ -140,7 +140,7 @@ TEST(StringTests, traits2)
TEST(StringTests, rempty)
{
- const char empty_text[] = "";
+ LString empty_text = ""_s;
RString r = empty_text;
EXPECT_EQ(r.size(), 0);
AString a = empty_text;
@@ -166,7 +166,7 @@ TEST(StringTests, rempty)
}
TEST(StringTests, rshort)
{
- const char short_text[] = "0123456789";
+ LString short_text = "0123456789"_s;
RString r = short_text;
EXPECT_EQ(r.size(), 10);
AString a = short_text;
@@ -193,13 +193,13 @@ TEST(StringTests, rshort)
TEST(StringTests, rlong)
{
- const char long_text[] =
+ LString long_text =
"01234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345 100"
"01234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345 200"
"01234567890123456789012345678901234567890123456789"
- "0123456789012345678901234567890123456789012345 300";
+ "0123456789012345678901234567890123456789012345 300"_s;
RString r = long_text;
EXPECT_EQ(r.size(), 300);
AString a = long_text;
diff --git a/src/strings/strings_test.cpp b/src/strings/strings_test.cpp
index dca463d..3abb5e1 100644
--- a/src/strings/strings_test.cpp
+++ b/src/strings/strings_test.cpp
@@ -32,10 +32,10 @@ TYPED_TEST_CASE_P(StringTest);
TYPED_TEST_P(StringTest, basic)
{
- TypeParam hi("Hello");
+ TypeParam hi("Hello"_s);
EXPECT_EQ(5, hi.size());
EXPECT_EQ(hi, hi);
- const char hi2[] = "Hello\0random garbage";
+ LString hi2 = "Hello\0random garbage"_s;
EXPECT_EQ(hi, hi2);
TypeParam hi0;
EXPECT_EQ(0, hi0.size());
@@ -47,9 +47,9 @@ TYPED_TEST_P(StringTest, basic)
TYPED_TEST_P(StringTest, order)
{
TypeParam a;
- TypeParam b("Hello");
- TypeParam c("Hello,");
- TypeParam d("World!");
+ TypeParam b("Hello"_s);
+ TypeParam c("Hello,"_s);
+ TypeParam d("World!"_s);
// not using EXPECT_LT, etc. for better visibility
@@ -158,7 +158,7 @@ TYPED_TEST_P(StringTest, order)
TYPED_TEST_P(StringTest, iterators)
{
- TypeParam hi("Hello");
+ TypeParam hi("Hello"_s);
EXPECT_EQ(hi.begin(), hi.begin());
EXPECT_NE(hi.begin(), hi.end());
EXPECT_EQ(5, std::distance(hi.begin(), hi.end()));
@@ -168,19 +168,19 @@ TYPED_TEST_P(StringTest, iterators)
TYPED_TEST_P(StringTest, xslice)
{
- TypeParam hi("Hello, World!");
- EXPECT_EQ(" World!", hi.xslice_t(6));
- EXPECT_EQ("Hello,", hi.xslice_h(6));
- EXPECT_EQ("World!", hi.xrslice_t(6));
- EXPECT_EQ("Hello, ", hi.xrslice_h(6));
+ TypeParam hi("Hello, World!"_s);
+ EXPECT_EQ(" World!"_s, hi.xslice_t(6));
+ EXPECT_EQ("Hello,"_s, hi.xslice_h(6));
+ EXPECT_EQ("World!"_s, hi.xrslice_t(6));
+ EXPECT_EQ("Hello, "_s, hi.xrslice_h(6));
typename TypeParam::iterator it = std::find(hi.begin(), hi.end(), ' ');
- EXPECT_EQ(" World!", hi.xislice_t(it));
- EXPECT_EQ("Hello,", hi.xislice_h(it));
- EXPECT_EQ("World", hi.xlslice(7, 5));
- EXPECT_EQ("World", hi.xpslice(7, 12));
- EXPECT_EQ("World", hi.xislice(hi.begin() + 7, hi.begin() + 12));
- EXPECT_TRUE(hi.startswith("Hello"));
- EXPECT_TRUE(hi.endswith("World!"));
+ EXPECT_EQ(" World!"_s, hi.xislice_t(it));
+ EXPECT_EQ("Hello,"_s, hi.xislice_h(it));
+ EXPECT_EQ("World"_s, hi.xlslice(7, 5));
+ EXPECT_EQ("World"_s, hi.xpslice(7, 12));
+ EXPECT_EQ("World"_s, hi.xislice(hi.begin() + 7, hi.begin() + 12));
+ EXPECT_TRUE(hi.startswith("Hello"_s));
+ EXPECT_TRUE(hi.endswith("World!"_s));
}
TYPED_TEST_P(StringTest, convert)
@@ -188,15 +188,15 @@ TYPED_TEST_P(StringTest, convert)
constexpr bool is_zstring = std::is_same<TypeParam, ZString>::value;
typedef typename std::conditional<is_zstring, TString, SString>::type Sstring;
typedef typename std::conditional<is_zstring, ZString, XString>::type Xstring;
- RString r = "r";
- AString a = "a";
- TString t = "t";
- Sstring s = "s";
- ZString z = "z";
- Xstring x = "x";
- VString<255> v = "v";
- const char l[] = "l";
- VString<5> hi = "hello";
+ RString r = "r"_s;
+ AString a = "a"_s;
+ TString t = "t"_s;
+ Sstring s = "s"_s;
+ ZString z = "z"_s;
+ Xstring x = "x"_s;
+ VString<255> v = "v"_s;
+ LString l = "l"_s;
+ VString<5> hi = "hello"_s;
TypeParam r2 = r;
TypeParam a2 = a;
@@ -270,7 +270,7 @@ INSTANTIATE_TYPED_TEST_CASE_P(StringStuff, StringTest, MostStringTypes);
TEST(VStringTest, basic)
{
- VString<5> hi = "Hello";
+ VString<5> hi = "Hello"_s;
EXPECT_EQ(5, hi.size());
EXPECT_EQ(hi, hi);
// truncation
@@ -278,7 +278,7 @@ TEST(VStringTest, basic)
EXPECT_EQ(5, hi2.size());
EXPECT_EQ(hi, hi2);
// short
- hi = "hi";
+ hi = "hi"_s;
EXPECT_EQ(2, hi.size());
VString<5> hi0;
EXPECT_EQ(0, hi0.size());
@@ -292,7 +292,7 @@ TYPED_TEST_CASE_P(NulStringTest);
TYPED_TEST_P(NulStringTest, basic)
{
- TypeParam hi("hello");
+ TypeParam hi("hello"_s);
EXPECT_EQ(hi.size(), strlen(hi.c_str()));
EXPECT_STREQ("hello", hi.c_str());
}
diff --git a/src/strings/tstring.cpp b/src/strings/tstring.cpp
index 5f463ca..27e8052 100644
--- a/src/strings/tstring.cpp
+++ b/src/strings/tstring.cpp
@@ -67,6 +67,10 @@ namespace strings
else
*this = RString(x);
}
+ TString::TString(const LString& l)
+ {
+ *this = XString(l);
+ }
TString::TString(XPair p)
: _s(p), _o(0)
diff --git a/src/strings/tstring.hpp b/src/strings/tstring.hpp
index 7003d37..38f41cd 100644
--- a/src/strings/tstring.hpp
+++ b/src/strings/tstring.hpp
@@ -43,10 +43,7 @@ namespace strings
TString(const XString&);
template<uint8_t n>
TString(const VString<n>& v);
- template<size_t n>
- TString(char (&s)[n]) = delete;
- template<size_t n>
- TString(const char (&s)[n]);
+ TString(const LString&);
//template<class It>
//TString(It b, It e) : _s(b, e), _o(0) {}
TString(XPair p);
diff --git a/src/strings/tstring.tcc b/src/strings/tstring.tcc
index 4eba13f..c62987b 100644
--- a/src/strings/tstring.tcc
+++ b/src/strings/tstring.tcc
@@ -25,8 +25,4 @@ namespace strings
TString::TString(const VString<n>& v)
: _s(v), _o(0)
{}
- template<size_t n>
- TString::TString(const char (&s)[n])
- : _s(s), _o(0)
- {}
} // namespace strings
diff --git a/src/strings/vstring.hpp b/src/strings/vstring.hpp
index 9952ff9..33dfb10 100644
--- a/src/strings/vstring.hpp
+++ b/src/strings/vstring.hpp
@@ -40,10 +40,7 @@ namespace strings
VString(ZString z);
template<uint8_t m>
VString(VString<m> v);
- template<size_t m>
- VString(char (&s)[m]) = delete;
- template<size_t m>
- VString(const char (&s)[m]);
+ VString(LString l);
VString(decltype(really_construct_from_a_pointer) e, const char *s);
VString(char c);
VString();
diff --git a/src/strings/vstring.tcc b/src/strings/vstring.tcc
index 1aa163d..ac3dc19 100644
--- a/src/strings/vstring.tcc
+++ b/src/strings/vstring.tcc
@@ -76,11 +76,9 @@ namespace strings
*this = XString(v);
}
template<uint8_t n>
- template<size_t m>
- VString<n>::VString(const char (&s)[m])
+ VString<n>::VString(LString l)
{
- static_assert(m <= n + 1, "string would truncate");
- *this = XString(s);
+ *this = XString(l);
}
template<uint8_t n>
VString<n>::VString(decltype(really_construct_from_a_pointer) e, const char *s)
@@ -143,7 +141,7 @@ namespace strings
char buffer[len + 1];
vsnprintf(buffer, len + 1, fmt, ap);
- out = const_(buffer);
+ out = VString<len>(strings::really_construct_from_a_pointer, buffer);
return len;
}
} // namespace strings
diff --git a/src/strings/xstring.cpp b/src/strings/xstring.cpp
index 0808104..5312445 100644
--- a/src/strings/xstring.cpp
+++ b/src/strings/xstring.cpp
@@ -40,6 +40,9 @@ namespace strings
XString::XString(const ZString& s)
: _b(&*s.begin()), _e(&*s.end()), _base(s.base())
{}
+ XString::XString(const LString& s)
+ : _b(&*s.begin()), _e(&*s.end()), _base(s.base())
+ {}
XString::XString(const char *b, const char *e, const RString *base_)
: _b(b), _e(e), _base(base_)
diff --git a/src/strings/xstring.hpp b/src/strings/xstring.hpp
index 8f6eac5..ad7c40d 100644
--- a/src/strings/xstring.hpp
+++ b/src/strings/xstring.hpp
@@ -22,6 +22,7 @@
# include "../sanity.hpp"
# include "base.hpp"
+# include "literal.hpp"
namespace strings
{
@@ -44,10 +45,7 @@ namespace strings
XString(const ZString& s);
template<uint8_t n>
XString(const VString<n>& s);
- template<size_t n>
- XString(char (&s)[n]) = delete;
- template<size_t n>
- XString(const char (&s)[n]);
+ XString(const LString& s);
// mostly internal
XString(const char *b, const char *e, const RString *base_);
XString(decltype(really_construct_from_a_pointer) e, const char *s, const RString *base_);
diff --git a/src/strings/xstring.tcc b/src/strings/xstring.tcc
index aee87f8..8115d23 100644
--- a/src/strings/xstring.tcc
+++ b/src/strings/xstring.tcc
@@ -25,8 +25,4 @@ namespace strings
XString::XString(const VString<n>& s)
: _b(&*s.begin()), _e(&*s.end()), _base(nullptr)
{}
- template<size_t n>
- XString::XString(const char (&s)[n])
- : _b(s), _e(s + strlen(s)), _base(nullptr)
- {}
} // namespace strings
diff --git a/src/strings/zstring.cpp b/src/strings/zstring.cpp
index e2a763f..323ba5f 100644
--- a/src/strings/zstring.cpp
+++ b/src/strings/zstring.cpp
@@ -26,7 +26,7 @@ namespace strings
{
ZString::ZString()
{
- *this = ZString("");
+ *this = ZString(""_s);
}
ZString::ZString(const RString& s)
: _b(&*s.begin()), _e(&*s.end()), _base(s.base())
@@ -37,6 +37,9 @@ namespace strings
ZString::ZString(const TString& s)
: _b(&*s.begin()), _e(&*s.end()), _base(s.base())
{}
+ ZString::ZString(const LString& s)
+ : _b(&*s.begin()), _e(&*s.end()), _base(s.base())
+ {}
ZString::ZString(const char *b, const char *e, const RString *base_)
: _b(b), _e(e), _base(base_)
{}
diff --git a/src/strings/zstring.hpp b/src/strings/zstring.hpp
index 717da88..c75c7c9 100644
--- a/src/strings/zstring.hpp
+++ b/src/strings/zstring.hpp
@@ -24,6 +24,7 @@
# include <cstring>
# include "base.hpp"
+# include "literal.hpp"
namespace strings
{
@@ -45,13 +46,10 @@ namespace strings
ZString(const XString&) = delete;
template<uint8_t n>
ZString(const VString<n>& s);
+ ZString(const LString& s);
// dangerous
ZString(const char *b, const char *e, const RString *base_);
ZString(decltype(really_construct_from_a_pointer), const char *s, const RString *base_);
- template<size_t n>
- ZString(char (&s)[n]) = delete;
- template<size_t n>
- ZString(const char (&s)[n], const RString *base_=nullptr);
iterator begin() const;
iterator end() const;
diff --git a/src/strings/zstring.tcc b/src/strings/zstring.tcc
index fe0e9f3..3ff7374 100644
--- a/src/strings/zstring.tcc
+++ b/src/strings/zstring.tcc
@@ -27,9 +27,4 @@ namespace strings
ZString::ZString(const VString<n>& s)
: _b(&*s.begin()), _e(&*s.end()), _base(nullptr)
{}
-
- template<size_t n>
- ZString::ZString(const char (&s)[n], const RString *base_)
- : _b(s), _e(s + strlen(s)), _base(base_)
- {}
} // namespace strings