From e122b380a35c6ce974bc449856dc81a1519ce8ad Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 8 Apr 2019 13:58:48 +0100 Subject: [PATCH] Added named base settings --- include/etl/format_spec.h | 39 ++++++++++++++++++++++++++++++++++++++ test/test_to_string.cpp | 11 +++++++++++ test/test_to_u16string.cpp | 11 +++++++++++ test/test_to_u32string.cpp | 12 ++++++++++++ test/test_to_wstring.cpp | 11 +++++++++++ 5 files changed, 84 insertions(+) diff --git a/include/etl/format_spec.h b/include/etl/format_spec.h index 74787281..238bc721 100644 --- a/include/etl/format_spec.h +++ b/include/etl/format_spec.h @@ -72,6 +72,45 @@ namespace etl return *this; } + //*************************************************************************** + /// Sets the base to binary. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& binary() + { + base(2); + return *this; + } + + //*************************************************************************** + /// Sets the base to octal. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& octal() + { + base(8); + return *this; + } + + //*************************************************************************** + /// Sets the base to decimal. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& decimal() + { + base(10); + return *this; + } + + //*************************************************************************** + /// Sets the base to hex. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& hex() + { + base(16); + return *this; + } //*************************************************************************** /// Gets the base. //*************************************************************************** diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index fc70aded..462dc7f2 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -238,5 +238,16 @@ namespace CHECK_EQUAL(etl::string<16>(STR("80000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); CHECK_EQUAL(etl::string<16>(STR("8000000000000000")), etl::to_string(int64_t(-9223372036854775808ll), str, Format().base(16).width(16).fill(STR('0')))); } + + //************************************************************************* + TEST(test_named_format_no_append) + { + etl::string<17> str; + + CHECK_EQUAL(etl::string<17>(STR("11110001001000000")), etl::to_string(123456, str, Format().binary())); + CHECK_EQUAL(etl::string<17>(STR("361100")), etl::to_string(123456, str, Format().octal())); + CHECK_EQUAL(etl::string<17>(STR("123456")), etl::to_string(123456, str, Format().decimal())); + CHECK_EQUAL(etl::string<17>(STR("1E240")), etl::to_string(123456, str, Format().hex())); + } }; } diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index 7e62521e..ed3a8e4b 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -238,5 +238,16 @@ namespace CHECK_EQUAL(etl::u16string<16>(STR("80000000")), etl::to_u16string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); CHECK_EQUAL(etl::u16string<16>(STR("8000000000000000")), etl::to_u16string(int64_t(-9223372036854775808ll), str, Format().base(16).width(16).fill(STR('0')))); } + + //************************************************************************* + TEST(test_named_format_no_append) + { + etl::u16string<17> str; + + CHECK_EQUAL(etl::u16string<17>(STR("11110001001000000")), etl::to_u16string(123456, str, Format().binary())); + CHECK_EQUAL(etl::u16string<17>(STR("361100")), etl::to_u16string(123456, str, Format().octal())); + CHECK_EQUAL(etl::u16string<17>(STR("123456")), etl::to_u16string(123456, str, Format().decimal())); + CHECK_EQUAL(etl::u16string<17>(STR("1E240")), etl::to_u16string(123456, str, Format().hex())); + } }; } diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp index ce161691..88482fce 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -238,5 +238,17 @@ namespace CHECK_EQUAL(etl::u32string<16>(STR("80000000")), etl::to_u32string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); CHECK_EQUAL(etl::u32string<16>(STR("8000000000000000")), etl::to_u32string(int64_t(-9223372036854775808ll), str, Format().base(16).width(16).fill(STR('0')))); } + + + //************************************************************************* + TEST(test_named_format_no_append) + { + etl::u32string<17> str; + + CHECK_EQUAL(etl::u32string<17>(STR("11110001001000000")), etl::to_u32string(123456, str, Format().binary())); + CHECK_EQUAL(etl::u32string<17>(STR("361100")), etl::to_u32string(123456, str, Format().octal())); + CHECK_EQUAL(etl::u32string<17>(STR("123456")), etl::to_u32string(123456, str, Format().decimal())); + CHECK_EQUAL(etl::u32string<17>(STR("1E240")), etl::to_u32string(123456, str, Format().hex())); + } }; } diff --git a/test/test_to_wstring.cpp b/test/test_to_wstring.cpp index d3094865..53cfca93 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -238,5 +238,16 @@ namespace CHECK_EQUAL(etl::wstring<16>(STR("80000000")), etl::to_wstring(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0')))); CHECK_EQUAL(etl::wstring<16>(STR("8000000000000000")), etl::to_wstring(int64_t(-9223372036854775808ll), str, Format().base(16).width(16).fill(STR('0')))); } + + //************************************************************************* + TEST(test_named_format_no_append) + { + etl::wstring<17> str; + + CHECK_EQUAL(etl::wstring<17>(STR("11110001001000000")), etl::to_wstring(123456, str, Format().binary())); + CHECK_EQUAL(etl::wstring<17>(STR("361100")), etl::to_wstring(123456, str, Format().octal())); + CHECK_EQUAL(etl::wstring<17>(STR("123456")), etl::to_wstring(123456, str, Format().decimal())); + CHECK_EQUAL(etl::wstring<17>(STR("1E240")), etl::to_wstring(123456, str, Format().hex())); + } }; }