forked from m2osw/csspp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicode_range.cpp
More file actions
188 lines (159 loc) · 4.82 KB
/
unicode_range.cpp
File metadata and controls
188 lines (159 loc) · 4.82 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// CSS Preprocessor
// Copyright (c) 2015-2019 Made to Order Software Corp. All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "csspp/unicode_range.h"
#include "csspp/exceptions.h"
#include <iomanip>
#include <iostream>
#include <sstream>
namespace csspp
{
namespace
{
wide_char_t constexpr range_to_start(range_value_t const range)
{
return range & 0xFFFFFFFFLL;
}
wide_char_t constexpr range_to_end(range_value_t const range)
{
return (range >> 32) & 0xFFFFFFFFLL;
}
range_value_t constexpr start_end_to_range(wide_char_t start, wide_char_t end)
{
return (static_cast<range_value_t>(end) << 32) | static_cast<range_value_t>(start);
}
void verify_range(range_value_t const range)
{
// verify maximum
wide_char_t const start(range_to_start(range));
if(start >= 0x110000)
{
std::stringstream ss;
ss << "the start parameter is limited to a maximum of 0x10FFFF, it is 0x" << std::hex << start << " now.";
throw csspp_exception_overflow(ss.str());
}
// verify minimum
wide_char_t const end(range_to_end(range));
if(end >= 0x200000) // special end to support all possible masks
{
std::stringstream ss;
ss << "the end parameter is limited to a maximum of 0x1FFFFF, it is 0x" << std::hex << end << " now.";
throw csspp_exception_overflow(ss.str());
}
// must always be properly ordered
if(start > end)
{
std::stringstream ss;
ss << "the start parameter (" << std::hex << start << ") cannot be larged than the end parameter (" << end << ") in a unicode range.";
throw csspp_exception_overflow(ss.str());
}
}
} // no name namespace
unicode_range_t::unicode_range_t(range_value_t range)
: f_range(range)
{
verify_range(f_range);
}
unicode_range_t::unicode_range_t(wide_char_t start, wide_char_t end)
: f_range(start_end_to_range(start, end))
{
verify_range(f_range);
}
void unicode_range_t::set_range(range_value_t range)
{
verify_range(range);
f_range = range;
}
void unicode_range_t::set_range(wide_char_t start, wide_char_t end)
{
range_value_t const range(start_end_to_range(start, end));
verify_range(range);
f_range = range;
}
range_value_t unicode_range_t::get_range() const
{
return f_range;
}
wide_char_t unicode_range_t::get_start() const
{
return range_to_start(f_range);
}
wide_char_t unicode_range_t::get_end() const
{
return range_to_end(f_range);
}
std::string unicode_range_t::to_string() const
{
wide_char_t start(range_to_start(f_range));
wide_char_t end(range_to_end(f_range));
// a special case for the 6 x '?' mask
if(start == 0 && end >= 0x10FFFF)
{
return "??????";
}
std::stringstream ss;
ss << std::hex << start;
// if start and end are equal
if(start == end)
{
// if equal, we return that one number
return ss.str();
}
// check whether we can use the question mark trick
{
std::stringstream filled;
filled << std::hex << std::setw(6) << std::setfill('0') << start;
std::string const start_str(filled.str());
filled.str("");
filled << std::setw(6) << end;
std::string const end_str(filled.str());
if(start_str.length() != 6 || end_str.length() != 6)
{
throw csspp_exception_logic("unexpected string length"); // LCOV_EXCL_LINE
}
size_t p(6);
for(; p > 0; --p)
{
if(start_str[p - 1] != '0' || end_str[p - 1] != 'f')
{
break;
}
}
std::string result(start_str.substr(0, p));
if(result == end_str.substr(0, p))
{
// we can use a plain ??? mask
result += std::string("??????", 6 - p);
// remove leading zeroes
while(result.front() == '0')
{
result.erase(result.begin());
}
return result;
}
}
// no question mark, just append the end
ss << "-" << end;
return ss.str();
}
} // namespace csspp
// Local Variables:
// mode: cpp
// indent-tabs-mode: nil
// c-basic-offset: 4
// tab-width: 4
// End:
// vim: ts=4 sw=4 et