From 43c058d3fea12cafb968a2abacaadf323e9b60f4 Mon Sep 17 00:00:00 2001 From: manga_osyo Date: Thu, 17 May 2012 16:02:24 +0900 Subject: [PATCH 1/2] add Sprout.String example --- libs/string/example/literals_to_string.cpp | 60 +++++++++++++++ libs/string/example/simple.cpp | 89 ++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 libs/string/example/literals_to_string.cpp create mode 100644 libs/string/example/simple.cpp diff --git a/libs/string/example/literals_to_string.cpp b/libs/string/example/literals_to_string.cpp new file mode 100644 index 00000000..e77bdeea --- /dev/null +++ b/libs/string/example/literals_to_string.cpp @@ -0,0 +1,60 @@ +// +// Sprout C++ Library +// +// Copyright (c) 2012 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/osyo-manga/cpp-half/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include + + +int +main(){ + + // + // String literal to Sprout.String + // + { + static constexpr auto str1 = sprout::to_string("homu"); + static_assert(str1 == "homu", ""); + static_assert(std::is_same const>{}, ""); + + static constexpr auto str2 = sprout::to_string(L"ほむほむ"); + static_assert(str2 == L"ほむほむ", ""); + static_assert(std::is_same const>{}, ""); + } + + // + // Integer literal to Sprout.String + // + { + static constexpr auto str = sprout::to_string(42); + static_assert(str == "42", ""); + } + + // + // Float literal to Sprout.String + // + { + static constexpr auto str = sprout::to_string(3.14f); + static_assert(str == "3.140000", ""); + } + + // + // Char literal to Sprout.String + // + { + static constexpr auto str = sprout::make_string('m', 'a', 'd', 'o'); + static_assert(str == "mado", ""); + static_assert(std::is_same const>{}, ""); + } + + return 0; +} diff --git a/libs/string/example/simple.cpp b/libs/string/example/simple.cpp new file mode 100644 index 00000000..a0d033aa --- /dev/null +++ b/libs/string/example/simple.cpp @@ -0,0 +1,89 @@ +// +// Sprout C++ Library +// +// Copyright (c) 2012 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/osyo-manga/cpp-half/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include +#include +#include +#include + + +// +// Sprout.String is constexpr string object +// +int +main(){ + + // + // String literal to Sprout.String + // + static constexpr sprout::string<4> str1 = sprout::to_string("homu"); + static constexpr auto str2 = sprout::to_string("mado"); + static constexpr auto str3 = sprout::to_string(42); + + // + // Comparison + // + static_assert(str1 == sprout::to_string("homu"), ""); + static_assert(str2 == "mado", ""); + static_assert(str3 == "42", ""); + static_assert(str1 != str2, ""); +// static_assert(str4 == "97", ""); + + // + // Accessor + // + static_assert(str1.front() == 'h', ""); + static_assert(str1[1] == 'o', ""); + static_assert(str1.at(2) == 'm', ""); + static_assert(str1.back() == 'u', ""); + + // + // String concatenation + // + static_assert((str1 + "homu") == "homuhomu", ""); + static_assert((str1 + str2) == "homumado", ""); + static_assert((str1 + sprout::to_string("42")) == "homu42", ""); + + // + // C style string + // + constexpr char const* cp = str1.c_str(); + static_assert(cp[0] == 'h', ""); + + // + // Iterator + // + static_assert(*str1.begin() == 'h', ""); + static_assert(*(str1.end() - 1) == 'u', ""); + static_assert(*(str1.rbegin() + 1) == 'm', ""); + static_assert(*(str1.rend() - 2) == 'o', ""); + + // + // IOStream + // + { + std::ostringstream os; + os << str1; + assert(os.str() == "homu"); + } + { + std::istringstream is("mami"); + sprout::string<4> str; + is >> str; + assert(str == "mami"); + } + + + return 0; +} From 9fcdb344f115ca0b48ffa7f53c348417c3b211ea Mon Sep 17 00:00:00 2001 From: manga_osyo Date: Thu, 17 May 2012 16:15:27 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=99=E8=A8=88=E3=81=AA=E3=83=98?= =?UTF-8?q?=E3=83=83=E3=83=80=E3=83=BC=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4=E3=81=A8=E6=96=87=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/string/example/simple.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/libs/string/example/simple.cpp b/libs/string/example/simple.cpp index a0d033aa..b129c688 100644 --- a/libs/string/example/simple.cpp +++ b/libs/string/example/simple.cpp @@ -13,7 +13,6 @@ // // #include -#include #include #include @@ -38,7 +37,6 @@ main(){ static_assert(str2 == "mado", ""); static_assert(str3 == "42", ""); static_assert(str1 != str2, ""); -// static_assert(str4 == "97", ""); // // Accessor