From 509a930b55cb57555542a0493641e9a51d04fb72 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 9 Jul 2020 14:21:40 +0100 Subject: [PATCH] Added stream manipulators --- include/etl/basic_format_spec.h | 124 ++++++-------- include/etl/basic_string_stream.h | 276 ++++++++++++++++++++++++++++++ include/etl/limits.h | 10 ++ include/etl/string_stream.h | 196 +-------------------- include/etl/u16string_stream.h | 139 +-------------- include/etl/u32string_stream.h | 139 +-------------- include/etl/version.h | 2 +- include/etl/wstring_stream.h | 139 +-------------- library.json | 2 +- library.properties | 2 +- support/Release notes.txt | 4 + test/codeblocks/ETL.cbp | 9 + test/test_string_stream.cpp | 53 +++++- test/test_u16string_stream.cpp | 60 +++++++ test/test_u32string_stream.cpp | 60 +++++++ test/test_wstring_stream.cpp | 60 +++++++ test/vs2019/etl.vcxproj | 1 + test/vs2019/etl.vcxproj.filters | 3 + 18 files changed, 597 insertions(+), 682 deletions(-) create mode 100644 include/etl/basic_string_stream.h diff --git a/include/etl/basic_format_spec.h b/include/etl/basic_format_spec.h index 0a81600d..6304890a 100644 --- a/include/etl/basic_format_spec.h +++ b/include/etl/basic_format_spec.h @@ -39,15 +39,14 @@ SOFTWARE. namespace etl { - //*************************************************************************** - // Stream formatting manipulators. - //*************************************************************************** namespace private_basic_format_spec { - //********************************* + //******************************************************* + // Structures returned by stream formatting manipulators. + //******************************************************* struct base_spec { - base_spec(uint_least8_t base_) + ETL_CONSTEXPR base_spec(uint_least8_t base_) : base(base_) { } @@ -58,7 +57,7 @@ namespace etl //********************************* struct width_spec { - width_spec(uint_least8_t width_) + ETL_CONSTEXPR width_spec(uint_least8_t width_) : width(width_) { } @@ -70,7 +69,7 @@ namespace etl template struct fill_spec { - fill_spec(TChar fill_) + ETL_CONSTEXPR fill_spec(TChar fill_) : fill(fill_) { } @@ -81,7 +80,7 @@ namespace etl //********************************* struct precision_spec { - precision_spec(uint_least8_t precision_) + ETL_CONSTEXPR precision_spec(uint_least8_t precision_) : precision(precision_) { } @@ -90,9 +89,9 @@ namespace etl }; //********************************* - struct upper_case_spec + struct uppercase_spec { - upper_case_spec(bool upper_case_) + ETL_CONSTEXPR uppercase_spec(bool upper_case_) : upper_case(upper_case_) { } @@ -103,7 +102,7 @@ namespace etl //********************************* struct boolalpha_spec { - boolalpha_spec(bool boolalpha_) + ETL_CONSTEXPR boolalpha_spec(bool boolalpha_) : boolalpha(boolalpha_) { } @@ -122,78 +121,62 @@ namespace etl }; } - //********************************* - static private_basic_format_spec::base_spec set_base(uint32_t base) + //*************************************************************************** + // Stream formatting manipulators. + //*************************************************************************** + static ETL_CONSTEXPR private_basic_format_spec::base_spec setbase(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) + static ETL_CONSTEXPR private_basic_format_spec::width_spec setw(uint32_t width) { return private_basic_format_spec::width_spec(width); } //********************************* template - static private_basic_format_spec::fill_spec set_fill(TChar fill) + static ETL_CONSTEXPR private_basic_format_spec::fill_spec setfill(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) + static ETL_CONSTEXPR private_basic_format_spec::precision_spec setprecision(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 ETL_CONST_OR_CONSTEXPR private_basic_format_spec::base_spec bin = { 2U }; //********************************* - static private_basic_format_spec::boolalpha_spec set_boolalpha(bool boolalpha) - { - return private_basic_format_spec::boolalpha_spec(boolalpha); - } + static ETL_CONST_OR_CONSTEXPR private_basic_format_spec::base_spec oct = { 8U }; + + //********************************* + static ETL_CONST_OR_CONSTEXPR private_basic_format_spec::base_spec dec = { 10U }; + + //********************************* + static ETL_CONST_OR_CONSTEXPR private_basic_format_spec::base_spec hex = { 16U }; + + //********************************* + static ETL_CONST_OR_CONSTEXPR private_basic_format_spec::left_spec left; + + //********************************* + static ETL_CONST_OR_CONSTEXPR private_basic_format_spec::right_spec right; + + //********************************* + static ETL_CONST_OR_CONSTEXPR private_basic_format_spec::boolalpha_spec boolalpha = { true }; + + //********************************* + static ETL_CONST_OR_CONSTEXPR private_basic_format_spec::boolalpha_spec noboolalpha = { false }; + + //********************************* + static ETL_CONST_OR_CONSTEXPR private_basic_format_spec::uppercase_spec uppercase = { true }; + + //********************************* + static ETL_CONST_OR_CONSTEXPR private_basic_format_spec::uppercase_spec nouppercase = { false }; //*************************************************************************** /// basic_format_spec @@ -212,15 +195,22 @@ namespace etl /// Left Justified = false //*************************************************************************** basic_format_spec() - : base_(10U) - , width_(0U) - , precision_(0U) - , upper_case_(true) - , left_justified_(false) - , boolalpha_(false) - , fill_(typename TString::value_type(' ')) { + clear(); + } + //*************************************************************************** + /// Clears the format spec back to default. + //*************************************************************************** + void clear() + { + base_ = 10U; + width_ = 0U; + precision_ = 0U; + upper_case_ = true; + left_justified_ = false; + boolalpha_ = false; + fill_ = typename TString::value_type(' '); } //*************************************************************************** diff --git a/include/etl/basic_string_stream.h b/include/etl/basic_string_stream.h new file mode 100644 index 00000000..2969af1f --- /dev/null +++ b/include/etl/basic_string_stream.h @@ -0,0 +1,276 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2020 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_BASIC_STRING_STREAM_INCLUDED +#define ETL_BASIC_STRING_STREAM_INCLUDED + +///\ingroup string + +#include "platform.h" + +namespace etl +{ + template + class basic_string_stream + { + public: + + typedef TFormat format_spec_type; + typedef TIString istring_type; + typedef typename TIString::value_type value_type; + typedef typename TIString::const_pointer const_pointer; + + //************************************************************************* + /// Construct from text. + //************************************************************************* + explicit basic_string_stream(TIString& text_) + : text(text_) + { + } + + //************************************************************************* + /// Construct from text and format spec. + //************************************************************************* + basic_string_stream(TIString& text_, const TFormat& spec_) + : text(text_) + , spec(spec_) + { + } + + //************************************************************************* + /// Set the format spec. + //************************************************************************* + void set_format(const TFormat& spec_) + { + spec = spec_; + } + + //************************************************************************* + /// Get a const reference to the format spec. + //************************************************************************* + const TFormat& get_format() const + { + return spec; + } + + //************************************************************************* + /// Get a reference to the current string. + //************************************************************************* + TIString& str() + { + return text; + } + + //************************************************************************* + /// Get a const reference to the current string. + //************************************************************************* + const TIString& str() const + { + return text; + } + + //************************************************************************* + /// Resets the stream to the supplied string. + //************************************************************************* + void str(const value_type* p) + { + text.assign(p); + } + + //************************************************************************* + /// Resets the stream to the supplied string. + //************************************************************************* + void str(const TIString& is) + { + text.assign(is); + } + + //************************************************************************* + /// Resets the stream to an empty string. + //************************************************************************* + void clear() + { + text.clear(); + } + + //************************************************************************* + /// Get the current size of the string. + //************************************************************************* + size_t size() const + { + return text.size(); + } + + //************************************************************************* + /// Stream operators. + //************************************************************************* + + //********************************* + /// TFormat + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, const TFormat& spec) + { + ss.spec = spec; + return ss; + } + + //********************************* + /// etl::base_spec from etl::setbase, etl::bin, etl::oct, etl::dec & etl::hex stream manipulators + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::base_spec spec) + { + ss.spec.base(spec.base); + return ss; + } + + //********************************* + /// etl::width_spec from etl::setw stream manipulator + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::width_spec spec) + { + ss.spec.width(spec.width); + return ss; + } + + //********************************* + /// etl::fill_spec from etl::setfill stream manipulator + //********************************* + template + friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::fill_spec spec) + { + ss.spec.fill(spec.fill); + return ss; + } + + //********************************* + /// etl::precision_spec from etl::setprecision stream manipulator + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::precision_spec spec) + { + ss.spec.precision(spec.precision); + return ss; + } + + //********************************* + /// etl::boolalpha_spec from etl::boolalpha & etl::noboolalpha stream manipulators + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::boolalpha_spec spec) + { + ss.spec.boolalpha(spec.boolalpha); + return ss; + } + + //********************************* + /// etl::uppercase_spec from etl::uppercase & etl::nouppercase stream manipulators + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::uppercase_spec spec) + { + ss.spec.upper_case(spec.upper_case); + return ss; + } + + //********************************* + /// etl::left_spec from etl::left stream manipulator + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::left_spec spec) + { + ss.spec.left(); + return ss; + } + + //********************************* + /// etl::right_spec from etl::left stream manipulator + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::right_spec spec) + { + ss.spec.right(); + return ss; + } + + //********************************* + /// From a string view + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, TStringView view) + { + etl::to_string(view, ss.text, ss.spec, true); + return ss; + } + + //********************************* + /// From a const pointer to a string + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, const_pointer p) + { + TStringView view(p); + ss << view; + return ss; + } + + //********************************* + /// From a string interface + //********************************* + friend basic_string_stream& operator <<(basic_string_stream& ss, const TIString& text) + { + etl::to_string(text, ss.text, ss.spec, true); + return ss; + } + + //********************************* + /// From a string + //********************************* + template