2017-10-02 15:25:59 +11:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "./utf8.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
struct test {
|
|
|
|
constexpr
|
2017-10-02 15:40:23 +11:00
|
|
|
test (T _mask, T _bits) noexcept:
|
2017-10-02 15:25:59 +11:00
|
|
|
mask (_mask),
|
2017-10-02 15:40:23 +11:00
|
|
|
bits (_bits)
|
2017-10-02 15:25:59 +11:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
constexpr bool
|
2017-10-02 15:40:23 +11:00
|
|
|
valid (T t) const noexcept
|
2017-10-02 15:25:59 +11:00
|
|
|
{
|
2017-10-02 15:40:23 +11:00
|
|
|
return (t & mask) == bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr T
|
|
|
|
value (T t) const noexcept
|
|
|
|
{
|
|
|
|
return t & ~mask;
|
2017-10-02 15:25:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
T mask;
|
2017-10-02 15:40:23 +11:00
|
|
|
T bits;
|
2017-10-02 15:25:59 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
static constexpr test<uint32_t>
|
|
|
|
operator"" _test (const char *str, size_t len)
|
|
|
|
{
|
|
|
|
uint32_t mask = 0;
|
2017-10-02 15:40:23 +11:00
|
|
|
uint32_t bits = 0;
|
2017-10-02 15:25:59 +11:00
|
|
|
|
|
|
|
if (str[0] != '0' || str[1] != 'b')
|
|
|
|
throw std::invalid_argument ("invalid bit test prefix");
|
|
|
|
|
|
|
|
for (size_t i = 2; i < len; ++i) {
|
|
|
|
auto c = str[i];
|
|
|
|
|
2017-10-02 15:40:23 +11:00
|
|
|
mask <<= 1;
|
|
|
|
bits <<= 1;
|
2017-10-02 15:25:59 +11:00
|
|
|
|
|
|
|
switch (c) {
|
2017-10-02 15:40:23 +11:00
|
|
|
case '0': mask |= 0x1; bits |= 0x0; break;
|
|
|
|
case '1': mask |= 0x1; bits |= 0x1; break;
|
|
|
|
case 'x': mask |= 0x0; bits |= 0x0; break;
|
2017-10-02 15:25:59 +11:00
|
|
|
default:
|
|
|
|
throw std::invalid_argument ("invalid bit test character");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:40:23 +11:00
|
|
|
return { mask, bits };
|
2017-10-02 15:25:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-10-02 16:15:19 +11:00
|
|
|
template <
|
|
|
|
typename InputT,
|
|
|
|
typename OutputT>
|
|
|
|
static OutputT
|
|
|
|
decode (util::view<InputT> src, OutputT dst)
|
2017-10-02 15:25:59 +11:00
|
|
|
{
|
2017-10-02 16:15:19 +11:00
|
|
|
using namespace util::utf8;
|
2017-10-02 15:25:59 +11:00
|
|
|
|
|
|
|
static constexpr
|
2017-10-02 16:15:19 +11:00
|
|
|
test<codepoint_t> PREFIX[] = {
|
2017-10-02 15:25:59 +11:00
|
|
|
"0b0xxxxxxx"_test,
|
|
|
|
"0b110xxxxx"_test,
|
|
|
|
"0b1110xxxx"_test,
|
|
|
|
"0b11110xxx"_test
|
|
|
|
};
|
|
|
|
|
2017-10-02 16:15:19 +11:00
|
|
|
for (auto cursor = src.cbegin (); cursor != src.cend (); ) {
|
|
|
|
codepoint_t c = std::to_integer<codepoint_t> (*cursor++);
|
2017-10-02 15:25:59 +11:00
|
|
|
|
2017-10-02 16:15:19 +11:00
|
|
|
int len = PREFIX[0].valid (c) ? 0 :
|
|
|
|
PREFIX[1].valid (c) ? 1 :
|
|
|
|
PREFIX[2].valid (c) ? 2 :
|
|
|
|
PREFIX[3].valid (c) ? 3 :
|
2017-10-02 15:25:59 +11:00
|
|
|
throw malformed_error {};
|
|
|
|
|
|
|
|
// get the simple ANSI case out of the way
|
|
|
|
if (!len) {
|
2017-10-02 16:15:19 +11:00
|
|
|
*dst++ = c;
|
2017-10-02 15:25:59 +11:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-02 16:15:19 +11:00
|
|
|
codepoint_t accum { PREFIX[len].value (c) };
|
2017-10-02 15:25:59 +11:00
|
|
|
|
2017-10-03 17:48:27 +11:00
|
|
|
// prepend each of the remaining bytes data to an accumulator
|
2017-10-02 15:25:59 +11:00
|
|
|
for (int i = 1; i <= len; ++i) {
|
2017-10-03 17:48:27 +11:00
|
|
|
static constexpr auto CONTINUATION = "0b10xxxxxx"_test;
|
2017-10-02 16:15:19 +11:00
|
|
|
if (cursor == src.cend ())
|
|
|
|
throw malformed_error {};
|
|
|
|
|
|
|
|
codepoint_t now = std::to_integer<codepoint_t> (*cursor++);
|
|
|
|
if (!CONTINUATION.valid (now))
|
2017-10-02 15:25:59 +11:00
|
|
|
throw malformed_error {};
|
|
|
|
|
2017-10-02 16:15:19 +11:00
|
|
|
accum <<= 6;
|
|
|
|
accum |= CONTINUATION.value (now);
|
2017-10-02 15:25:59 +11:00
|
|
|
}
|
|
|
|
|
2017-10-03 17:48:27 +11:00
|
|
|
// check that the codepoint is the right size by seeing if the unique
|
|
|
|
// bits present in the decoded size codepoint are actually used.
|
|
|
|
//
|
|
|
|
// these could theoretically be provided to the user, but they may be
|
|
|
|
// misused so we will throw an error instead.
|
2017-10-02 15:25:59 +11:00
|
|
|
static constexpr
|
|
|
|
codepoint_t LEVEL_MASK[] {
|
|
|
|
0b00000000'00000000'01111111,
|
|
|
|
0b00000000'00000111'10000000,
|
|
|
|
0b00000000'11111000'00000000,
|
|
|
|
0b00011111'00000000'00000000
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!(accum & LEVEL_MASK[len]))
|
|
|
|
throw overlong_error {};
|
|
|
|
|
|
|
|
// utf16 surrogates should not be present in utf8
|
|
|
|
if (accum >= 0xD800 && accum <= 0xDFFF)
|
|
|
|
throw illegal_codepoint {};
|
|
|
|
|
|
|
|
// reject the BOM
|
|
|
|
if (accum == 0xfffe || accum == 0xffff)
|
|
|
|
throw illegal_codepoint {};
|
|
|
|
|
2017-10-02 16:15:19 +11:00
|
|
|
*dst++ = accum;
|
2017-10-02 15:25:59 +11:00
|
|
|
}
|
|
|
|
|
2017-10-02 16:15:19 +11:00
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
std::vector<util::utf8::codepoint_t>
|
|
|
|
util::utf8::decode (view<const std::byte*> src)
|
|
|
|
{
|
|
|
|
std::vector<codepoint_t> dst;
|
|
|
|
dst.reserve (src.size ());
|
|
|
|
|
|
|
|
::decode (src, std::back_inserter (dst));
|
2017-10-02 15:25:59 +11:00
|
|
|
return dst;
|
|
|
|
}
|