diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index 668e1515..0f6c05ea 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -70,15 +70,6 @@ SOFTWARE. namespace etl { - //*************************************************************************** - /// Alternative strlen for all character types. - //*************************************************************************** - template - size_t strlen(const T* t) - { - return etl::char_traits::length(t); - } - //*************************************************************************** ///\ingroup string /// Exception base for strings diff --git a/include/etl/bitset.h b/include/etl/bitset.h index e9ab7d2a..fd177ad3 100644 --- a/include/etl/bitset.h +++ b/include/etl/bitset.h @@ -48,10 +48,13 @@ SOFTWARE. #include "integral_limits.h" #include "binary.h" #include "char_traits.h" +#include "static_assert.h" +#include "error_handler.h" #include "private/minmax_push.h" -#include "error_handler.h" +#undef ETL_FILE +#define ETL_FILE "52" #if defined(ETL_COMPILER_KEIL) #pragma diag_suppress 1300 @@ -88,7 +91,21 @@ namespace etl public: bitset_nullptr(string_type file_name_, numeric_type line_number_) - : bitset_exception("bitset: nullptr", file_name_, line_number_) + : bitset_exception(ETL_ERROR_TEXT("bitset: nullptr", ETL_FILE"A"), file_name_, line_number_) + { + } + }; + + //*************************************************************************** + /// Bitset type_too_small exception. + ///\ingroup bitset + //*************************************************************************** + class bitset_type_too_small : public bitset_exception + { + public: + + bitset_type_too_small(string_type file_name_, numeric_type line_number_) + : bitset_exception(ETL_ERROR_TEXT("bitset:type_too_small", ETL_FILE"B"), file_name_, line_number_) { } }; @@ -295,7 +312,7 @@ namespace etl { reset(); - size_t i = std::min(NBITS, strlen(text)); + size_t i = std::min(NBITS, etl::strlen(text)); while (i > 0) { @@ -305,6 +322,98 @@ namespace etl return *this; } + //************************************************************************* + /// Set from a string. + //************************************************************************* + ibitset& from_string(const char* text) + { + reset(); + + size_t i = std::min(NBITS, etl::strlen(text)); + + while (i > 0) + { + set(--i, *text++ == L'1'); + } + + return *this; + } + + //************************************************************************* + /// Set from a wide string. + //************************************************************************* + ibitset& from_string(const wchar_t* text) + { + reset(); + + size_t i = std::min(NBITS, etl::strlen(text)); + + while (i > 0) + { + set(--i, *text++ == L'1'); + } + + return *this; + } + + //************************************************************************* + /// Set from a u16 string. + //************************************************************************* + ibitset& from_string(const char16_t* text) + { + reset(); + + size_t i = std::min(NBITS, etl::strlen(text)); + + while (i > 0) + { + set(--i, *text++ == u'1'); + } + + return *this; + } + + //************************************************************************* + /// Set from a u32 string. + //************************************************************************* + ibitset& from_string(const char32_t* text) + { + reset(); + + size_t i = std::min(NBITS, etl::strlen(text)); + + while (i > 0) + { + set(--i, *text++ == U'1'); + } + + return *this; + } + + //************************************************************************* + /// Put to a value. + //************************************************************************* + template + typename etl::enable_if::value, T>::type + value() const + { + T v = T(0); + + const bool OK = (sizeof(T) * CHAR_BIT) >= (SIZE * BITS_PER_ELEMENT); + + ETL_ASSERT(OK, ETL_ERROR(etl::bitset_type_too_small)); + + if (OK) + { + for (size_t i = 0; i < SIZE; ++i) + { + v |= T(pdata[i]) << T(i * BITS_PER_ELEMENT); + } + } + + return v; + } + //************************************************************************* /// Resets the bitset. //************************************************************************* @@ -820,6 +929,58 @@ namespace etl return *this; } + //************************************************************************* + /// Set from a string. + //************************************************************************* + bitset& from_string(const char* text) + { + ibitset::from_string(text); + + return *this; + } + + //************************************************************************* + /// Set from a wide string. + //************************************************************************* + bitset& from_string(const wchar_t* text) + { + ibitset::from_string(text); + + return *this; + } + + //************************************************************************* + /// Set from a u16 string. + //************************************************************************* + bitset& from_string(const char16_t* text) + { + ibitset::from_string(text); + + return *this; + } + + //************************************************************************* + /// Set from a u32 string. + //************************************************************************* + bitset& from_string(const char32_t* text) + { + ibitset::from_string(text); + + return *this; + } + + //************************************************************************* + /// Put to a value. + //************************************************************************* + template + typename etl::enable_if::value, T>::type + value() const + { + ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (ARRAY_SIZE * BITS_PER_ELEMENT), "Type too small"); + + return ibitset::value(); + } + //************************************************************************* /// Reset all of the bits. //************************************************************************* @@ -1021,4 +1182,6 @@ void swap(etl::bitset& lhs, etl::bitset& rhs) #include "private/minmax_pop.h" +#undef ETL_FILE + #endif diff --git a/include/etl/char_traits.h b/include/etl/char_traits.h index 164a3b8d..5048b4d1 100644 --- a/include/etl/char_traits.h +++ b/include/etl/char_traits.h @@ -239,6 +239,16 @@ namespace etl return (e == eof()) ? eof() - 1 : e; } }; + + + //*************************************************************************** + /// Alternative strlen for all character types. + //*************************************************************************** + template + size_t strlen(const T* t) + { + return etl::char_traits::length(t); + } } #endif diff --git a/include/etl/file_error_numbers.txt b/include/etl/file_error_numbers.txt index 281c75ea..e752f7c3 100644 --- a/include/etl/file_error_numbers.txt +++ b/include/etl/file_error_numbers.txt @@ -49,3 +49,4 @@ 49 type_select 50 binary 51 delegate +52 bitset diff --git a/include/etl/version.h b/include/etl/version.h index 5e4bc5e4..a30ba738 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,8 +38,8 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 35 -#define ETL_VERSION_PATCH 5 +#define ETL_VERSION_MINOR 36 +#define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) "." ETL_STRINGIFY(ETL_VERSION_MINOR) "." ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_STRINGIFY(ETL_VERSION_MAJOR) L"." ETL_STRINGIFY(ETL_VERSION_MINOR) L"." ETL_STRINGIFY(ETL_VERSION_PATCH) diff --git a/library.json b/library.json index 0dacced1..8a3aae1c 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Embedded Template Library", - "version": "14.35.5", + "version": "14.36.0", "authors": { "name": "John Wellbelove", "email": "" diff --git a/library.properties b/library.properties index b01b4c49..dba7ab60 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Embedded Template Library -version=14.35.5 +version=14.36.0 author= John Wellbelove maintainer=John Wellbelove license=MIT diff --git a/support/Release notes.txt b/support/Release notes.txt index 01ec4948..fdf1276c 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.36.0 +Added wchar_t, u16 and u32 version of the 'set from string' function. + =============================================================================== 14.35.5 Bug fix for etl::multiset & etl::multimap erase. diff --git a/test/test_bitset.cpp b/test/test_bitset.cpp index 6e7ea3fc..c7efedfb 100644 --- a/test/test_bitset.cpp +++ b/test/test_bitset.cpp @@ -1,4 +1,4 @@ -/****************************************************************************** +/****************************************************************************** The MIT License(MIT) Embedded Template Library. @@ -180,6 +180,109 @@ namespace } } + //************************************************************************* + TEST(test_from_string) + { + std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); + etl::bitset<60> data; + + data.from_string("110001001000110100010101100111001100010010001101000101011001"); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + CHECK_EQUAL(compare.none(), data.none()); + CHECK_EQUAL(compare.any(), data.any()); + CHECK_EQUAL(compare.all(), data.all()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(compare.test(i), data.test(i)); + } + } + + //************************************************************************* + TEST(test_from_wstring) + { + std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); + etl::bitset<60> data; + + data.from_string(L"110001001000110100010101100111001100010010001101000101011001"); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + CHECK_EQUAL(compare.none(), data.none()); + CHECK_EQUAL(compare.any(), data.any()); + CHECK_EQUAL(compare.all(), data.all()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(compare.test(i), data.test(i)); + } + } + + //************************************************************************* + TEST(test_from_u16string) + { + std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); + etl::bitset<60> data; + + data.from_string(u"110001001000110100010101100111001100010010001101000101011001"); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + CHECK_EQUAL(compare.none(), data.none()); + CHECK_EQUAL(compare.any(), data.any()); + CHECK_EQUAL(compare.all(), data.all()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(compare.test(i), data.test(i)); + } + } + + //************************************************************************* + TEST(test_from_u32string) + { + std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); + etl::bitset<60> data; + + data.from_string(U"110001001000110100010101100111001100010010001101000101011001"); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + CHECK_EQUAL(compare.none(), data.none()); + CHECK_EQUAL(compare.any(), data.any()); + CHECK_EQUAL(compare.all(), data.all()); + + for (size_t i = 0; i < data.size(); ++i) + { + CHECK_EQUAL(compare.test(i), data.test(i)); + } + } + + //************************************************************************* + TEST(test_value) + { + etl::bitset<60> data("110001001000110100010101100111001100010010001101000101011001"); + uint64_t value = data.value(); + + CHECK_EQUAL(885187510387921241ull, value); + } + + //************************************************************************* + TEST(test_value_type_too_small) + { + etl::bitset<60> data("110001001000110100010101100111001100010010001101000101011001"); + + etl::ibitset& idata= data; + + uint32_t value = 0U; + + CHECK_THROW(value = idata.value(), etl::bitset_type_too_small); + + CHECK_EQUAL(0U, value); + } + //************************************************************************* TEST(test_position_set) { @@ -235,7 +338,7 @@ namespace etl::ibitset& ridata = idata.reset(); compare.reset(); - + CHECK_EQUAL(compare.size(), ridata.size()); CHECK_EQUAL(compare.count(), ridata.count()); CHECK_EQUAL(compare.none(), ridata.none());