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

发布于 2024-12-18 12:10:45 字数 4668 浏览 5 评论 0原文

我有一个广泛使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2024-12-25 12:10:45

您可以转换为 NSString 并记录它。这是一个 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

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...

@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
星星的軌跡 2024-12-25 12:10:45

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

CFString 位于 CoreFoundation 中,这是一个 C 级 API所以应该可以从非 objc 土地调用。

包含 CFStringCreateWithBytes(...)CFStringCreateWithCString(...) 并传入适当的 <代码>CFStringEncoding

Perhaps 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> contains CFStringCreateWithBytes(...) or perhaps CFStringCreateWithCString(...) and passing in the appropriate CFStringEncoding

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文