Added stream manipulators

This commit is contained in:
John Wellbelove 2020-07-09 14:21:40 +01:00
parent f40f0c1a37
commit 509a930b55
18 changed files with 597 additions and 682 deletions

View File

@ -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 <typename TChar>
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 <typename TChar>
static private_basic_format_spec::fill_spec<TChar> set_fill(TChar fill)
static ETL_CONSTEXPR private_basic_format_spec::fill_spec<TChar> setfill(TChar fill)
{
return private_basic_format_spec::fill_spec<TChar>(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(' ');
}
//***************************************************************************

View File

@ -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 <typename TFormat, typename TIString, typename TStringView>
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 <typename TChar>
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::fill_spec<TChar> 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 <template <size_t> class TString, size_t SIZE>
friend basic_string_stream& operator <<(basic_string_stream& ss, const TString<SIZE>& text)
{
const TIString& itext = text;
etl::to_string(itext, ss.str(), ss.get_format(), true);
return ss;
}
//*********************************
/// From anything else
//*********************************
template <typename T>
friend basic_string_stream& operator <<(basic_string_stream& ss, const T& value)
{
etl::to_string(value, ss.text, ss.spec, true);
return ss;
}
private:
TIString& text;
TFormat spec;
basic_string_stream(const basic_string_stream&) ETL_DELETE;
basic_string_stream& operator =(const basic_string_stream&) ETL_DELETE;
};
}
#endif

View File

@ -46,6 +46,11 @@ SOFTWARE.
#include <float.h>
#include <math.h>
#if defined(ETL_COMPILER_MICROSOFT)
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
#if ETL_NOT_USING_STL
#define ETL_LOG10_OF_2(x) (((x) * 301) / 1000)
@ -101,6 +106,7 @@ namespace etl
denorm_present = 1
};
class etl_integral_limits
{
public:
@ -621,4 +627,8 @@ namespace etl
}
#endif
#if defined(ETL_COMPILER_MICROSOFT)
#pragma warning(pop)
#endif
#endif

View File

@ -38,203 +38,11 @@ SOFTWARE.
#include "format_spec.h"
#include "to_string.h"
#include "string_view.h"
#include "basic_string_stream.h"
namespace etl
{
class string_stream
{
public:
typedef etl::format_spec format_spec_type;
typedef etl::istring istring_type;
typedef etl::istring::value_type value_type;
//*************************************************************************
/// Construct from text.
//*************************************************************************
explicit string_stream(etl::istring& text_)
: text(text_)
{
}
//*************************************************************************
/// Construct from text and format spec.
//*************************************************************************
string_stream(etl::istring& text_, const etl::format_spec& spec_)
: text(text_)
, spec(spec_)
{
}
//*************************************************************************
/// Set the format spec.
//*************************************************************************
void set_format(const etl::format_spec& spec_)
{
spec = spec_;
}
//*************************************************************************
/// Get a const reference to the format spec.
//*************************************************************************
const etl::format_spec& get_format() const
{
return spec;
}
//*************************************************************************
/// Get a reference to the current string.
//*************************************************************************
etl::istring& str()
{
return text;
}
//*************************************************************************
/// Get a const reference to the current string.
//*************************************************************************
const etl::istring& str() const
{
return text;
}
//*************************************************************************
/// Resets the stream to the supplied string.
//*************************************************************************
void str(const etl::istring::value_type* p)
{
text.assign(p);
}
//*************************************************************************
/// Resets the stream to the supplied string.
//*************************************************************************
void str(const etl::istring& is)
{
text.assign(is);
}
//*************************************************************************
/// Get the current size of the string.
//*************************************************************************
size_t size() const
{
return text.size();
}
//*************************************************************************
/// Stream operators.
//*************************************************************************
friend string_stream& operator <<(string_stream& ss, const etl::format_spec& spec)
{
ss.spec = spec;
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::base_spec spec)
{
ss.spec.base(spec.base);
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::width_spec spec)
{
ss.spec.width(spec.width);
return ss;
}
//*********************************
template <typename TChar>
friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::fill_spec<TChar> spec)
{
ss.spec.fill(spec.fill);
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::precision_spec spec)
{
ss.spec.precision(spec.precision);
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::boolalpha_spec spec)
{
ss.spec.boolalpha(spec.boolalpha);
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::upper_case_spec spec)
{
ss.spec.upper_case(spec.upper_case);
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::left_spec spec)
{
ss.spec.left();
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, etl::private_basic_format_spec::right_spec spec)
{
ss.spec.right();
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, etl::string_view view)
{
etl::to_string(view, ss.text, ss.spec, true);
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, etl::istring::const_pointer p)
{
etl::string_view view(p);
ss << view;
return ss;
}
//*********************************
friend string_stream& operator <<(string_stream& ss, const etl::istring& text)
{
etl::to_string(text, ss.text, ss.spec, true);
return ss;
}
//*********************************
template <size_t SIZE>
friend string_stream& operator <<(string_stream& ss, const etl::string<SIZE>& text)
{
const etl::istring& itext = text;
etl::to_string(itext, ss.text, ss.spec, true);
return ss;
}
//*********************************
template <typename T>
friend string_stream& operator <<(string_stream& ss, const T& value)
{
etl::to_string(value, ss.text, ss.spec, true);
return ss;
}
private:
etl::istring& text;
etl::format_spec spec;
string_stream(const string_stream&) ETL_DELETE;
string_stream& operator =(const string_stream&) ETL_DELETE;
};
typedef etl::basic_string_stream<etl::format_spec, etl::istring, etl::string_view> string_stream;
}
#endif

