如何在源文件中嵌入unicode字符串常量? [英] How can I embed unicode string constants in a source file?

查看:205
本文介绍了如何在源文件中嵌入unicode字符串常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一些单元测试来验证我们处理各种资源,除了正常的拉丁字母:Cyrilic,Hebrew等。使用其他字符集。

I'm writing some unit tests which are going to verify our handling of various resources that use other character sets apart from the normal latin alphabet: Cyrilic, Hebrew etc.

我遇到的问题是,我找不到一种方法来嵌入预期的测试源文件:这里是我想要做的一个例子...

The problem I have is that I cannot find a way to embed the expectations in the test source file: here's an example of what I'm trying to do...

///
/// Protected: TestGetHebrewConfigString
///  
void CPrIniFileReaderTest::TestGetHebrewConfigString()
{
    prwstring strHebrewTestFilePath = GetTestFilePath( strHebrewTestFileName );
    CPrIniFileReader prIniListReader( strHebrewTestFilePath.c_str() );
    prIniListReader.SetCurrentSection( strHebrewSubSection );	

    CPPUNIT_ASSERT( prIniListReader.GetConfigString( L"דונדארןמע" ) == L"דונהשךוק") );
}

这很简单。以前,我使用一个宏,它调用一个例程来将一个窄字符串转换为一个宽字符串(我们在应用程序中使用towstring,所以它现有的代码)

This quite simply doesnt work. Previously I worked around this using a macro which calls a routine to transform a narrow string to a wide string (we use towstring all over the place in our applications so it's existing code)

#define UNICODE_CONSTANT( CONSTANT ) towstring( CONSTANT )

wstring towstring( LPCSTR lpszValue )
{
    wostringstream os;
    os << lpszValue;
    return os.str();
}

上述测试中的断言成为:

The assertion in the test above then became:

CPPUNIT_ASSERT( prIniListReader.GetConfigString( UNICODE_CONSTANT( "דונדארןמע" ) ) == UNICODE_CONSTANT( "דונהשךוק" ) );

这在OS X上正常工作,但现在我正在移植到linux,测试都是失败的:这一切都感觉很痛苦。任何人都可以告诉我,如果他们有一个更好的解决方案这个问题?

This worked OK on OS X but now I'm porting to linux and I'm finding that the tests are all failing: it all feels rather hackish as well. Can anyone tell me if they have a nicer solution to this problem?

推荐答案

一个冗长但可移植的方式是建立你的字符串数字转义码。例如:

A tedious but portable way is to build your strings using numeric escape codes. For example:

wchar_t *string = L"דונדארןמע";

变为:

wchar_t *string = "\x05d3\x05d5\x05e0\x05d3\x05d0\x05e8\x05df\x05de\x05e2";

您必须将所有的Unicode字符转换为数字转义。

You have to convert all your Unicode characters to numeric escapes. That way your source code becomes encoding-independent.

您可以使用在线工具进行转换,例如 this one 。它输出JavaScript转义格式 \uXXXX ,因此只需搜索&用 \x 替换 \u 以获取C格式。

You can use online tools for conversion, such as this one. It outputs the JavaScript escape format \uXXXX, so just search & replace \u with \x to get the C format.

这篇关于如何在源文件中嵌入unicode字符串常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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