(update to the new pikotools api): use a char32_t character as a main character when converting strings

Use a char32_t instead of a wchar_t type. This is needed on systems
where sizeof(wchar_t) is equal to 2.

This affects classes based on the pt::Stream such as Log and HtmlTextStream.
This commit is contained in:
Tomasz Sowa 2024-06-01 00:33:15 +02:00
parent af0d34beb9
commit 5d457f3d4b
Signed by: tomasz.sowa
GPG Key ID: 662CC1438638588B
4 changed files with 56 additions and 30 deletions

View File

@ -197,6 +197,13 @@ Log & Log::operator<<(wchar_t s)
} }
Log & Log::operator<<(char32_t s)
{
pt::Log::operator<<(s);
return *this;
}
Log & Log::operator<<(unsigned short s) Log & Log::operator<<(unsigned short s)
{ {
pt::Log::operator<<(s); pt::Log::operator<<(s);

View File

@ -74,6 +74,7 @@ public:
virtual Log & operator<<(char s); virtual Log & operator<<(char s);
virtual Log & operator<<(unsigned char); virtual Log & operator<<(unsigned char);
virtual Log & operator<<(wchar_t s); virtual Log & operator<<(wchar_t s);
virtual Log & operator<<(char32_t s);
virtual Log & operator<<(bool); virtual Log & operator<<(bool);
virtual Log & operator<<(short); virtual Log & operator<<(short);
virtual Log & operator<<(int s); virtual Log & operator<<(int s);

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2010-2021, Tomasz Sowa * Copyright (c) 2010-2024, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -33,6 +33,7 @@
*/ */
#include "htmltextstream.h" #include "htmltextstream.h"
#include "convert/misc.h"
namespace Winix namespace Winix
{ {
@ -245,6 +246,14 @@ return *this;
} }
HtmlTextStream & HtmlTextStream::PutChar(char32_t c)
{
buffer.operator<<(c);
return *this;
}
HtmlTextStream & HtmlTextStream::PutChar(bool val) HtmlTextStream & HtmlTextStream::PutChar(bool val)
{ {
buffer.operator<<(val); buffer.operator<<(val);
@ -306,6 +315,13 @@ HtmlTextStream & HtmlTextStream::operator<<(RawText<wchar_t> raw)
} }
HtmlTextStream & HtmlTextStream::operator<<(RawText<char32_t> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<bool> raw) HtmlTextStream & HtmlTextStream::operator<<(RawText<bool> raw)
{ {
buffer.operator<<(raw.par); buffer.operator<<(raw.par);
@ -453,13 +469,12 @@ HtmlTextStream & HtmlTextStream::EPutText(const char * str)
bool correct; bool correct;
size_t utf8_len; size_t utf8_len;
// CHECKME
while( (utf8_len = pt::utf8_to_int(str, res, correct)) > 0 ) while( (utf8_len = pt::utf8_to_int(str, res, correct)) > 0 )
{ {
if( !correct ) if( !correct )
res = 0xFFFD; // U+FFFD "replacement character" res = 0xFFFD; // U+FFFD "replacement character"
ETextPutChar(static_cast<wchar_t>(res)); ETextPutChar(static_cast<char32_t>(res));
str += utf8_len; str += utf8_len;
} }
@ -475,6 +490,11 @@ HtmlTextStream & HtmlTextStream::EPutText(const std::string & str)
HtmlTextStream & HtmlTextStream::EPutText(const wchar_t * str) HtmlTextStream & HtmlTextStream::EPutText(const wchar_t * str)
{ {
/*
* we assume the sizeof(wchar) is 4 here
* as at the moment winix works only on unixes
* so just copy directly those characters
*/
for( ; *str ; ++str ) for( ; *str ; ++str )
ETextPutChar(*str); ETextPutChar(*str);
@ -528,39 +548,22 @@ HtmlTextStream & HtmlTextStream::ETextPutChar(char c)
HtmlTextStream & HtmlTextStream::ETextPutChar(unsigned char c) HtmlTextStream & HtmlTextStream::ETextPutChar(unsigned char c)
{ {
return ETextPutChar(static_cast<wchar_t>(c)); return ETextPutChar(static_cast<char32_t>(c));
} }
HtmlTextStream & HtmlTextStream::ETextPutChar(wchar_t c) HtmlTextStream & HtmlTextStream::ETextPutChar(wchar_t c)
{ {
if( c == '<' ) return ETextPutChar(static_cast<char32_t>(c));
buffer << L"&lt;"; }
else
if( c == '>' )
buffer << L"&gt;"; HtmlTextStream & HtmlTextStream::ETextPutChar(char32_t c)
else {
if( c == '&' ) if( !pt::try_esc_to_html(c, buffer) )
buffer << L"&amp;";
else
if( c == '\"' )
buffer << L"&quot;";
else
if( c == '\'' )
buffer << L"&#39;"; // (it is "&apos;" but IE8 has a problem with &apos;) (&apos; is valid in HTML5, but not HTML4)
else
if( c == 10 )
buffer << L"&#10;";
else
if( c == 13 )
buffer << L"&#13;";
else
if( c == 0 )
buffer << L"&#0;";
else
buffer << c; buffer << c;
return *this; return *this;
} }
@ -650,6 +653,17 @@ return *this;
} }
HtmlTextStream & HtmlTextStream::operator<<(char32_t v)
{
if( escape )
ETextPutChar(v);
else
PutChar(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(bool v) HtmlTextStream & HtmlTextStream::operator<<(bool v)
{ {
/* /*

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2010-2022, Tomasz Sowa * Copyright (c) 2010-2024, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -148,6 +148,7 @@ public:
HtmlTextStream & PutChar(char c); HtmlTextStream & PutChar(char c);
HtmlTextStream & PutChar(unsigned char c); HtmlTextStream & PutChar(unsigned char c);
HtmlTextStream & PutChar(wchar_t c); HtmlTextStream & PutChar(wchar_t c);
HtmlTextStream & PutChar(char32_t c);
HtmlTextStream & PutChar(bool val); HtmlTextStream & PutChar(bool val);
@ -171,6 +172,7 @@ public:
HtmlTextStream & operator<<(RawText<char> raw); HtmlTextStream & operator<<(RawText<char> raw);
HtmlTextStream & operator<<(RawText<unsigned char> raw); HtmlTextStream & operator<<(RawText<unsigned char> raw);
HtmlTextStream & operator<<(RawText<wchar_t> raw); HtmlTextStream & operator<<(RawText<wchar_t> raw);
HtmlTextStream & operator<<(RawText<char32_t> raw);
HtmlTextStream & operator<<(RawText<bool> raw); HtmlTextStream & operator<<(RawText<bool> raw);
HtmlTextStream & operator<<(RawText<short> raw); HtmlTextStream & operator<<(RawText<short> raw);
@ -219,6 +221,7 @@ public:
HtmlTextStream & ETextPutChar(char c); HtmlTextStream & ETextPutChar(char c);
HtmlTextStream & ETextPutChar(unsigned char c); HtmlTextStream & ETextPutChar(unsigned char c);
HtmlTextStream & ETextPutChar(wchar_t c); HtmlTextStream & ETextPutChar(wchar_t c);
HtmlTextStream & ETextPutChar(char32_t c);
/* /*
@ -234,6 +237,7 @@ public:
HtmlTextStream & operator<<(char); HtmlTextStream & operator<<(char);
HtmlTextStream & operator<<(unsigned char); HtmlTextStream & operator<<(unsigned char);
HtmlTextStream & operator<<(wchar_t); HtmlTextStream & operator<<(wchar_t);
HtmlTextStream & operator<<(char32_t);
HtmlTextStream & operator<<(bool); HtmlTextStream & operator<<(bool);
HtmlTextStream & operator<<(short); HtmlTextStream & operator<<(short);
HtmlTextStream & operator<<(int); HtmlTextStream & operator<<(int);