View File

@ -38,146 +38,11 @@ SOFTWARE.
#include "u16format_spec.h"
#include "to_u16string.h"
#include "string_view.h"
#include "basic_string_stream.h"
namespace etl
{
class u16string_stream
{
public:
typedef etl::u16format_spec format_spec_type;
typedef etl::iu16string istring_type;
typedef etl::iu16string::value_type value_type;
//*************************************************************************
/// Construct from text.
//*************************************************************************
explicit u16string_stream(etl::iu16string& text_)
: text(text_)
{
}
//*************************************************************************
/// Construct from text and format spec.
//*************************************************************************
u16string_stream(etl::iu16string& text_, const etl::u16format_spec& spec_)
: text(text_)
, spec(spec_)
{
}
//*************************************************************************
/// Set the format spec.
//*************************************************************************
void set_format(const etl::u16format_spec& spec_)
{
spec = spec_;
}
//*************************************************************************
/// Get a const reference to the format spec.
//*************************************************************************
const etl::u16format_spec& get_format() const
{
return spec;
}
//*************************************************************************
/// Get a reference to the current string.
//*************************************************************************
etl::iu16string& str()
{
return text;
}
//*************************************************************************
/// Get a const reference to the current string.
//*************************************************************************
const etl::iu16string& str() const
{
return text;
}
//*************************************************************************
/// Resets the stream to the supplied string.
//*************************************************************************
void str(const etl::iu16string::value_type* p)
{
text.assign(p);
}
//*************************************************************************
/// Resets the stream to the supplied string.
//*************************************************************************
void str(const etl::iu16string& is)
{
text.assign(is);
}
//*************************************************************************
/// Get the current size of the string.
//*************************************************************************
size_t size() const
{
return text.size();
}
//*************************************************************************
/// Stream operators.
//*************************************************************************
friend u16string_stream& operator <<(u16string_stream& ss, const etl::u16format_spec& spec)
{
ss.spec = spec;
return ss;
}
//*********************************
friend u16string_stream& operator <<(u16string_stream& ss, etl::u16string_view view)
{
etl::to_string(view, ss.text, ss.spec, true);
return ss;
}
//*********************************
friend u16string_stream& operator <<(u16string_stream& ss, etl::iu16string::const_pointer p)
{
etl::u16string_view view(p);
ss << view;
return ss;
}
//*********************************
friend u16string_stream& operator <<(u16string_stream& ss, const etl::iu16string& text)
{
etl::to_string(text, ss.text, ss.spec, true);
return ss;
}
//*********************************
template <size_t SIZE>
friend u16string_stream& operator <<(u16string_stream& ss, const etl::u16string<SIZE>& text)
{
const etl::iu16string& itext = text;
etl::to_string(itext, ss.text, ss.spec, true);
return ss;
}
//*********************************
template <typename T>
friend u16string_stream& operator <<(u16string_stream& ss, const T& value)
{
etl::to_string(value, ss.text, ss.spec, true);
return ss;
}
private:
etl::iu16string& text;
etl::u16format_spec spec;
u16string_stream(const u16string_stream&) ETL_DELETE;
u16string_stream& operator =(const u16string_stream&) ETL_DELETE;
};
typedef etl::basic_string_stream<etl::u16format_spec, etl::iu16string, etl::u16string_view> u16string_stream;
}
#endif

