寻找与NSLog一起使用std :: wstring的最便宜的方法

时间:2011-11-26 12:22:11

标签: c++ objective-c std nslog wstring

我有一个广泛使用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);

Here(最初来自here)我发现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]);

问题就解决了。但我会放弃多平台优势......我想。

2 个答案:

答案 0 :(得分:4)

您可以转换为NSString并记录它。这是取自here的NSString类别。不知道它的效果如何,但它看起来很合理......

@interface NSString (wstring_additions)
+(NSString*) stringWithwstring:(const std::wstring&)string;
-(std::wstring) getwstring;
@end

@implementation NSString (wstring_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] autorelease];
    return result;
}

-(std::wstring) getwstring
{
    NSData* asData = [self dataUsingEncoding:kEncoding_wchar_t];
    return std::wstring((wchar_t*)[asData bytes], [asData length] / sizeof(wchar_t));
}

@end

答案 1 :(得分:1)

也许如果您使用CoreFoundation将其转换为CFStringRef,然后将其传递给CFLog(..)或将其转换为NSString *(免费桥接)并使用NSLog(...)

CFStrings在CoreFoundation中,这是一个C级API,所以应该可以从非objc land调用。

<CoreFoundation/CFString.h>包含CFStringCreateWithBytes(...)CFStringCreateWithCString(...)并传入适当的CFStringEncoding