/* * This file is a part of PikoTools * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2010-2023, 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: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. 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. * * 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 HOLDER 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_pikotools_src_utf8_utf8 #define headerfile_pikotools_src_utf8_utf8 #include #include "textstream/stream.h" namespace pt { /* * public methods are also defined in utf8_stream.h * */ /*! UTF-8, a transformation format of ISO 10646 http://tools.ietf.org/html/rfc3629 when wchar_t is 4 bytes length we use UTF-32 when wchar_t is 2 bytes length we use UTF-16 (with surrogate pairs) UTF-16 http://www.ietf.org/rfc/rfc2781.txt */ /*! returns true if 'c' is a correct unicode character RENAMEME to is_correct_unicode_char */ bool utf8_check_range(int c); /*! returns true if 'c' is a correct unicode character this method is used when reading from an utf8 string how_many_chars - means how many characters from utf8 string were read */ bool utf8_check_range(int c, int how_many_bytes); /* * returns true if 'c' is a characters from the surrogate range * (c>=0xD800 && c<=0xDFFF) * */ bool is_surrogate_char(int c); /* * returns true if 'c' is a first character from the surrogate pair * (c>=0xD800 && c<=0xDBFF) */ bool is_first_surrogate_char(int c); /* * returns true if 'c' is a second character from the surrogate pair * (c>=0xDC00 && c<=0xDFFF) */ bool is_second_surrogate_char(int c); /* * returns a code point from two surrogate pair characters */ bool surrogate_pair_to_int(int c1, int c2, int & z); /* * * * * convertions from UTF-8 * * * */ /*! converting one character from UTF-8 to an int */ size_t utf8_to_int(const char * utf8, size_t utf8_len, int & res, bool & correct); size_t utf8_to_int(const char * utf8, int & res, bool & correct); size_t utf8_to_int(const std::string & utf8, int & res, bool & correct); size_t utf8_to_int(std::istream & utf8, int & res, bool & correct); size_t utf8_to_int(const Stream & utf8, size_t stream_index, int & res, bool & correct); template size_t utf8_to_int(StreamIteratorType & iterator_in, const StreamIteratorType & iterator_end, int & res, bool & correct); /*! converting one character from int to wide stream returns true if a character was inserted to the stream */ template bool int_to_wide(int c, StreamType & res); /*! converting one character from int to wide string this method will not terminate the output string with a null character return how many characters have been written (0, 1 or 2) */ size_t int_to_wide(int c, wchar_t * res, size_t max_buf_len); /*! converting one character from int to wide string returns true if a character was inserted to the string */ bool int_to_wide(int c, std::wstring & res); /*! converting UTF-8 string to a wide string */ bool utf8_to_wide(const char * utf8, size_t utf8_len, std::wstring & res, bool clear = true, int mode = 1); bool utf8_to_wide(const char * utf8, std::wstring & res, bool clear = true, int mode = 1); bool utf8_to_wide(const std::string & utf8, std::wstring & res, bool clear = true, int mode = 1); bool utf8_to_wide(std::istream & utf8, std::wstring & res, bool clear = true, int mode = 1); template bool utf8_to_wide(const char * utf8, size_t utf8_len, StreamType & res, bool clear = true, int mode = 1); // need to be tested template bool utf8_to_wide(const char * utf8, StreamType & res, bool clear = true, int mode = 1); // need to be tested template bool utf8_to_wide(const std::string & utf8, StreamType & res, bool clear = true, int mode = 1); // need to be tested template bool utf8_to_wide(std::istream & utf8, StreamType & res, bool clear = true, int mode = 1); template bool utf8_to_wide(const Stream & stream, StreamOrStringType & res, bool clear = true, int mode = 1); template bool utf8_to_wide(StreamIteratorType & iterator_in, const StreamIteratorType & iterator_end, StreamOrStringType & out_stream, bool clear_stream = true, int mode = 1); template class TextStreamBase; // defined at the end in textstream.h template bool utf8_to_wide(const TextStreamBase & utf8, StreamOrStringType & out_stream, bool clear_stream = true, int mode = 1); template bool utf8_to_wide(StreamIteratorType & iterator_in, const StreamIteratorType & iterator_end, wchar_t * out_buffer, size_t max_buffer_len, int mode = 1, bool * was_buffer_sufficient_large = nullptr); template bool utf8_to_wide(const StreamType & stream, wchar_t * out_buffer, size_t max_buffer_len, bool * was_buffer_sufficient_large = nullptr, int mode = 1); /* * * * * convertions to UTF-8 * * * */ /*! converting one int character to UTF-8 */ size_t int_to_utf8(int z, char * utf8, size_t utf8_max_len); size_t int_to_utf8(int z, std::string & utf8, bool clear = true); template size_t int_to_utf8(int z, StreamType & utf8); /*! converting a wide string to UTF-8 string */ bool wide_to_utf8(const wchar_t * wide_string, size_t string_len, std::string & utf8, bool clear = true, int mode = 1); bool wide_to_utf8(const wchar_t * wide_string, std::string & utf8, bool clear = true, int mode = 1); bool wide_to_utf8(const std::wstring & wide_string, std::string & utf8, bool clear = true, int mode = 1); template bool wide_to_utf8(const wchar_t * wide_string, size_t string_len, StreamType & utf8, int mode = 1); template bool wide_to_utf8(const wchar_t * wide_string, StreamType & utf8, int mode = 1); template bool wide_to_utf8(const std::wstring & wide_string, StreamType & utf8, int mode = 1); 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 = 1); bool wide_to_utf8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1); bool wide_to_utf8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1); bool wide_to_utf8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1); bool wide_to_utf8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1); bool wide_to_utf8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1); template bool wide_stream_to_utf8(StreamType & buffer, std::string & utf8, bool clear = true, int mode = 1); template bool wide_stream_to_utf8(const Stream & stream, StreamType & utf8, bool clear = true, int mode = 1); template bool wide_stream_to_utf8(StreamTypeIn & buffer, StreamTypeOut & utf8, bool clear = true, int mode = 1); template bool wide_stream_to_utf8(StreamType & buffer, char * utf8, std::size_t max_buffer_size, bool * was_buffer_sufficient_large = nullptr, int mode = 1); } // namespace #include "utf8/utf8_templates.h" #endif