为什么提升区域设置不提供字符级别规则类型?
Why boost locale didn't provide character level rule type?
Env: boost1.53.0 c++11;
C++ 的新手。
在提升区域设置边界分析中,为 word(例如。boundary::word_letter
, boundary::word_number
) 和句子,但没有字符的边界规则类型。我想要的只是像isUpperCase(), isLowerCase(), isDigit(), isPunctuation()
这样的东西.
尝试了提升字符串算法,但不起作用。
boost::locale::generator gen;
std::locale loc = gen("ru_RU.UTF-8");
std::string context = "ДВ";
std::cout << boost::algorithm::all(context, boost::algorithm::is_upper(loc));
为什么这些功能可以在Java或Python中轻松访问,但在C++中却如此混乱?有什么办法可以实现这些目标吗?
这在VS 2013下对我有用。
locale::global(locale("ru-RU"));
std::string context = "ДВ";
std::cout << any_of(context.begin(), context.end(), boost::algorithm::is_upper());
打印1
如何初始化区域设置非常重要。
更新:
这是可以在 Ubuntu 下工作的解决方案。
#include <iostream>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/locale.hpp>
using namespace std;
int main()
{
locale::global(locale("ru_RU"));
wstring context = L"ДВ";
wcout << boolalpha << any_of(context.begin(), context.end(), boost::algorithm::is_upper());
wcout<<endl;
wstring context1 = L"ПРИВЕТ, МИР"; //HELLO WORLD in russian
wcout << boolalpha << any_of(context1.begin(), context1.end(), boost::algorithm::is_upper());
wcout<<endl;
wstring context2 = L"привет мир"; //hello world in russian
wcout << boolalpha << any_of(context2.begin(), context2.end(), boost::algorithm::is_upper());
return 0;
}
指纹
true
true
false
这也适用于boost::algorithm::all。
wstring context = L"ДВ";
wcout << boolalpha << boost::algorithm::all(context, boost::algorithm::is_upper());
Boost.locale 基于 ICU,ICU 本身确实提供了字符级别分类,这看起来非常简单易读(更多是 Java 风格)。
下面是一个简单的示例。
#include <unicode/brkiter.h>
#include <unicode/utypes.h>
#include <unicode/uchar.h>
int main()
{
UnicodeString s("А аБ Д д2 -");
UErrorCode status = U_ERROR_WARNING_LIMIT;
Locale ru("ru", "RU");
BreakIterator* bi = BreakIterator::createCharacterInstance(ru, status);
bi->setText(s);
int32_t p = bi->first();
while(p != BreakIterator::DONE) {
std::string type;
if(u_isUUppercase(s.charAt(p)))
type = "upper" ;
if(u_isULowercase(s.charAt(p)))
type = "lower" ;
if(u_isUWhiteSpace(s.charAt(p)))
type = "whitespace" ;
if(u_isdigit(s.charAt(p)))
type = "digit" ;
if(u_ispunct(s.charAt(p)))
type = "punc" ;
printf("Boundary at position %d is %sn", p, type.c_str());
p= bi->next();
}
delete bi;
return 0;
}
相关文章:
- C++中的严格别名规则和类型别名
- 使用C++11标准的哪些规则来确定({..})中表达式的类型
- 如何在不违反类型别名规则的情况下解释消息负载?
- 强制转换为不相关的引用类型是否违反严格的别名规则?
- POD 类型的二进制 I/O 如何不违反别名规则
- 在 Objective-C++ 中应用于__weak指针时,通过关键字推导类型"auto"规则是什么?
- 有关不完整类型的Sfinae的特殊规则
- 精神X3,如何获得属性类型以匹配规则类型
- C++20 中的严格别名规则是否允许标准 c++ unicode 字符和下划线类型之间"reinterpret
- 初始化中的模板转换运算符类型推导规则是什么?
- 将临时值存储为某种数据类型时,算术运算的标准规则是什么
- MISRA C++规则 14-5-1:在与类型关联的命名空间中声明的泛型函数模板的名称
- 本地类规则是否与c++14返回类型推导一致
- 将字符阵列施放到另一种类型中是否违反了严格的确定规则
- 要求将各种类型的各种类型传递给微控制器上的规则引擎.施放问题
- 隐式数值类型转换的规则
- 在makefile中更改构建规则以构建多种文件类型
- 用删除类型的指针的stof()/stoi()将打破严格的确定规则
- C++函数类型模板参数推导规则
- 为什么提升区域设置不提供字符级别规则类型?