diff --git a/include/etl/basic_format_spec.h b/include/etl/basic_format_spec.h index 6ffa332b..0a81600d 100644 --- a/include/etl/basic_format_spec.h +++ b/include/etl/basic_format_spec.h @@ -39,6 +39,165 @@ SOFTWARE. namespace etl { + //*************************************************************************** + // Stream formatting manipulators. + //*************************************************************************** + namespace private_basic_format_spec + { + //********************************* + struct base_spec + { + base_spec(uint_least8_t base_) + : base(base_) + { + } + + const uint_least8_t base; + }; + + //********************************* + struct width_spec + { + width_spec(uint_least8_t width_) + : width(width_) + { + } + + const uint_least8_t width; + }; + + //********************************* + template + struct fill_spec + { + fill_spec(TChar fill_) + : fill(fill_) + { + } + + const TChar fill; + }; + + //********************************* + struct precision_spec + { + precision_spec(uint_least8_t precision_) + : precision(precision_) + { + } + + const uint_least8_t precision; + }; + + //********************************* + struct upper_case_spec + { + upper_case_spec(bool upper_case_) + : upper_case(upper_case_) + { + } + + const bool upper_case; + }; + + //********************************* + struct boolalpha_spec + { + boolalpha_spec(bool boolalpha_) + : boolalpha(boolalpha_) + { + } + + const bool boolalpha; + }; + + //********************************* + struct left_spec + { + }; + + //********************************* + struct right_spec + { + }; + } + + //********************************* + static private_basic_format_spec::base_spec set_base(uint32_t base) + { + return private_basic_format_spec::base_spec(base); + } + + //********************************* + static private_basic_format_spec::base_spec binary() + { + return private_basic_format_spec::base_spec(2U); + } + + //********************************* + static private_basic_format_spec::base_spec octal() + { + return private_basic_format_spec::base_spec(8U); + } + + //********************************* + static private_basic_format_spec::base_spec decimal() + { + return private_basic_format_spec::base_spec(10U); + } + + //********************************* + static private_basic_format_spec::base_spec hex() + { + return private_basic_format_spec::base_spec(16U); + } + + //********************************* + static private_basic_format_spec::width_spec set_width(uint32_t width) + { + return private_basic_format_spec::width_spec(width); + } + + //********************************* + template + static private_basic_format_spec::fill_spec set_fill(TChar fill) + { + return private_basic_format_spec::fill_spec(fill); + } + + //********************************* + static private_basic_format_spec::left_spec set_left() + { + return private_basic_format_spec::left_spec(); + } + + //********************************* + static private_basic_format_spec::right_spec set_right() + { + return private_basic_format_spec::right_spec(); + } + + //********************************* + static private_basic_format_spec::precision_spec set_precision(uint32_t precision) + { + return private_basic_format_spec::precision_spec(precision); + } + + //********************************* + static private_basic_format_spec::upper_case_spec set_upper_case(bool upper_case) + { + return private_basic_format_spec::upper_case_spec(upper_case); + } + + //********************************* + static private_basic_format_spec::boolalpha_spec set_boolalpha(bool boolalpha) + { + return private_basic_format_spec::boolalpha_spec(boolalpha); + } + + //*************************************************************************** + /// basic_format_spec + //*************************************************************************** template class basic_format_spec { @@ -53,9 +212,9 @@ namespace etl /// Left Justified = false //*************************************************************************** basic_format_spec() - : base_(10) - , width_(0) - , precision_(0) + : base_(10U) + , width_(0U) + , precision_(0U) , upper_case_(true) , left_justified_(false) , boolalpha_(false) @@ -68,7 +227,7 @@ namespace etl /// Sets the base. /// \return A reference to the basic_format_spec. //*************************************************************************** - basic_format_spec& base(const uint32_t b) + basic_format_spec& base(uint32_t b) { base_ = static_cast(b); return *this; @@ -126,7 +285,7 @@ namespace etl /// Sets the width. /// \return A reference to the basic_format_spec. //*************************************************************************** - basic_format_spec& width(const uint32_t w) + basic_format_spec& width(uint32_t w) { width_ = static_cast(w); return *this; @@ -144,9 +303,9 @@ namespace etl /// Sets the precision. /// \return A reference to the basic_format_spec. //*************************************************************************** - basic_format_spec& precision(const uint32_t w) + basic_format_spec& precision(uint32_t p) { - precision_ = static_cast(w); + precision_ = static_cast(p); return *this; } @@ -162,7 +321,7 @@ namespace etl /// Sets the upper case flag. /// \return A reference to the basic_format_spec. //*************************************************************************** - basic_format_spec& upper_case(const bool u) + basic_format_spec& upper_case(bool u) { upper_case_ = u; return *this; @@ -180,7 +339,7 @@ namespace etl /// Sets the fill character. /// \return A reference to the basic_format_spec. //*************************************************************************** - basic_format_spec& fill(const typename TString::value_type c) + basic_format_spec& fill(typename TString::value_type c) { fill_ = c; return *this; diff --git a/include/etl/string_stream.h b/include/etl/string_stream.h index f5ecdef4..940be77e 100644 --- a/include/etl/string_stream.h +++ b/include/etl/string_stream.h @@ -131,6 +131,63 @@ namespace etl return ss; } + //********************************* + friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::base_spec spec) + { + ss.spec.base(spec.base); + return ss; + } + + //********************************* + friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::width_spec spec) + { + ss.spec.width(spec.width); + return ss; + } + + //********************************* + template + friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::fill_spec spec) + { + ss.spec.fill(spec.fill); + return ss; + } + + //********************************* + friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::precision_spec spec) + { + ss.spec.precision(spec.precision); + return ss; + } + + //********************************* + friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::boolalpha_spec spec) + { + ss.spec.boolalpha(spec.boolalpha); + return ss; + } + + //********************************* + friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::upper_case_spec spec) + { + ss.spec.upper_case(spec.upper_case); + return ss; + } + + //********************************* + friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::left_spec spec) + { + ss.spec.left(); + return ss; + } + + //********************************* + friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::right_spec spec) + { + ss.spec.right(); + return ss; + } + //********************************* friend string_stream& operator <<(string_stream& ss, etl::string_view view) { diff --git a/test/test_string_stream.cpp b/test/test_string_stream.cpp index 82b06836..51835b94 100644 --- a/test/test_string_stream.cpp +++ b/test/test_string_stream.cpp @@ -125,6 +125,27 @@ namespace CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result); } + //************************************************************************* + TEST(test_custom_multi_inline_format) + { + String str; + Format format1 = Format().base(10).width(10).fill(STR('#')); + Format format2 = Format().base(10).width(8).fill(STR('*')).left(); + Format format3 = Format().base(16).width(4).fill(STR(' ')).right(); + + Stream ss(str); + + int value = 123; + String hello(STR("Hello")); + ss << etl::set_base(10) << etl::set_width(10) << etl::set_fill(STR('#')) << hello + << etl::set_width(8) << etl::set_fill(STR('*')) << etl::set_left() << STR("World") + << etl::set_base(16) << etl::set_width(4) << etl::set_fill(STR(' ')) << etl::set_right() << value; + + String result = ss.str(); + + CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result); + } + //************************************************************************* TEST(test_custom_class_default_format) {