我正在寻找一种显示UTF-8字符串且其不可打印/无效字符已转义的方法。在ASCII时代,我习惯于使用isprint
决定是应原样打印字符还是对其进行转义。使用UTF-8,迭代更加困难,但是Boost.Locale可以很好地做到这一点。但是,我没有找到任何内容来确定某个字符是否可打印,甚至是否有效。
在以下源代码中,字符串"Hello あにま ➦ 👙 𝕫⊆𝕢 \x02\x01\b \xff\xff\xff "
包含一些不可打印的坏人(例如\b
),其他则是普通的无效序列(\xff\xff\xff
)。我应该执行哪种测试来确定字符是否可打印?
// Based on an example of Boost.Locale.
#include <boost/locale.hpp>
#include <iostream>
#include <iomanip>
int main()
{
using namespace boost::locale;
using namespace std;
generator gen;
std::locale loc = gen("");
locale::global(loc);
cout.imbue(loc);
string text = "Hello あにま ➦ 👙 𝕫⊆𝕢 \x02\x01\b \xff\xff\xff ";
cout << text << endl;
boundary::ssegment_index index(boundary::character, text.begin(), text.end());
for (auto p: index)
{
cout << '[' << p << '|';
for (uint8_t c: p)
cout << std::hex << std::setw(2) << std::setfill('0') << int(c);
cout << "] ";
}
cout << '\n';
}
运行时,它给出
[H|48] [e|65] [l|6c] [l|6c] [o|6f] [ |20] [あ|e38182] [に|e381ab] [ま|e381be]
[ |20] [➦|e29ea6] [ |20] [👙|f09f9199] [ |20] [𝕫|f09d95ab]
[⊆|e28a86] [𝕢|f09d95a2] [ |20] [|02] [|01] |08] [ |20] [??? |ffffff20]
我应该如何确定
[|01]
不可打印,[??? |ffffff20]
也不可打印,[o|6f]
不可打印,[👙|f09f9199]
也不可打印?粗略地讲,测试应该使我能够决定是打印[|]对的左成员,还是打印不isprint
的右成员。谢谢
最佳答案
Unicode具有每个代码点的属性,其中包括general category和技术报告,其中列出了regex classifications(字母,图形等)。 unicode print
分类包含选项卡,而std::isprint
(使用C语言环境)则不包含。 print
确实包含字母,标记,数字,标点,符号,空格和格式代码点。格式代码指向do not include CR
or LF
,但确实包括相邻字符的code points that affect the appearance。我相信这正是您想要的(选项卡除外);该规范经过精心设计以支持这些字符属性。
大多数分类函数(例如std::isprint
)一次只能获得单个标量值,因此UTF32是显而易见的编码选择。不幸的是,不能保证您的系统支持UTF32语言环境,也不能保证wchar_t
是保存所有unicode代码点所需的20位。因此,如果可以的话,我会考虑使用 boost::spirit::char_encoding::unicode
进行分类。它具有所有unicode类别的内部表,并实现了regex技术报告中列出的分类。看起来它使用的是较旧的Unicode 5.2数据库,但是提供了用于生成表的C++,并且可以将其应用于更新的文件。
多字节UTF8序列仍将需要转换为单独的代码点(UTF32),您特别提到了能够跳过无效的UTF8序列的功能。由于我是C++程序员,因此我决定不必要地向屏幕发送垃圾邮件,并实现constexpr UTF8-> UTF32函数:
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <boost/range/iterator_range.hpp>
#include <boost/spirit/home/support/char_encoding/unicode.hpp>
namespace {
struct multi_byte_info {
std::uint8_t id_mask;
std::uint8_t id_matcher;
std::uint8_t data_mask;
};
constexpr const std::uint8_t multi_byte_id_mask = 0xC0;
constexpr const std::uint8_t multi_byte_id_matcher = 0x80;
constexpr const std::uint8_t multi_byte_data_mask = 0x3F;
constexpr const std::uint8_t multi_byte_bits = 6;
constexpr const multi_byte_info multi_byte_infos[] = {
// skip 1 byte info
{0xE0, 0xC0, 0x1F},
{0xF0, 0xE0, 0x0F},
{0xF8, 0xF0, 0x07}};
constexpr const unsigned max_length =
(sizeof(multi_byte_infos) / sizeof(multi_byte_info));
constexpr const std::uint32_t overlong[] = {0x80, 0x800, 0x10000};
constexpr const std::uint32_t max_code_point = 0x10FFFF;
}
enum class extraction : std::uint8_t { success, failure };
struct extraction_attempt {
std::uint32_t code_point;
std::uint8_t bytes_processed;
extraction status;
};
template <typename Iterator>
constexpr extraction_attempt next_code_point(Iterator position,
const Iterator &end) {
static_assert(
std::is_same<typename std::iterator_traits<Iterator>::iterator_category,
std::random_access_iterator_tag>{},
"bad iterator type");
extraction_attempt result{0, 0, extraction::failure};
if (end - position) {
result.code_point = std::uint8_t(*position);
++position;
++result.bytes_processed;
if (0x7F < result.code_point) {
unsigned expected_length = 1;
for (const auto info : multi_byte_infos) {
if ((result.code_point & info.id_mask) == info.id_matcher) {
result.code_point &= info.data_mask;
break;
}
++expected_length;
}
if (max_length < expected_length || (end - position) < expected_length) {
return result;
}
for (unsigned byte = 0; byte < expected_length; ++byte) {
const std::uint8_t next_byte = *(position + byte);
if ((next_byte & multi_byte_id_mask) != multi_byte_id_matcher) {
return result;
}
result.code_point <<= multi_byte_bits;
result.code_point |= (next_byte & multi_byte_data_mask);
++result.bytes_processed;
}
if (max_code_point < result.code_point) {
return result;
}
if (overlong[expected_length - 1] > result.code_point) {
return result;
}
}
result.status = extraction::success;
} // end multi-byte processing
return result;
}
template <typename Range>
constexpr extraction_attempt next_code_point(const Range &range) {
return next_code_point(std::begin(range), std::end(range));
}
template <typename T>
boost::iterator_range<T>
next_character_bytes(const boost::iterator_range<T> &range,
const extraction_attempt result) {
return boost::make_iterator_range(range.begin(),
range.begin() + result.bytes_processed);
}
template <std::size_t Length>
constexpr bool test(const char (&range)[Length],
const extraction expected_status,
const std::uint32_t expected_code_point,
const std::uint8_t expected_bytes_processed) {
const extraction_attempt result =
next_code_point(std::begin(range), std::end(range) - 1);
switch (expected_status) {
case extraction::success:
return result.status == extraction::success &&
result.bytes_processed == expected_bytes_processed &&
result.code_point == expected_code_point;
case extraction::failure:
return result.status == extraction::failure &&
result.bytes_processed == expected_bytes_processed;
default:
return false;
}
}
int main() {
static_assert(test("F", extraction::success, 'F', 1), "");
static_assert(test("\0", extraction::success, 0, 1), "");
static_assert(test("\x7F", extraction::success, 0x7F, 1), "");
static_assert(test("\xFF\xFF", extraction::failure, 0, 1), "");
static_assert(test("\xDF", extraction::failure, 0, 1), "");
static_assert(test("\xDF\xFF", extraction::failure, 0, 1), "");
static_assert(test("\xC1\xBF", extraction::failure, 0, 2), "");
static_assert(test("\xC2\x80", extraction::success, 0x80, 2), "");
static_assert(test("\xDF\xBF", extraction::success, 0x07FF, 2), "");
static_assert(test("\xEF\xBF", extraction::failure, 0, 1), "");
static_assert(test("\xEF\xBF\xFF", extraction::failure, 0, 2), "");
static_assert(test("\xE0\x9F\xBF", extraction::failure, 0, 3), "");
static_assert(test("\xE0\xA0\x80", extraction::success, 0x800, 3), "");
static_assert(test("\xEF\xBF\xBF", extraction::success, 0xFFFF, 3), "");
static_assert(test("\xF7\xBF\xBF", extraction::failure, 0, 1), "");
static_assert(test("\xF7\xBF\xBF\xFF", extraction::failure, 0, 3), "");
static_assert(test("\xF0\x8F\xBF\xBF", extraction::failure, 0, 4), "");
static_assert(test("\xF0\x90\x80\x80", extraction::success, 0x10000, 4), "");
static_assert(test("\xF4\x8F\xBF\xBF", extraction::success, 0x10FFFF, 4), "");
static_assert(test("\xF7\xBF\xBF\xBF", extraction::failure, 0, 4), "");
static_assert(test("𝕫", extraction::success, 0x1D56B, 4), "");
constexpr const static char text[] =
"Hello あにま ➦ 👙 𝕫⊆𝕢 \x02\x01\b \xff\xff\xff ";
std::cout << text << std::endl;
auto data = boost::make_iterator_range(text);
while (!data.empty()) {
const extraction_attempt result = next_code_point(data);
switch (result.status) {
case extraction::success:
if (boost::spirit::char_encoding::unicode::isprint(result.code_point)) {
std::cout << next_character_bytes(data, result);
break;
}
default:
case extraction::failure:
std::cout << "[";
std::cout << std::hex << std::setw(2) << std::setfill('0');
for (const auto byte : next_character_bytes(data, result)) {
std::cout << int(std::uint8_t(byte));
}
std::cout << "]";
break;
}
data.advance_begin(result.bytes_processed);
}
return 0;
}
输出:
Hello あにま ➦ 👙 𝕫⊆𝕢 ���
Hello あにま ➦ 👙 𝕫⊆𝕢 [02][01][08] [ff][ff][ff] [00]
如果我的UTF8-> UTF32实现吓到您,或者您需要对用户区域设置的支持,请执行以下操作:
std::mbtoc32
boost::locale::conv
和C++ 11 std::codecvt
utf::next
(和非 throw utf8::internal::validate_next
)。这并不表示对异常的副作用(肯定有一些副作用)。
关于c++ - Boost.Locale和isprint,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26676977/