nuclear@1: /************************************************************************************ nuclear@1: nuclear@1: PublicHeader: OVR.h nuclear@1: Filename : OVR_UTF8Util.h nuclear@1: Content : UTF8 Unicode character encoding/decoding support nuclear@1: Created : September 19, 2012 nuclear@1: Notes : nuclear@1: nuclear@1: Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. nuclear@1: nuclear@1: Use of this software is subject to the terms of the Oculus license nuclear@1: agreement provided at the time of installation or download, or which nuclear@1: otherwise accompanies this software in either electronic or hard copy form. nuclear@1: nuclear@1: ************************************************************************************/ nuclear@1: nuclear@1: #ifndef OVR_UTF8Util_h nuclear@1: #define OVR_UTF8Util_h nuclear@1: nuclear@1: #include "OVR_Types.h" nuclear@1: nuclear@1: namespace OVR { namespace UTF8Util { nuclear@1: nuclear@1: //----------------------------------------------------------------------------------- nuclear@1: nuclear@1: // *** UTF8 string length and indexing. nuclear@1: nuclear@1: // Determines the length of UTF8 string in characters. nuclear@1: // If source length is specified (in bytes), null 0 character is counted properly. nuclear@1: SPInt OVR_STDCALL GetLength(const char* putf8str, SPInt length = -1); nuclear@1: nuclear@1: // Gets a decoded UTF8 character at index; you can access up to the index returned nuclear@1: // by GetLength. 0 will be returned for out of bounds access. nuclear@1: UInt32 OVR_STDCALL GetCharAt(SPInt index, const char* putf8str, SPInt length = -1); nuclear@1: nuclear@1: // Converts UTF8 character index into byte offset. nuclear@1: // -1 is returned if index was out of bounds. nuclear@1: SPInt OVR_STDCALL GetByteIndex(SPInt index, const char* putf8str, SPInt length = -1); nuclear@1: nuclear@1: nuclear@1: // *** 16-bit Unicode string Encoding/Decoding routines. nuclear@1: nuclear@1: // Determines the number of bytes necessary to encode a string. nuclear@1: // Does not count the terminating 0 (null) character. nuclear@1: SPInt OVR_STDCALL GetEncodeStringSize(const wchar_t* pchar, SPInt length = -1); nuclear@1: nuclear@1: // Encodes a unicode (UCS-2 only) string into a buffer. The size of buffer must be at nuclear@1: // least GetEncodeStringSize() + 1. nuclear@1: void OVR_STDCALL EncodeString(char *pbuff, const wchar_t* pchar, SPInt length = -1); nuclear@1: nuclear@1: // Decode UTF8 into a wchar_t buffer. Must have GetLength()+1 characters available. nuclear@1: // Characters over 0xFFFF are replaced with 0xFFFD. nuclear@1: // Returns the length of resulting string (number of characters) nuclear@1: UPInt OVR_STDCALL DecodeString(wchar_t *pbuff, const char* putf8str, SPInt bytesLen = -1); nuclear@1: nuclear@1: nuclear@1: // *** Individual character Encoding/Decoding. nuclear@1: nuclear@1: // Determined the number of bytes necessary to encode a UCS character. nuclear@1: int OVR_STDCALL GetEncodeCharSize(UInt32 ucsCharacter); nuclear@1: nuclear@1: // Encodes the given UCS character into the given UTF-8 buffer. nuclear@1: // Writes the data starting at buffer[offset], and nuclear@1: // increments offset by the number of bytes written. nuclear@1: // May write up to 6 bytes, so make sure there's room in the buffer nuclear@1: void OVR_STDCALL EncodeChar(char* pbuffer, SPInt* poffset, UInt32 ucsCharacter); nuclear@1: nuclear@1: // Return the next Unicode character in the UTF-8 encoded buffer. nuclear@1: // Invalid UTF-8 sequences produce a U+FFFD character as output. nuclear@1: // Advances *utf8_buffer past the character returned. Pointer advance nuclear@1: // occurs even if the terminating 0 character is hit, since that allows nuclear@1: // strings with middle '\0' characters to be supported. nuclear@1: UInt32 OVR_STDCALL DecodeNextChar_Advance0(const char** putf8Buffer); nuclear@1: nuclear@1: // Safer version of DecodeNextChar, which doesn't advance pointer if nuclear@1: // null character is hit. nuclear@1: inline UInt32 DecodeNextChar(const char** putf8Buffer) nuclear@1: { nuclear@1: UInt32 ch = DecodeNextChar_Advance0(putf8Buffer); nuclear@1: if (ch == 0) nuclear@1: (*putf8Buffer)--; nuclear@1: return ch; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: }} // OVR::UTF8Util nuclear@1: nuclear@1: #endif