]>
git.proxmox.com Git - libgit2.git/blob - src/win32/utf-conv.c
2 * Copyright (C) 2009-2012 the libgit2 contributors
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
11 #define U16_LEAD(c) (wchar_t)(((c)>>10)+0xd7c0)
12 #define U16_TRAIL(c) (wchar_t)(((c)&0x3ff)|0xdc00)
15 void git__utf8_to_16(wchar_t *dest
, size_t length
, const char *src
)
17 wchar_t *pDest
= dest
;
19 const uint8_t* pSrc
= (uint8_t*) src
;
21 assert(dest
&& src
&& length
);
25 while(*pSrc
&& length
> 0) {
31 * ASCII, or a trail byte in lead position which is treated like
32 * a single-byte sequence for better character boundary
33 * resynchronization after illegal sequences.
35 *pDest
++ = (wchar_t)ch
;
37 } else if(ch
< 0xe0) { /* U+0080..U+07FF */
39 /* 0x3080 = (0xc0 << 6) + 0x80 */
40 *pDest
++ = (wchar_t)((ch
<< 6) + *pSrc
++ - 0x3080);
43 } else if(ch
< 0xf0) { /* U+0800..U+FFFF */
44 if (pSrc
[0] && pSrc
[1]) {
45 /* no need for (ch & 0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */
46 /* 0x2080 = (0x80 << 6) + 0x80 */
47 ch
= (ch
<< 12) + (*pSrc
++ << 6);
48 *pDest
++ = (wchar_t)(ch
+ *pSrc
++ - 0x2080);
51 } else /* f0..f4 */ { /* U+10000..U+10FFFF */
52 if (length
>= 1 && pSrc
[0] && pSrc
[1] && pSrc
[2]) {
53 /* 0x3c82080 = (0xf0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80 */
54 ch
= (ch
<< 18) + (*pSrc
++ << 12);
56 ch
+= *pSrc
++ - 0x3c82080;
57 *(pDest
++) = U16_LEAD(ch
);
58 *(pDest
++) = U16_TRAIL(ch
);
59 length
--; /* two bytes for this character */
64 /* truncated character at the end */
73 void git__utf8_to_16(wchar_t *dest
, size_t length
, const char *src
)
75 MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dest
, (int)length
);
78 void git__utf16_to_8(char *out
, const wchar_t *input
)
80 WideCharToMultiByte(CP_UTF8
, 0, input
, -1, out
, GIT_WIN_PATH
, NULL
, NULL
);