oculus1

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