- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在寻找一种方法来显示 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 类别的内部表,并实现了正则表达式技术报告中列出的分类。看起来它使用的是较旧的 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
(和非抛出 utf8::internal::validate_next
)。
it: a reference to an iterator pointing to the beginning of an UTF-8 encoded code point. After the function returns, it is incremented to point to the beginning of the next code point.
这并不表示对异常的副作用(肯定有一些)。
关于c++ - Boost.Locale 和 isprint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26676977/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!