View File

@ -38,146 +38,11 @@ SOFTWARE.
#include "u32format_spec.h"
#include "to_u32string.h"
#include "string_view.h"
#include "basic_string_stream.h"
namespace etl
{
class u32string_stream
{
public:
typedef etl::u32format_spec format_spec_type;
typedef etl::iu32string istring_type;
typedef etl::iu32string::value_type value_type;
//*************************************************************************
/// Construct from text.
//*************************************************************************
explicit u32string_stream(etl::iu32string& text_)
: text(text_)
{
}
//*************************************************************************
/// Construct from text and format spec.
//*************************************************************************
u32string_stream(etl::iu32string& text_, const etl::u32format_spec& spec_)
: text(text_)
, spec(spec_)
{
}
//*************************************************************************
/// Set the format spec.
//*************************************************************************
void set_format(const etl::u32format_spec& spec_)
{
spec = spec_;
}
//*************************************************************************
/// Get a const reference to the format spec.
//*************************************************************************
const etl::u32format_spec& get_format() const
{
return spec;
}
//*************************************************************************
/// Get a reference to the current string.
//*************************************************************************
etl::iu32string& str()
{
return text;
}
//*************************************************************************
/// Get a const reference to the current string.
//*************************************************************************
const etl::iu32string& str() const
{
return text;
}
//*************************************************************************
/// Resets the stream to the supplied string.
//*************************************************************************
void str(const etl::iu32string::value_type* p)
{
text.assign(p);
}
//*************************************************************************
/// Resets the stream to the supplied string.
//*************************************************************************
void str(const etl::iu32string& is)
{
text.assign(is);
}
//*************************************************************************
/// Get the current size of the string.
//*************************************************************************
size_t size() const
{
return text.size();
}
//*************************************************************************
/// Stream operators.
//*************************************************************************
friend u32string_stream& operator <<(u32string_stream& ss, const etl::u32format_spec& spec)
{
ss.spec = spec;
return ss;
}
//*********************************
friend u32string_stream& operator <<(u32string_stream& ss, etl::u32string_view view)
{
etl::to_string(view, ss.text, ss.spec, true);
return ss;
}
//*********************************
friend u32string_stream& operator <<(u32string_stream& ss, etl::iu32string::const_pointer p)
{
etl::u32string_view view(p);
ss << view;
return ss;
}
//*********************************
friend u32string_stream& operator <<(u32string_stream& ss, const etl::iu32string& text)
{
etl::to_string(text, ss.text, ss.spec, true);
return ss;
}
//*********************************
template <size_t SIZE>
friend u32string_stream& operator <<(u32string_stream& ss, const etl::u32string<SIZE>& text)
{
const etl::iu32string& itext = text;
etl::to_string(itext, ss.text, ss.spec, true);
return ss;
}
//*********************************
template <typename T>
friend u32string_stream& operator <<(u32string_stream& ss, const T& value)
{
etl::to_string(value, ss.text, ss.spec, true);
return ss;
}
private:
etl::iu32string& text;
etl::u32format_spec spec;
u32string_stream(const u32string_stream&) ETL_DELETE;
u32string_stream& operator =(const u32string_stream&) ETL_DELETE;
};
typedef etl::basic_string_stream<etl::u32format_spec, etl::iu32string, etl::u32string_view> u32string_stream;
}
#endif

View File

@ -38,7 +38,7 @@ SOFTWARE.
///\ingroup utilities
#define ETL_VERSION_MAJOR 18
#define ETL_VERSION_MINOR 5
#define ETL_VERSION_MINOR 6
#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)

View File

