From 9f061e8882a4f6ed1c81128408366528239f554a Mon Sep 17 00:00:00 2001 From: manga_osyo Date: Tue, 22 May 2012 18:11:37 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=83=98=E3=83=83=E3=82=BF=E3=83=BC?= =?UTF-8?q?=E9=83=A8=E3=81=AE=20README=20=E3=81=AE=E3=82=A2=E3=83=89?= =?UTF-8?q?=E3=83=AC=E3=82=B9=E3=81=8C=E9=81=95=E3=81=A3=E3=81=A6=E3=81=84?= =?UTF-8?q?=E3=81=9F=E3=81=AE=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/fizzbuzz/main.cpp | 198 ++++++++++----------- libs/string/example/literals_to_string.cpp | 120 ++++++------- libs/string/example/simple.cpp | 173 +++++++++--------- libs/weed/example/__TIME__.cpp | 136 +++++++------- libs/weed/example/remove_space.cpp | 72 ++++---- 5 files changed, 349 insertions(+), 350 deletions(-) diff --git a/example/fizzbuzz/main.cpp b/example/fizzbuzz/main.cpp index 2ee7187d..07159fe7 100644 --- a/example/fizzbuzz/main.cpp +++ b/example/fizzbuzz/main.cpp @@ -1,99 +1,99 @@ -// -// 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 -#include -#include - - -struct fizzbuzz{ - typedef sprout::string<12> result_type; - - constexpr result_type - operator ()(int n) const{ - return n % 15 == 0 ? sprout::to_string("FizzBuzz") - : n % 3 == 0 ? sprout::to_string("Fizz") - : n % 5 == 0 ? sprout::to_string("Buzz") - : sprout::to_string(n); - } -}; - - -int -main(){ - typedef fizzbuzz::result_type string; - - // - // Test - // - static_assert(fizzbuzz()( 1) == "1", ""); - static_assert(fizzbuzz()( 2) == "2", ""); - static_assert(fizzbuzz()( 3) == "Fizz", ""); - static_assert(fizzbuzz()( 5) == "Buzz", ""); - static_assert(fizzbuzz()(15) == "FizzBuzz", ""); - - // - // Sequence [1..15] - // - constexpr auto source = sprout::iota( - sprout::pit >(), - 1 - ); - - // - // Transform to FizzBuzz - // - constexpr auto result = sprout::transform( - sprout::begin(source), - sprout::end(source), - sprout::pit >(), - fizzbuzz() - ); - - // - // Check result - // - constexpr auto fizzbuzz_result = sprout::make_array( - sprout::to_string("1"), - sprout::to_string("2"), - sprout::to_string("Fizz"), - sprout::to_string("4"), - sprout::to_string("Buzz"), - sprout::to_string("Fizz"), - sprout::to_string("7"), - sprout::to_string("8"), - sprout::to_string("Fizz"), - sprout::to_string("Buzz"), - sprout::to_string("11"), - sprout::to_string("Fizz"), - sprout::to_string("13"), - sprout::to_string("14"), - sprout::to_string("FizzBuzz") - ); - // Equal result sequence - static_assert(result == fizzbuzz_result, ""); - - // - // Output - // - for(auto&& str : result){ - std::cout << str << ", "; - } - std::cout << std::endl; - - return 0; -} +// +// 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/bolero-MURAKAMI/Sprout/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include +#include +#include +#include +#include +#include + + +struct fizzbuzz{ + typedef sprout::string<12> result_type; + + constexpr result_type + operator ()(int n) const{ + return n % 15 == 0 ? sprout::to_string("FizzBuzz") + : n % 3 == 0 ? sprout::to_string("Fizz") + : n % 5 == 0 ? sprout::to_string("Buzz") + : sprout::to_string(n); + } +}; + + +int +main(){ + typedef fizzbuzz::result_type string; + + // + // Test + // + static_assert(fizzbuzz()( 1) == "1", ""); + static_assert(fizzbuzz()( 2) == "2", ""); + static_assert(fizzbuzz()( 3) == "Fizz", ""); + static_assert(fizzbuzz()( 5) == "Buzz", ""); + static_assert(fizzbuzz()(15) == "FizzBuzz", ""); + + // + // Sequence [1..15] + // + constexpr auto source = sprout::iota( + sprout::pit >(), + 1 + ); + + // + // Transform to FizzBuzz + // + constexpr auto result = sprout::transform( + sprout::begin(source), + sprout::end(source), + sprout::pit >(), + fizzbuzz() + ); + + // + // Check result + // + constexpr auto fizzbuzz_result = sprout::make_array( + sprout::to_string("1"), + sprout::to_string("2"), + sprout::to_string("Fizz"), + sprout::to_string("4"), + sprout::to_string("Buzz"), + sprout::to_string("Fizz"), + sprout::to_string("7"), + sprout::to_string("8"), + sprout::to_string("Fizz"), + sprout::to_string("Buzz"), + sprout::to_string("11"), + sprout::to_string("Fizz"), + sprout::to_string("13"), + sprout::to_string("14"), + sprout::to_string("FizzBuzz") + ); + // Equal result sequence + static_assert(result == fizzbuzz_result, ""); + + // + // Output + // + for(auto&& str : result){ + std::cout << str << ", "; + } + std::cout << std::endl; + + return 0; +} diff --git a/libs/string/example/literals_to_string.cpp b/libs/string/example/literals_to_string.cpp index 4d9fd40f..6be849c5 100644 --- a/libs/string/example/literals_to_string.cpp +++ b/libs/string/example/literals_to_string.cpp @@ -1,60 +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; -} +// +// 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/bolero-MURAKAMI/Sprout/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 index 4d9a14e7..5abf7049 100644 --- a/libs/string/example/simple.cpp +++ b/libs/string/example/simple.cpp @@ -1,87 +1,86 @@ -// -// 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 - - -// -// 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; -} +// +// 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 + + +// +// 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; +} diff --git a/libs/weed/example/__TIME__.cpp b/libs/weed/example/__TIME__.cpp index fa569d4d..fff1b53b 100644 --- a/libs/weed/example/__TIME__.cpp +++ b/libs/weed/example/__TIME__.cpp @@ -1,68 +1,68 @@ -// -// 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 - -// -// __TIME__ Parser -// -int -main(){ - namespace w = sprout::weed; - - // - // __TIME__ to Sprout.String - // - static constexpr auto time = sprout::to_string(__TIME__); -// static constexpr auto time = sprout::to_string("23:22:45"); - - - // - // parse __TIME__ - // - constexpr auto expr = w::int_ >> ':' >> w::int_ >> ':' >> w::int_; - static constexpr auto result = w::parse(time.begin(), time.end(), parser); - static_assert(result.success(), "failed parse"); - - - // - // get result - // - static constexpr sprout::array result_attr = result.attr(); - static constexpr auto hour = result_attr[0]; - static constexpr auto minute = result_attr[1]; - static constexpr auto second = result_attr[2]; - -// static_assert(hour == 23, ""); -// static_assert(minute == 22, ""); -// static_assert(second == 45, ""); - - std::cout << hour << std::endl; - std::cout << minute << std::endl; - std::cout << second << std::endl; - - - // - // compile time output - // - namespace m = boost::mpl; - typedef m::print>::type hour_; - typedef m::print>::type minute_; - typedef m::print>::type second_; - - return 0; -} +// +// 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/bolero-MURAKAMI/Sprout/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include +#include +#include +#include + +// +// __TIME__ Parser +// +int +main(){ + namespace w = sprout::weed; + + // + // __TIME__ to Sprout.String + // + static constexpr auto time = sprout::to_string(__TIME__); +// static constexpr auto time = sprout::to_string("23:22:45"); + + + // + // parse __TIME__ + // + constexpr auto expr = w::int_ >> ':' >> w::int_ >> ':' >> w::int_; + static constexpr auto result = w::parse(time.begin(), time.end(), parser); + static_assert(result.success(), "failed parse"); + + + // + // get result + // + static constexpr sprout::array result_attr = result.attr(); + static constexpr auto hour = result_attr[0]; + static constexpr auto minute = result_attr[1]; + static constexpr auto second = result_attr[2]; + +// static_assert(hour == 23, ""); +// static_assert(minute == 22, ""); +// static_assert(second == 45, ""); + + std::cout << hour << std::endl; + std::cout << minute << std::endl; + std::cout << second << std::endl; + + + // + // compile time output + // + namespace m = boost::mpl; + typedef m::print>::type hour_; + typedef m::print>::type minute_; + typedef m::print>::type second_; + + return 0; +} diff --git a/libs/weed/example/remove_space.cpp b/libs/weed/example/remove_space.cpp index 8b90f9d7..60a72142 100644 --- a/libs/weed/example/remove_space.cpp +++ b/libs/weed/example/remove_space.cpp @@ -1,36 +1,36 @@ -// -// 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(){ - namespace w = sprout::weed; - - static constexpr auto max_string_size = 32; - static constexpr auto space = *w::omit[ w::space ]; - static constexpr auto remove_space = *w::lim(space >> w::char_); - - static constexpr auto source = sprout::to_string(" homu : mami= 10 "); - static constexpr auto result = w::parse( - sprout::begin(source), sprout::end(source), - remove_space - ); - - static_assert(result.success(), "fail remove_space parse"); - static_assert(result.attr() == "homu:mami=10", ""); - - return 0; -} +// +// 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/bolero-MURAKAMI/Sprout/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include + + +int +main(){ + namespace w = sprout::weed; + + static constexpr auto max_string_size = 32; + static constexpr auto space = *w::omit[ w::space ]; + static constexpr auto remove_space = *w::lim(space >> w::char_); + + static constexpr auto source = sprout::to_string(" homu : mami= 10 "); + static constexpr auto result = w::parse( + sprout::begin(source), sprout::end(source), + remove_space + ); + + static_assert(result.success(), "fail remove_space parse"); + static_assert(result.attr() == "homu:mami=10", ""); + + return 0; +} From aeeba70cd19911b72b687a7c6092cc398763e1a3 Mon Sep 17 00:00:00 2001 From: manga_osyo Date: Tue, 22 May 2012 18:54:46 +0900 Subject: [PATCH 2/2] fixed bug --- libs/weed/example/__TIME__.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/weed/example/__TIME__.cpp b/libs/weed/example/__TIME__.cpp index fff1b53b..7f4f6868 100644 --- a/libs/weed/example/__TIME__.cpp +++ b/libs/weed/example/__TIME__.cpp @@ -34,7 +34,7 @@ main(){ // // parse __TIME__ // - constexpr auto expr = w::int_ >> ':' >> w::int_ >> ':' >> w::int_; + constexpr auto parser = w::int_ >> ':' >> w::int_ >> ':' >> w::int_; static constexpr auto result = w::parse(time.begin(), time.end(), parser); static_assert(result.success(), "failed parse");