你能将 php 函数quoted_printable_decode() 转换为基于 NSString 的 Objective-C 函数/类别方法吗?

发布于 2024-07-13 02:16:22 字数 222 浏览 6 评论 0原文

http://php.net/quoted_printable_decode 中,我找到了使用 preg_replace 来完成此操作的方法。 有人知道可以将普通 NSString 转换为 RFC 2045 第 6.7 节的代码吗?

提前致谢!

In http://php.net/quoted_printable_decode, I found ways to do it using preg_replace. Anyone who knows any code that could convert a normal NSString to something RFC 2045 section 6.7?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

强者自强 2024-07-20 02:16:22

Cocoa 上没有方法来解码带引号的可打印字符串,但您可以轻松地自己编写一些内容,例如:

@interface NSString (QuotedPrintableStrings)
+(NSString*)stringWithQuotedPrintableString:(const char *)qpString;
@end

@implementation NSString (QuotedPrintableStrings)

+(NSString*)stringWithQuotedPrintableString:(const char *)qpString
{
    const char *p = qpString;
    char *ep, *utf8_string = malloc(strlen(qpString) * sizeof(char));
    NSParameterAssert( utf8_string );
    ep = utf8_string;

    while( *p ) {
        switch( *p ) {
            case '=':
                NSAssert1( *(p + 1) != 0 && *(p + 2) != 0, @"Malformed QP String: %s", qpString);
                if( *(p + 1) != '\r' ) {
                    int i, byte[2];
                    for( i = 0; i < 2; i++ ) {
                        byte[i] = *(p + i + 1);
                        if( isdigit(byte[i]) )
                            byte[i] -= 0x30;
                        else
                            byte[i] -= 0x37;
                        NSAssert( byte[i] >= 0 && byte[i] < 16, @"bad encoded character");
                    }
                    *(ep++) = (char) (byte[0] << 4) | byte[1];
                }
                p += 3;
                continue;
            default:
                *(ep++) = *(p++);
                continue;
        }
    }
    return [[[NSString alloc] initWithBytesNoCopy:utf8_string length:strlen(utf8_string) encoding:NSUTF8StringEncoding freeWhenDone:YES] autorelease];
}

@end

There's no method on Cocoa to decode a quoted printable string, but you could easily write something yourself, like:

@interface NSString (QuotedPrintableStrings)
+(NSString*)stringWithQuotedPrintableString:(const char *)qpString;
@end

@implementation NSString (QuotedPrintableStrings)

+(NSString*)stringWithQuotedPrintableString:(const char *)qpString
{
    const char *p = qpString;
    char *ep, *utf8_string = malloc(strlen(qpString) * sizeof(char));
    NSParameterAssert( utf8_string );
    ep = utf8_string;

    while( *p ) {
        switch( *p ) {
            case '=':
                NSAssert1( *(p + 1) != 0 && *(p + 2) != 0, @"Malformed QP String: %s", qpString);
                if( *(p + 1) != '\r' ) {
                    int i, byte[2];
                    for( i = 0; i < 2; i++ ) {
                        byte[i] = *(p + i + 1);
                        if( isdigit(byte[i]) )
                            byte[i] -= 0x30;
                        else
                            byte[i] -= 0x37;
                        NSAssert( byte[i] >= 0 && byte[i] < 16, @"bad encoded character");
                    }
                    *(ep++) = (char) (byte[0] << 4) | byte[1];
                }
                p += 3;
                continue;
            default:
                *(ep++) = *(p++);
                continue;
        }
    }
    return [[[NSString alloc] initWithBytesNoCopy:utf8_string length:strlen(utf8_string) encoding:NSUTF8StringEncoding freeWhenDone:YES] autorelease];
}

@end
往事风中埋 2024-07-20 02:16:22

对于其他寻找此功能的人,Jason Coco 的 答案非常有效,但有一个重要的错误。 返回之前需要在 utf8_string 末尾添加一个空字符。 因此,在 return 语句之前添加行 *ep = '\0'; 就可以了。 另外,我将其修改为返回 NSData 对象而不是 NSString,因为解码后的字符串可能使用与 UTF-8 不同的字符编码。 像 return [NSData dataWithBytes:(char *)utf8_string length:strlen(utf8_string)]; 之类的东西效果很好。 然后调用方法可以使用适当的编码将返回的数据填充回 NSString 中。

For others looking for this functionality, Jason Coco's answer works really well, but has one important bug. You need to add a null character to the end of the utf8_string before returning. So just before the return statement, add the line *ep = '\0'; and that should do the trick. Also, I modified it to return an NSData object rather than an NSString, since the decoded string may use a different character encoding than UTF-8. Something like return [NSData dataWithBytes:(char *)utf8_string length:strlen(utf8_string)]; works well. Then the calling method can take care of stuffing the returned data back into an NSString using the appropriate encoding.

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