寻找将 std::wstring 与 NSLog 一起使用的最便宜的方法
我有一个广泛使用 wstring 的库。我需要使用 NSLog 输出更改和外部数据是否有一种简单的方法(不太昂贵)来使用中间函数输出 wstring。 使用 va_list 将每个 wstring 转换为 NSString 是我现在能想到的唯一方法。
编辑:更精确。我有一个多平台库。我添加了一个日志记录宏 MYLog。
编辑 我必须从 C++ 调用我的 MYLog,但此时我无法访问 Objective-C。所以问题是在调用 MYLog 之前我无法转换 std::wstring。
通过 MYLog 我希望能够使用 NSLog 或中间体,如下所示:
MYLog("Received %ls(%d) from user %ls %ls cp: %ls /nRAW:/t%ls",
&d.name, d.id, &d.user.firstName, &d.user.lastName,
&d.caption, &d.rawText);
此处(最初来自 这里)我发现对 NSString 的这个很好的补充:
@interface NSString (cppstring_additions)
+(NSString*) stringWithwstring:(const std::wstring&)string;
+(NSString*) stringWithstring:(const std::string&)string;
-(std::wstring) getwstring;
-(std::string) getstring;
@end
@implementation NSString (cppstring_additions)
#if TARGET_RT_BIG_ENDIAN
const NSStringEncoding kEncoding_wchar_t = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32BE);
#else
const NSStringEncoding kEncoding_wchar_t = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32LE);
#endif
+(NSString*) stringWithwstring:(const std::wstring&)ws
{
char* data = (char*)ws.data();
unsigned size = ws.size() * sizeof(wchar_t);
NSString* result = [[NSString alloc] initWithBytes:data length:size encoding:kEncoding_wchar_t];
return result;
}
+(NSString*) stringWithstring:(const std::string&)s
{
NSString* result = [[NSString alloc] initWithUTF8String:s.c_str()];
return result;
}
-(std::wstring) getwstring
{
NSData* asData = [self dataUsingEncoding:kEncoding_wchar_t];
return std::wstring((wchar_t*)[asData bytes], [asData length] / sizeof(wchar_t));
}
-(std::string) getstring
{
return [self UTF8String];
}
@end
在不浪费太多时间(我的)的情况下,我能想到的就是创建一个由我的 MYLog 调用的中间函数:
#define LAD(data) [logString appendFormat:[NSString stringWithFormat:@"%%%@", c], data]
#define LAP(type) LAD(va_arg(listPointer, type))
void MyLogImplementation(NSString* message, ...)
{
NSMutableString* logString = [[NSMutableString alloc] init];
va_list listPointer;
va_start(listPointer, message);
NSArray* lmc = [message componentsSeparatedByString:@"%"];
int counter = 0;
//NSLog(@"there are %d components in %@.", [lmc count], message);
BOOL ignoreNext = NO;
for (NSString* c in lmc)
{
//NSLog(@"Testing %@", c);
if (ignoreNext)
{
[logString appendFormat:@"%%%@", c];
ignoreNext = NO;
}
if (0 == [c length])
{
ignoreNext = !ignoreNext;
}
else if (0 == counter && '%' != [message characterAtIndex:0] )
{
[logString appendFormat:@"%@", c];
}
else
{
switch ([c characterAtIndex:0])
{
case 'd':
case 'i':
LAP(int);
break;
case 'X':
case 'x':
LAP(int);
break;
case '@':
LAP(NSObject*);
break;
case 'f':
LAP(double);
break;
case 'c':
LAP(char);
break;
case 'l':
switch ([c characterAtIndex:1])
{
case 's':
{
std::wstring* str = va_arg(listPointer, std::wstring*);
NSString* nstr = [NSString stringWithwstring:str];
[logString appendFormat:@"%@", nstr];
[nstr release];
if (2 < [c length])
{
[logString appendString:[c substringFromIndex:2]];
}
}
break;
}
break;
default:
[logString appendFormat:[NSString stringWithFormat:@"%%%@", c]];
}
}
++counter;
}
NSLog(@"%@", logString);
[logString release];
va_end(listPointer);
}
但这 - 在我看来 - 效率非常低。有更好的方法来实现我正在做的事情吗?我知道我可以简单地将其转换为:
NSLog(@"Received %@(%d) from user %@ %@ cp: %@ /nRAW:/t%@",
[NSString stringWithwstring:d.name], d.id,
[NSString stringWithwstring:d.user.firstName],
[NSString stringWithwstring:d.user.lastName],
[NSString stringWithwstring:d.caption],
[NSString stringWithwstring:d.rawText]);
问题就会得到解决。但我想我会失去多平台优势......
I have a library which uses wstring extensively. I need to output changes and external data using NSLog Is there a simple way (not too expensive) to output the wstring using an intermediate function.
Converting each wstring to NSString using va_list is the only way I can think of right now.
Edit: More precisions. I have a multi platform lib. I added a logging macro MYLog.
Edit
I have to call my MYLog from C++ and I don't have access to Objective-C at that point. So the problem is that I can't convert the std::wstring before I do the call to MYLog.
Through MYLog I would like to be able to use NSLog or an intermediate like follows:
MYLog("Received %ls(%d) from user %ls %ls cp: %ls /nRAW:/t%ls",
&d.name, d.id, &d.user.firstName, &d.user.lastName,
&d.caption, &d.rawText);
Here (originally from here) I found this nice addition to NSString:
@interface NSString (cppstring_additions)
+(NSString*) stringWithwstring:(const std::wstring&)string;
+(NSString*) stringWithstring:(const std::string&)string;
-(std::wstring) getwstring;
-(std::string) getstring;
@end
@implementation NSString (cppstring_additions)
#if TARGET_RT_BIG_ENDIAN
const NSStringEncoding kEncoding_wchar_t = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32BE);
#else
const NSStringEncoding kEncoding_wchar_t = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32LE);
#endif
+(NSString*) stringWithwstring:(const std::wstring&)ws
{
char* data = (char*)ws.data();
unsigned size = ws.size() * sizeof(wchar_t);
NSString* result = [[NSString alloc] initWithBytes:data length:size encoding:kEncoding_wchar_t];
return result;
}
+(NSString*) stringWithstring:(const std::string&)s
{
NSString* result = [[NSString alloc] initWithUTF8String:s.c_str()];
return result;
}
-(std::wstring) getwstring
{
NSData* asData = [self dataUsingEncoding:kEncoding_wchar_t];
return std::wstring((wchar_t*)[asData bytes], [asData length] / sizeof(wchar_t));
}
-(std::string) getstring
{
return [self UTF8String];
}
@end
All I could think of without wasting too much time (of mine) was to create an intermediate function called by my MYLog:
#define LAD(data) [logString appendFormat:[NSString stringWithFormat:@"%%%@", c], data]
#define LAP(type) LAD(va_arg(listPointer, type))
void MyLogImplementation(NSString* message, ...)
{
NSMutableString* logString = [[NSMutableString alloc] init];
va_list listPointer;
va_start(listPointer, message);
NSArray* lmc = [message componentsSeparatedByString:@"%"];
int counter = 0;
//NSLog(@"there are %d components in %@.", [lmc count], message);
BOOL ignoreNext = NO;
for (NSString* c in lmc)
{
//NSLog(@"Testing %@", c);
if (ignoreNext)
{
[logString appendFormat:@"%%%@", c];
ignoreNext = NO;
}
if (0 == [c length])
{
ignoreNext = !ignoreNext;
}
else if (0 == counter && '%' != [message characterAtIndex:0] )
{
[logString appendFormat:@"%@", c];
}
else
{
switch ([c characterAtIndex:0])
{
case 'd':
case 'i':
LAP(int);
break;
case 'X':
case 'x':
LAP(int);
break;
case '@':
LAP(NSObject*);
break;
case 'f':
LAP(double);
break;
case 'c':
LAP(char);
break;
case 'l':
switch ([c characterAtIndex:1])
{
case 's':
{
std::wstring* str = va_arg(listPointer, std::wstring*);
NSString* nstr = [NSString stringWithwstring:str];
[logString appendFormat:@"%@", nstr];
[nstr release];
if (2 < [c length])
{
[logString appendString:[c substringFromIndex:2]];
}
}
break;
}
break;
default:
[logString appendFormat:[NSString stringWithFormat:@"%%%@", c]];
}
}
++counter;
}
NSLog(@"%@", logString);
[logString release];
va_end(listPointer);
}
But this - it seems to me - is very inefficient. Is there a better way to achieve what i'm doing? I know I could simply transform this to:
NSLog(@"Received %@(%d) from user %@ %@ cp: %@ /nRAW:/t%@",
[NSString stringWithwstring:d.name], d.id,
[NSString stringWithwstring:d.user.firstName],
[NSString stringWithwstring:d.user.lastName],
[NSString stringWithwstring:d.caption],
[NSString stringWithwstring:d.rawText]);
And the problem would be solved. But I would loose the multiplaform advantage... I guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以转换为 NSString 并记录它。这是一个 NSString 类别,取自此处。不知道效果如何,但看起来很合理......
You could convert to an NSString and log that. This is a NSString category taken from here. Don't know how well it works but it looks sensible...
也许如果您使用 CoreFoundation 将其转换为 CFStringRef,然后将其传递给 CFLog(..) 或将其转换为 NSString* (免费桥接)并使用 NSLog(...)
CFString 位于 CoreFoundation 中,这是一个 C 级 API所以应该可以从非 objc 土地调用。
包含CFStringCreateWithBytes(...)
或CFStringCreateWithCString(...)
并传入适当的 <代码>CFStringEncodingPerhaps if you convert it to a CFStringRef using CoreFoundation and then pass that to either CFLog(..) or cast it to NSString* (toll free bridged) and use NSLog(...)
CFStrings are in CoreFoundation which is a C level API and so should be callable from non objc land.
<CoreFoundation/CFString.h>
containsCFStringCreateWithBytes(...)
or perhapsCFStringCreateWithCString(...)
and passing in the appropriateCFStringEncoding