CharsetConverter.cpp
2.14 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
#include <vector>
#include <iterator>
#include "utf8.h"
#include "CharsetConverter.hpp"
using namespace std;
uint32_t UTF8CharsetConverter::peek(const char*& it, const char* end) const {
return utf8::peek_next(it, end);
}
uint32_t UTF8CharsetConverter::next(const char*& it, const char* end) const {
return utf8::next(it, end);
}
void UTF8CharsetConverter::append(uint32_t cp, string& result) const {
utf8::append(cp, back_inserter(result));
}
uint32_t UTF16CharsetConverter::peek(const char*& it, const char* end) const {
/*
* Borrowed from utfcpp
*/
uint32_t cp = utf8::internal::mask16(*it);
// Take care of surrogate pairs first
if (utf8::internal::is_lead_surrogate(cp)) {
uint32_t trail_surrogate = utf8::internal::mask16(*(it + 1));
cp = (cp << 10) + trail_surrogate + utf8::internal::SURROGATE_OFFSET;
}
return cp;
}
uint32_t UTF16CharsetConverter::next(const char*& it, const char* end) const {
/*
* Borrowed from utfcpp
*/
uint32_t cp = utf8::internal::mask16(*it++);
// Take care of surrogate pairs first
if (utf8::internal::is_lead_surrogate(cp)) {
uint32_t trail_surrogate = utf8::internal::mask16(*it++);
cp = (cp << 10) + trail_surrogate + utf8::internal::SURROGATE_OFFSET;
}
return cp;
}
static void appendWCharToString(uint16_t wchar, string& result) {
uint8_t b[2] = {
static_cast<uint8_t>(wchar & 0x00FF),
static_cast<uint8_t>((wchar & 0xFF00) >> 8)};
char* chars = reinterpret_cast<char*>(b);
result.push_back(chars[0]);
result.push_back(chars[1]);
}
void UTF16CharsetConverter::append(uint32_t cp, string& result) const {
/*
* borrowed from utfcpp
*/
if (cp > 0xffff) { //make a surrogate pair
uint16_t wchar1 = static_cast<uint16_t> ((cp >> 10) + utf8::internal::LEAD_OFFSET);
uint16_t wchar2 = static_cast<uint16_t> ((cp & 0x3ff) + utf8::internal::TRAIL_SURROGATE_MIN);
appendWCharToString(wchar1, result);
appendWCharToString(wchar2, result);
} else {
uint16_t wchar = static_cast<uint16_t> (cp);
appendWCharToString(wchar, result);
}
}