added class Stream (textstream/stream.h) which acts as a base class for TextStream

TextStream is making conversions wide/utf8 now
This commit is contained in:
2021-06-20 14:13:23 +02:00
parent 865837d911
commit 819c49e638
3 changed files with 212 additions and 42 deletions

View File

@@ -39,11 +39,13 @@
#define headerfile_picotools_textstream_textstream
#include <string>
#include "stream.h"
#include "space/space.h"
#include "date/date.h"
#include "convert/inttostr.h"
#include "membuffer/membuffer.h"
#include "types.h"
#include "utf8/utf8.h"
// for snprintf
#include <cstdio>
@@ -58,10 +60,9 @@ namespace pt
similar to std::ostringstream
StringType can be either std::string or std::wstring
this class doesn't use UTF-8 in any kind
*/
template<typename CharT, size_t stack_size, size_t heap_block_size>
class TextStreamBase
class TextStreamBase : public Stream
{
public:
@@ -128,13 +129,13 @@ public:
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
TextStreamBase & operator<<(const TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg);
template<typename in_buffer_type>
TextStreamBase & write(const in_buffer_type * buf, size_t len);
TextStreamBase & write(const char * buf, size_t len);
TextStreamBase & write(const wchar_t * buf, size_t len);
// write double value in a specified format
// format is the same as in the snprintf function, e.g. write("%f", 10.0)
TextStreamBase & write(const char * format, double val);
TextStreamBase & write(const wchar_t * format, double val);
// TextStreamBase & write(const char * format, double val);
// TextStreamBase & write(const wchar_t * format, double val);
TextStreamBase & fill_up_if_needed(wchar_t fill_up_char, size_t existing_length);
@@ -231,10 +232,19 @@ void TextStreamBase<char_type, stack_size, heap_block_size>::to_string(std::stri
if( str.capacity() < str.size() + size() )
str.reserve(str.size() + size());
const_iterator i = begin();
for( ; i != end() ; ++i)
str += static_cast<char>(*i);
if constexpr (sizeof(char_type) == sizeof(char) )
{
const_iterator i = begin();
for( ; i != end() ; ++i)
str += *i;
}
else
{
// test me
wide_stream_to_utf8(*this, str);
}
}
@@ -248,10 +258,20 @@ void TextStreamBase<char_type, stack_size, heap_block_size>::to_string(std::wstr
if( str.capacity() < str.size() + size() )
str.reserve(str.size() + size());
const_iterator i = begin();
if constexpr (sizeof(char_type) == sizeof(wchar_t) )
{
const_iterator i = begin();
for( ; i != end() ; ++i)
str += static_cast<wchar_t>(*i);
for( ; i != end() ; ++i)
str += *i;
}
else
{
// IMPROVE ME don't use a temporary object
std::string utf8;
to_string(utf8);
utf8_to_wide(utf8, str, false);
}
}
@@ -275,8 +295,15 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(const char * str)
{
for( ; *str ; ++str)
buffer.append(static_cast<char_type>(*str));
if constexpr ( sizeof(char_type) == sizeof(char) )
{
for( ; *str ; ++str)
buffer.append(*str);
}
else
{
utf8_to_wide(str, *this, false);
}
return *this;
}
@@ -286,10 +313,7 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(const std::string & str)
{
if( sizeof(char_type) == sizeof(char) )
buffer.append(str.c_str(), str.size());
else
operator<<(str.c_str());
operator<<(str.c_str());
return *this;
}
@@ -300,8 +324,15 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(const wchar_t * str)
{
for( ; *str ; ++str)
buffer.append(static_cast<char_type>(*str));
if constexpr (sizeof(char_type) == sizeof(wchar_t) )
{
for( ; *str ; ++str)
buffer.append(*str);
}
else
{
wide_to_utf8(str, *this);
}
return *this;
}
@@ -312,10 +343,7 @@ template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::operator<<(const std::wstring & str)
{
if( sizeof(char_type) == sizeof(wchar_t) )
buffer.append(str.c_str(), str.size());
else
operator<<(str.c_str());
operator<<(str.c_str());
return *this;
}
@@ -496,12 +524,20 @@ return *this;
template<typename char_type, size_t stack_size, size_t heap_block_size>
template<typename in_buffer_type>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::write(const in_buffer_type * buf, size_t len)
TextStreamBase<char_type, stack_size, heap_block_size>::write(const char * str, size_t len)
{
buffer.append(buf, len);
if constexpr ( sizeof(char_type) == sizeof(char) )
{
for(size_t i=0 ; i < len ; ++i)
buffer.append(str[i]);
}
else
{
utf8_to_wide(str, *this, false);
}
return *this;
}
@@ -509,24 +545,42 @@ return *this;
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::write(const char * format, double val)
TextStreamBase<char_type, stack_size, heap_block_size>::write(const wchar_t * str, size_t len)
{
char buf[100];
if constexpr (sizeof(char_type) == sizeof(wchar_t) )
{
for(size_t i=0 ; i < len ; ++i)
buffer.append(str[i]);
}
else
{
wide_to_utf8(str, *this);
}
snprintf(buf, sizeof(buf)/sizeof(char), format, val);
return operator<<(buf);
return *this;
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
TextStreamBase<char_type, stack_size, heap_block_size> &
TextStreamBase<char_type, stack_size, heap_block_size>::write(const wchar_t * format, double val)
{
wchar_t buf[100];
swprintf(buf, sizeof(buf)/sizeof(wchar_t), format, val);
return operator<<(buf);
}
//template<typename char_type, size_t stack_size, size_t heap_block_size>
//TextStreamBase<char_type, stack_size, heap_block_size> &
//TextStreamBase<char_type, stack_size, heap_block_size>::write(const char * format, double val)
//{
//char buf[100];
//
// snprintf(buf, sizeof(buf)/sizeof(char), format, val);
// return operator<<(buf);
//}
//
//
//template<typename char_type, size_t stack_size, size_t heap_block_size>
//TextStreamBase<char_type, stack_size, heap_block_size> &
//TextStreamBase<char_type, stack_size, heap_block_size>::write(const wchar_t * format, double val)
//{
//wchar_t buf[100];
//
// swprintf(buf, sizeof(buf)/sizeof(wchar_t), format, val);
// return operator<<(buf);
//}