相关疑难解决方法(0)

在Active Record Callback中验证日语字符

我有一个需要验证半宽和全宽日文字符的日文项目,半宽度允许14个字符,全宽度允许7个字符.

有没有人知道如何实现它?

现在我的模特

class Customer
   validates_length_of :name, :maximum => 14
end
Run Code Online (Sandbox Code Playgroud)

不是一个好选择

我目前正在使用ror 2.3.5可以使用全宽和半宽

ruby validation ruby-on-rails cjk rails-activerecord

6
推荐指数
2
解决办法
2513
查看次数

如何检测终端中的unicode字符串宽度?

我正在开发一个基于终端的程序,它具有unicode支持.在某些情况下,我需要确定字符串在打印之前将消耗多少个终端列.不幸的是,有些字符是2列宽(中文等),但我发现这个答案表明检测全角字符的好方法是从ICU库调用u_getIntPropertyValue().

现在我正在尝试解析我的UTF8字符串的字符并将它们传递给此函数.我现在遇到的问题是u_getIntPropertyValue()需要一个UTF-32代码点.

从utf8字符串获取此信息的最佳方法是什么?我目前正在尝试使用boost :: locale(在我的程序中的其他位置使用),但是我无法获得干净的转换.来自boost :: locale的我的UTF32字符串预先设置了零宽度字符以指示字节顺序.显然我可以跳过字符串的前四个字节,但有更简洁的方法吗?

这是我目前丑陋的解决方案:

inline size_t utf8PrintableSize(const std::string &str, std::locale loc)
{
    namespace ba = boost::locale::boundary;
    ba::ssegment_index map(ba::character, str.begin(), str.end(), loc);
    size_t widthCount = 0;
    for (ba::ssegment_index::iterator it = map.begin(); it != map.end(); ++it)
    {
        ++widthCount;
        std::string utf32Char = boost::locale::conv::from_utf(it->str(), std::string("utf-32"));

        UChar32 utf32Codepoint = 0;
        memcpy(&utf32Codepoint, utf32Char.c_str()+4, sizeof(UChar32));

        int width = u_getIntPropertyValue(utf32Codepoint, UCHAR_EAST_ASIAN_WIDTH);
        if ((width == U_EA_FULLWIDTH) || (width == U_EA_WIDE))
        {
            ++widthCount;
        }

    }
    return widthCount;
}
Run Code Online (Sandbox Code Playgroud)

c++ linux unicode utf-8 utf-32

5
推荐指数
1
解决办法
531
查看次数