reorganization in utf8
- utf8 auxiliary functions moved to utf8_private.h file - in utf8.h are shown only functions available for consumers - template functions has been moved to utf8_template.h (in utf8.h are only declarations) utf8_template.h is included at the end of utf8.h - functions which take std::ostream changed to template (the stream is a template argument now)
This commit is contained in:
271
utf8/utf8_templates.h
Normal file
271
utf8/utf8_templates.h
Normal file
@@ -0,0 +1,271 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2021, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||
* project may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef headerfile_picotools_utf8_utf8_templates
|
||||
#define headerfile_picotools_utf8_utf8_templates
|
||||
|
||||
// this file is included at the end of utf8.h
|
||||
|
||||
#include "utf8_private.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
/*!
|
||||
converting UTF-8 string to a TextStreamBase<wchar_t,...> stream
|
||||
(need to be tested)
|
||||
*/
|
||||
// need to be tested
|
||||
template<size_t stack_size, size_t heap_block_size>
|
||||
bool UTF8ToWide(const char * utf8, size_t utf8_len, TextStreamBase<wchar_t, stack_size, heap_block_size> & res, bool clear, int mode)
|
||||
{
|
||||
if( clear )
|
||||
res.clear();
|
||||
|
||||
bool status = private_namespace::UTF8ToWideGeneric(utf8, utf8_len, mode, [&res](int c) {
|
||||
private_namespace::IntToWide(c, res);
|
||||
});
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<size_t stack_size, size_t heap_block_size>
|
||||
bool UTF8ToWide(const char * utf8, TextStreamBase<wchar_t, stack_size, heap_block_size> & res, bool clear, int mode)
|
||||
{
|
||||
size_t utf8_len = 0;
|
||||
|
||||
while( utf8[utf8_len] != 0 )
|
||||
utf8_len += 1;
|
||||
|
||||
return UTF8ToWide(utf8, utf8_len, res, clear, mode);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<size_t stack_size, size_t heap_block_size>
|
||||
bool UTF8ToWide(const std::string & utf8, TextStreamBase<wchar_t, stack_size, heap_block_size> & res, bool clear, int mode)
|
||||
{
|
||||
return UTF8ToWide(utf8.c_str(), utf8.size(), res, clear, mode);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// need to be tested
|
||||
template<size_t stack_size, size_t heap_block_size>
|
||||
bool UTF8ToWide(std::istream & utf8, TextStreamBase<wchar_t, stack_size, heap_block_size> & res, bool clear, int mode)
|
||||
{
|
||||
int z;
|
||||
bool correct, was_error = false;
|
||||
|
||||
if( clear )
|
||||
res.clear();
|
||||
|
||||
while( UTF8ToInt(utf8, z, correct) > 0 )
|
||||
{
|
||||
if( !correct )
|
||||
{
|
||||
if( mode == 1 )
|
||||
res << 0xFFFD; // U+FFFD "replacement character"
|
||||
|
||||
was_error = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
private_namespace::IntToWide(z, res);
|
||||
}
|
||||
}
|
||||
|
||||
return !was_error;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this function converts one wide character into UTF-8 stream
|
||||
|
||||
input:
|
||||
z - wide character
|
||||
|
||||
output:
|
||||
utf8 - a UTF-8 stream for the output sequence
|
||||
|
||||
the function returns how many characters have been written to the utf8 stream,
|
||||
zero means that 'z' is an incorrect unicode character
|
||||
*/
|
||||
template<typename StreamType>
|
||||
size_t IntToUTF8(int z, StreamType & utf8)
|
||||
{
|
||||
char buf[10];
|
||||
|
||||
size_t len = IntToUTF8(z, buf, sizeof(buf)/sizeof(char));
|
||||
|
||||
if( len > 0 )
|
||||
utf8.write(buf, len);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this function converts a wide string into UTF-8 stream
|
||||
|
||||
input:
|
||||
wide_string - a wide string for converting
|
||||
string_len - size of the string
|
||||
mode - what to do with errors when converting
|
||||
0: skip an invalid character
|
||||
1: put U+FFFD "replacement character" istead of the invalid character (default)
|
||||
|
||||
output:
|
||||
utf8 - a UTF-8 stream for the output sequence
|
||||
|
||||
this function returns false if there were some errors when converting
|
||||
*/
|
||||
template<typename StreamType>
|
||||
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, StreamType & utf8, int mode)
|
||||
{
|
||||
bool was_error = false;
|
||||
size_t chars;
|
||||
|
||||
while( string_len > 0 )
|
||||
{
|
||||
chars = private_namespace::WideOneToUTF8(wide_string, string_len, utf8, was_error, mode);
|
||||
wide_string += chars;
|
||||
string_len -= chars;
|
||||
}
|
||||
|
||||
return !was_error;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this function converts a wide string into UTF-8 stream
|
||||
|
||||
input:
|
||||
wide_string - a null terminated wide string for converting
|
||||
mode - what to do with errors when converting
|
||||
0: skip an invalid character
|
||||
1: put U+FFFD "replacement character" istead of the invalid character (default)
|
||||
|
||||
output:
|
||||
utf8 - a UTF-8 stream for the output sequence
|
||||
|
||||
this function returns false if there were some errors when converting
|
||||
*/
|
||||
template<typename StreamType>
|
||||
bool WideToUTF8(const wchar_t * wide_string, StreamType & utf8, int mode)
|
||||
{
|
||||
bool was_error = false;
|
||||
|
||||
while( *wide_string )
|
||||
wide_string += private_namespace::WideOneToUTF8(wide_string, utf8, was_error, mode);
|
||||
|
||||
return !was_error;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this function converts a wide string (std::wstring) into UTF-8 stream
|
||||
|
||||
input:
|
||||
wide_string - a wide string for converting
|
||||
mode - what to do with errors when converting
|
||||
0: skip an invalid character
|
||||
1: put U+FFFD "replacement character" istead of the invalid character (default)
|
||||
|
||||
output:
|
||||
utf8 - a UTF-8 stream for the output sequence
|
||||
|
||||
this function returns false if there were some errors when converting
|
||||
*/
|
||||
template<typename StreamType>
|
||||
bool WideToUTF8(const std::wstring & wide_string, StreamType & utf8, int mode)
|
||||
{
|
||||
return WideToUTF8(wide_string.c_str(), wide_string.size(), utf8, mode);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<size_t stack_size, size_t heap_block_size>
|
||||
void WideToUTF8(TextStreamBase<wchar_t, stack_size, heap_block_size> & buffer, std::string & utf8, bool clear, int mode)
|
||||
{
|
||||
if( clear )
|
||||
utf8.clear();
|
||||
|
||||
private_namespace::WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
|
||||
utf8.append(utf8_buffer, buffer_len);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// not tested
|
||||
template<size_t stack_size, size_t heap_block_size>
|
||||
void WideToUTF8(TextStreamBase<wchar_t, stack_size, heap_block_size> & buffer, std::ostream & utf8, int mode)
|
||||
{
|
||||
private_namespace::WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
|
||||
utf8.write(utf8_buffer, buffer_len);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace PT
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user