@ -38,146 +38,11 @@ SOFTWARE.
#include "wformat_spec.h"
#include "to_wstring.h"
#include "string_view.h"
#include "basic_string_stream.h"
namespace etl
{
class wstring_stream
{
public:
typedef etl::wformat_spec format_spec_type;
typedef etl::iwstring istring_type;
typedef etl::iwstring::value_type value_type;
//*************************************************************************
/// Construct from text.
//*************************************************************************
explicit wstring_stream(etl::iwstring& text_)
: text(text_)
{
}
//*************************************************************************
/// Construct from text and format spec.
//*************************************************************************
wstring_stream(etl::iwstring& text_, const etl::wformat_spec& spec_)
: text(text_)
, spec(spec_)
{
}
//*************************************************************************
/// Set the format spec.
//*************************************************************************
void set_format(const etl::wformat_spec& spec_)
{
spec = spec_;
}
//*************************************************************************
/// Get a const reference to the format spec.
//*************************************************************************
const etl::wformat_spec& get_format() const
{
return spec;
}
//*************************************************************************
/// Get a reference to the current string.
//*************************************************************************
etl::iwstring& str()
{
return text;
}
//*************************************************************************
/// Get a const reference to the current string.
//*************************************************************************
const etl::iwstring& str() const
{
return text;
}
//*************************************************************************
/// Resets the stream to the supplied string.
//*************************************************************************
void str(const etl::iwstring::value_type* p)
{
text.assign(p);
}
//*************************************************************************
/// Resets the stream to the supplied string.
//*************************************************************************
void str(const etl::iwstring& is)
{
text.assign(is);
}
//*************************************************************************
/// Get the current size of the string.
//*************************************************************************
size_t size() const
{
return text.size();
}
//*************************************************************************
/// Stream operators.
//*************************************************************************
friend wstring_stream& operator <<(wstring_stream& ss, const etl::wformat_spec& spec)
{
ss.spec = spec;
return ss;
}
//*********************************
friend wstring_stream& operator <<(wstring_stream& ss, etl::wstring_view view)
{
etl::to_string(view, ss.text, ss.spec, true);
return ss;
}
//*********************************
friend wstring_stream& operator <<(wstring_stream& ss, etl::iwstring::const_pointer p)
{
etl::wstring_view view(p);
ss << view;
return ss;
}
//*********************************
friend wstring_stream& operator <<(wstring_stream& ss, const etl::iwstring& text)
{
etl::to_string(text, ss.text, ss.spec, true);
return ss;
}
//*********************************
template <size_t SIZE>
friend wstring_stream& operator <<(wstring_stream& ss, const etl::wstring<SIZE>& text)
{
const etl::iwstring& itext = text;
etl::to_string(itext, ss.text, ss.spec, true);
return ss;
}
//*********************************
template <typename T>
friend wstring_stream& operator <<(wstring_stream& ss, const T& value)
{
etl::to_string(value, ss.text, ss.spec, true);
return ss;
}
private:
etl::iwstring& text;
etl::wformat_spec spec;
wstring_stream(const wstring_stream&) ETL_DELETE;
wstring_stream& operator =(const wstring_stream&) ETL_DELETE;
};
typedef etl::basic_string_stream<etl::wformat_spec, etl::iwstring, etl::wstring_view> wstring_stream;
}
#endif

View File

