我正在寻找一种方法来显示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]
也是如此?粗略地说,测试应该允许我决定是否打印[|] -pair的左侧成员,或者不是isprint
时打印右侧成员。
由于
答案 0 :(得分:3)
Unicode具有每个代码点的属性,其中包括general category,技术报告列出regex classifications(alpha,图形等)。 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类别的内部表,并实现了正则表达式技术报告中列出的分类。看起来它使用较旧的Unicode 5.2数据库,但提供了用于生成表的C ++,并且可以应用于较新的文件。
多字节UTF8序列仍然需要转换为单个代码点(UTF32),并且您特别提到了跳过无效UTF8序列的能力。由于我是一名C ++程序员,我决定不必要地对你的屏幕进行垃圾邮件,并实现一个constexpr UTF8-&gt; 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-&gt; UTF32实施让您感到害怕,或者您需要支持用户区域设置:
std::mbtoc32
boost::locale::conv
和C ++ 11 std::codecvt
utf::next
(和非投掷utf8::internal::validate_next
)。
it:对指向UTF-8编码代码点开头的迭代器的引用。函数返回后,它会递增以指向下一个代码点的开头。
并不表示对异常的副作用(肯定有一些)。