From 7feb3c1cc4dc456fedc96e7160b7cac15bc757b9 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 6 Apr 2019 21:10:20 +0100 Subject: [PATCH] Created integral to_string. Started etl::string tests. --- include/etl/cstring.h | 2 + include/etl/format_spec.h | 165 +++++++++++++++++++++++++ include/etl/negative.h | 60 +++++++++ include/etl/private/to_string_helper.h | 120 ++++++++++++++++++ include/etl/to_string.h | 144 ++++++++++++++------- include/etl/to_u16string.h | 149 ++++++++++++++++++++++ include/etl/to_u32string.h | 149 ++++++++++++++++++++++ include/etl/to_wstring.h | 149 ++++++++++++++++++++++ include/etl/u16string.h | 2 + include/etl/u32string.h | 2 + include/etl/wstring.h | 2 + test/codeblocks/ETL.cbp | 7 ++ test/test_to_string.cpp | 85 ++++--------- test/vs2017/etl.vcxproj | 6 + test/vs2017/etl.vcxproj.filters | 18 +++ 15 files changed, 948 insertions(+), 112 deletions(-) create mode 100644 include/etl/format_spec.h create mode 100644 include/etl/negative.h create mode 100644 include/etl/private/to_string_helper.h create mode 100644 include/etl/to_u16string.h create mode 100644 include/etl/to_u32string.h create mode 100644 include/etl/to_wstring.h diff --git a/include/etl/cstring.h b/include/etl/cstring.h index 8fdd1aaa..147d3c4c 100644 --- a/include/etl/cstring.h +++ b/include/etl/cstring.h @@ -55,6 +55,8 @@ namespace etl { public: + typedef istring base_type; + typedef istring::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/include/etl/format_spec.h b/include/etl/format_spec.h new file mode 100644 index 00000000..d8dca693 --- /dev/null +++ b/include/etl/format_spec.h @@ -0,0 +1,165 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_FORMAT_SPEC_INCLUDED +#define ETL_FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "static_assert.h" + +namespace etl +{ + template + class format_spec + { + public: + + //*************************************************************************** + /// Default constructor. + /// Sets:- + /// Base = 10 + /// Width = 0 + /// Upper case (for hex) = true + /// Rifght Justified = true + //*************************************************************************** + format_spec() + : base_(10) + , width_(0) + , upper_case_(true) + , right_justified_(true) + , fill_(typename TString::value_type(' ')) + { + + } + + //*************************************************************************** + /// Sets the base. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& base(const uint32_t b) + { + base_ = static_cast(b); + return *this; + } + + //*************************************************************************** + /// Gets the base. + //*************************************************************************** + uint32_t base() const + { + return base_; + } + + //*************************************************************************** + /// Sets the width. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& width(const uint32_t w) + { + width_ = static_cast(w); + return *this; + } + + //*************************************************************************** + /// Gets the width. + //*************************************************************************** + uint32_t width() const + { + return width_; + } + + //*************************************************************************** + /// Sets the upper case flag. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& upper_case(const bool u) + { + upper_case_ = u; + return *this; + } + + //*************************************************************************** + /// Gets the upper case flag. + //*************************************************************************** + bool upper_case(const bool u) const + { + return upper_case_; + } + + //*************************************************************************** + /// Sets the fill character. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& fill(const typename TString::value_type c) + { + fill_ = c; + return *this; + } + + //*************************************************************************** + /// Gets the fill character. + //*************************************************************************** + typename TString::value_type fill() const + { + return fill_; + } + + //*************************************************************************** + /// Sets the right justify flag. + /// \return A reference to the format_spec. + //*************************************************************************** + format_spec& right_justified(const bool r) + { + right_justified_ = r; + return *this; + } + + //*************************************************************************** + /// Gets the right justify flag. + //*************************************************************************** + bool right_justified() const + { + return right_justified_; + } + + private: + + uint_least8_t base_; + uint_least8_t width_; + bool upper_case_; + bool right_justified_; + typename TString::value_type fill_; + }; +} + +#endif diff --git a/include/etl/negative.h b/include/etl/negative.h new file mode 100644 index 00000000..7b2a6b33 --- /dev/null +++ b/include/etl/negative.h @@ -0,0 +1,60 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2018 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_NEGATIVE_INCLUDED +#define ETL_NEGATIVE_INCLUDED + +#include "type_traits.h" + +namespace etl +{ + //*************************************************************************** + // For signed types. + //*************************************************************************** + template + typename etl::enable_if::value, bool>::type + is_negative(const T value) + { + return (value < T(0)); + } + + //*************************************************************************** + // For unsigned types. + //*************************************************************************** + template + typename etl::enable_if::value, T>::type + is_negative(const T) + { + return false; + } +} + +#endif + diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h new file mode 100644 index 00000000..903f4e39 --- /dev/null +++ b/include/etl/private/to_string_helper.h @@ -0,0 +1,120 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_TO_STRING_HELPER_INCLUDED +#define ETL_TO_STRING_HELPER_INCLUDED + +///\ingroup private + +#include "../platform.h" +#include "../absolute.h" +#include "../negative.h" +#include "../format_spec.h" +#include "../type_traits.h" + +#include "../stl/algorithm.h" +#include "../stl/iterator.h" + +namespace etl +{ + //*************************************************************************** + /// Helper function for integrals. + //*************************************************************************** + template + typename etl::enable_if::value, const TIString&>::type + to_string_helper(T value, + TIString& str, + const etl::format_spec& format, + const bool append) + { + typedef typename TIString::value_type type; + typedef typename TIString::iterator iterator; + + const bool negative = etl::is_negative(value); + + if (!append) + { + str.clear(); + } + + iterator start = str.end(); + + if (value == 0) + { + str.push_back(type('0')); + } + else + { + // Extract the digits, in reverse order. + while (value != 0) + { + T remainder = etl::absolute(value % T(format.base())); + str.push_back((remainder > 9) ? type('a' + (remainder - 10)) : type('0' + remainder)); + value = value / T(format.base()); + } + + // If number is negative, append '-' + if ((format.base() == 10) && negative) + { + str.push_back(type('-')); + } + + // Reverse the string we appended. + std::reverse(start, str.end()); + } + + // Do we have a width specification? + if (format.width() != 0) + { + uint32_t length = static_cast(std::distance(start, str.end())); + + // Is the length of the string less than the width? + if (length < format.width()) + { + uint32_t fill_length = format.width() - length; + + if (format.right_justified()) + { + // Insert fill characters on the left. + str.insert(start, fill_length, format.fill()); + } + else + { + // Insert fill characters on the right. + str.insert(str.end(), fill_length, format.fill()); + } + } + } + + return str; + } +} + +#endif diff --git a/include/etl/to_string.h b/include/etl/to_string.h index 54c5021b..51f1991b 100644 --- a/include/etl/to_string.h +++ b/include/etl/to_string.h @@ -28,73 +28,121 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef ETL_TO_STRING_INCLUDED -#define ETL_TO_STRING_INCLUDED +#ifndef ETL_TO_U32STRING_INCLUDED +#define ETL_TO_U32STRING_INCLUDED -///\ingroup utilities +///\ingroup string #include "platform.h" #include "type_traits.h" -#include "stl/algorithm.h" +#include "cstring.h" +#include "format_spec.h" +#include "private/to_string_helper.h" namespace etl { - template - void to_string_helper(T value, const bool negative, TString& str, const int base, const bool upper_case) + //*************************************************************************** + /// For signed integrals less than 64 bits. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value, const etl::istring&>::type + to_string(const T value, etl::istring& str, const bool append = false) { - typedef typename TString::value_type type; - typedef typename TString::iterator iterator; + etl::format_spec format; - iterator start = str.end(); - - if (value == 0) - { - str.push_back(type('0')); - } - else - { - // Extract the digits, in reverse order. - while (value != 0) - { - T remainder = value % base; - str.push_back((remainder > 9) ? type('a' + (remainder - 10)) : type('0' + remainder)); - value = value / base; - } - - // If number is negative, append '-' - if ((base == 10) && negative) - { - str.push_back(type('-')); - } - - // Reverse the string we appended. - std::reverse(start, str.end()); - } + return to_string_helper(int32_t(value), str, format, append); } //*************************************************************************** - /// + /// For signed integrals less than 64 bits. Supplied format spec. //*************************************************************************** - template - typename etl::enable_if::value && etl::is_signed::value, void>::type - to_string(T value, TString& str, const int base = 10, const bool upper_case = true) + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value, const etl::istring&>::type + to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) { - typedef typename etl::make_unsigned::type Unsigned; - - const bool negative = (value < 0); - const Unsigned uvalue = (negative ? -value : value); - - to_string_helper(uvalue, negative, str, base, upper_case); + return to_string_helper(int32_t(value), str, format, append); } //*************************************************************************** - /// + /// For unsigned integrals less then 64 bits. Default format spec. //*************************************************************************** - template - typename etl::enable_if::value && etl::is_unsigned::value, void>::type - to_string(T value, TString& str, const int base = 10, const bool upper_case = true) + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value, const etl::istring&>::type + to_string(const T value, etl::istring& str, const bool append = false) { - to_string_helper(value, false, str, base, upper_case); + etl::format_spec format; + + return to_string_helper(uint32_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value, const etl::istring&>::type + to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(uint32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + etl::is_same::value, const etl::istring&>::type + to_string(const T value, etl::istring& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(int64_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + etl::is_same::value, const etl::istring&>::type + to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(int64_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + etl::is_same::value, const etl::istring&>::type + to_string(const T value, etl::istring& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(uint64_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + etl::is_same::value, const etl::istring&>::type + to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(uint64_t(value), str, format, append); } } diff --git a/include/etl/to_u16string.h b/include/etl/to_u16string.h new file mode 100644 index 00000000..15e05d7b --- /dev/null +++ b/include/etl/to_u16string.h @@ -0,0 +1,149 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_TO_U16STRING_INCLUDED +#define ETL_TO_U16STRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "u16string.h" +#include "format_spec.h" +#include "private/to_string_helper.h" + +namespace etl +{ + //*************************************************************************** + /// For signed integrals less than 64 bits. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value, const etl::iu16string&>::type + to_string(const T value, etl::iu16string& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(int32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value, const etl::iu16string&>::type + to_string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(int32_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned integrals less then 64 bits. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value, const etl::iu16string&>::type + to_string(const T value, etl::iu16string& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(uint32_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value, const etl::iu16string&>::type + to_string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(uint32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + etl::is_same::value, const etl::iu16string&>::type + to_string(const T value, etl::iu16string& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(int64_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + etl::is_same::value, const etl::iu16string&>::type + to_string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(int64_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + etl::is_same::value, const etl::iu16string&>::type + to_string(const T value, etl::iu16string& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(uint64_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + etl::is_same::value, const etl::iu16string&>::type + to_string(const T value, etl::iu16string& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(uint64_t(value), str, format, append); + } +} + +#endif diff --git a/include/etl/to_u32string.h b/include/etl/to_u32string.h new file mode 100644 index 00000000..d2c7baee --- /dev/null +++ b/include/etl/to_u32string.h @@ -0,0 +1,149 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_TO_U32STRING_INCLUDED +#define ETL_TO_U32STRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "u32string.h" +#include "format_spec.h" +#include "private/to_string_helper.h" + +namespace etl +{ + //*************************************************************************** + /// For signed integrals less than 64 bits. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value, const etl::iu32string&>::type + to_string(const T value, etl::iu32string& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(int32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value, const etl::iu32string&>::type + to_string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(int32_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned integrals less then 64 bits. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value, const etl::iu32string&>::type + to_string(const T value, etl::iu32string& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(uint32_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value, const etl::iu32string&>::type + to_string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(uint32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + etl::is_same::value, const etl::iu32string&>::type + to_string(const T value, etl::iu32string& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(int64_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + etl::is_same::value, const etl::iu32string&>::type + to_string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(int64_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + etl::is_same::value, const etl::iu32string&>::type + to_string(const T value, etl::iu32string& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(uint64_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + etl::is_same::value, const etl::iu32string&>::type + to_string(const T value, etl::iu32string& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(uint64_t(value), str, format, append); + } +} + +#endif diff --git a/include/etl/to_wstring.h b/include/etl/to_wstring.h new file mode 100644 index 00000000..f84fe55a --- /dev/null +++ b/include/etl/to_wstring.h @@ -0,0 +1,149 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_TO_WSTRING_INCLUDED +#define ETL_TO_WSTRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "wstring.h" +#include "format_spec.h" +#include "private/to_string_helper.h" + +namespace etl +{ + //*************************************************************************** + /// For signed integrals less than 64 bits. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value, const etl::iwstring&>::type + to_string(const T value, etl::iwstring& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(int32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value, const etl::iwstring&>::type + to_string(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(int32_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned integrals less then 64 bits. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value, const etl::iwstring&>::type + to_string(const T value, etl::iwstring& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(uint32_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value, const etl::iwstring&>::type + to_string(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(uint32_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + etl::is_same::value, const etl::iwstring&>::type + to_string(const T value, etl::iwstring& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(int64_t(value), str, format, append); + } + + //*************************************************************************** + /// For signed 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + etl::is_same::value, const etl::iwstring&>::type + to_string(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(int64_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + etl::is_same::value, const etl::iwstring&>::type + to_string(const T value, etl::iwstring& str, const bool append = false) + { + etl::format_spec format; + + return to_string_helper(uint64_t(value), str, format, append); + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + etl::is_same::value, const etl::iwstring&>::type + to_string(const T value, etl::iwstring& str, const etl::format_spec& format, const bool append = false) + { + return to_string_helper(uint64_t(value), str, format, append); + } +} + +#endif diff --git a/include/etl/u16string.h b/include/etl/u16string.h index f0d7a08f..0a8d271c 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -55,6 +55,8 @@ namespace etl { public: + typedef iu16string base_type; + typedef iu16string::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 27792fcb..3ad04ae6 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -55,6 +55,8 @@ namespace etl { public: + typedef iu32string base_type; + typedef iu32string::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/include/etl/wstring.h b/include/etl/wstring.h index 388525c7..44ba1798 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -55,6 +55,8 @@ namespace etl { public: + typedef iwstring base_type; + typedef iwstring::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index daf6c462..05f44e6b 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -197,6 +197,7 @@ + @@ -247,6 +248,7 @@ + @@ -297,6 +299,10 @@ + + + + @@ -428,6 +434,7 @@ + diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index 5453ab57..9fa6c684 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -32,84 +32,41 @@ SOFTWARE. #include "etl/to_string.h" #include "etl/cstring.h" +#include "etl/format_spec.h" #undef STR #define STR(x) x namespace { - /* A utility function to reverse a string */ - void reverse(char str[], int length) - { - int start = 0; - int end = length - 1; - while (start < end) - { - std::swap(*(str + start), *(str + end)); - start++; - end--; - } - } - - // Implementation of itoa() - char* itoa(int num, char* str, int base) - { - int i = 0; - bool isNegative = false; - - /* Handle 0 explicitely, otherwise empty string is printed for 0 */ - if (num == 0) - { - str[i++] = '0'; - str[i] = '\0'; - return str; - } - - // In standard itoa(), negative numbers are handled only with - // base 10. Otherwise numbers are considered unsigned. - if (num < 0 && base == 10) - { - isNegative = true; - num = -num; - } - - // Process individual digits - while (num != 0) - { - int rem = num % base; - str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0'; - num = num / base; - } - - // If number is negative, append '-' - if (isNegative) - str[i++] = '-'; - - str[i] = '\0'; // Append string terminator - - // Reverse the string - reverse(str, i); - - return str; - } + typedef etl::format_spec Format; SUITE(test_string_char) { //************************************************************************* - TEST(test_x) + TEST(test_default_format_no_append) { - //char str[100]; + etl::string<20> str; - etl::string<10> str; + CHECK(etl::string<20>(STR("0")) == etl::to_string(uint8_t(0), str)); + CHECK(etl::string<20>(STR("0")) == etl::to_string(uint16_t(0), str)); + CHECK(etl::string<20>(STR("0")) == etl::to_string(uint32_t(0), str)); + CHECK(etl::string<20>(STR("0")) == etl::to_string(uint64_t(0), str)); - etl::to_string(char(127), str, 10); + CHECK(etl::string<20>(STR("128")) == etl::to_string(uint8_t(128), str)); + CHECK(etl::string<20>(STR("32768")) == etl::to_string(uint16_t(32768), str)); + CHECK(etl::string<20>(STR("2147483648")) == etl::to_string(uint32_t(2147483648ul), str)); + CHECK(etl::string<20>(STR("9223372036854775808")) == etl::to_string(uint64_t(9223372036854775808ull), str)); - str.clear(); - etl::to_string(char(-128), str, 10); - //itoa(-1567, str, 10); - //itoa(1567, str, 2); - //itoa(1567, str, 8); - //itoa(1567, str, 16); + CHECK(etl::string<20>(STR("127")) == etl::to_string(int8_t(127), str)); + CHECK(etl::string<20>(STR("32767")) == etl::to_string(int16_t(32767), str)); + CHECK(etl::string<20>(STR("2147483647")) == etl::to_string(int32_t(2147483647ll), str)); + CHECK(etl::string<20>(STR("9223372036854775807")) == etl::to_string(int64_t(9223372036854775807ll), str)); + + CHECK(etl::string<20>(STR("-128")) == etl::to_string(int8_t(-128), str)); + CHECK(etl::string<20>(STR("-32768")) == etl::to_string(int16_t(-32768), str)); + CHECK(etl::string<20>(STR("-2147483648")) == etl::to_string(int32_t(-2147483648ll), str)); + CHECK(etl::string<20>(STR("-9223372036854775808")) == etl::to_string(int64_t(-9223372036854775808ll), str)); } }; } diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index 7da9d676..ac1dcfeb 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -371,12 +371,15 @@ + + + @@ -435,6 +438,9 @@ + + + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index c70fac98..59beb25d 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -735,6 +735,24 @@ ETL\Utilities + + ETL\Utilities + + + ETL\Utilities + + + ETL\Utilities + + + ETL\Utilities + + + ETL\Utilities + + + ETL\Private +