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>
|
2020-02-22 05:46:35 +00:00
|
|
|
#include <Poco/UTF8Encoding.h>
|
2015-10-14 12:12:56 +00:00
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE2__
|
2018-04-20 19:45:23 +00:00
|
|
|
#include <emmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
2020-08-06 01:59:02 +00:00
|
|
|
if (first_octet < 0x80 || first_octet >= 0xF8) /// The specs of UTF-8.
|
2017-04-01 07:20:54 +00:00
|
|
|
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:45:23 +00:00
|
|
|
const auto end = data + size;
|
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE2__
|
2018-04-20 19:45:23 +00:00
|
|
|
constexpr auto bytes_sse = sizeof(__m128i);
|
|
|
|
const auto src_end_sse = data + size / bytes_sse * bytes_sse;
|
|
|
|
|
|
|
|
const auto threshold = _mm_set1_epi8(0xBF);
|
|
|
|
|
|
|
|
for (; data < src_end_sse; data += bytes_sse)
|
|
|
|
res += __builtin_popcount(_mm_movemask_epi8(
|
|
|
|
_mm_cmpgt_epi8(_mm_loadu_si128(reinterpret_cast<const __m128i *>(data)), threshold)));
|
|
|
|
#endif
|
2017-07-11 20:29:33 +00:00
|
|
|
|
2018-04-20 19:45:23 +00:00
|
|
|
for (; data < end; ++data) /// Skip UTF-8 continuation bytes.
|
|
|
|
res += static_cast<Int8>(*data) > static_cast<Int8>(0xBF);
|
2017-07-11 20:29:33 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2015-10-14 12:12:56 +00:00
|
|
|
|
2020-02-22 05:46:35 +00:00
|
|
|
template <typename CharT, typename = std::enable_if_t<sizeof(CharT) == 1>>
|
|
|
|
int convert(const CharT * bytes)
|
|
|
|
{
|
|
|
|
static const Poco::UTF8Encoding utf8;
|
|
|
|
return utf8.convert(reinterpret_cast<const uint8_t *>(bytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CharT, typename = std::enable_if_t<sizeof(CharT) == 1>>
|
|
|
|
int convert(int ch, CharT * bytes, int length)
|
|
|
|
{
|
|
|
|
static const Poco::UTF8Encoding utf8;
|
|
|
|
return utf8.convert(ch, reinterpret_cast<uint8_t *>(bytes), length);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CharT, typename = std::enable_if_t<sizeof(CharT) == 1>>
|
|
|
|
int queryConvert(const CharT * bytes, int length)
|
|
|
|
{
|
|
|
|
static const Poco::UTF8Encoding utf8;
|
|
|
|
return utf8.queryConvert(reinterpret_cast<const uint8_t *>(bytes), length);
|
|
|
|
}
|
|
|
|
|
2018-09-29 14:14:29 +00:00
|
|
|
/// returns UTF-8 wcswidth. Invalid sequence is treated as zero width character.
|
|
|
|
/// `prefix` is used to compute the `\t` width which extends the string before
|
|
|
|
/// and include `\t` to the nearest longer length with multiple of eight.
|
|
|
|
size_t computeWidth(const UInt8 * data, size_t size, size_t prefix = 0) noexcept;
|
|
|
|
|
2020-06-02 01:20:44 +00:00
|
|
|
|
|
|
|
/** Calculate the maximum number of bytes, so that substring of this size fits in 'limit' width.
|
|
|
|
*
|
|
|
|
* For example, we have string "x你好", it has 3 code points and visible width of 5 and byte size of 7.
|
|
|
|
|
|
|
|
* Suppose we have limit = 3.
|
|
|
|
* Then we have to return 4 as maximum number of bytes
|
|
|
|
* and the truncated string will be "x你": two code points, visible width 3, byte size 4.
|
|
|
|
*
|
|
|
|
* The same result will be for limit 4, because the last character would not fit.
|
|
|
|
*/
|
2020-05-31 22:12:13 +00:00
|
|
|
size_t computeBytesBeforeWidth(const UInt8 * data, size_t size, size_t prefix, size_t limit) noexcept;
|
|
|
|
|
2015-10-14 12:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|