UnicodeConverter.h#

Parent directory (strings)

Utility functions for handling Unicode.

Includes#

  • PVRCore/Errors.h

  • cstdint

  • vector

Namespaces#

Classes#

Typedefs#

Source Code#

#pragma once
#include "PVRCore/Errors.h"
#include <vector>
#include <cstdint>

namespace pvr {
// UTF types
typedef unsigned char utf8;
typedef unsigned short utf16;
typedef unsigned int utf32;

namespace utils {
class UnicodeConversionError : public std::runtime_error
{
public:
    UnicodeConversionError(std::string message) : std::runtime_error("Unicode Conversion Failed: " + message) {}
};

class UnicodeConverter
{
public:
    static uint32_t unicodeCount(const utf8* unicodeString);

    static uint32_t unicodeCount(const utf16* unicodeString);

    static uint32_t unicodeCount(const utf32* unicodeString);

    static void convertAsciiToUnicode(const char* asciiString, std::vector<utf8>& unicodeString);

    static void convertAsciiToUnicode(const char* asciiString, std::vector<utf16>& unicodeString)
    {
        if (!isAsciiChar(asciiString)) { throw UnicodeConversionError("Parameter [asciiString] was not actually a valid ASCII string"); }
        convertUTF8ToUTF16(reinterpret_cast<const utf8*>(asciiString), unicodeString);
    }

    static void convertAsciiToUnicode(const char* asciiString, std::vector<utf32>& utf32StringOut)
    {
        if (!isAsciiChar(asciiString)) { throw UnicodeConversionError("Parameter [asciiString] was not actually a valid ASCII string"); }
        convertUTF8ToUTF32(reinterpret_cast<const utf8*>(asciiString), utf32StringOut);
    }

    static void convertUTF8ToUTF16(const utf8* utf8String, std::vector<utf16>& utf16StringOut);

    static void convertUTF8ToUTF32(const utf8* utf8String, std::vector<utf32>& utf32StringOut);

    static void convertUTF16ToUTF8(const utf16* utf16String, std::vector<utf8>& utf8StringOut);

    static void convertUTF16ToUTF32(const utf16* utf16String, std::vector<utf32>& utf32StringOut);

    static void convertUTF32ToUTF8(const utf32* utf32String, std::vector<utf8>& utf8StringOut);

    static void convertUTF32ToUTF16(const utf32* utf32String, std::vector<utf16>& utf16StringOut);

    static bool isValidUnicode(const utf8* unicodeString);

    static bool isValidUnicode(const utf16* unicodeString);

    static bool isValidUnicode(const utf32* unicodeString);

    static bool isAsciiChar(const char* asciiString);

    static bool isAsciiChar(char asciiChar);

private:
    static bool isValidCodePoint(utf32 codePoint);
};
} // namespace utils
} // namespace pvr