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