libcruft-util/cruft/util/utf8.cpp

157 lines
4.0 KiB
C++
Raw Permalink Normal View History

2017-10-02 15:25:59 +11:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2017-10-02 15:25:59 +11:00
*
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
*/
#include "./utf8.hpp"
///////////////////////////////////////////////////////////////////////////////
template <typename T>
struct test {
constexpr
test (T _mask, T _bits) noexcept:
2017-10-02 15:25:59 +11:00
mask (_mask),
bits (_bits)
2017-10-02 15:25:59 +11:00
{ ; }
constexpr bool
valid (T t) const noexcept
2017-10-02 15:25:59 +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;
T bits;
2017-10-02 15:25:59 +11:00
};
//-----------------------------------------------------------------------------
static constexpr test<uint32_t>
operator""_test (const char *str, size_t len)
2017-10-02 15:25:59 +11:00
{
uint32_t mask = 0;
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];
mask <<= 1;
bits <<= 1;
2017-10-02 15:25:59 +11:00
switch (c) {
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");
}
}
return { mask, bits };
2017-10-02 15:25:59 +11:00
}
///////////////////////////////////////////////////////////////////////////////
template <
typename InputT,
typename OutputT>
static OutputT
decode (cruft::view<InputT> src, OutputT dst)
2017-10-02 15:25:59 +11:00
{
using namespace cruft::utf8;
2017-10-02 15:25:59 +11:00
static constexpr
test<codepoint_t> PREFIX[] = {
2017-10-02 15:25:59 +11:00
"0b0xxxxxxx"_test,
"0b110xxxxx"_test,
"0b1110xxxx"_test,
"0b11110xxx"_test
};
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
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) {
*dst++ = c;
2017-10-02 15:25:59 +11:00
continue;
}
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;
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 {};
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 {};
*dst++ = accum;
2017-10-02 15:25:59 +11:00
}
return dst;
}
///////////////////////////////////////////////////////////////////////////////
std::vector<cruft::utf8::codepoint_t>
cruft::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;
}