mirror of
https://github.com/ETLCPP/etl.git
synced 2026-07-01 06:48:44 +08:00
Merge branch 'feature/string-streams' into development
# Conflicts: # include/etl/version.h
This commit is contained in:
commit
e9f861c74a
@ -248,6 +248,28 @@ namespace etl
|
||||
return boolalpha_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Equality operator.
|
||||
//***************************************************************************
|
||||
friend bool operator ==(const basic_format_spec& lhs, const basic_format_spec& rhs)
|
||||
{
|
||||
return (lhs.base_ == rhs.base_) &&
|
||||
(lhs.width_ == rhs.width_) &&
|
||||
(lhs.precision_ == rhs.precision_) &&
|
||||
(lhs.upper_case_ == rhs.upper_case_) &&
|
||||
(lhs.left_justified_ == rhs.left_justified_) &&
|
||||
(lhs.boolalpha_ == rhs.boolalpha_) &&
|
||||
(lhs.fill_ == rhs.fill_);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Inequality operator.
|
||||
//***************************************************************************
|
||||
friend bool operator !=(const basic_format_spec& lhs, const basic_format_spec& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
uint_least8_t base_;
|
||||
|
||||
@ -88,7 +88,7 @@ namespace etl
|
||||
void add_boolean(const bool value,
|
||||
TIString& str,
|
||||
const etl::basic_format_spec<TIString>& format,
|
||||
const bool append)
|
||||
bool append)
|
||||
{
|
||||
typedef typename TIString::value_type type;
|
||||
typedef typename TIString::iterator iterator;
|
||||
@ -136,7 +136,7 @@ namespace etl
|
||||
void add_integral(T value,
|
||||
TIString& str,
|
||||
const etl::basic_format_spec<TIString>& format,
|
||||
const bool append)
|
||||
bool append)
|
||||
{
|
||||
typedef typename TIString::value_type type;
|
||||
typedef typename TIString::iterator iterator;
|
||||
@ -228,7 +228,7 @@ namespace etl
|
||||
void add_floating_point(T value,
|
||||
TIString& str,
|
||||
const etl::basic_format_spec<TIString>& format,
|
||||
const bool append)
|
||||
bool append)
|
||||
{
|
||||
typedef typename TIString::iterator iterator;
|
||||
typedef typename TIString::value_type type;
|
||||
@ -289,22 +289,64 @@ namespace etl
|
||||
void add_pointer(const volatile void* value,
|
||||
TIString& str,
|
||||
const etl::basic_format_spec<TIString>& format,
|
||||
const bool append)
|
||||
bool append)
|
||||
{
|
||||
uintptr_t p = reinterpret_cast<uintptr_t>(value);
|
||||
|
||||
return etl::private_to_string::add_integral(p, str, format, append);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Helper function for strings.
|
||||
//***************************************************************************
|
||||
template <typename TIString>
|
||||
void add_string(const TIString& value,
|
||||
TIString& str,
|
||||
const etl::basic_format_spec<TIString>& format,
|
||||
bool append)
|
||||
{
|
||||
if (!append)
|
||||
{
|
||||
str.clear();
|
||||
}
|
||||
|
||||
typename TIString::iterator start = str.end();
|
||||
|
||||
str.insert(str.end(), value.begin(), value.end());
|
||||
|
||||
etl::private_to_string::add_alignment(str, start, format);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Helper function for string views.
|
||||
//***************************************************************************
|
||||
template <typename TSringView, typename TIString>
|
||||
void add_string_view(const TSringView& value,
|
||||
TIString& str,
|
||||
const etl::basic_format_spec<TIString>& format,
|
||||
bool append)
|
||||
{
|
||||
if (!append)
|
||||
{
|
||||
str.clear();
|
||||
}
|
||||
|
||||
typename TIString::iterator start = str.end();
|
||||
|
||||
str.insert(str.end(), value.begin(), value.end());
|
||||
|
||||
etl::private_to_string::add_alignment(str, start, format);
|
||||
}
|
||||
|
||||
//*********************************************************************************************************
|
||||
|
||||
//***************************************************************************
|
||||
/// For booleans. Default format spec.
|
||||
//***************************************************************************etl::basic_format_spec<TIString>
|
||||
//***************************************************************************
|
||||
template <typename TIString>
|
||||
const TIString& to_string(const bool value,
|
||||
TIString& str,
|
||||
const bool append = false)
|
||||
bool append = false)
|
||||
{
|
||||
etl::basic_format_spec<TIString> format;
|
||||
|
||||
@ -320,7 +362,7 @@ namespace etl
|
||||
const TIString& to_string(const bool value,
|
||||
TIString& str,
|
||||
const etl::basic_format_spec<TIString>& format,
|
||||
const bool append = false)
|
||||
bool append = false)
|
||||
{
|
||||
etl::private_to_string::add_boolean(value, str, format, append);
|
||||
|
||||
@ -333,7 +375,7 @@ namespace etl
|
||||
template <typename TIString>
|
||||
const TIString& to_string(const volatile void* value,
|
||||
TIString& str,
|
||||
const bool append = false)
|
||||
bool append = false)
|
||||
{
|
||||
etl::basic_format_spec<TIString> format;
|
||||
|
||||
@ -349,7 +391,7 @@ namespace etl
|
||||
const TIString& to_string(const volatile void* value,
|
||||
TIString& str,
|
||||
const etl::basic_format_spec<TIString>& format,
|
||||
const bool append = false)
|
||||
bool append = false)
|
||||
{
|
||||
etl::private_to_string::add_pointer(value, str, format, append);
|
||||
|
||||
@ -364,7 +406,7 @@ namespace etl
|
||||
typename etl::enable_if<etl::is_integral<T>::value &&
|
||||
etl::is_signed<T>::value &&
|
||||
!etl::is_same<T, bool>::value>::value, const TIString& > ::type
|
||||
to_string(const T value, TIString& str, const bool append = false)
|
||||
to_string(const T value, TIString& str, bool append = false)
|
||||
{
|
||||
etl::basic_format_spec<TIString> format;
|
||||
|
||||
@ -380,7 +422,7 @@ namespace etl
|
||||
typename etl::enable_if<etl::is_integral<T>::value &&
|
||||
etl::is_signed<T>::value &&
|
||||
!etl::is_same<T, bool>::value>::value, const TIString& > ::type
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, const bool append = false)
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, bool append = false)
|
||||
{
|
||||
etl::private_to_string::add_integral(int32_t(value), str, format, append);
|
||||
|
||||
@ -394,7 +436,7 @@ namespace etl
|
||||
typename etl::enable_if<etl::is_integral<T>::value &&
|
||||
etl::is_unsigned<T>::value &&
|
||||
!etl::is_same<T, bool>::value>::value, const TIString& > ::type
|
||||
to_string(const T value, TIString& str, const bool append = false)
|
||||
to_string(const T value, TIString& str, bool append = false)
|
||||
{
|
||||
etl::basic_format_spec<TIString> format;
|
||||
|
||||
@ -410,7 +452,7 @@ namespace etl
|
||||
typename etl::enable_if<etl::is_integral<T>::value &&
|
||||
etl::is_unsigned<T>::value &&
|
||||
!etl::is_same<T, bool>::value>::value, const TIString& > ::type
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, const bool append = false)
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, bool append = false)
|
||||
{
|
||||
etl::private_to_string::add_integral(uint32_t(value), str, format, append);
|
||||
|
||||
@ -425,7 +467,7 @@ namespace etl
|
||||
etl::is_signed<T>::value &&
|
||||
!etl::is_same<T, bool>::value &&
|
||||
!etl::is_same<T, int64_t>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const bool append = false)
|
||||
to_string(const T value, TIString& str, bool append = false)
|
||||
{
|
||||
etl::basic_format_spec<TIString> format;
|
||||
|
||||
@ -442,7 +484,7 @@ namespace etl
|
||||
etl::is_signed<T>::value &&
|
||||
!etl::is_same<T, bool>::value &&
|
||||
!etl::is_same<T, int64_t>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, const bool append = false)
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, bool append = false)
|
||||
{
|
||||
etl::private_to_string::add_integral(int32_t(value), str, format, append);
|
||||
|
||||
@ -457,7 +499,7 @@ namespace etl
|
||||
etl::is_unsigned<T>::value &&
|
||||
!etl::is_same<T, bool>::value &&
|
||||
!etl::is_same<T, uint64_t>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const bool append = false)
|
||||
to_string(const T value, TIString& str, bool append = false)
|
||||
{
|
||||
etl::basic_format_spec<TIString> format;
|
||||
|
||||
@ -474,7 +516,7 @@ namespace etl
|
||||
etl::is_unsigned<T>::value &&
|
||||
!etl::is_same<T, bool>::value &&
|
||||
!etl::is_same<T, uint64_t>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, const bool append = false)
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, bool append = false)
|
||||
{
|
||||
etl::private_to_string::add_integral(uint32_t(value), str, format, append);
|
||||
|
||||
@ -489,7 +531,7 @@ namespace etl
|
||||
etl::is_signed<T>::value &&
|
||||
!etl::is_same<T, bool>::value &&
|
||||
etl::is_same<T, int64_t>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const bool append = false)
|
||||
to_string(const T value, TIString& str, bool append = false)
|
||||
{
|
||||
etl::basic_format_spec<TIString> format;
|
||||
|
||||
@ -506,7 +548,7 @@ namespace etl
|
||||
etl::is_signed<T>::value &&
|
||||
!etl::is_same<T, bool>::value &&
|
||||
etl::is_same<T, int64_t>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, const bool append = false)
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, bool append = false)
|
||||
{
|
||||
etl::private_to_string::add_integral(int64_t(value), str, format, append);
|
||||
|
||||
@ -521,7 +563,7 @@ namespace etl
|
||||
etl::is_unsigned<T>::value &&
|
||||
!etl::is_same<T, bool>::value &&
|
||||
etl::is_same<T, uint64_t>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const bool append = false)
|
||||
to_string(const T value, TIString& str, bool append = false)
|
||||
{
|
||||
etl::basic_format_spec<TIString> format;
|
||||
|
||||
@ -538,7 +580,7 @@ namespace etl
|
||||
etl::is_unsigned<T>::value &&
|
||||
!etl::is_same<T, bool>::value &&
|
||||
etl::is_same<T, uint64_t>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, const bool append = false)
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, bool append = false)
|
||||
{
|
||||
etl::private_to_string::add_integral(uint64_t(value), str, format, append);
|
||||
|
||||
@ -551,7 +593,7 @@ namespace etl
|
||||
//***************************************************************************
|
||||
template <typename T, typename TIString>
|
||||
typename etl::enable_if<etl::is_floating_point<T>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const bool append = false)
|
||||
to_string(const T value, TIString& str, bool append = false)
|
||||
{
|
||||
etl::basic_format_spec<TIString> format;
|
||||
|
||||
@ -565,7 +607,7 @@ namespace etl
|
||||
//***************************************************************************
|
||||
template <typename T, typename TIString>
|
||||
typename etl::enable_if<etl::is_floating_point<T>::value, const TIString&>::type
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, const bool append = false)
|
||||
to_string(const T value, TIString& str, const etl::basic_format_spec<TIString>& format, bool append = false)
|
||||
{
|
||||
etl::private_to_string::add_floating_point(value, str, format, append);
|
||||
|
||||
|
||||
180
include/etl/string_stream.h
Normal file
180
include/etl/string_stream.h
Normal file
@ -0,0 +1,180 @@
|
||||
///\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_STRING_STREAM_INCLUDED
|
||||
#define ETL_STRING_STREAM_INCLUDED
|
||||
|
||||
///\ingroup string
|
||||
|
||||
#include "platform.h"
|
||||
#include "cstring.h"
|
||||
#include "format_spec.h"
|
||||
#include "to_string.h"
|
||||
#include "string_view.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_, etl::format_spec spec_)
|
||||
: text(text_)
|
||||
, spec(spec_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Set the format spec.
|
||||
//*************************************************************************
|
||||
void set_format(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, etl::format_spec spec)
|
||||
{
|
||||
ss.spec = spec;
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -43,9 +43,11 @@ namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// !etl::istring && !etl::string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
const etl::istring& to_string(const T value, etl::istring& str, const bool append = false)
|
||||
typename etl::enable_if<!etl::is_same<T, etl::istring>::value && !etl::is_same<T, etl::string_view>::value, const etl::istring&>::type
|
||||
to_string(const T value, etl::istring& str, bool append = false)
|
||||
{
|
||||
etl::format_spec format;
|
||||
|
||||
@ -53,13 +55,71 @@ namespace etl
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec..
|
||||
/// Supplied format spec.
|
||||
/// !etl::istring && !etl::string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
const etl::istring& to_string(const T value, etl::istring& str, const etl::format_spec& format, const bool append = false)
|
||||
typename etl::enable_if<!etl::is_same<T, etl::istring>::value && !etl::is_same<T, etl::string_view>::value, const etl::istring&>::type
|
||||
to_string(const T value, etl::istring& str, const etl::format_spec& format, bool append = false)
|
||||
{
|
||||
return private_to_string::to_string(value, str, format, append);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// etl::istring
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::istring>::value, const etl::istring&>::type
|
||||
to_string(const T& value, etl::istring& str, bool append = false)
|
||||
{
|
||||
etl::format_spec format;
|
||||
|
||||
private_to_string::add_string(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// etl::istring
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::istring>::value, const etl::istring&>::type
|
||||
to_string(const etl::istring& value, T& str, const etl::format_spec& format, bool append = false)
|
||||
{
|
||||
private_to_string::add_string(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// etl::string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::string_view>::value, const etl::istring&>::type
|
||||
to_string(T value, etl::istring& str, bool append = false)
|
||||
{
|
||||
etl::format_spec format;
|
||||
|
||||
private_to_string::add_string_view(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// etl::string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::string_view>::value, const etl::istring&>::type
|
||||
to_string(T value, etl::istring& str, const etl::format_spec& format, bool append = false)
|
||||
{
|
||||
private_to_string::add_string_view(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -43,9 +43,11 @@ namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// !etl::iu16string && !etl::u16string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
const etl::iu16string& to_string(const T value, etl::iu16string& str, const bool append = false)
|
||||
typename etl::enable_if<!etl::is_same<T, etl::iu16string>::value && !etl::is_same<T, etl::u16string_view>::value, const etl::iu16string&>::type
|
||||
to_string(const T value, etl::iu16string& str, bool append = false)
|
||||
{
|
||||
etl::u16format_spec format;
|
||||
|
||||
@ -54,12 +56,70 @@ namespace etl
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// !etl::iu16string && !etl::u16string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
const etl::iu16string& to_string(const T value, etl::iu16string& str, const etl::u16format_spec& format, const bool append = false)
|
||||
typename etl::enable_if<!etl::is_same<T, etl::iu16string>::value && !etl::is_same<T, etl::u16string_view>::value, const etl::iu16string&>::type
|
||||
to_string(const T value, etl::iu16string& str, const etl::u16format_spec& format, bool append = false)
|
||||
{
|
||||
return private_to_string::to_string(value, str, format, append);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// etl::iu16string
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::iu16string>::value, const etl::iu16string&>::type
|
||||
to_string(const T& value, etl::iu16string& str, bool append = false)
|
||||
{
|
||||
etl::u16format_spec format;
|
||||
|
||||
private_to_string::add_string(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// etl::iu16string
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::iu16string>::value, const etl::iu16string&>::type
|
||||
to_string(const etl::iu16string& value, T& str, const etl::u16format_spec& format, bool append = false)
|
||||
{
|
||||
private_to_string::add_string(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// etl::u16string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::u16string_view>::value, const etl::iu16string&>::type
|
||||
to_string(T value, etl::iu16string& str, bool append = false)
|
||||
{
|
||||
etl::u16format_spec format;
|
||||
|
||||
private_to_string::add_string_view(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// etl::u16string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::u16string_view>::value, const etl::iu16string&>::type
|
||||
to_string(T value, etl::iu16string& str, const etl::u16format_spec& format, bool append = false)
|
||||
{
|
||||
private_to_string::add_string_view(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -43,9 +43,11 @@ namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// !etl::iu32string && !etl::u32string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
const etl::iu32string& to_string(const T value, etl::iu32string& str, const bool append = false)
|
||||
typename etl::enable_if<!etl::is_same<T, etl::iu32string>::value && !etl::is_same<T, etl::u32string_view>::value, const etl::iu32string&>::type
|
||||
to_string(const T value, etl::iu32string& str, bool append = false)
|
||||
{
|
||||
etl::u32format_spec format;
|
||||
|
||||
@ -54,12 +56,70 @@ namespace etl
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// !etl::iu32string && !etl::u32string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
const etl::iu32string& to_string(const T value, etl::iu32string& str, const etl::u32format_spec& format, const bool append = false)
|
||||
typename etl::enable_if<!etl::is_same<T, etl::iu32string>::value && !etl::is_same<T, etl::u32string_view>::value, const etl::iu32string&>::type
|
||||
to_string(const T value, etl::iu32string& str, const etl::u32format_spec& format, bool append = false)
|
||||
{
|
||||
return private_to_string::to_string(value, str, format, append);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// etl::iu32string
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::iu32string>::value, const etl::iu32string&>::type
|
||||
to_string(const T& value, etl::iu32string& str, bool append = false)
|
||||
{
|
||||
etl::u32format_spec format;
|
||||
|
||||
private_to_string::add_string(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// etl::iu32string
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::iu32string>::value, const etl::iu32string&>::type
|
||||
to_string(const etl::iu32string& value, T& str, const etl::u32format_spec& format, bool append = false)
|
||||
{
|
||||
private_to_string::add_string(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// etl::u32string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::u32string_view>::value, const etl::iu32string&>::type
|
||||
to_string(T value, etl::iu32string& str, bool append = false)
|
||||
{
|
||||
etl::u32format_spec format;
|
||||
|
||||
private_to_string::add_string_view(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// etl::u32string_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::u32string_view>::value, const etl::iu32string&>::type
|
||||
to_string(T value, etl::iu32string& str, const etl::u32format_spec& format, bool append = false)
|
||||
{
|
||||
private_to_string::add_string_view(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -43,9 +43,11 @@ namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// !etl::iwstring && !etl::wstring_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
const etl::iwstring& to_string(const T value, etl::iwstring& str, const bool append = false)
|
||||
typename etl::enable_if<!etl::is_same<T, etl::iwstring>::value && !etl::is_same<T, etl::wstring_view>::value, const etl::iwstring&>::type
|
||||
to_string(const T value, etl::iwstring& str, bool append = false)
|
||||
{
|
||||
etl::wformat_spec format;
|
||||
|
||||
@ -54,12 +56,70 @@ namespace etl
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// !etl::iwstring && !etl::wstring_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
const etl::iwstring& to_string(const T value, etl::iwstring& str, const etl::wformat_spec& format, const bool append = false)
|
||||
typename etl::enable_if<!etl::is_same<T, etl::iwstring>::value && !etl::is_same<T, etl::wstring_view>::value, const etl::iwstring&>::type
|
||||
to_string(const T value, etl::iwstring& str, const etl::wformat_spec& format, bool append = false)
|
||||
{
|
||||
return private_to_string::to_string(value, str, format, append);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// etl::iwstring
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::iwstring>::value, const etl::iwstring&>::type
|
||||
to_string(const T& value, etl::iwstring& str, bool append = false)
|
||||
{
|
||||
etl::wformat_spec format;
|
||||
|
||||
private_to_string::add_string(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// etl::iwstring
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::iwstring>::value, const etl::iwstring&>::type
|
||||
to_string(const etl::iwstring& value, T& str, const etl::wformat_spec& format, bool append = false)
|
||||
{
|
||||
private_to_string::add_string(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Default format spec.
|
||||
/// etl::wstring_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::wstring_view>::value, const etl::iwstring&>::type
|
||||
to_string(T value, etl::iwstring& str, bool append = false)
|
||||
{
|
||||
etl::wformat_spec format;
|
||||
|
||||
private_to_string::add_string_view(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Supplied format spec.
|
||||
/// etl::wstring_view
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_same<T, etl::wstring_view>::value, const etl::iwstring&>::type
|
||||
to_string(T value, etl::iwstring& str, const etl::wformat_spec& format, bool append = false)
|
||||
{
|
||||
private_to_string::add_string_view(value, str, format, append);
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
180
include/etl/u16string_stream.h
Normal file
180
include/etl/u16string_stream.h
Normal file
@ -0,0 +1,180 @@
|
||||
///\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_U16STRING_STREAM_INCLUDED
|
||||
#define ETL_U16STRING_STREAM_INCLUDED
|
||||
|
||||
///\ingroup string
|
||||
|
||||
#include "platform.h"
|
||||
#include "u16string.h"
|
||||
#include "u16format_spec.h"
|
||||
#include "to_u16string.h"
|
||||
#include "string_view.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_, etl::u16format_spec spec_)
|
||||
: text(text_)
|
||||
, spec(spec_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Set the format spec.
|
||||
//*************************************************************************
|
||||
void set_format(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, 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;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
180
include/etl/u32string_stream.h
Normal file
180
include/etl/u32string_stream.h
Normal file
@ -0,0 +1,180 @@
|
||||
///\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_U32STRING_STREAM_INCLUDED
|
||||
#define ETL_U32STRING_STREAM_INCLUDED
|
||||
|
||||
///\ingroup string
|
||||
|
||||
#include "platform.h"
|
||||
#include "u32string.h"
|
||||
#include "u32format_spec.h"
|
||||
#include "to_u32string.h"
|
||||
#include "string_view.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_, etl::u32format_spec spec_)
|
||||
: text(text_)
|
||||
, spec(spec_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Set the format spec.
|
||||
//*************************************************************************
|
||||
void set_format(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, 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;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -38,7 +38,7 @@ SOFTWARE.
|
||||
///\ingroup utilities
|
||||
|
||||
#define ETL_VERSION_MAJOR 18
|
||||
#define ETL_VERSION_MINOR 4
|
||||
#define ETL_VERSION_MINOR 5
|
||||
#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)
|
||||
|
||||
180
include/etl/wstring_stream.h
Normal file
180
include/etl/wstring_stream.h
Normal file
@ -0,0 +1,180 @@
|
||||
///\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_WSTRING_STREAM_INCLUDED
|
||||
#define ETL_WSTRING_STREAM_INCLUDED
|
||||
|
||||
///\ingroup string
|
||||
|
||||
#include "platform.h"
|
||||
#include "cstring.h"
|
||||
#include "wformat_spec.h"
|
||||
#include "to_wstring.h"
|
||||
#include "string_view.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_, etl::wformat_spec spec_)
|
||||
: text(text_)
|
||||
, spec(spec_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Set the format spec.
|
||||
//*************************************************************************
|
||||
void set_format(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, 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;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Embedded Template Library",
|
||||
"version": "18.4.0",
|
||||
"version": "18.5.0",
|
||||
"authors": {
|
||||
"name": "John Wellbelove",
|
||||
"email": "john.wellbelove@etlcpp.com"
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
name=Embedded Template Library
|
||||
version=18.4.0
|
||||
version=18.5.0
|
||||
author= John Wellbelove <john.wellbelove@etlcpp.com>
|
||||
maintainer=John Wellbelove <john.wellbelove@etlcpp.com>
|
||||
license=MIT
|
||||
|
||||
@ -1,3 +1,17 @@
|
||||
===============================================================================
|
||||
18.5.0
|
||||
Added string streams
|
||||
etl::string_stream
|
||||
etl::wstring_stream
|
||||
etl::u16string_stream
|
||||
etl::u32string_stream
|
||||
|
||||
Added string formatting to etl::to_string from
|
||||
etl::string etl::string_view
|
||||
etl::wstring etl::wstring_view
|
||||
etl::u16string etl::u16string_view
|
||||
etl::u32string etl::u32string_view
|
||||
|
||||
===============================================================================
|
||||
18.4.0
|
||||
Refactored etl::error_handler to use etl::delegate style implementation.
|
||||
|
||||
220
test/test_string_stream.cpp
Normal file
220
test/test_string_stream.cpp
Normal file
@ -0,0 +1,220 @@
|
||||
/******************************************************************************
|
||||
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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "UnitTest++/UnitTest++.h"
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#include "etl/string_stream.h"
|
||||
#include "etl/cstring.h"
|
||||
#include "etl/format_spec.h"
|
||||
|
||||
#undef STR
|
||||
#define STR(x) x
|
||||
|
||||
namespace
|
||||
{
|
||||
using String = etl::string<50>;
|
||||
using IString = etl::istring;
|
||||
using Stream = etl::string_stream;
|
||||
using Format = etl::format_spec;
|
||||
|
||||
//***********************************
|
||||
struct Custom
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
//***********************************
|
||||
Stream& operator <<(Stream& ss, const Custom& value)
|
||||
{
|
||||
ss << STR("X = ") << value.x << STR(" : Y = ") << value.y;
|
||||
return ss;
|
||||
}
|
||||
|
||||
//***********************************
|
||||
std::ostream& operator << (std::ostream& os, const IString& str)
|
||||
{
|
||||
for (auto c : str)
|
||||
{
|
||||
os << c;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
SUITE(test_string_stream)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_default_format)
|
||||
{
|
||||
String str;
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
int value = 123;
|
||||
String hello(STR("Hello"));
|
||||
ss << hello << STR(" World ") << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello World 123")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_format)
|
||||
{
|
||||
String str;
|
||||
Format format = Format().base(10).width(10).fill(STR('#'));
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
int value = 123;
|
||||
String hello(STR("Hello"));
|
||||
ss << format << hello << STR("World") << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("#####Hello#####World#######123")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_multi_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 << format1 << hello << format2 << STR("World") << format3 << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_class_default_format)
|
||||
{
|
||||
String str;
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
String value_str(STR("Value: "));
|
||||
IString& ivalue_str = value_str;
|
||||
Custom value = { 1, 2 };
|
||||
|
||||
ss << ivalue_str << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Value: X = 1 : Y = 2")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_format)
|
||||
{
|
||||
String str;
|
||||
Format format = Format().base(16).width(4).fill(STR(' ')).right();
|
||||
|
||||
Stream ss(str, format);
|
||||
|
||||
Format format2 = ss.get_format();
|
||||
|
||||
CHECK(format == format2);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_set_from_character_pointer)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(STR("Hello"));
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), ss.str());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_set_from_string)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), ss.str());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_istring)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
IString& istr = ss.str();
|
||||
istr.resize(istr.size() - 1);
|
||||
|
||||
CHECK_EQUAL(String(STR("Hell")), istr);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_const_istring)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
const IString& istr = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), istr);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_size)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
CHECK_EQUAL(str.size(), ss.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
220
test/test_u16string_stream.cpp
Normal file
220
test/test_u16string_stream.cpp
Normal file
@ -0,0 +1,220 @@
|
||||
/******************************************************************************
|
||||
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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "UnitTest++/UnitTest++.h"
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#include "etl/u16string_stream.h"
|
||||
#include "etl/u16string.h"
|
||||
#include "etl/format_spec.h"
|
||||
|
||||
#undef STR
|
||||
#define STR(x) u##x
|
||||
|
||||
namespace
|
||||
{
|
||||
using String = etl::u16string<50>;
|
||||
using IString = etl::iu16string;
|
||||
using Stream = etl::u16string_stream;
|
||||
using Format = etl::u16format_spec;
|
||||
|
||||
//***********************************
|
||||
struct Custom
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
//***********************************
|
||||
Stream& operator <<(Stream& ss, const Custom& value)
|
||||
{
|
||||
ss << STR("X = ") << value.x << STR(" : Y = ") << value.y;
|
||||
return ss;
|
||||
}
|
||||
|
||||
//***********************************
|
||||
std::ostream& operator << (std::ostream& os, const IString& str)
|
||||
{
|
||||
for (auto c : str)
|
||||
{
|
||||
os << c;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
SUITE(test_string_stream)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_default_format)
|
||||
{
|
||||
String str;
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
int value = 123;
|
||||
String hello(STR("Hello"));
|
||||
ss << hello << STR(" World ") << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello World 123")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_format)
|
||||
{
|
||||
String str;
|
||||
Format format = Format().base(10).width(10).fill(STR('#'));
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
int value = 123;
|
||||
String hello(STR("Hello"));
|
||||
ss << format << hello << STR("World") << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("#####Hello#####World#######123")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_multi_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 << format1 << hello << format2 << STR("World") << format3 << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_class_default_format)
|
||||
{
|
||||
String str;
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
String value_str(STR("Value: "));
|
||||
IString& ivalue_str = value_str;
|
||||
Custom value = { 1, 2 };
|
||||
|
||||
ss << ivalue_str << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Value: X = 1 : Y = 2")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_format)
|
||||
{
|
||||
String str;
|
||||
Format format = Format().base(16).width(4).fill(STR(' ')).right();
|
||||
|
||||
Stream ss(str, format);
|
||||
|
||||
Format format2 = ss.get_format();
|
||||
|
||||
CHECK(format == format2);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_set_from_character_pointer)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(STR("Hello"));
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), ss.str());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_set_from_string)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), ss.str());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_istring)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
IString& istr = ss.str();
|
||||
istr.resize(istr.size() - 1);
|
||||
|
||||
CHECK_EQUAL(String(STR("Hell")), istr);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_const_istring)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
const IString& istr = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), istr);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_size)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
CHECK_EQUAL(str.size(), ss.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
220
test/test_u32string_stream.cpp
Normal file
220
test/test_u32string_stream.cpp
Normal file
@ -0,0 +1,220 @@
|
||||
/******************************************************************************
|
||||
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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "UnitTest++/UnitTest++.h"
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#include "etl/u32string_stream.h"
|
||||
#include "etl/u32string.h"
|
||||
#include "etl/format_spec.h"
|
||||
|
||||
#undef STR
|
||||
#define STR(x) U##x
|
||||
|
||||
namespace
|
||||
{
|
||||
using String = etl::u32string<50>;
|
||||
using IString = etl::iu32string;
|
||||
using Stream = etl::u32string_stream;
|
||||
using Format = etl::u32format_spec;
|
||||
|
||||
//***********************************
|
||||
struct Custom
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
//***********************************
|
||||
Stream& operator <<(Stream& ss, const Custom& value)
|
||||
{
|
||||
ss << STR("X = ") << value.x << STR(" : Y = ") << value.y;
|
||||
return ss;
|
||||
}
|
||||
|
||||
//***********************************
|
||||
std::ostream& operator << (std::ostream& os, const IString& str)
|
||||
{
|
||||
for (auto c : str)
|
||||
{
|
||||
os << c;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
SUITE(test_string_stream)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_default_format)
|
||||
{
|
||||
String str;
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
int value = 123;
|
||||
String hello(STR("Hello"));
|
||||
ss << hello << STR(" World ") << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello World 123")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_format)
|
||||
{
|
||||
String str;
|
||||
Format format = Format().base(10).width(10).fill(STR('#'));
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
int value = 123;
|
||||
String hello(STR("Hello"));
|
||||
ss << format << hello << STR("World") << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("#####Hello#####World#######123")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_multi_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 << format1 << hello << format2 << STR("World") << format3 << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_class_default_format)
|
||||
{
|
||||
String str;
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
String value_str(STR("Value: "));
|
||||
IString& ivalue_str = value_str;
|
||||
Custom value = { 1, 2 };
|
||||
|
||||
ss << ivalue_str << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Value: X = 1 : Y = 2")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_format)
|
||||
{
|
||||
String str;
|
||||
Format format = Format().base(16).width(4).fill(STR(' ')).right();
|
||||
|
||||
Stream ss(str, format);
|
||||
|
||||
Format format2 = ss.get_format();
|
||||
|
||||
CHECK(format == format2);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_set_from_character_pointer)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(STR("Hello"));
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), ss.str());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_set_from_string)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), ss.str());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_istring)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
IString& istr = ss.str();
|
||||
istr.resize(istr.size() - 1);
|
||||
|
||||
CHECK_EQUAL(String(STR("Hell")), istr);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_const_istring)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
const IString& istr = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), istr);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_size)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
CHECK_EQUAL(str.size(), ss.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
220
test/test_wstring_stream.cpp
Normal file
220
test/test_wstring_stream.cpp
Normal file
@ -0,0 +1,220 @@
|
||||
/******************************************************************************
|
||||
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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "UnitTest++/UnitTest++.h"
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#include "etl/wstring_stream.h"
|
||||
#include "etl/wstring.h"
|
||||
#include "etl/format_spec.h"
|
||||
|
||||
#undef STR
|
||||
#define STR(x) L##x
|
||||
|
||||
namespace
|
||||
{
|
||||
using String = etl::wstring<50>;
|
||||
using IString = etl::iwstring;
|
||||
using Stream = etl::wstring_stream;
|
||||
using Format = etl::wformat_spec;
|
||||
|
||||
//***********************************
|
||||
struct Custom
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
//***********************************
|
||||
Stream& operator <<(Stream& ss, const Custom& value)
|
||||
{
|
||||
ss << STR("X = ") << value.x << STR(" : Y = ") << value.y;
|
||||
return ss;
|
||||
}
|
||||
|
||||
//***********************************
|
||||
std::ostream& operator << (std::ostream& os, const IString& str)
|
||||
{
|
||||
for (auto c : str)
|
||||
{
|
||||
os << c;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
SUITE(test_string_stream)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_default_format)
|
||||
{
|
||||
String str;
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
int value = 123;
|
||||
String hello(STR("Hello"));
|
||||
ss << hello << STR(" World ") << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello World 123")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_format)
|
||||
{
|
||||
String str;
|
||||
Format format = Format().base(10).width(10).fill(STR('#'));
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
int value = 123;
|
||||
String hello(STR("Hello"));
|
||||
ss << format << hello << STR("World") << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("#####Hello#####World#######123")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_multi_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 << format1 << hello << format2 << STR("World") << format3 << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("#####HelloWorld*** 7B")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_custom_class_default_format)
|
||||
{
|
||||
String str;
|
||||
|
||||
Stream ss(str);
|
||||
|
||||
String value_str(STR("Value: "));
|
||||
IString& ivalue_str = value_str;
|
||||
Custom value = { 1, 2 };
|
||||
|
||||
ss << ivalue_str << value;
|
||||
|
||||
String result = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Value: X = 1 : Y = 2")), result);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_format)
|
||||
{
|
||||
String str;
|
||||
Format format = Format().base(16).width(4).fill(STR(' ')).right();
|
||||
|
||||
Stream ss(str, format);
|
||||
|
||||
Format format2 = ss.get_format();
|
||||
|
||||
CHECK(format == format2);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_set_from_character_pointer)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(STR("Hello"));
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), ss.str());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_set_from_string)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), ss.str());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_istring)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
IString& istr = ss.str();
|
||||
istr.resize(istr.size() - 1);
|
||||
|
||||
CHECK_EQUAL(String(STR("Hell")), istr);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_const_istring)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
const IString& istr = ss.str();
|
||||
|
||||
CHECK_EQUAL(String(STR("Hello")), istr);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get_size)
|
||||
{
|
||||
String str;
|
||||
Stream ss(str);
|
||||
|
||||
ss.str(String(STR("Hello")));
|
||||
|
||||
CHECK_EQUAL(str.size(), ss.size());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -1121,6 +1121,7 @@
|
||||
<ClInclude Include="..\..\include\etl\queue_spsc_isr.h" />
|
||||
<ClInclude Include="..\..\include\etl\queue_mpmc_mutex.h" />
|
||||
<ClInclude Include="..\..\include\etl\sqrt.h" />
|
||||
<ClInclude Include="..\..\include\etl\string_stream.h" />
|
||||
<ClInclude Include="..\..\include\etl\string_view.h" />
|
||||
<ClInclude Include="..\..\include\etl\task.h" />
|
||||
<ClInclude Include="..\..\include\etl\timer.h" />
|
||||
@ -1131,7 +1132,9 @@
|
||||
<ClInclude Include="..\..\include\etl\type_lookup.h" />
|
||||
<ClInclude Include="..\..\include\etl\type_select.h" />
|
||||
<ClInclude Include="..\..\include\etl\u16format_spec.h" />
|
||||
<ClInclude Include="..\..\include\etl\u16string_stream.h" />
|
||||
<ClInclude Include="..\..\include\etl\u32format_spec.h" />
|
||||
<ClInclude Include="..\..\include\etl\u32string_stream.h" />
|
||||
<ClInclude Include="..\..\include\etl\variant_pool.h" />
|
||||
<ClInclude Include="..\..\include\etl\version.h" />
|
||||
<ClInclude Include="..\..\include\etl\algorithm.h" />
|
||||
@ -1228,6 +1231,7 @@
|
||||
<ClInclude Include="..\..\include\etl\visitor.h" />
|
||||
<ClInclude Include="..\..\include\etl\wformat_spec.h" />
|
||||
<ClInclude Include="..\..\include\etl\wstring.h" />
|
||||
<ClInclude Include="..\..\include\etl\wstring_stream.h" />
|
||||
<ClInclude Include="..\data.h" />
|
||||
<ClInclude Include="..\etl_profile.h" />
|
||||
<ClInclude Include="..\murmurhash3.h" />
|
||||
@ -1618,6 +1622,7 @@
|
||||
<ClCompile Include="..\test_stack.cpp" />
|
||||
<ClCompile Include="..\test_string_char.cpp" />
|
||||
<ClCompile Include="..\test_string_char_external_buffer.cpp" />
|
||||
<ClCompile Include="..\test_string_stream.cpp" />
|
||||
<ClCompile Include="..\test_string_u16.cpp" />
|
||||
<ClCompile Include="..\test_string_u16_external_buffer.cpp" />
|
||||
<ClCompile Include="..\test_string_u32.cpp" />
|
||||
@ -1634,6 +1639,8 @@
|
||||
<ClCompile Include="..\test_type_lookup.cpp" />
|
||||
<ClCompile Include="..\test_type_select.cpp" />
|
||||
<ClCompile Include="..\test_type_traits.cpp" />
|
||||
<ClCompile Include="..\test_u16string_stream.cpp" />
|
||||
<ClCompile Include="..\test_u32string_stream.cpp" />
|
||||
<ClCompile Include="..\test_unordered_map.cpp" />
|
||||
<ClCompile Include="..\test_unordered_multimap.cpp" />
|
||||
<ClCompile Include="..\test_unordered_multiset.cpp" />
|
||||
@ -1648,6 +1655,7 @@
|
||||
<ClCompile Include="..\test_vector_pointer.cpp" />
|
||||
<ClCompile Include="..\test_vector_pointer_external_buffer.cpp" />
|
||||
<ClCompile Include="..\test_visitor.cpp" />
|
||||
<ClCompile Include="..\test_wstring_stream.cpp" />
|
||||
<ClCompile Include="..\test_xor_checksum.cpp" />
|
||||
<ClCompile Include="..\test_xor_rotate_checksum.cpp" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -855,6 +855,18 @@
|
||||
<ClInclude Include="..\..\include\etl\parameter_pack.h">
|
||||
<Filter>ETL\Utilities</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\string_stream.h">
|
||||
<Filter>ETL\Strings</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\wstring_stream.h">
|
||||
<Filter>ETL\Strings</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\u16string_stream.h">
|
||||
<Filter>ETL\Strings</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\u32string_stream.h">
|
||||
<Filter>ETL\Strings</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\main.cpp">
|
||||
@ -1331,6 +1343,18 @@
|
||||
<ClCompile Include="..\test_parameter_pack.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_string_stream.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_wstring_stream.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_u16string_stream.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_u32string_stream.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\library.properties">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user