【发布时间】:2019-06-23 13:26:19
【问题描述】:
我需要将 UTF-8 编码的字符串截断为不超过预定义的字节大小。特定协议还要求,截断的字符串仍然形成有效的 UTF-8 编码,即不必拆分多字节序列。
鉴于structure of the UTF-8 encoding,我可以继续前进,计算每个代码点的编码大小,直到达到最大字节数。不过,O(n) 并不是很吸引人。是否有一种算法可以更快地完成,最好在(摊销的)O(1) 时间内完成?
【问题讨论】:
我需要将 UTF-8 编码的字符串截断为不超过预定义的字节大小。特定协议还要求,截断的字符串仍然形成有效的 UTF-8 编码,即不必拆分多字节序列。
鉴于structure of the UTF-8 encoding,我可以继续前进,计算每个代码点的编码大小,直到达到最大字节数。不过,O(n) 并不是很吸引人。是否有一种算法可以更快地完成,最好在(摊销的)O(1) 时间内完成?
【问题讨论】:
2019 年 6 月 24 日更新: 睡了一夜之后,问题似乎比我第一次尝试看起来要容易得多。由于历史原因,我在下面留下了之前的答案。
UTF-8 编码为self-synchronizing。这使得可以确定符号流中任意选择的代码单元是否是代码序列的开始。 UTF-8 序列可以拆分到代码序列开头的左侧。
代码序列的开头可以是 ASCII 字符 (0xxxxxxxb),也可以是多字节序列中的前导字节 (11xxxxxxb)。尾随字节遵循模式10xxxxxxb。 UTF-8 编码的开头满足条件(code_unit & 0b11000000) != 0b10000000,换句话说:它不是尾随字节。
不超过请求字节数的最长 UTF-8 序列可以通过应用以下算法在恒定时间 (O(1)) 内确定:
输入代码:
#include <string_view>
size_t find_max_utf8_length(std::string_view sv, size_t max_byte_count)
{
// 1. Input no longer than max byte count
if (sv.size() <= max_byte_count)
{
return sv.size();
}
// 2. Input longer than max byte count
while ((sv[max_byte_count] & 0b11000000) == 0b10000000)
{
--max_byte_count;
}
return max_byte_count;
}
#include <iostream>
#include <iomanip>
#include <string_view>
#include <string>
int main()
{
using namespace std::literals::string_view_literals;
std::cout << "max size output\n=== ==== ======" << std::endl;
auto test{u8"€«test»"sv};
for (size_t count{0}; count <= test.size(); ++count)
{
auto byte_count{find_max_utf8_length(test, count)};
std::cout << std::setw(3) << std::setfill(' ') << count
<< std::setw(5) << std::setfill(' ') << byte_count
<< " " << std::string(begin(test), byte_count) << std::endl;
}
}
产生以下输出:
max size output === ==== ====== 0 0 1 0 2 0 3 3 € 4 3 € 5 5 €« 6 6 €«t 7 7 €«te 8 8 €«tes 9 9 €«test 10 9 €«test 11 11 €«test»
此算法仅在 UTF-8 编码上运行。它不会尝试以任何方式处理 Unicode。虽然它总是会产生一个有效的 UTF-8 编码序列,但编码的代码点可能不会形成有意义的 Unicode 字形。
算法在恒定时间内完成。考虑到每个 UTF-8 编码最多 4 个字节的当前限制,无论输入大小如何,最终循环最多旋转 3 次。该算法将继续工作并在恒定时间内完成,以防 UTF-8 编码被更改为每个编码代码点最多允许 5 或 6 个字节。
上一个答案
这可以在 O(1) 中完成,方法是将问题分解为以下几种情况:
max_byte_count - 1 处找出编码中的相对位置:
0xxxxxxxb),我们处于自然边界,可以在其后截断字符串。0xxxxxxxb) 或多字节序列的开头 (11xxxxxxb),则我们位于多字节序列的尾部,即自然边界。11xxxxxxb)。剪切该字符之前的字符串。以下代码在给定最大字节数的情况下计算截断字符串的长度。输入需要形成有效的 UTF-8 编码。
#include <string_view>
size_t find_max_utf8_length(std::string_view sv, size_t max_byte_count)
{
// 1. No longer than max byte count
if (sv.size() <= max_byte_count)
{
return sv.size();
}
// 2. Longer than byte count
auto c0{static_cast<unsigned char>(sv[max_byte_count - 1])};
if ((c0 & 0b10000000) == 0)
{
// 2.1 ASCII
return max_byte_count;
}
auto c1{static_cast<unsigned char>(sv[max_byte_count])};
if (((c1 & 0b10000000) == 0) || ((c1 & 0b11000000) == 0b11000000))
{
// 2.2. At end of multi-byte sequence
return max_byte_count;
}
// 2.3. At start or middle of multi-byte sequence
unsigned char c{};
do
{
--max_byte_count;
c = static_cast<unsigned char>(sv[max_byte_count]);
} while ((c & 0b11000000) != 0b11000000);
return max_byte_count;
}
以下测试代码
#include <iostream>
#include <iomanip>
#include <string_view>
#include <string>
int main()
{
using namespace std::literals::string_view_literals;
std::cout << "max size output\n=== ==== ======" << std::endl;
auto test{u8"€«test»"sv};
for (size_t count{0}; count <= test.size(); ++count)
{
auto byte_count{find_max_utf8_length(test, count)};
std::cout << std::setw(3) << std::setfill(' ') << count
<< std::setw(5) << std::setfill(' ') << byte_count
<< " " << std::string(begin(test), byte_count) << std::endl;
}
}
产生this output:
max size output === ==== ====== 0 0 1 0 2 0 3 3 € 4 3 € 5 5 €« 6 6 €«t 7 7 €«te 8 8 €«tes 9 9 €«test 10 9 €«test 11 11 €«test»
【讨论】: