Encoding of a String

Shaun Wexler dev at macfoh.com
Tue Jun 20 10:14:19 CDT 2006


On Jun 20, 2006, at 3:55 AM, Philip Mötteli wrote:

> Is there a way to find out the encoding of a given fbl::String  
> instance? I mean, is the string in UTF8, ASCII, ISO-Latin, Unicode,…?

You can compensate by [avoiding] conversion on the CF/NS side.  There  
are several ways to reduce copying as well, but to illustrate, here  
is an improved version of your NSString category, which has a max of  
one length lookup at O(1) and one copy:

//
//  PMVCPPNSString.h
//
//  Created by Philip Mötteli on 17. 6. 06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import <Foundation/NSString.h>

#import <Valentina/VX.h>

extern BOOL databasePrefersUTF16;

@interface NSString (PMVCPPNSString)

+ (NSString *)stringWithFBLString:(FBL::String *)fblString;
- (FBL::String *)fblString;

@end

//
//  PMVCPPNSString.mm
//
//  Created by Philip Mötteli on 17. 6. 06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "PMVCPPNSString.h"

BOOL databasePrefersUTF16 = YES;

@implementation NSString (PMVCPPNSString)

+ (NSString *)stringWithFBLString:(FBL::String *)fblString
{
     if (fblString)
     {
         if (!fblString->isSingleByte()) {
             const UniChar *utf16str;
             if ((utf16str = fblString->c_str())) {
                 CFStringRef string = CFStringCreateWithCharacters 
(NULL, utf16str, fblString->length());
                 return [(NSString *)string autorelease];
             }
         }
         else {
             const char *utf8str;
             if ((utf8str = fblString->getBufferA())) {
                 CFStringRef string = CFStringCreateWithCString(NULL,  
utf8str, kCFStringEncodingUTF8);
                 return [(NSString *)string autorelease];
             }
         }
     }

     return nil;
}

- (FBL::String *)fblString
{
     if (databasePrefersUTF16)
     {
         const UniChar *utf16str;
         if ((utf16str = CFStringGetCharactersPtr((CFStringRef)self))) {
             return new FBL::String(utf16str);
         }
         else {
             CFIndex length = CFStringGetLength((CFStringRef)self);
             UniChar utf16buf[length + 1];
             CFStringGetCharacters((CFStringRef)self, CFRangeMake(0,  
length), utf16buf);
             utf16buf[length] = 0;
             return new FBL::String((UniChar *)utf16buf, length);
         }
     }

     const char *utf8str;
     if ((utf8str = CFStringGetCStringPtr((CFStringRef)self,  
kCFStringEncodingUTF8))) {
         return new FBL::String(utf8str, -1, "UTF-8");
     }
     else {
         CFIndex length = CFStringGetLength((CFStringRef)self);
         char utf8buf[CFStringGetMaximumSizeForEncoding(length,  
kCFStringEncodingUTF8)];
         CFStringGetCString((CFStringRef)self, utf8buf, length,  
kCFStringEncodingUTF8);
         return new FBL::String(utf8buf, length, "UTF-8");
     }
}

@end

-- 
Shaun Wexler
MacFOH
http://www.macfoh.com



More information about the Valentina mailing list