mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
Merge pull request #6 from osyo-manga/example_prototype
add Sprout.String example
This commit is contained in:
commit
237ec76266
2 changed files with 147 additions and 0 deletions
60
libs/string/example/literals_to_string.cpp
Normal file
60
libs/string/example/literals_to_string.cpp
Normal file
|
@ -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
|
||||
// <http://www.boost.org/LICENSE_1_0.txt>
|
||||
//
|
||||
#include <sprout/string.hpp>
|
||||
|
||||
|
||||
int
|
||||
main(){
|
||||
|
||||
//
|
||||
// String literal to Sprout.String
|
||||
//
|
||||
{
|
||||
static constexpr auto str1 = sprout::to_string("homu");
|
||||
static_assert(str1 == "homu", "");
|
||||
static_assert(std::is_same<decltype(str1), sprout::string<4> const>{}, "");
|
||||
|
||||
static constexpr auto str2 = sprout::to_string(L"ほむほむ");
|
||||
static_assert(str2 == L"ほむほむ", "");
|
||||
static_assert(std::is_same<decltype(str2), sprout::wstring<4> 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<decltype(str), sprout::string<4> const>{}, "");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
87
libs/string/example/simple.cpp
Normal file
87
libs/string/example/simple.cpp
Normal file
|
@ -0,0 +1,87 @@
|
|||
//
|
||||
// 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
|
||||
// <http://www.boost.org/LICENSE_1_0.txt>
|
||||
//
|
||||
#include <sprout/string.hpp>
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
//
|
||||
// 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, "");
|
||||
|
||||
//
|
||||
// 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;
|
||||
}
|
Loading…
Reference in a new issue