2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#pragma once
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
#include "common/Common.h"
|
2025-08-18 00:31:26 +00:00
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
#include <string>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//! Unicode utility functions
|
|
|
|
|
/*!
|
|
|
|
|
This class provides functions for converting between various Unicode
|
|
|
|
|
encodings and the current locale encoding.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
class Unicode
|
|
|
|
|
{
|
2012-06-10 16:50:54 +00:00
|
|
|
public:
|
2024-07-02 19:07:06 +00:00
|
|
|
//! @name accessors
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
|
|
//! Test UTF-8 string for validity
|
|
|
|
|
/*!
|
|
|
|
|
Returns true iff the string contains a valid sequence of UTF-8
|
|
|
|
|
encoded characters.
|
|
|
|
|
*/
|
2025-01-06 00:55:07 +00:00
|
|
|
static bool isUTF8(const std::string &);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Convert from UTF-8 to UCS-2 encoding
|
|
|
|
|
/*!
|
2025-04-27 19:39:23 +00:00
|
|
|
Convert from UTF-8 to UCS-2. If errors is not nullptr then *errors
|
2024-07-02 19:07:06 +00:00
|
|
|
is set to true iff any character could not be encoded in UCS-2.
|
|
|
|
|
Decoding errors do not set *errors.
|
|
|
|
|
*/
|
2025-04-27 19:39:23 +00:00
|
|
|
static std::string UTF8ToUCS2(const std::string &, bool *errors = nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Convert from UTF-8 to UCS-4 encoding
|
|
|
|
|
/*!
|
2025-04-27 19:39:23 +00:00
|
|
|
Convert from UTF-8 to UCS-4. If errors is not nullptr then *errors
|
2024-07-02 19:07:06 +00:00
|
|
|
is set to true iff any character could not be encoded in UCS-4.
|
|
|
|
|
Decoding errors do not set *errors.
|
|
|
|
|
*/
|
2025-04-27 19:39:23 +00:00
|
|
|
static std::string UTF8ToUCS4(const std::string &, bool *errors = nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Convert from UTF-8 to UTF-16 encoding
|
|
|
|
|
/*!
|
2025-04-27 19:39:23 +00:00
|
|
|
Convert from UTF-8 to UTF-16. If errors is not nullptr then *errors
|
2024-07-02 19:07:06 +00:00
|
|
|
is set to true iff any character could not be encoded in UTF-16.
|
|
|
|
|
Decoding errors do not set *errors.
|
|
|
|
|
*/
|
2025-04-27 19:39:23 +00:00
|
|
|
static std::string UTF8ToUTF16(const std::string &, bool *errors = nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Convert from UTF-8 to UTF-32 encoding
|
|
|
|
|
/*!
|
2025-04-27 19:39:23 +00:00
|
|
|
Convert from UTF-8 to UTF-32. If errors is not nullptr then *errors
|
2024-07-02 19:07:06 +00:00
|
|
|
is set to true iff any character could not be encoded in UTF-32.
|
|
|
|
|
Decoding errors do not set *errors.
|
|
|
|
|
*/
|
2025-04-27 19:39:23 +00:00
|
|
|
static std::string UTF8ToUTF32(const std::string &, bool *errors = nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Convert from UCS-2 to UTF-8
|
|
|
|
|
/*!
|
2025-04-27 19:39:23 +00:00
|
|
|
Convert from UCS-2 to UTF-8. If errors is not nullptr then *errors is
|
2024-07-02 19:07:06 +00:00
|
|
|
set to true iff any character could not be decoded.
|
|
|
|
|
*/
|
2025-07-11 16:55:59 +00:00
|
|
|
static std::string UCS2ToUTF8(const std::string_view &, bool *errors = nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Convert from UCS-4 to UTF-8
|
|
|
|
|
/*!
|
2025-04-27 19:39:23 +00:00
|
|
|
Convert from UCS-4 to UTF-8. If errors is not nullptr then *errors is
|
2024-07-02 19:07:06 +00:00
|
|
|
set to true iff any character could not be decoded.
|
|
|
|
|
*/
|
2025-05-22 00:31:48 +00:00
|
|
|
static std::string UCS4ToUTF8(const std::string_view &, bool *errors = nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Convert from UTF-16 to UTF-8
|
|
|
|
|
/*!
|
2025-04-27 19:39:23 +00:00
|
|
|
Convert from UTF-16 to UTF-8. If errors is not nullptr then *errors is
|
2024-07-02 19:07:06 +00:00
|
|
|
set to true iff any character could not be decoded.
|
|
|
|
|
*/
|
2025-05-22 00:31:48 +00:00
|
|
|
static std::string UTF16ToUTF8(const std::string_view &, bool *errors = nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//! Convert from UTF-32 to UTF-8
|
|
|
|
|
/*!
|
2025-04-27 19:39:23 +00:00
|
|
|
Convert from UTF-32 to UTF-8. If errors is not nullptr then *errors is
|
2024-07-02 19:07:06 +00:00
|
|
|
set to true iff any character could not be decoded.
|
|
|
|
|
*/
|
2025-05-22 00:31:48 +00:00
|
|
|
static std::string UTF32ToUTF8(const std::string_view &, bool *errors = nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
//@}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
// internal conversion to UTF8
|
2025-01-11 02:13:19 +00:00
|
|
|
static std::string doUCS2ToUTF8(const uint8_t *src, uint32_t n, bool *errors);
|
|
|
|
|
static std::string doUCS4ToUTF8(const uint8_t *src, uint32_t n, bool *errors);
|
|
|
|
|
static std::string doUTF16ToUTF8(const uint8_t *src, uint32_t n, bool *errors);
|
|
|
|
|
static std::string doUTF32ToUTF8(const uint8_t *src, uint32_t n, bool *errors);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// convert characters to/from UTF8
|
2025-01-11 02:13:19 +00:00
|
|
|
static uint32_t fromUTF8(const uint8_t *&src, uint32_t &size);
|
|
|
|
|
static void toUTF8(std::string &dst, uint32_t c, bool *errors);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2025-01-11 02:13:19 +00:00
|
|
|
static uint32_t s_invalid;
|
|
|
|
|
static uint32_t s_replacement;
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|