为了提高duilib创建布局控件的效率,在LuaDui项目中使用rapidxml解析器替换了duilib库自带的xml解析器。

duilib使用unicode编译,所以rapidxml需要解析unicode xml字符串。

  使用rapidxml解析unicode字符串很简单,只需在rapidxml的模板参数中设置为TCHAR即可,所以定义以下类型方便使用。

#include <rapidxml/rapidxml.hpp>
typedef rapidxml::xml_document<TCHAR> XmlDoc;
typedef rapidxml::xml_node<TCHAR> XmlNode;
typedef rapidxml::xml_attribute<TCHAR> XmlAttr;

  在使用过程中发现了解析xml中的中文字符出现bug,解析如下xml会出现问题抛出异常。

<?xml version="1.0" encoding="UTF-8"?>
<Window caption="0,0,0,30" sizebox="5,5,5,5" mininfo="480,360" defaultfontcolor="#ff010000" width="600" height="480">
<Font name="微软雅黑" size="12" bold="false"/><VerticalLayout bkcolor="#ff019bd0" inset="1,1,1,1" bordersize="1" bordercolor="#FF010000"><HorizontalLayout height="30" inset="5,0,0,0"><Label name="标题" text="调试窗口" textcolor="#FFFFFFFF"></Label><Control /><Button name="minbtn" width="40" height="22" text="最小化" bkcolor="#ff3fd536"><Event click="DebugUIEvent.minBtnClick" /></Button><Button name="closebtn" width="47" height="22" text="关闭" bkcolor="#ffef2f4d"><Event click="DebugUIEvent.closeBtnClick" /></Button></HorizontalLayout><VerticalLayout  bkcolor="#66ffffff"></VerticalLayout>
</VerticalLayout>
</Window>

  断点时发现在解析 text="最小化" 属性时出现问题,解析text值的时候把后面的内容全部当做text的属性值,无法再往下解析了。

