mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Added 'from_string' functions.
Added 'value' functions.
This commit is contained in:
parent
ba903e37ba
commit
4dd4574743
@ -70,15 +70,6 @@ SOFTWARE.
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Alternative strlen for all character types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
size_t strlen(const T* t)
|
||||
{
|
||||
return etl::char_traits<T>::length(t);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup string
|
||||
/// Exception base for strings
|
||||
|
||||
@ -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 T>
|
||||
typename etl::enable_if<etl::is_integral<T>::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<MAXN>& from_string(const char* text)
|
||||
{
|
||||
ibitset::from_string(text);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Set from a wide string.
|
||||
//*************************************************************************
|
||||
bitset<MAXN>& from_string(const wchar_t* text)
|
||||
{
|
||||
ibitset::from_string(text);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Set from a u16 string.
|
||||
//*************************************************************************
|
||||
bitset<MAXN>& from_string(const char16_t* text)
|
||||
{
|
||||
ibitset::from_string(text);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Set from a u32 string.
|
||||
//*************************************************************************
|
||||
bitset<MAXN>& from_string(const char32_t* text)
|
||||
{
|
||||
ibitset::from_string(text);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Put to a value.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value, T>::type
|
||||
value() const
|
||||
{
|
||||
ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (ARRAY_SIZE * BITS_PER_ELEMENT), "Type too small");
|
||||
|
||||
return ibitset::value<T>();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Reset all of the bits.
|
||||
//*************************************************************************
|
||||
@ -1021,4 +1182,6 @@ void swap(etl::bitset<MAXN>& lhs, etl::bitset<MAXN>& rhs)
|
||||
|
||||
#include "private/minmax_pop.h"
|
||||
|
||||
#undef ETL_FILE
|
||||
|
||||
#endif
|
||||
|
||||
@ -239,6 +239,16 @@ namespace etl
|
||||
return (e == eof()) ? eof() - 1 : e;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//***************************************************************************
|
||||
/// Alternative strlen for all character types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
size_t strlen(const T* t)
|
||||
{
|
||||
return etl::char_traits<T>::length(t);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -49,3 +49,4 @@
|
||||
49 type_select
|
||||
50 binary
|
||||
51 delegate
|
||||
52 bitset
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Embedded Template Library",
|
||||
"version": "14.35.5",
|
||||
"version": "14.36.0",
|
||||
"authors": {
|
||||
"name": "John Wellbelove",
|
||||
"email": "<john.wellbelove@etlcpp.com>"
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
name=Embedded Template Library
|
||||
version=14.35.5
|
||||
version=14.36.0
|
||||
author= John Wellbelove <john.wellbelove@etlcpp.com>
|
||||
maintainer=John Wellbelove <john.wellbelove@etlcpp.com>
|
||||
license=MIT
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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<uint64_t>();
|
||||
|
||||
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<uint32_t>(), 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());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user