Menu

[r744]: / phprpc_3.0 / cpp / utf8.hpp  Maximize  Restore  History

Download this file

170 lines (141 with data), 4.1 kB

  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**********************************************************\
| |
| The implementation of PHPRPC Protocol 3.0 |
| |
| utf8.hpp |
| |
| Release 3.0 |
| Copyright by Team-PHPRPC |
| |
| WebSite: http://www.phprpc.org/ |
| http://www.phprpc.net/ |
| http://www.phprpc.com/ |
| http://sourceforge.net/projects/php-rpc/ |
| |
| Authors: Chen fei <cf850118@163.com> |
| |
| This file may be distributed and/or modified under the |
| terms of the GNU Lesser General Public License (LGPL) |
| version 3.0 as published by the Free Software Foundation |
| and appearing in the included file LICENSE. |
| |
\**********************************************************/
/* UTF8 encode/decode library.
*
* Copyright: Chen fei <cf850118@163.com>
* Version: 3.0
* LastModified: Nov 30, 2009
* This library is free. You can redistribute it and/or modify it.
*/
#ifndef UTF8_INCLUDED
#define UTF8_INCLUDED
#include "common.hpp"
#ifdef WIN32
#include <windows.h>
#define UTF8Encode(data) ansi_to_utf8((data))
#define UTF8Decode(data) utf8_to_ansi((data))
#else
#define UTF8Encode(data) (data)
#define UTF8Decode(data) (data)
#endif
namespace phprpc
{
std::string utf16_to_utf8(const std::wstring & data)
{
std::string retval;
retval.reserve(data.size() * 3);
std::wstring::const_iterator iter = data.begin();
while (iter != data.end())
{
ushort wc = (ushort)*iter++;
if (wc < 0x00000080)
{
retval.push_back((char)wc);
}
else if (wc < 0x00000800)
{
retval.push_back((char)(0xC0 | ((wc >> 6) & 0x1F)));
retval.push_back((char)(0x80 | (wc & 0x3F)));
}
else
{
retval.push_back((char)(0xE0 | ((wc >> 12) & 0x0F)));
retval.push_back((char)(0x80 | ((wc >> 6) & 0x3F)));
retval.push_back((char)(0x80 | (wc & 0x3F)));
}
}
return retval;
}
std::wstring utf8_to_utf16(const std::string & data)
{
std::wstring retval;
retval.reserve(data.size());
std::string::const_iterator iter = data.begin();
while (iter != data.end())
{
char mb = *iter++;
uint cc = 0;
uint wc;
while ((cc < 7) && (mb & (1 << (7 - cc))))
{
cc++;
}
if (cc == 1 || cc > 6)
{
continue;
}
if (cc == 0)
{
wc = mb;
}
else
{
wc = (mb & ((1 << (7 - cc)) - 1)) << ((cc - 1) * 6);
while (--cc > 0)
{
mb = *iter++;
if (((mb >> 6) & 0x03) != 2 )
{
return retval;
}
wc |= (mb & 0x3F) << ((cc - 1) * 6);
}
}
if (wc & 0xFFFF0000)
{
wc = L'?';
}
retval.push_back(wc);
}
return retval;
}
#ifdef WIN32
std::string ansi_to_utf8(const std::string & data)
{
if (data.empty())
{
return "";
}
size_t len = data.size() + 1;
wchar * ws = new wchar[len];
MultiByteToWideChar(CP_ACP, 0, data.c_str(), (int)len, ws, (int)len * 2);
std::string retval = utf16_to_utf8(ws);
delete ws;
return retval;
}
std::string utf8_to_ansi(const std::string & data)
{
if (data.empty())
{
return "";
}
size_t len = data.size() + 1;
char * s = new char[len * 2];
WideCharToMultiByte(CP_ACP, 0, utf8_to_utf16(data).c_str(), -1, s, (int)len * 2, NULL, NULL);
std::string retval(s);
delete s;
return retval;
}
#endif
} // namespace phprpc
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.