如何识别C ++中的RTL字符串 [英] How can I recognize RTL strings in C++

查看:362
本文介绍了如何识别C ++中的RTL字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道我的文字在打印之前的方向。

I need to know the direction of my text before printing.

我使用Unicode字符。

I'm using Unicode Characters.

我如何在C ++中做这个?

How can I do that in C++?

推荐答案

如果你不想使用ICU, unicode数据库(例如,使用python脚本)。它是一个分号分隔的文本文件,每行代表一个字符代码点。在每行中查找第五条记录 - 这是字符类。如果它是 R AL ,你有一个RTL字符,'L'是一个LTR字符。其他类是弱或中性类型(如数字),我想你会想忽略。使用该信息,您可以生成所有RTL字符的查找表,然后在C ++代码中使用它。如果你真的关心代码大小,你可以通过使用范围(而不是每个字符的条目)来最小化查找表在代码中占用的大小,因为大多数字符都在其BiDi类的块中。

If you don't want to use ICU, you can always manually parse the unicode database (.e.g., with a python script). It's a semicolon-separated text file, with each line representing a character code point. Look for the fifth record in each line - that's the character class. If it's R or AL, you have an RTL character, and 'L' is an LTR character. Other classes are weak or neutral types (like numerals), which I guess you'd want to ignore. Using that info, you can generate a lookup table of all RTL characters and then use it in your C++ code. If you really care about code size, you can minimize the size the lookup table takes in your code by using ranges (instead of an entry for each character), since most characters come in blocks of their BiDi class.

现在,定义一个名为 GetCharDirection(wchar_t ch)的函数,它返回一个枚举值(比如: Dir_LTR Dir_RTL Dir_Neutral )。

Now, define a function called GetCharDirection(wchar_t ch) which returns an enum value (say: Dir_LTR, Dir_RTL or Dir_Neutral) by checking the lookup table.

现在你可以定义一个函数 GetStringDirection(const wchar_t *),它会遍历字符串中的所有字符,直到它遇到一个不是Dir_Neutral的字符。字符串中的第一个非中性字符应设置该字符串的基本方向。或者至少这是ICU似乎工作。

Now you can define a function GetStringDirection(const wchar_t*) which runs through all characters in the string until it encounters a character which is not Dir_Neutral. This first non-neutral character in the string should set the base direction for that string. Or at least that's how ICU seems to work.

这篇关于如何识别C ++中的RTL字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