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
|
// license:BSD-3-Clause
// copyright-holders:Nathan Woods
/***************************************************************************
charconv.cpp
Imgtool character set conversion routines.
***************************************************************************/
#include "charconv.h"
#include "corestr.h"
#include <algorithm>
imgtool::simple_charconverter imgtool::charconverter_iso_8859_1(nullptr, nullptr);
//-------------------------------------------------
// simple_charconverter::simple_charconverter
//-------------------------------------------------
imgtool::simple_charconverter::simple_charconverter(const char32_t lowpage[0x80], const char32_t highpage[0x80], unicode_normalization_form norm)
: m_norm(norm), m_lowpage(lowpage), m_highpage(highpage)
{
// build the reverse lookup table
for (int i = 0; i < 256; i++)
{
const char32_t *page = i >= 128 ? m_highpage : m_lowpage;
char32_t unicode_char = page ? page[i % 128] : i;
m_reverse_lookup.emplace_back(unicode_char, (char)i);
}
// and sort it
std::sort(m_reverse_lookup.begin(), m_reverse_lookup.end(), [](const std::pair<char32_t, char> &a, const std::pair<char32_t, char> &b)
{
return b.first > a.first;
});
}
//-------------------------------------------------
// from_utf8
//-------------------------------------------------
void imgtool::simple_charconverter::from_utf8(std::ostream &dest, std::string_view src) const
{
// normalize the incoming unicode
std::string const normalized_src = normalize_unicode(src, m_norm);
auto nsrc = std::string_view(normalized_src);
while (!nsrc.empty())
{
// get the next character
char32_t ch;
int rc = uchar_from_utf8(&ch, nsrc);
if (rc < 0)
{
ch = 0xFFFD;
rc = 1;
}
nsrc.remove_prefix(rc);
// do the reverse lookup
auto lookup = std::lower_bound(m_reverse_lookup.begin(), m_reverse_lookup.end(), ch, [](const std::pair<char32_t, char> &a, const char32_t &b)
{
return a.first < b;
});
if (lookup == m_reverse_lookup.end())
throw charconverter_exception();
// and output the results
dest << lookup->second;
}
}
//-------------------------------------------------
// to_utf8
//-------------------------------------------------
void imgtool::simple_charconverter::to_utf8(std::ostream &dest, std::string_view src) const
{
for (uint8_t c : src)
{
// which page is this in?
const char32_t *page = ((c & 0x80) == 0) ? m_lowpage : m_highpage;
// is this page present?
if ((c & 0x80) == 0)
{
// no - pass it on
dest << c;
}
else
{
// yes - we need to do a lookup
size_t base = ((c & 0x80) == 0) ? 0x00 : 0x80;
char32_t ch = page[((unsigned char)(c)) - base];
if (ch == 0)
throw charconverter_exception();
dest << utf8_from_uchar(ch);
}
}
}
|