2015-10-14 12:12:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <Common/BitHelpers.h>
|
2015-10-14 12:12:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
namespace UTF8
|
|
|
|
{
|
|
|
|
|
|
|
|
static const UInt8 CONTINUATION_OCTET_MASK = 0b11000000u;
|
|
|
|
static const UInt8 CONTINUATION_OCTET = 0b10000000u;
|
|
|
|
|
|
|
|
/// return true if `octet` binary repr starts with 10 (octet is a UTF-8 sequence continuation)
|
2015-10-15 12:54:33 +00:00
|
|
|
inline bool isContinuationOctet(const UInt8 octet)
|
2015-10-14 12:12:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return (octet & CONTINUATION_OCTET_MASK) == CONTINUATION_OCTET;
|
2015-10-14 12:12:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 03:20:48 +00:00
|
|
|
/// moves `s` backward until either first non-continuation octet or begin
|
|
|
|
inline void syncBackward(const UInt8 * & s, const UInt8 * const begin)
|
2015-10-14 12:12:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
while (isContinuationOctet(*s) && s > begin)
|
|
|
|
--s;
|
2015-10-14 12:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// moves `s` forward until either first non-continuation octet or string end is met
|
2015-10-15 12:55:34 +00:00
|
|
|
inline void syncForward(const UInt8 * & s, const UInt8 * const end)
|
2015-10-14 12:12:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
while (s < end && isContinuationOctet(*s))
|
|
|
|
++s;
|
2015-10-14 12:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// returns UTF-8 code point sequence length judging by it's first octet
|
2017-07-11 20:29:33 +00:00
|
|
|
inline size_t seqLength(const UInt8 first_octet)
|
2015-10-14 12:12:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (first_octet < 0x80u)
|
|
|
|
return 1;
|
2015-10-14 12:12:56 +00:00
|
|
|
|
2017-07-11 20:29:33 +00:00
|
|
|
const size_t bits = 8;
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto first_zero = bitScanReverse(static_cast<UInt8>(~first_octet));
|
2015-10-14 12:12:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return bits - 1 - first_zero;
|
2015-10-14 12:12:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 20:29:33 +00:00
|
|
|
inline size_t countCodePoints(const UInt8 * data, size_t size)
|
|
|
|
{
|
|
|
|
size_t res = 0;
|
|
|
|
|
2018-04-20 19:18:05 +00:00
|
|
|
/// TODO SIMD implementation looks quite simple.
|
|
|
|
for (auto end = data + size; data < end; ++data) /// Skip UTF-8 continuation bytes.
|
2017-07-11 20:29:33 +00:00
|
|
|
res += (*data <= 0x7F || *data >= 0xC0);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2015-10-14 12:12:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|