oculus1

annotate libovr/Src/Kernel/OVR_UTF8Util.h @ 24:8419d8a13cee

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 04 Oct 2013 14:50:26 +0300
parents e2f9e4603129
children
rev   line source
nuclear@3 1 /************************************************************************************
nuclear@3 2
nuclear@3 3 PublicHeader: OVR.h
nuclear@3 4 Filename : OVR_UTF8Util.h
nuclear@3 5 Content : UTF8 Unicode character encoding/decoding support
nuclear@3 6 Created : September 19, 2012
nuclear@3 7 Notes :
nuclear@3 8
nuclear@3 9 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
nuclear@3 10
nuclear@3 11 Use of this software is subject to the terms of the Oculus license
nuclear@3 12 agreement provided at the time of installation or download, or which
nuclear@3 13 otherwise accompanies this software in either electronic or hard copy form.
nuclear@3 14
nuclear@3 15 ************************************************************************************/
nuclear@3 16
nuclear@3 17 #ifndef OVR_UTF8Util_h
nuclear@3 18 #define OVR_UTF8Util_h
nuclear@3 19
nuclear@3 20 #include "OVR_Types.h"
nuclear@3 21
nuclear@3 22 namespace OVR { namespace UTF8Util {
nuclear@3 23
nuclear@3 24 //-----------------------------------------------------------------------------------
nuclear@3 25
nuclear@3 26 // *** UTF8 string length and indexing.
nuclear@3 27
nuclear@3 28 // Determines the length of UTF8 string in characters.
nuclear@3 29 // If source length is specified (in bytes), null 0 character is counted properly.
nuclear@3 30 SPInt OVR_STDCALL GetLength(const char* putf8str, SPInt length = -1);
nuclear@3 31
nuclear@3 32 // Gets a decoded UTF8 character at index; you can access up to the index returned
nuclear@3 33 // by GetLength. 0 will be returned for out of bounds access.
nuclear@3 34 UInt32 OVR_STDCALL GetCharAt(SPInt index, const char* putf8str, SPInt length = -1);
nuclear@3 35
nuclear@3 36 // Converts UTF8 character index into byte offset.
nuclear@3 37 // -1 is returned if index was out of bounds.
nuclear@3 38 SPInt OVR_STDCALL GetByteIndex(SPInt index, const char* putf8str, SPInt length = -1);
nuclear@3 39
nuclear@3 40
nuclear@3 41 // *** 16-bit Unicode string Encoding/Decoding routines.
nuclear@3 42
nuclear@3 43 // Determines the number of bytes necessary to encode a string.
nuclear@3 44 // Does not count the terminating 0 (null) character.
nuclear@3 45 SPInt OVR_STDCALL GetEncodeStringSize(const wchar_t* pchar, SPInt length = -1);
nuclear@3 46
nuclear@3 47 // Encodes a unicode (UCS-2 only) string into a buffer. The size of buffer must be at
nuclear@3 48 // least GetEncodeStringSize() + 1.
nuclear@3 49 void OVR_STDCALL EncodeString(char *pbuff, const wchar_t* pchar, SPInt length = -1);
nuclear@3 50
nuclear@3 51 // Decode UTF8 into a wchar_t buffer. Must have GetLength()+1 characters available.
nuclear@3 52 // Characters over 0xFFFF are replaced with 0xFFFD.
nuclear@3 53 // Returns the length of resulting string (number of characters)
nuclear@3 54 UPInt OVR_STDCALL DecodeString(wchar_t *pbuff, const char* putf8str, SPInt bytesLen = -1);
nuclear@3 55
nuclear@3 56
nuclear@3 57 // *** Individual character Encoding/Decoding.
nuclear@3 58
nuclear@3 59 // Determined the number of bytes necessary to encode a UCS character.
nuclear@3 60 int OVR_STDCALL GetEncodeCharSize(UInt32 ucsCharacter);
nuclear@3 61
nuclear@3 62 // Encodes the given UCS character into the given UTF-8 buffer.
nuclear@3 63 // Writes the data starting at buffer[offset], and
nuclear@3 64 // increments offset by the number of bytes written.
nuclear@3 65 // May write up to 6 bytes, so make sure there's room in the buffer
nuclear@3 66 void OVR_STDCALL EncodeChar(char* pbuffer, SPInt* poffset, UInt32 ucsCharacter);
nuclear@3 67
nuclear@3 68 // Return the next Unicode character in the UTF-8 encoded buffer.
nuclear@3 69 // Invalid UTF-8 sequences produce a U+FFFD character as output.
nuclear@3 70 // Advances *utf8_buffer past the character returned. Pointer advance
nuclear@3 71 // occurs even if the terminating 0 character is hit, since that allows
nuclear@3 72 // strings with middle '\0' characters to be supported.
nuclear@3 73 UInt32 OVR_STDCALL DecodeNextChar_Advance0(const char** putf8Buffer);
nuclear@3 74
nuclear@3 75 // Safer version of DecodeNextChar, which doesn't advance pointer if
nuclear@3 76 // null character is hit.
nuclear@3 77 inline UInt32 DecodeNextChar(const char** putf8Buffer)
nuclear@3 78 {
nuclear@3 79 UInt32 ch = DecodeNextChar_Advance0(putf8Buffer);
nuclear@3 80 if (ch == 0)
nuclear@3 81 (*putf8Buffer)--;
nuclear@3 82 return ch;
nuclear@3 83 }
nuclear@3 84
nuclear@3 85
nuclear@3 86 }} // OVR::UTF8Util
nuclear@3 87
nuclear@3 88 #endif