最后终于找到了问题所在,rapidxml为提高解析效率,定义了如下的表:

        template<int Dummy>struct lookup_tables{static const unsigned char lookup_whitespace[256];              // Whitespace tablestatic const unsigned char lookup_node_name[256];               // Node name tablestatic const unsigned char lookup_text[256];                    // Text tablestatic const unsigned char lookup_text_pure_no_ws[256];         // Text tablestatic const unsigned char lookup_text_pure_with_ws[256];       // Text tablestatic const unsigned char lookup_attribute_name[256];          // Attribute name tablestatic const unsigned char lookup_attribute_data_1[256];        // Attribute data table with single quotestatic const unsigned char lookup_attribute_data_1_pure[256];   // Attribute data table with single quotestatic const unsigned char lookup_attribute_data_2[256];        // Attribute data table with double quotesstatic const unsigned char lookup_attribute_data_2_pure[256];   // Attribute data table with double quotesstatic const unsigned char lookup_digits[256];                  // Digitsstatic const unsigned char lookup_upcase[256];                  // To uppercase conversion table for ASCII characters};

  来识别xml中的标志符,在进行查找的时候直接通过数组直接找到使用了

如下操作:

internal::lookup_tables<0>::lookup_text_pure_no_ws[static_cast<unsigned char>(ch)];

但在unicode下static_cast<unsigned char>(ch)的ch是wchar占两个字节直接转换为unsigned char会出现判断出错问题。所以要在rapidxml中解析unicode需要修改rapidxml代码:

       // Detect whitespace characterstruct whitespace_pred{static unsigned char test(Ch ch){if(ch<=255)return internal::lookup_tables<0>::lookup_whitespace[static_cast<unsigned char>(ch)];elsereturn 0;}};// Detect node name characterstruct node_name_pred{static unsigned char test(Ch ch){if(ch<=255)return internal::lookup_tables<0>::lookup_node_name[static_cast<unsigned char>(ch)];elsereturn 1;}};// Detect attribute name characterstruct attribute_name_pred{static unsigned char test(Ch ch){if(ch<=255)return internal::lookup_tables<0>::lookup_attribute_name[static_cast<unsigned char>(ch)];elsereturn 1;}};// Detect text character (PCDATA)struct text_pred{static unsigned char test(Ch ch){if(ch<=255)return internal::lookup_tables<0>::lookup_text[static_cast<unsigned char>(ch)];elsereturn 1;}};// Detect text character (PCDATA) that does not require processingstruct text_pure_no_ws_pred{static unsigned char test(Ch ch){if(ch<=255)return internal::lookup_tables<0>::lookup_text_pure_no_ws[static_cast<unsigned char>(ch)];elsereturn 1;}};// Detect text character (PCDATA) that does not require processingstruct text_pure_with_ws_pred{static unsigned char test(Ch ch){if(ch<=255)return internal::lookup_tables<0>::lookup_text_pure_with_ws[static_cast<unsigned char>(ch)];elsereturn 1;}};// Detect attribute value charactertemplate<Ch Quote>struct attribute_value_pred{static unsigned char test(Ch ch){if (Quote == Ch('\''))if(ch<=255)return internal::lookup_tables<0>::lookup_attribute_data_1[static_cast<unsigned char>(ch)];elsereturn 1;if (Quote == Ch('\"'))if(ch<=255)return internal::lookup_tables<0>::lookup_attribute_data_2[static_cast<unsigned char>(ch)];elsereturn 1;return 0;       // Should never be executed, to avoid warnings on Comeau}};// Detect attribute value charactertemplate<Ch Quote>struct attribute_value_pure_pred{static unsigned char test(Ch ch){if (Quote == Ch('\''))if(ch<=255)return internal::lookup_tables<0>::lookup_attribute_data_1_pure[static_cast<unsigned char>(ch)];elsereturn 1;if (Quote == Ch('\"'))if(ch<=255)return internal::lookup_tables<0>::lookup_attribute_data_2_pure[static_cast<unsigned char>(ch)];elsereturn 1;return 0;       // Should never be executed, to avoid warnings on Comeau}};

  

转载于:https://www.cnblogs.com/zentel/p/4162602.html

rapidxml对unicode的支持相关推荐

  1. python2 csv 中文_Python 2.6中对csv文件的常规Unicode / UTF-8支持

    泛舟湖上清波郎朗 http://docs.python.org/library/csv.html#examples提供的有关如何读取Unicode的示例代码似乎已过时,因为它不适用于Python 2. ...

  2. 在Java中中文需要用双引号吗_Java语言支持Unicode,自然支持中文,所以Java中可以使用中文的双引号、单引号、分号代替英文的双引号、单引号、分号。...

    A.Operated on patients.B.Played music to the patients.C.Cleaned the floo 试述厂商的要素使用原则. 用科学记数法记出的数7.04 ...

  3. TensorFlow支持Unicode,中文NLP终于省心了

    整理 | 非主流 出品 | AI科技大本营 终于,TensorFlow 增加了对 Unicode 的支持. 什么是 Unicode?Unicode 是计算机科学领域里的一项业界标准,包括字符集.编码方 ...

  4. TensorFlow 支持 Unicode,中文 NLP 终于省心了

    整理 | 非主流 出品 | AI科技大本营 终于,TensorFlow 增加了对 Unicode 的支持. 什么是 Unicode?Unicode 是计算机科学领域里的一项业界标准,包括字符集.编码方 ...

  5. python程序采用unicode编码、英文字符,Python 与 Unicode

    unicode 与 utf8 的关系 unicode 定义了统一的字符集, UTF8 则是一种编码 unicode 字符集的方式. 在 python2 中, str 类型, unicode 类型都是  ...

  6. 关于编码、unicode、utf-8的讨论

    2019独角兽企业重金招聘Python工程师标准>>> 很多国际化应用的让我理解了这么一个道理:Unicode是为更方便的做国际化应用设计的,而Java核心的字符是基于UNICODE ...

  7. IBM投资4000万美圆,加强对SAP®应用的支持

    IBM宣布将在未来5年内投资4000万美元,对System z 系列解决方案运行SAP®应用进行测试.实现以及提供支持,还将对现有的SAP技术中心进行改造.这项正在进行的,对位于德国和美国的SAP应用 ...

  8. python3中unicode怎么写_详解python3中ascii与Unicode使用

    这篇文章主要为大家详解python3中ascii与Unicode使用的相关资料,需要的朋友可以参考下# Auther: Aaron Fan ''' ASCII:不支持中文,1个英文占1个字节 Unic ...

  9. 【转】python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode...

    原文网址:http://blog.csdn.net/trochiluses/article/details/16825269 摘要:在进行python脚本的编写时,如果我们用python来处理网页数据 ...

最新文章

  1. SSI —— 开源的人类行为分析解决方案
  2. 日本面向未来的特定科技领域技术预见分析
  3. Java反射实践:从反射中理解class
  4. Oracle归档已满的处理办法
  5. 用Tableau画圆形网络关系图
  6. 看图轻松理解数据结构与算法系列(合并排序)
  7. .net 出现的异常
  8. java自动推断类型_Java 7的类型推断
  9. android设置控件形状,Android控件自定义形状
  10. centos7开启vnc服务_CentOS 7.2搭建VNC远程桌面服务的方法
  11. python方法调用方法_Python之方法调用
  12. json序列化时忽略属性设置
  13. mono webservice问题请教
  14. Shiro框架中有三个核心概念:Subject ,SecurityManager和Realms。
  15. c语言5的阶乘流程图_求n流程图(求n的阶乘的算法框图)
  16. java 获取已有字体,java获取本机所有可用字体
  17. Android应用签名证书(.keystore)生成
  18. Linux那些事儿 之 戏说USB(21)向左走,向右走
  19. Games101课程笔记_lecture20_color颜色
  20. python解法:【PAT520砖石争霸赛】7-2真的恭喜你(10)

热门文章

  1. OpenCV基本图形绘制之椭圆
  2. Codeforces Round #533 (Div. 2) 部分题解A~D
  3. 如何使用 GRUB 2 直接从硬盘运行 ISO 文件
  4. 【Swift学习】Swift编程之旅---析构方法(十九)
  5. OpenFileDialog获取文件名和文件路径问题
  6. 五分钟深入 Hadoop 内核
  7. Ibatis中SqlMapClientTemplate和SqlMapClient的区别
  8. 关于 TCP window size
  9. LoadRunner参数化---数据文件属性 之 数据分配方法和数据更新方法
  10. 提交注册信息到数据库中