forked from m417z/Textify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLEncode.cpp
59 lines (50 loc) · 1.01 KB
/
URLEncode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "stdafx.h"
#include "URLEncode.h"
namespace
{
CStringA UTF16toUTF8(const CStringW &utf16);
}
namespace URLEncoder
{
CStringW Encode(const CStringW &str)
{
const CStringA utf8str = UTF16toUTF8(str);
int utf8len = utf8str.GetLength();
CStringW result;
result.Preallocate(utf8len * 4);
for(int i = 0; i < utf8len; i++)
{
char c = utf8str[i];
if(
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '_' || c == '.' || c == '~'
)
{
result += c;
}
else
{
result.AppendFormat(L"%%%02X", (DWORD)(BYTE)c);
}
}
result.FreeExtra();
return result;
}
}
namespace
{
CStringA UTF16toUTF8(const CStringW &utf16)
{
CStringA utf8;
int len = WideCharToMultiByte(CP_UTF8, 0, utf16, utf16.GetLength(), NULL, 0, NULL, NULL);
if(len > 1)
{
char *ptr = utf8.GetBuffer(len);
WideCharToMultiByte(CP_UTF8, 0, utf16, utf16.GetLength(), ptr, len, NULL, NULL);
utf8.ReleaseBuffer(len);
}
return utf8;
}
}