2012-01-11 12:15:36 +01:00
|
|
|
/*
|
2012-01-12 03:46:16 +01:00
|
|
|
* This file is a part of PikoTools
|
2012-01-11 12:15:36 +01:00
|
|
|
* and is distributed under the (new) BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-03-15 19:34:51 +01:00
|
|
|
* Copyright (c) 2010-2021, Tomasz Sowa
|
2012-01-11 12:15:36 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-06-15 19:54:50 +02:00
|
|
|
#include <fstream>
|
2012-01-11 12:15:36 +01:00
|
|
|
#include "utf8.h"
|
2021-03-15 19:34:51 +01:00
|
|
|
#include "utf8_private.h"
|
2012-01-11 12:15:36 +01:00
|
|
|
|
|
|
|
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
|
2021-05-20 16:11:12 +02:00
|
|
|
namespace pt
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
returns true if 'c' is a correct unicode character
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool utf8_check_range(int c)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
return c>=0 && c<=0x10FFFF && !(c>=0xD800 && c<=0xDFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-07 15:53:19 +02:00
|
|
|
/*!
|
|
|
|
returns true if 'c' is a correct unicode character
|
|
|
|
|
|
|
|
this method is used when reading from an utf8 string
|
|
|
|
how_many_bytes - means how many bytes from the utf8 string were read
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool utf8_check_range(int c, int how_many_bytes)
|
2021-05-07 15:53:19 +02:00
|
|
|
{
|
|
|
|
if( c >= 0x0000 && c <= 0x007f && how_many_bytes == 1 )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c >= 0x0080 && c <= 0x07ff && how_many_bytes == 2 )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c >= 0x0800 && c < 0xD800 && how_many_bytes == 3)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c > 0xDFFF && c <= 0xffff && how_many_bytes == 3)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c >= 0x10000 && c <= 0x10FFFF && how_many_bytes == 4 )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-11 12:15:36 +01:00
|
|
|
|
|
|
|
|
2021-06-14 13:48:32 +02:00
|
|
|
bool is_surrogate_char(int c)
|
|
|
|
{
|
|
|
|
return (c>=0xD800 && c<=0xDFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool is_first_surrogate_char(int c)
|
|
|
|
{
|
|
|
|
return (c>=0xD800 && c<=0xDBFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool is_second_surrogate_char(int c)
|
|
|
|
{
|
|
|
|
return (c>=0xDC00 && c<=0xDFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool surrogate_pair_to_int(int c1, int c2, int & z)
|
|
|
|
{
|
|
|
|
z = 0xFFFD; // U+FFFD "replacement character";
|
|
|
|
|
|
|
|
if( is_first_surrogate_char(c1) )
|
|
|
|
{
|
|
|
|
if( is_second_surrogate_char(c2) )
|
|
|
|
{
|
|
|
|
z = 0x10000 + (((c1 & 0x3FF) << 10) | (c2 & 0x3FF));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-11 12:15:36 +01:00
|
|
|
/*!
|
|
|
|
this function converts one UTF-8 character into one wide-character
|
|
|
|
|
|
|
|
input:
|
|
|
|
utf8 - an input UTF-8 string
|
|
|
|
utf8_len - size of the input string,
|
|
|
|
the string should be at least 4 bytes length for correctly
|
|
|
|
recognized the utf-8 sequence
|
|
|
|
|
|
|
|
output:
|
|
|
|
res - an output character
|
|
|
|
correct - true if it is a correct character
|
|
|
|
|
|
|
|
the function returns how many characters have been used from the input string
|
|
|
|
(returns zero only if utf8_len is zero)
|
|
|
|
even if there are errors the functions returns a different from zero value
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
size_t utf8_to_int(const char * utf8, size_t utf8_len, int & res, bool & correct)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
size_t i, len;
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
correct = false;
|
|
|
|
|
|
|
|
if( utf8_len == 0 )
|
|
|
|
return 0;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
if( !private_namespace::utf8_to_int_first_octet(utf8[0], len, res) )
|
2012-01-11 12:15:36 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if( utf8_len < len )
|
|
|
|
return utf8_len;
|
|
|
|
|
|
|
|
for(i=1 ; i<len ; ++i)
|
2021-03-15 19:34:51 +01:00
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
if( !private_namespace::utf8_to_int_add_next_octet(utf8[i], res) )
|
2012-01-11 12:15:36 +01:00
|
|
|
return i;
|
2021-03-15 19:34:51 +01:00
|
|
|
}
|
2012-01-11 12:15:36 +01:00
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
if( utf8_check_range(res, len) )
|
2012-01-11 12:15:36 +01:00
|
|
|
correct = true;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts one UTF-8 character into one wide-character
|
|
|
|
|
|
|
|
input:
|
|
|
|
utf8 - an input UTF-8 string (null terminated)
|
|
|
|
|
|
|
|
output:
|
|
|
|
res - an output character
|
|
|
|
correct - true if it is a correct character
|
|
|
|
|
|
|
|
the function returns how many characters have been used from the input string
|
|
|
|
(returns zero only if the string has '\0' at the first character)
|
|
|
|
even if there are errors the functions returns a different from zero value
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
size_t utf8_to_int(const char * utf8, int & res, bool & correct)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
size_t i, len;
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
correct = false;
|
|
|
|
|
|
|
|
if( *utf8 == 0 )
|
|
|
|
return 0;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
if( !private_namespace::utf8_to_int_first_octet(utf8[0], len, res) )
|
2012-01-11 12:15:36 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
for(i=1 ; i<len ; ++i)
|
|
|
|
{
|
|
|
|
if( utf8[i] == 0 )
|
|
|
|
return i;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
if( !private_namespace::utf8_to_int_add_next_octet(utf8[i], res) )
|
2012-01-11 12:15:36 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
if( utf8_check_range(res, len) )
|
2012-01-11 12:15:36 +01:00
|
|
|
correct = true;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts one UTF-8 character into one wide-character
|
|
|
|
|
|
|
|
input:
|
|
|
|
utf8 - an input UTF-8 string
|
|
|
|
|
|
|
|
output:
|
|
|
|
res - an output character
|
|
|
|
correct - true if it is a correct character
|
|
|
|
|
|
|
|
the function returns how many characters have been used from the input string
|
|
|
|
(returns zero only if utf8 is empty)
|
|
|
|
even if there are errors the functions returns a different from zero value
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
size_t utf8_to_int(const std::string & utf8, int & res, bool & correct)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
return utf8_to_int(utf8.c_str(), utf8.size(), res, correct);
|
2012-01-11 12:15:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts one UTF-8 character into one wide-character
|
|
|
|
|
|
|
|
input:
|
|
|
|
utf8 - an input UTF-8 stream
|
|
|
|
|
|
|
|
output:
|
|
|
|
res - an output character
|
|
|
|
correct - true if it is a correct character
|
|
|
|
|
|
|
|
the function returns how many characters have been used from the input stream
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
size_t utf8_to_int(std::istream & utf8, int & res, bool & correct)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
size_t i, len;
|
|
|
|
unsigned char uz;
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
correct = false;
|
|
|
|
|
|
|
|
uz = utf8.get();
|
|
|
|
|
|
|
|
if( !utf8 )
|
|
|
|
return 0;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
if( !private_namespace::utf8_to_int_first_octet(uz, len, res) )
|
2012-01-11 12:15:36 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
for(i=1 ; i<len ; ++i)
|
|
|
|
{
|
|
|
|
uz = utf8.get();
|
|
|
|
|
|
|
|
if( !utf8 )
|
|
|
|
return i;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
if( !private_namespace::utf8_to_int_add_next_octet(uz, res) )
|
2012-01-11 12:15:36 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
if( utf8_check_range(res, len) )
|
2012-01-11 12:15:36 +01:00
|
|
|
correct = true;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-24 20:52:48 +02:00
|
|
|
// new function, need to be tested a little more especially when sizeof(wchar_t) is 2
|
|
|
|
size_t utf8_to_int(const Stream & utf8, size_t stream_index, int & res, bool & correct)
|
|
|
|
{
|
|
|
|
size_t i, len;
|
|
|
|
unsigned char uz;
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
correct = false;
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
if( stream_index < utf8.size() )
|
|
|
|
{
|
|
|
|
uz = utf8.get_char(stream_index);
|
|
|
|
|
|
|
|
if( !private_namespace::utf8_to_int_first_octet(uz, len, res) )
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if( stream_index + len < utf8.size() + 1 )
|
|
|
|
{
|
|
|
|
for(i=1 ; i<len ; ++i)
|
|
|
|
{
|
|
|
|
uz = utf8.get_char(stream_index + i);
|
|
|
|
|
|
|
|
if( !private_namespace::utf8_to_int_add_next_octet(uz, res) )
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( utf8_check_range(res, len) )
|
|
|
|
correct = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = utf8.size() - stream_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-11 12:15:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
static void int_to_wide(int c, std::wstring & res)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
if( sizeof(wchar_t)==2 && c>0xffff )
|
|
|
|
{
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
// UTF16 surrogate pairs
|
2012-01-11 12:15:36 +01:00
|
|
|
c -= 0x10000;
|
|
|
|
res += static_cast<wchar_t>(((c >> 10) & 0x3FF) + 0xD800);
|
|
|
|
res += static_cast<wchar_t>((c & 0x3FF) + 0xDC00);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res += static_cast<wchar_t>(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-14 13:48:32 +02:00
|
|
|
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
/*!
|
|
|
|
this function converts an utf8 string into wide string (std::wstring)
|
|
|
|
|
|
|
|
input:
|
|
|
|
utf8 - an input utf8 string
|
|
|
|
utf8_len - size of the input 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:
|
|
|
|
res - an output wide string
|
|
|
|
|
|
|
|
the function returns false if there were some errors when converting
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool utf8_to_wide(const char * utf8, size_t utf8_len, std::wstring & res, bool clear, int mode)
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
{
|
|
|
|
if( clear )
|
|
|
|
res.clear();
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
bool status = private_namespace::utf8_to_wide_generic(utf8, utf8_len, mode, [&res](int c) {
|
|
|
|
int_to_wide(c, res);
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-11 12:15:36 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts an utf8 string into wide string (std::wstring)
|
|
|
|
|
|
|
|
input:
|
|
|
|
utf8 - an input utf8 null terminated 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:
|
|
|
|
res - an output wide string
|
|
|
|
|
|
|
|
the function returns false if there were some errors when converting
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool utf8_to_wide(const char * utf8, std::wstring & res, bool clear, int mode)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
size_t utf8_len = 0;
|
|
|
|
|
|
|
|
while( utf8[utf8_len] != 0 )
|
|
|
|
utf8_len += 1;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
return utf8_to_wide(utf8, utf8_len, res, clear, mode);
|
2012-01-11 12:15:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts an utf8 string into wide string (std::wstring)
|
|
|
|
|
|
|
|
input:
|
|
|
|
utf8 - an input utf8 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:
|
|
|
|
res - an output wide string
|
|
|
|
|
|
|
|
the function returns false if there were some errors when converting
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool utf8_to_wide(const std::string & utf8, std::wstring & res, bool clear, int mode)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
return utf8_to_wide(utf8.c_str(), utf8.size(), res, clear, mode);
|
2012-01-11 12:15:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts an utf8 stream into wide string (std::wstring)
|
|
|
|
|
|
|
|
input:
|
|
|
|
utf8 - an input utf8 stream
|
|
|
|
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:
|
|
|
|
res - an output wide string
|
|
|
|
|
|
|
|
the function returns false if there were some errors when converting
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool utf8_to_wide(std::istream & utf8, std::wstring & res, bool clear, int mode)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
int z;
|
|
|
|
bool correct, was_error = false;
|
|
|
|
|
|
|
|
if( clear )
|
|
|
|
res.clear();
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
while( utf8_to_int(utf8, z, correct) > 0 )
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
if( !correct )
|
|
|
|
{
|
|
|
|
if( mode == 1 )
|
|
|
|
res += 0xFFFD; // U+FFFD "replacement character"
|
|
|
|
|
|
|
|
was_error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
int_to_wide(z, res);
|
2012-01-11 12:15:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !was_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
|
2012-01-11 12:15:36 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts one wide character into UTF-8 sequence
|
|
|
|
|
|
|
|
input:
|
|
|
|
z - wide character
|
|
|
|
|
|
|
|
output:
|
|
|
|
utf8 - a buffer for the output sequence
|
|
|
|
utf8_len - the size of the buffer
|
|
|
|
|
|
|
|
the function returns how many characters have been written to the utf8,
|
|
|
|
zero means the utf8 buffer is too small or 'z' is an incorrect unicode character
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
size_t int_to_utf8(int z, char * utf8, size_t utf8_max_len)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
char buf[10];
|
|
|
|
int i = 0;
|
|
|
|
int mask = 0x3f; // 6 first bits set
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
if( utf8_max_len==0 || !utf8_check_range(z) )
|
2012-01-11 12:15:36 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( z <= 0x7f )
|
|
|
|
{
|
|
|
|
utf8[0] = static_cast<char>(z);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
buf[i] = 0x80 | (z & 0x3f);
|
|
|
|
i += 1;
|
|
|
|
z >>= 6;
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
|
|
|
while( (z & (~mask)) != 0 );
|
|
|
|
|
|
|
|
unsigned int first = -1;
|
|
|
|
first <<= (7 - i);
|
|
|
|
first |= (z & mask);
|
|
|
|
|
|
|
|
if( size_t(i+1) > utf8_max_len )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
utf8[0] = static_cast<char>(first);
|
|
|
|
|
|
|
|
int a = 1;
|
|
|
|
for(--i; i>=0 ; --i, ++a)
|
|
|
|
utf8[a] = buf[i];
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts one wide character into UTF-8 string
|
|
|
|
|
|
|
|
input:
|
|
|
|
z - wide character
|
|
|
|
|
|
|
|
output:
|
|
|
|
utf8 - a UTF-8 string for the output sequence (the string is not cleared)
|
|
|
|
|
|
|
|
the function returns how many characters have been written to the utf8 string,
|
|
|
|
zero means that 'z' is an incorrect unicode character
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
size_t int_to_utf8(int z, std::string & utf8, bool clear)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
char buf[10];
|
|
|
|
|
|
|
|
if( clear )
|
|
|
|
utf8.clear();
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
size_t len = int_to_utf8(z, buf, sizeof(buf)/sizeof(char));
|
2012-01-11 12:15:36 +01:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for(i=0 ; i<len ; ++i)
|
|
|
|
utf8 += buf[i];
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts a wide string into UTF-8 string
|
|
|
|
|
|
|
|
input:
|
|
|
|
wide_string - a wide string for converting
|
|
|
|
string_len - the 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 string for the output sequence (the string is not cleared)
|
|
|
|
|
|
|
|
this function returns false if there were some errors when converting
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool wide_to_utf8(const wchar_t * wide_string, size_t string_len, std::string & utf8, bool clear, int mode)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
bool was_error = false;
|
|
|
|
size_t chars;
|
|
|
|
|
|
|
|
if( clear )
|
|
|
|
utf8.clear();
|
|
|
|
|
|
|
|
while( string_len > 0 )
|
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
chars = private_namespace::wide_one_to_utf8(wide_string, string_len, utf8, was_error, mode);
|
2012-01-11 12:15:36 +01:00
|
|
|
wide_string += chars;
|
|
|
|
string_len -= chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !was_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts a wide string into UTF-8 string
|
|
|
|
|
|
|
|
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 string for the output sequence (the string is not cleared)
|
|
|
|
|
|
|
|
this function returns false if there were some errors when converting
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool wide_to_utf8(const wchar_t * wide_string, std::string & utf8, bool clear, int mode)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
|
|
|
bool was_error = false;
|
|
|
|
|
|
|
|
if( clear )
|
|
|
|
utf8.clear();
|
|
|
|
|
|
|
|
while( *wide_string )
|
2021-05-21 00:24:56 +02:00
|
|
|
wide_string += private_namespace::wide_one_to_utf8(wide_string, utf8, was_error, mode);
|
2012-01-11 12:15:36 +01:00
|
|
|
|
|
|
|
return !was_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts a wide string (std::wstring) into UTF-8 string
|
|
|
|
|
|
|
|
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 string for the output sequence (the string is not cleared)
|
|
|
|
|
|
|
|
this function returns false if there were some errors when converting
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool wide_to_utf8(const std::wstring & wide_string, std::string & utf8, bool clear, int mode)
|
2012-01-11 12:15:36 +01:00
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
return wide_to_utf8(wide_string.c_str(), wide_string.size(), utf8, clear, mode);
|
2012-01-11 12:15:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
/*!
|
|
|
|
this function converts a wide string into UTF-8 stream
|
2012-01-11 12:15:36 +01:00
|
|
|
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
input:
|
|
|
|
wide_string - a wide string for converting
|
|
|
|
string_len - lenght of the wide 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 buffer for the UTF-8 stream
|
|
|
|
utf8_len - the size of the buffer
|
|
|
|
utf8_written - how many bytes have been written to the buffer
|
|
|
|
|
|
|
|
this function returns false if there were some errors when converting or the output buffer was too small,
|
|
|
|
the output string is not null terminated
|
|
|
|
|
|
|
|
if there is an error when converting (there is an incorrect character in the wide string) the function
|
|
|
|
will continue converting but if the buffer is too small the function breaks immediately
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool wide_to_utf8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode)
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
{
|
|
|
|
bool was_error = false;
|
|
|
|
bool was_buffer_to_small;
|
|
|
|
size_t chars, utf8_saved;
|
|
|
|
|
|
|
|
utf8_written = 0;
|
|
|
|
|
|
|
|
while( string_len > 0 )
|
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
chars = private_namespace::wide_one_to_utf8(wide_string, string_len, utf8, utf8_len, utf8_saved, was_buffer_to_small, was_error, mode);
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
|
|
|
|
if( was_buffer_to_small )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* if the buffer was too small break immediately
|
|
|
|
* and set the was_error flag
|
|
|
|
*/
|
|
|
|
was_error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wide_string += chars;
|
|
|
|
string_len -= chars;
|
|
|
|
utf8 += utf8_saved;
|
|
|
|
utf8_len -= utf8_saved;
|
|
|
|
utf8_written += utf8_saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 buffer for the UTF-8 stream
|
|
|
|
utf8_len - the size of the buffer
|
|
|
|
utf8_written - how many bytes have been written to the buffer
|
|
|
|
|
|
|
|
this function returns false if there were some errors when converting or the output buffer was too small,
|
|
|
|
the output string is not null terminated
|
|
|
|
|
|
|
|
if there is an error when converting (there is an incorrect character in the wide string) the function
|
|
|
|
will continue converting but if the buffer is too small the function breaks immediately
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool wide_to_utf8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode)
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
return wide_to_utf8(wide_string.c_str(), wide_string.size(), utf8, utf8_len, utf8_written, mode);
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts a wide string into UTF-8 stream
|
|
|
|
|
|
|
|
input:
|
|
|
|
wide_string - a wide string for converting
|
|
|
|
string_len - lenght of the wide 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 buffer for the UTF-8 stream
|
|
|
|
utf8_len - the size of the buffer
|
|
|
|
|
|
|
|
this function returns false if there were some errors when converting or the output buffer was too small,
|
|
|
|
the output string is null terminated (even if there were errors during converting)
|
|
|
|
|
|
|
|
if there is an error when converting (there is an incorrect character in the wide string) the function
|
|
|
|
will continue converting but if the buffer is too small the function breaks immediately
|
|
|
|
(in both cases the utf8 buffer is null terminated)
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool wide_to_utf8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode)
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
{
|
|
|
|
size_t utf8_saved;
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
if( utf8_len == 0 )
|
|
|
|
return false;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
res = wide_to_utf8(wide_string, string_len, utf8, utf8_len - 1, utf8_saved, mode);
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
utf8[utf8_saved] = 0;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
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 buffer for the UTF-8 stream
|
|
|
|
utf8_len - the size of the buffer
|
|
|
|
|
|
|
|
this function returns false if there were some errors when converting or the output buffer was too small,
|
|
|
|
the output string is null terminated (even if there were errors during converting)
|
|
|
|
|
|
|
|
if there is an error when converting (there is an incorrect character in the wide string) the function
|
|
|
|
will continue converting but if the buffer is too small the function breaks immediately
|
|
|
|
(in both cases the utf8 buffer is null terminated)
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool wide_to_utf8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode)
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
{
|
2021-05-21 00:24:56 +02:00
|
|
|
return wide_to_utf8(wide_string.c_str(), wide_string.size(), utf8, utf8_len, mode);
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
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 buffer for the UTF-8 stream
|
|
|
|
utf8_len - the size of the buffer
|
|
|
|
utf8_written - how many bytes have been written to the buffer
|
2012-01-11 12:15:36 +01:00
|
|
|
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
this function returns false if there were some errors when converting or the output buffer was too small,
|
|
|
|
the output string is not null terminated
|
2012-01-11 12:15:36 +01:00
|
|
|
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
if there is an error when converting (there is an incorrect character in the wide string) the function
|
|
|
|
will continue converting but if the buffer is too small the function breaks immediately
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool wide_to_utf8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode)
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
{
|
|
|
|
bool was_error = false;
|
|
|
|
bool was_buffer_to_small;
|
|
|
|
size_t chars, utf8_saved;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
utf8_written = 0;
|
|
|
|
|
|
|
|
while( *wide_string )
|
|
|
|
{
|
|
|
|
len = (*(wide_string+1) == 0) ? 1 : 2;
|
2021-05-21 00:24:56 +02:00
|
|
|
chars = private_namespace::wide_one_to_utf8(wide_string, len, utf8, utf8_len, utf8_saved, was_buffer_to_small, was_error, mode);
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
|
|
|
|
if( was_buffer_to_small )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* if the buffer was too small break immediately
|
|
|
|
* and set the was_error flag
|
|
|
|
*/
|
|
|
|
was_error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wide_string += chars;
|
|
|
|
utf8 += utf8_saved;
|
|
|
|
utf8_len -= utf8_saved;
|
|
|
|
utf8_written += utf8_saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !was_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this function converts a wide string 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 buffer for the UTF-8 stream
|
|
|
|
utf8_len - the size of the buffer
|
|
|
|
|
|
|
|
this function returns false if there were some errors when converting or the output buffer was too small,
|
|
|
|
the output string is null terminated (even if there were errors during converting)
|
|
|
|
|
|
|
|
if there is an error when converting (there is an incorrect character in the wide string) the function
|
|
|
|
will continue converting but if the buffer is too small the function breaks immediately
|
|
|
|
(in both cases the utf8 buffer is null terminated)
|
|
|
|
*/
|
2021-05-21 00:24:56 +02:00
|
|
|
bool wide_to_utf8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode)
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
{
|
|
|
|
size_t utf8_saved;
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
if( utf8_len == 0 )
|
|
|
|
return false;
|
|
|
|
|
2021-05-21 00:24:56 +02:00
|
|
|
res = wide_to_utf8(wide_string, utf8, utf8_len - 1, utf8_saved, mode);
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
utf8[utf8_saved] = 0;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2012-01-11 12:15:36 +01:00
|
|
|
|