@ -1,6 +1,6 @@
{
"name": "Embedded Template Library",
"version": "18.5.0",
"version": "18.6.0",
"authors": {
"name": "John Wellbelove",
"email": "john.wellbelove@etlcpp.com"

View File

@ -1,5 +1,5 @@
name=Embedded Template Library
version=18.5.0
version=18.6.0
author= John Wellbelove <john.wellbelove@etlcpp.com>
maintainer=John Wellbelove <john.wellbelove@etlcpp.com>
license=MIT

View File

@ -1,3 +1,7 @@
===============================================================================
18.6.0
Added stream manipulators
===============================================================================
18.5.0
Added string streams

View File

@ -225,6 +225,7 @@
<Unit filename="../../include/etl/atomic/atomic_std.h" />
<Unit filename="../../include/etl/basic_format_spec.h" />
<Unit filename="../../include/etl/basic_string.h" />
<Unit filename="../../include/etl/basic_string_stream.h" />
<Unit filename="../../include/etl/binary.h" />
<Unit filename="../../include/etl/bit_stream.h" />
<Unit filename="../../include/etl/bitset.h" />
@ -433,6 +434,7 @@
<Unit filename="../../include/etl/stack.h" />
<Unit filename="../../include/etl/state_chart.h" />
<Unit filename="../../include/etl/static_assert.h" />
<Unit filename="../../include/etl/string_stream.h" />
<Unit filename="../../include/etl/string_view.h" />
<Unit filename="../../include/etl/task.h" />
<Unit filename="../../include/etl/temp.h" />
@ -448,8 +450,10 @@
<Unit filename="../../include/etl/type_traits.h" />
<Unit filename="../../include/etl/u16format_spec.h" />
<Unit filename="../../include/etl/u16string.h" />
<Unit filename="../../include/etl/u16string_stream.h" />
<Unit filename="../../include/etl/u32format_spec.h" />
<Unit filename="../../include/etl/u32string.h" />
<Unit filename="../../include/etl/u32string_stream.h" />
<Unit filename="../../include/etl/unordered_map.h" />
<Unit filename="../../include/etl/unordered_multimap.h" />
<Unit filename="../../include/etl/unordered_multiset.h" />
@ -463,6 +467,7 @@
<Unit filename="../../include/etl/visitor.h" />
<Unit filename="../../include/etl/wformat_spec.h" />
<Unit filename="../../include/etl/wstring.h" />
<Unit filename="../../include/etl/wstring_stream.h" />
<Unit filename="../ExtraCheckMacros.h" />
<Unit filename="../data.h" />
<Unit filename="../etl_profile.h" />
@ -570,6 +575,7 @@
<Unit filename="../test_stack.cpp" />
<Unit filename="../test_state_chart.cpp" />
<Unit filename="../test_string_char.cpp" />
<Unit filename="../test_string_stream.cpp" />
<Unit filename="../test_string_u16.cpp" />
<Unit filename="../test_string_u32.cpp" />
<Unit filename="../test_string_view.cpp" />
@ -583,6 +589,8 @@
<Unit filename="../test_type_lookup.cpp" />
<Unit filename="../test_type_select.cpp" />
<Unit filename="../test_type_traits.cpp" />
<Unit filename="../test_u16string_stream.cpp" />
<Unit filename="../test_u32string_stream.cpp" />
<Unit filename="../test_unordered_map.cpp" />
<Unit filename="../test_unordered_multimap.cpp" />
<Unit filename="../test_unordered_multiset.cpp" />
@ -597,6 +605,7 @@
<Unit filename="../test_vector_pointer.cpp" />
<Unit filename="../test_vector_pointer_external_buffer.cpp" />
<Unit filename="../test_visitor.cpp" />
<Unit filename="../test_wstring_stream.cpp" />
<Unit filename="../test_xor_checksum.cpp" />
<Unit filename="../test_xor_rotate_checksum.cpp" />
<Extensions>

View File

@ -125,21 +125,60 @@ namespace
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
}
//*************************************************************************
TEST(test_custom_inline_format)
{
String str;
Stream ss(str);
int value = 123456;
ss << etl::setbase(10U) << etl::setw(20) << etl::setfill(STR('-')) << etl::left;
ss << etl::bin << value;
CHECK_EQUAL(String(STR("11110001001000000---")), ss.str());
ss.clear();
ss << etl::oct << value;
CHECK_EQUAL(String(STR("361100--------------")), ss.str());
ss.clear();
ss << etl::dec << value;
CHECK_EQUAL(String(STR("123456--------------")), ss.str());
ss.clear();
ss << etl::hex << etl::uppercase << value;
CHECK_EQUAL(String(STR("1E240---------------")), ss.str());
ss.clear();
ss << etl::hex << etl::nouppercase << value;
CHECK_EQUAL(String(STR("1e240---------------")), ss.str());
ss.clear();
ss << etl::setw(0);
ss << etl::noboolalpha << false << STR(" ") << true << STR(" ") << etl::boolalpha << false << STR(" ") << true;
CHECK_EQUAL(String(STR("0 1 false true")), ss.str());
ss.clear();
ss << etl::setprecision(4) << 3.1415927;
CHECK_EQUAL(String(STR("3.1416")), ss.str());
ss.clear();
ss << STR("abcdef") << STR(" ") << etl::uppercase << STR("abcdef");
CHECK_EQUAL(String(STR("abcdef abcdef")), ss.str());
}
//*************************************************************************
TEST(test_custom_multi_inline_format)
{
String str;
Format format1 = Format().base(10).width(10).fill(STR('#'));
Format format2 = Format().base(10).width(8).fill(STR('*')).left();
Format format3 = Format().base(16).width(4).fill(STR(' ')).right();
Stream ss(str);
int value = 123;
String hello(STR("Hello"));
ss << etl::set_base(10) << etl::set_width(10) << etl::set_fill(STR('#')) << hello
<< etl::set_width(8) << etl::set_fill(STR('*')) << etl::set_left() << STR("World")
<< etl::set_base(16) << etl::set_width(4) << etl::set_fill(STR(' ')) << etl::set_right() << value;
ss << etl::dec << etl::setw(10) << etl::setfill(STR('#')) << hello
<< etl::setw(8) << etl::setfill(STR('*')) << etl::left << STR("World")
<< etl::hex << etl::setw(4) << etl::setfill(STR(' ')) << etl::right << value;
String result = ss.str();

View File

@ -125,6 +125,66 @@ namespace
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
}
//*************************************************************************
TEST(test_custom_inline_format)
{
String str;
Stream ss(str);
int value = 123456;
ss << etl::setw(20) << etl::setfill(STR('-')) << etl::left;
ss << etl::bin << value;
CHECK_EQUAL(String(STR("11110001001000000---")), ss.str());
ss.clear();
ss << etl::oct << value;
CHECK_EQUAL(String(STR("361100--------------")), ss.str());
ss.clear();
ss << etl::dec << value;
CHECK_EQUAL(String(STR("123456--------------")), ss.str());
ss.clear();
ss << etl::hex << etl::uppercase << value;
CHECK_EQUAL(String(STR("1E240---------------")), ss.str());
ss.clear();
ss << etl::hex << etl::nouppercase << value;
CHECK_EQUAL(String(STR("1e240---------------")), ss.str());
ss.clear();
ss << etl::setw(0);
ss << etl::noboolalpha << false << STR(" ") << true << STR(" ") << etl::boolalpha << false << STR(" ") << true;
CHECK_EQUAL(String(STR("0 1 false true")), ss.str());
ss.clear();
ss << etl::setprecision(4) << 3.1415927;
CHECK_EQUAL(String(STR("3.1416")), ss.str());
ss.clear();
ss << STR("abcdef") << STR(" ") << etl::uppercase << STR("abcdef");
CHECK_EQUAL(String(STR("abcdef abcdef")), ss.str());
}
//*************************************************************************
TEST(test_custom_multi_inline_format)
{
String str;
Stream ss(str);
int value = 123;
String hello(STR("Hello"));
ss << etl::dec << etl::setw(10) << etl::setfill(STR('#')) << hello
<< etl::setw(8) << etl::setfill(STR('*')) << etl::left << STR("World")
<< etl::hex << etl::setw(4) << etl::setfill(STR(' ')) << etl::right << value;
String result = ss.str();
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
}
//*************************************************************************
TEST(test_custom_class_default_format)
{

View File

@ -125,6 +125,66 @@ namespace
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
}
//*************************************************************************
TEST(test_custom_inline_format)
{
String str;
Stream ss(str);
int value = 123456;
ss << etl::setw(20) << etl::setfill(STR('-')) << etl::left;
ss << etl::bin << value;
CHECK_EQUAL(String(STR("11110001001000000---")), ss.str());
ss.clear();
ss << etl::oct << value;
CHECK_EQUAL(String(STR("361100--------------")), ss.str());
ss.clear();
ss << etl::dec << value;
CHECK_EQUAL(String(STR("123456--------------")), ss.str());
ss.clear();
ss << etl::hex << etl::uppercase << value;
CHECK_EQUAL(String(STR("1E240---------------")), ss.str());
ss.clear();
ss << etl::hex << etl::nouppercase << value;
CHECK_EQUAL(String(STR("1e240---------------")), ss.str());
ss.clear();
ss << etl::setw(0);
ss << etl::noboolalpha << false << STR(" ") << true << STR(" ") << etl::boolalpha << false << STR(" ") << true;
CHECK_EQUAL(String(STR("0 1 false true")), ss.str());
ss.clear();
ss << etl::setprecision(4) << 3.1415927;
CHECK_EQUAL(String(STR("3.1416")), ss.str());
ss.clear();
ss << STR("abcdef") << STR(" ") << etl::uppercase << STR("abcdef");
CHECK_EQUAL(String(STR("abcdef abcdef")), ss.str());
}
//*************************************************************************
TEST(test_custom_multi_inline_format)
{
String str;
Stream ss(str);
int value = 123;
String hello(STR("Hello"));
ss << etl::dec << etl::setw(10) << etl::setfill(STR('#')) << hello
<< etl::setw(8) << etl::setfill(STR('*')) << etl::left << STR("World")
<< etl::hex << etl::setw(4) << etl::setfill(STR(' ')) << etl::right << value;
String result = ss.str();
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
}
//*************************************************************************
TEST(test_custom_class_default_format)
{

View File

@ -125,6 +125,66 @@ namespace
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
}
//*************************************************************************
TEST(test_custom_inline_format)
{
String str;
Stream ss(str);
int value = 123456;
ss << etl::setw(20) << etl::setfill(STR('-')) << etl::left;
ss << etl::bin << value;
CHECK_EQUAL(String(STR("11110001001000000---")), ss.str());
ss.clear();
ss << etl::oct << value;
CHECK_EQUAL(String(STR("361100--------------")), ss.str());
ss.clear();
ss << etl::dec << value;
CHECK_EQUAL(String(STR("123456--------------")), ss.str());
ss.clear();
ss << etl::hex << etl::uppercase << value;
CHECK_EQUAL(String(STR("1E240---------------")), ss.str());
ss.clear();
ss << etl::hex << etl::nouppercase << value;
CHECK_EQUAL(String(STR("1e240---------------")), ss.str());
ss.clear();
ss << etl::setw(0);
ss << etl::noboolalpha << false << STR(" ") << true << STR(" ") << etl::boolalpha << false << STR(" ") << true;
CHECK_EQUAL(String(STR("0 1 false true")), ss.str());
ss.clear();
ss << etl::setprecision(4) << 3.1415927;
CHECK_EQUAL(String(STR("3.1416")), ss.str());
ss.clear();
ss << STR("abcdef") << STR(" ") << etl::uppercase << STR("abcdef");
CHECK_EQUAL(String(STR("abcdef abcdef")), ss.str());
}
//*************************************************************************
TEST(test_custom_multi_inline_format)
{
String str;
Stream ss(str);
int value = 123;
String hello(STR("Hello"));
ss << etl::dec << etl::setw(10) << etl::setfill(STR('#')) << hello
<< etl::setw(8) << etl::setfill(STR('*')) << etl::left << STR("World")
<< etl::hex << etl::setw(4) << etl::setfill(STR(' ')) << etl::right << value;
String result = ss.str();
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
}
//*************************************************************************
TEST(test_custom_class_default_format)
{

View File

@ -1013,6 +1013,7 @@
<ClInclude Include="..\..\include\etl\atomic\atomic_llvm_sync.h" />
<ClInclude Include="..\..\include\etl\atomic\atomic_std.h" />
<ClInclude Include="..\..\include\etl\basic_format_spec.h" />
<ClInclude Include="..\..\include\etl\basic_string_stream.h" />
<ClInclude Include="..\..\include\etl\bit_stream.h" />
<ClInclude Include="..\..\include\etl\callback_timer.h" />
<ClInclude Include="..\..\include\etl\combinations.h" />

View File

@ -867,6 +867,9 @@
<ClInclude Include="..\..\include\etl\u32string_stream.h">
<Filter>ETL\Strings</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\basic_string_stream.h">
<Filter>ETL\Strings</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\main.cpp">