rev |
line source |
nuclear@0
|
1 /************************************************************************************
|
nuclear@0
|
2
|
nuclear@0
|
3 PublicHeader: OVR_Kernel.h
|
nuclear@0
|
4 Filename : OVR_UTF8Util.h
|
nuclear@0
|
5 Content : UTF8 Unicode character encoding/decoding support
|
nuclear@0
|
6 Created : September 19, 2012
|
nuclear@0
|
7 Notes :
|
nuclear@0
|
8
|
nuclear@0
|
9 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
|
nuclear@0
|
10
|
nuclear@0
|
11 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
|
nuclear@0
|
12 you may not use the Oculus VR Rift SDK except in compliance with the License,
|
nuclear@0
|
13 which is provided at the time of installation or download, or which
|
nuclear@0
|
14 otherwise accompanies this software in either electronic or hard copy form.
|
nuclear@0
|
15
|
nuclear@0
|
16 You may obtain a copy of the License at
|
nuclear@0
|
17
|
nuclear@0
|
18 http://www.oculusvr.com/licenses/LICENSE-3.2
|
nuclear@0
|
19
|
nuclear@0
|
20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
|
nuclear@0
|
21 distributed under the License is distributed on an "AS IS" BASIS,
|
nuclear@0
|
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
nuclear@0
|
23 See the License for the specific language governing permissions and
|
nuclear@0
|
24 limitations under the License.
|
nuclear@0
|
25
|
nuclear@0
|
26 ************************************************************************************/
|
nuclear@0
|
27
|
nuclear@0
|
28 #ifndef OVR_UTF8Util_h
|
nuclear@0
|
29 #define OVR_UTF8Util_h
|
nuclear@0
|
30
|
nuclear@0
|
31 #include "OVR_Types.h"
|
nuclear@0
|
32
|
nuclear@0
|
33 namespace OVR { namespace UTF8Util {
|
nuclear@0
|
34
|
nuclear@0
|
35 //-----------------------------------------------------------------------------------
|
nuclear@0
|
36
|
nuclear@0
|
37 // *** UTF8 string length and indexing.
|
nuclear@0
|
38
|
nuclear@0
|
39 // Determines the length of UTF8 string in characters.
|
nuclear@0
|
40 // If source length is specified (in bytes), null 0 character is counted properly.
|
nuclear@0
|
41 intptr_t OVR_STDCALL GetLength(const char* putf8str, intptr_t length = -1);
|
nuclear@0
|
42
|
nuclear@0
|
43 // Gets a decoded UTF8 character at index; you can access up to the index returned
|
nuclear@0
|
44 // by GetLength. 0 will be returned for out of bounds access.
|
nuclear@0
|
45 uint32_t OVR_STDCALL GetCharAt(intptr_t index, const char* putf8str, intptr_t length = -1);
|
nuclear@0
|
46
|
nuclear@0
|
47 // Converts UTF8 character index into byte offset.
|
nuclear@0
|
48 // -1 is returned if index was out of bounds.
|
nuclear@0
|
49 intptr_t OVR_STDCALL GetByteIndex(intptr_t index, const char* putf8str, intptr_t length = -1);
|
nuclear@0
|
50
|
nuclear@0
|
51
|
nuclear@0
|
52 // *** 16-bit Unicode string Encoding/Decoding routines.
|
nuclear@0
|
53
|
nuclear@0
|
54 // Determines the number of bytes necessary to encode a string.
|
nuclear@0
|
55 // Does not count the terminating 0 (null) character.
|
nuclear@0
|
56 intptr_t OVR_STDCALL GetEncodeStringSize(const wchar_t* pchar, intptr_t length = -1);
|
nuclear@0
|
57
|
nuclear@0
|
58 // Encodes a unicode (UCS-2 only) string into a buffer. The size of buffer must be at
|
nuclear@0
|
59 // least GetEncodeStringSize() + 1.
|
nuclear@0
|
60 void OVR_STDCALL EncodeString(char *pbuff, const wchar_t* pchar, intptr_t length = -1);
|
nuclear@0
|
61
|
nuclear@0
|
62 // Decode UTF8 into a wchar_t buffer. Must have GetLength()+1 characters available.
|
nuclear@0
|
63 // Characters over 0xFFFF are replaced with 0xFFFD.
|
nuclear@0
|
64 // Returns the length of resulting string (number of characters)
|
nuclear@0
|
65 size_t OVR_STDCALL DecodeString(wchar_t *pbuff, const char* putf8str, intptr_t bytesLen = -1);
|
nuclear@0
|
66
|
nuclear@0
|
67
|
nuclear@0
|
68 // *** Individual character Encoding/Decoding.
|
nuclear@0
|
69
|
nuclear@0
|
70 // Determined the number of bytes necessary to encode a UCS character.
|
nuclear@0
|
71 int OVR_STDCALL GetEncodeCharSize(uint32_t ucsCharacter);
|
nuclear@0
|
72
|
nuclear@0
|
73 // Encodes the given UCS character into the given UTF-8 buffer.
|
nuclear@0
|
74 // Writes the data starting at buffer[offset], and
|
nuclear@0
|
75 // increments offset by the number of bytes written.
|
nuclear@0
|
76 // May write up to 6 bytes, so make sure there's room in the buffer
|
nuclear@0
|
77 void OVR_STDCALL EncodeChar(char* pbuffer, intptr_t* poffset, uint32_t ucsCharacter);
|
nuclear@0
|
78
|
nuclear@0
|
79 // Return the next Unicode character in the UTF-8 encoded buffer.
|
nuclear@0
|
80 // Invalid UTF-8 sequences produce a U+FFFD character as output.
|
nuclear@0
|
81 // Advances *utf8_buffer past the character returned. Pointer advance
|
nuclear@0
|
82 // occurs even if the terminating 0 character is hit, since that allows
|
nuclear@0
|
83 // strings with middle '\0' characters to be supported.
|
nuclear@0
|
84 uint32_t OVR_STDCALL DecodeNextChar_Advance0(const char** putf8Buffer);
|
nuclear@0
|
85
|
nuclear@0
|
86 // Safer version of DecodeNextChar, which doesn't advance pointer if
|
nuclear@0
|
87 // null character is hit.
|
nuclear@0
|
88 inline uint32_t DecodeNextChar(const char** putf8Buffer)
|
nuclear@0
|
89 {
|
nuclear@0
|
90 uint32_t ch = DecodeNextChar_Advance0(putf8Buffer);
|
nuclear@0
|
91 if (ch == 0)
|
nuclear@0
|
92 (*putf8Buffer)--;
|
nuclear@0
|
93 return ch;
|
nuclear@0
|
94 }
|
nuclear@0
|
95
|
nuclear@0
|
96
|
nuclear@0
|
97 }} // OVR::UTF8Util
|
nuclear@0
|
98
|
nuclear@0
|
99 #endif
|