// 引入标准库支持
#include <locale>
#include <codecvt>
#include <string>
#include <vector>
#include <cwctype>
#include <cwchar>
// 标准库提供的判断函数
std::iswblank
源代码:
cpp
#include <iostream>
#include <unordered_set>
#include <cwchar>
// 定义所有需检测的Unicode空白字符(按Unicode码点排序)
const std::unordered_set<wchar_t> UNICODE_WHITESPACE_CHARS = {
// ASCII空白
L'\t', // 水平制表符 (U+0009)
L'\n', // 换行符 (U+000A)
L'\v', // 垂直制表符 (U+000B)
L'\f', // 换页符 (U+000C)
L'\r', // 回车符 (U+000D)
L' ', // 普通空格 (U+0020),
// 特殊排版空格
L'\u00A0', // 不换行空格 (U+00A0)
L'\u2000', // 半角空格 (U+2000)
L'\u2001', // 全角空格 (U+2001)
L'\u2002', // 窄空格 (U+2002)
L'\u2003', // 中等空格 (U+2003)
L'\u2004', // 三分空格 (U+2004)
L'\u2005', // 四分空格 (U+2005)
L'\u2006', // 六分空格 (U+2006)
L'\u2007', // 数字空格 (U+2007)
L'\u2008', // 标点空格 (U+2008)
L'\u2009', // 细空格 (U+2009)
L'\u200A', // 极细空格 (U+200A),
// 中文/日文全角空格
L'\u3000', // 表意空格 (U+3000),
// 零宽空格
L'\u200B', // 零宽空格 (U+200B)
L'\uFEFF', // BOM零宽不换行 (U+FEFF),
// 数学/特殊空格
L'\u205F', // 数学空格 (U+205F)
L'\u1680' // 欧甘空格 (U+1680)
};
// 判断是否为任何类型的空白字符
bool is_any_whitespace(wchar_t ch) {
return UNICODE_WHITESPACE_CHARS.count(ch) > 0;
}
// 扩展:判断字符串是否全为空白字符
bool is_whitespace_string(const std::wstring& str) {
for (wchar_t ch : str) {
if (!is_any_whitespace(ch)) return false;
}
return !str.empty();
}
int main() {
// 测试用例
struct TestCase {
std::wstring input;
bool expected;
};
TestCase tests[] = {
{L"Hello World", false},
{L"\t\n \u3000", true},
{L"\u200B", true},
{L"\uFEFF", true},
{L"\u205F", true},
{L"\u00A0", true},
{L"A", false},
{L"\u4E00", false} // 中文字符
};
for (const auto& test : tests) {
std::wcout << L"输入: '" << test.input << L"' -> "
<< (is_any_whitespace(test.input[0]) ? L"空白" : L"非空白") << L"\n";
}
return 0;
}