27#ifndef OOENCODINGCONVERTER_EXCLUDE
42#define USE_COMPATIBILITY_MAPPING 0
44#define USE_COMPATIBILITY_MAPPING 0
48#define PROFILE_ENCODING_CONVERTER 0
54#if PROFILE_ENCODING_CONVERTER
56static NSTimer *sProfileTimer =
nil;
58static unsigned sCacheHits = 0;
59static unsigned sCacheMisses = 0;
63@interface OOEncodingConverter (Private)
65- (NSData *) performConversionForString:(NSString *)string;
72- (id) initWithEncoding:(NSStringEncoding)encoding substitutions:(NSDictionary *)substitutions
77 _cache = [[
OOCache alloc] init];
78 [_cache setPruneThreshold:kCachePruneThreshold];
79 [_cache setName:@"Text encoding"];
80 _substitutions = [substitutions copy];
83#if PROFILE_ENCODING_CONVERTER
84 if (sProfiledConverter ==
nil)
86 sProfiledConverter =
self;
87 sProfileTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(profileFire:) userInfo:nil repeats:YES];
96- (id) initWithFontPList:(NSDictionary *)fontPList
98 return [
self initWithEncoding:EncodingFromString([fontPList oo_stringForKey:@"encoding"]) substitutions:[fontPList oo_dictionaryForKey:@"substitutions"]];
105 [_substitutions release];
107#if PROFILE_ENCODING_CONVERTER
108 sProfiledConverter =
nil;
109 [sProfileTimer invalidate];
119- (NSString *) descriptionComponents
121 return [NSString stringWithFormat:@"encoding: %lu", _encoding];
125- (NSData *) convertString:(NSString *)string
129#if USE_COMPATIBILITY_MAPPING
131 string = [string precomposedStringWithCompatibilityMapping];
134 if (
string ==
nil)
return [NSData data];
136 data = [_cache objectForKey:string];
139 data = [
self performConversionForString:string];
140 if (data !=
nil) [_cache setObject:data forKey:string];
142#if PROFILE_ENCODING_CONVERTER
155- (NSStringEncoding) encoding
163@implementation OOEncodingConverter (Private)
165- (NSData *) performConversionForString:(NSString *)string
167 NSString *subst =
nil;
168 NSEnumerator *substEnum =
nil;
169 NSMutableString *
mutable =
nil;
171 mutable = [[string mutableCopy] autorelease];
172 if (
mutable ==
nil)
return nil;
174 for (substEnum = [_substitutions keyEnumerator]; (subst = [substEnum nextObject]); )
176 [mutable replaceOccurrencesOfString:subst
177 withString:[_substitutions objectForKey:subst]
179 range:NSMakeRange(0, [mutable length])];
182 return [mutable dataUsingEncoding:_encoding allowLossyConversion:YES];
186#if PROFILE_ENCODING_CONVERTER
194- (void) profileFire:(
id)junk
196 float ratio = (float)sCacheHits / (
float)(sCacheHits + sCacheMisses);
197 OOLog(
@"strings.encoding.profile",
@"Cache hits: %u, misses: %u, ratio: %.2g", sCacheHits, sCacheMisses, ratio);
198 sCacheHits = sCacheMisses = 0;
217#define kWindowsLatin1Str @"windows-latin-1"
218#define kWindowsLatin2Str @"windows-latin-2"
219#define kWindowsCyrillicStr @"windows-cyrillic"
220#define kWindowsGreekStr @"windows-greek"
221#define kWindowsTurkishStr @"windows-turkish"
228 case NSWindowsCP1252StringEncoding:
231 case NSWindowsCP1250StringEncoding:
234 case NSWindowsCP1251StringEncoding:
237 case NSWindowsCP1253StringEncoding:
240 case NSWindowsCP1254StringEncoding:
251 if ([name isEqualToString:
kWindowsLatin1Str])
return NSWindowsCP1252StringEncoding;
252 if ([name isEqualToString:
kWindowsLatin2Str])
return NSWindowsCP1250StringEncoding;
254 if ([name isEqualToString:
kWindowsGreekStr])
return NSWindowsCP1253StringEncoding;
256 return (NSStringEncoding)NSNotFound;
#define kWindowsCyrillicStr
static const NSUInteger kCachePruneThreshold
#define kWindowsLatin1Str
NSStringEncoding EncodingFromString(NSString *name)
#define kWindowsLatin2Str
NSString * StringFromEncoding(NSStringEncoding encoding)
#define kWindowsTurkishStr
#define OOLog(class, format,...)