/
normalizer.hpp
101 lines (84 loc) · 2.78 KB
/
normalizer.hpp
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
#ifndef SHAND_ENCODING_STRING_NORMALIZER_INCLUDE
#define SHAND_ENCODING_STRING_NORMALIZER_INCLUDE
// Copyright Akira Takahashi 2013
// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <stdexcept>
#include <boost/predef.h>
#if BOOST_OS_WINDOWS
#include <Windows.h>
#endif
namespace shand {
#if BOOST_OS_WINDOWS
template <class Encoding>
encoding_string<Encoding> windows_normalize(::NORM_FORM form,
const encoding_string<Encoding>& src)
{
const encoding_string<encoding::utf16> utf16 = encode<encoding::utf16>(src);
const int length = ::NormalizeString(
form,
utf16.c_str(),
utf16.raw_str().size(),
NULL,
0);
std::wstring composed(length, wchar_t());
if (::NormalizeString(
form,
utf16.c_str(),
utf16.raw_str().size(),
&composed[0],
length) > 0) {
return encode<Encoding>(encoding_string<encoding::utf16>(composed.c_str()));
}
throw std::runtime_error("conversion error");
}
#endif
template <class Encoding>
class normalizer;
template <>
class normalizer<encoding::utf8> {
public:
static encoding_string<encoding::utf8> compose(const encoding_string<encoding::utf8>& utf8)
{
if (utf8.empty())
return utf8;
#if BOOST_OS_WINDOWS
return windows_normalize(NormalizationC, utf8);
#else
throw std::runtime_error("not implemented");
#endif
}
static encoding_string<encoding::utf8> decompose(const encoding_string<encoding::utf8>& utf8)
{
if (utf8.empty())
return utf8;
#if BOOST_OS_WINDOWS
return windows_normalize(NormalizationD, utf8);
#else
throw std::runtime_error("not implemented");
#endif
}
static encoding_string<encoding::utf8> compatible_compose(const encoding_string<encoding::utf8>& utf8)
{
if (utf8.empty())
return utf8;
#if BOOST_OS_WINDOWS
return windows_normalize(NormalizationKC, utf8);
#else
throw std::runtime_error("not implemented");
#endif
}
static encoding_string<encoding::utf8> compatible_decompose(const encoding_string<encoding::utf8>& utf8)
{
if (utf8.empty())
return utf8;
#if BOOST_OS_WINDOWS
return windows_normalize(NormalizationKD, utf8);
#else
throw std::runtime_error("not implemented");
#endif
}
};
} // namespace shand
#endif // SHAND_ENCODING_STRING_NORMALIZER_INCLUDE