Reformatted code in preparation of code change.

This commit is contained in:
Tilo Prütz 2012-04-18 09:41:53 +02:00
parent bcd10037ad
commit 80f0dd5426
5 changed files with 353 additions and 331 deletions

View File

@ -42,15 +42,18 @@ struct NSRangeEntries {
return result;
}
void NSFreeRangeEntries(NSRangeEntries *self) {
if (self == nil) {
return;
}
NSResetRangeEntries(self);
NSZoneFree(NULL,self->entries);
NSZoneFree(NULL,self);
void NSFreeRangeEntries(NSRangeEntries *self)
{
if (self == nil) {
return;
}
NSResetRangeEntries(self);
NSZoneFree(NULL, self->entries);
NSZoneFree(NULL, self);
}
void NSResetRangeEntries(NSRangeEntries *self) {
NSInteger i;

View File

@ -559,12 +559,12 @@ NSString *NSStringWithDateFormatLocale(NSTimeInterval interval,NSString *format,
NSString *NSReadStringInString(NSString *aString, NSCharacterSet *characterSet, NSUInteger position, NSUInteger maxLength, NSUInteger *endPosition)
{
NSMutableString *resultString = [NSMutableString stringWithCapacity:maxLength];
for(NSUInteger i = 0; i < maxLength; i++) {
if(position+i >= [aString length]) {
break;
}
unichar c = [aString characterAtIndex:position+i];
if([characterSet characterIsMember:c] == NO) {
break;
@ -581,7 +581,7 @@ NSInteger NSReadIntegerInString(NSString *aString, NSCharacterSet *characterSet,
unichar firstChar = [aString characterAtIndex:position];
BOOL negate = NO;
NSInteger i;
if (firstChar == '+') {
position++;
}
@ -589,47 +589,47 @@ NSInteger NSReadIntegerInString(NSString *aString, NSCharacterSet *characterSet,
position++;
negate = YES;
}
str = NSReadStringInString(aString, characterSet, position, maxLength, endPosition);
i = [str integerValue];
if (negate == YES) {
i *= -1;
}
return i;
}
// might as well use the same code since they're the exact same formatting specifiers
// ok. we need at minimum the year. everything else is optional.
// weekday information is useless.
NSCalendarDate *NSCalendarDateWithStringDateFormatLocale(NSString *string, NSString *format,
NSDictionary *locale) {
NSUInteger pos,fmtLength=[format length];
unichar fmtBuffer[fmtLength],unicode;
NSInteger years = NSNotFound, months = NSNotFound, days = NSNotFound, hours = NSNotFound, minutes = NSNotFound, seconds = NSNotFound, milliseconds = NSNotFound;
NSInteger AMPMMultiplier = 0;
NSTimeInterval adjustment = 0;
NSArray *monthNames, *shortMonthNames, *AMPMDesignations;
NSTimeZone *timeZone = nil;
NSTimeInterval timeInterval;
NSCalendarDate *calendarDate;
NSUInteger currentPostion = 0;
NSCalendarDate *NSCalendarDateWithStringDateFormatLocale(NSString *string, NSString *format, NSDictionary *locale)
{
NSUInteger pos, fmtLength = [format length];
unichar fmtBuffer[fmtLength], unicode;
NSInteger years = NSNotFound, months = NSNotFound, days = NSNotFound, hours = NSNotFound, minutes = NSNotFound, seconds = NSNotFound, milliseconds = NSNotFound;
NSInteger AMPMMultiplier = 0;
NSTimeInterval adjustment = 0;
NSArray *monthNames, *shortMonthNames, *AMPMDesignations;
NSTimeZone *timeZone = nil;
NSTimeInterval timeInterval;
NSCalendarDate *calendarDate;
NSUInteger currentPostion = 0;
enum {
STATE_SCANNING,
STATE_PERCENT,
STATE_CONVERSION
} state=STATE_SCANNING;
} state = STATE_SCANNING;
if ([string length] == 0)
if ([string length] == 0) {
return nil;
}
if (locale == nil)
if (locale == nil) {
locale = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
}
monthNames = [locale objectForKey:NSMonthNameArray];
shortMonthNames = [locale objectForKey:NSShortMonthNameArray];
@ -638,38 +638,38 @@ NSDictionary *locale) {
// although we don't use the weekday arrays for anything, the spec
// says to check them anyway.
if ([monthNames count] > 12 || [shortMonthNames count] > 12 ||
[[locale objectForKey:NSShortWeekDayNameArray] count] > 7 ||
[[locale objectForKey:NSWeekDayNameArray] count] > 7)
[[locale objectForKey:NSShortWeekDayNameArray] count] > 7 ||
[[locale objectForKey:NSWeekDayNameArray] count] > 7) {
return nil;
}
[format getCharacters:fmtBuffer];
for(pos=0;pos<fmtLength;pos++){
unicode=fmtBuffer[pos];
for (pos = 0; pos < fmtLength; pos++) {
unicode = fmtBuffer[pos];
switch(state){
switch (state) {
case STATE_SCANNING:
if(unicode=='%') {
state=STATE_PERCENT;
}
else {
if (unicode == '%') {
state = STATE_PERCENT;
} else {
currentPostion++;
}
break;
case STATE_PERCENT:
switch(unicode){
switch (unicode) {
case '.':
case ' ':
default:
pos--;
state=STATE_CONVERSION;
state = STATE_CONVERSION;
break;
}
break;
case STATE_CONVERSION:
switch(unicode){
switch (unicode) {
case '%':
currentPostion++;
break;
@ -683,53 +683,51 @@ NSDictionary *locale) {
break;
// month or its abbreviation. look it up in the arrays..
case 'b':{
case 'b': {
NSString *temp;
NSEnumerator *enumerator = [shortMonthNames objectEnumerator];
NSString *shortMonthName;
months = NSNotFound;
int month = 1;
temp = NSReadStringInString(string, [NSCharacterSet letterCharacterSet], currentPostion, 255, &currentPostion);
while ((shortMonthName = [enumerator nextObject]) != nil) {
if ([shortMonthName caseInsensitiveCompare:temp] == NSOrderedSame) {
months = month;
break;
}
else {
} else {
month++;
}
}
//month not found
if(months == NSNotFound) {
if (months == NSNotFound) {
return nil;
}
break;
}
case 'B':{
case 'B': {
NSString *temp;
NSEnumerator *enumerator = [monthNames objectEnumerator];
NSString *monthName;
months = NSNotFound;
int month = 1;
temp = NSReadStringInString(string, [NSCharacterSet letterCharacterSet], currentPostion, 255, &currentPostion);
while ((monthName = [enumerator nextObject]) != nil) {
if ([monthName caseInsensitiveCompare:temp] == NSOrderedSame) {
months = month;
break;
}
else {
} else {
month++;
}
}
//month not found
if(months == NSNotFound) {
if (months == NSNotFound) {
return nil;
}
break;
@ -761,8 +759,8 @@ NSDictionary *locale) {
NSInteger numberOfDays = NSReadIntegerInString(string, [NSCharacterSet decimalDigitCharacterSet], currentPostion, 3, &currentPostion);
adjustment += numberOfDays * 86400.0;
}
break;
}
case 'm':
months = NSReadIntegerInString(string, [NSCharacterSet decimalDigitCharacterSet], currentPostion, 2, &currentPostion);
@ -775,13 +773,14 @@ NSDictionary *locale) {
case 'p': {
NSString *temp;
temp = NSReadStringInString(string, [NSCharacterSet letterCharacterSet], currentPostion, 255, &currentPostion);
AMPMMultiplier = [AMPMDesignations indexOfObject:temp];
if (AMPMMultiplier == NSNotFound)
if (AMPMMultiplier == NSNotFound) {
return nil;
AMPMMultiplier++; // e.g. 0 = 1, 1 = 2...
}
AMPMMultiplier++; // e.g. 0 = 1, 1 = 2...
break;
}
@ -796,10 +795,10 @@ NSDictionary *locale) {
}
case 'x':
return NSCalendarDateWithStringDateFormatLocale(string,[locale objectForKey:NSDateFormatString],locale);
return NSCalendarDateWithStringDateFormatLocale(string, [locale objectForKey:NSDateFormatString], locale);
case 'X':
return NSCalendarDateWithStringDateFormatLocale(string,[locale objectForKey:NSTimeFormatString],locale);
return NSCalendarDateWithStringDateFormatLocale(string, [locale objectForKey:NSTimeFormatString], locale);
case 'y':
years = NSReadIntegerInString(string, [NSCharacterSet decimalDigitCharacterSet], currentPostion, 2, &currentPostion);
@ -817,7 +816,7 @@ NSDictionary *locale) {
case 'Z': {
NSString *temp;
temp = NSReadStringInString(string, [NSCharacterSet letterCharacterSet], currentPostion, 255, &currentPostion);
timeZone = [NSTimeZone timeZoneWithName:temp];
@ -834,15 +833,16 @@ NSDictionary *locale) {
break;
}
}
state=STATE_SCANNING;
state = STATE_SCANNING;
break;
}
}
}
// now that we've got whatever information we can get from the string,
// try to make an NSCalendarDate of it.
if (AMPMMultiplier != 0 && hours != NSNotFound)
// now that we've got whatever information we can get from the string,
// try to make an NSCalendarDate of it.
if (AMPMMultiplier != 0 && hours != NSNotFound) {
hours *= AMPMMultiplier;
}
// maybe we've been given the number of days in the year but not the month/day
if (months == NSNotFound && days == NSNotFound) {
@ -851,28 +851,33 @@ NSDictionary *locale) {
}
// if no year, then this year
if (years == NSNotFound)
if (years == NSNotFound) {
years = [[NSCalendarDate date] yearOfCommonEra];
}
if (hours == NSNotFound)
if (hours == NSNotFound) {
hours = 0;
if (minutes == NSNotFound)
}
if (minutes == NSNotFound) {
minutes = 0;
if (seconds == NSNotFound)
}
if (seconds == NSNotFound) {
seconds = 0;
if (milliseconds == NSNotFound)
}
if (milliseconds == NSNotFound) {
milliseconds = 0;
}
if (timeZone == nil)
if (timeZone == nil) {
timeZone = [NSTimeZone defaultTimeZone];
}
timeInterval = NSTimeIntervalWithComponents(years, months, days, hours, minutes, seconds, milliseconds);
timeInterval += adjustment;
timeInterval = timeInterval-[timeZone secondsFromGMTForDate:[NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval]];
timeInterval = timeInterval - [timeZone secondsFromGMTForDate:[NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval]];
calendarDate = [[[NSCalendarDate allocWithZone:NULL]
initWithTimeIntervalSinceReferenceDate:timeInterval] autorelease];
calendarDate = [[[NSCalendarDate allocWithZone:NULL] initWithTimeIntervalSinceReferenceDate:timeInterval] autorelease];
[calendarDate setTimeZone:timeZone];
return calendarDate;

View File

@ -44,7 +44,7 @@ static NSNumberFormatterBehavior _defaultFormatterBehavior=NSNumberFormatterBeha
[super init];
_behavior=_defaultFormatterBehavior;
_numberStyle=NSNumberFormatterNoStyle;
_thousandSeparator = @",";
_decimalSeparator = @".";
_attributedStringForNil=[[NSAttributedString allocWithZone:NULL] initWithString:@"(null)"];
@ -60,150 +60,151 @@ static NSNumberFormatterBehavior _defaultFormatterBehavior=NSNumberFormatterBeha
/*
* The format string (on 10.4 - which Cocotron currently supports) follows this standard: http://unicode.org/reports/tr35/tr35-4.html#Number_Format_Patterns
* The parser isn't handling every option in the string but at least the common things such as: [prefix] #,##0.### [suffix]
*
*
*/
static void extractFormat(NSString *format,
NSString **prefix, NSString **suffix,
NSUInteger *minimumIntegerDigitsp, NSUInteger *maximumIntegerDigitsp,
NSUInteger *minimumFractionDigitsp, NSUInteger *maximumFractionDigitsp,
NSUInteger *groupingSizep, NSUInteger *secondaryGroupingSizep)
NSString **prefix, NSString **suffix,
NSUInteger *minimumIntegerDigitsp, NSUInteger *maximumIntegerDigitsp,
NSUInteger *minimumFractionDigitsp, NSUInteger *maximumFractionDigitsp,
NSUInteger *groupingSizep, NSUInteger *secondaryGroupingSizep)
{
NSUInteger length=[format length];
NSUInteger prefixLength=0;
NSUInteger suffixLength=0;
NSUInteger length = [format length];
NSUInteger prefixLength = 0;
NSUInteger suffixLength = 0;
NSUInteger groupingSize = 0;
NSUInteger secondaryGroupingSize = 0;
unichar buffer[length];
unichar prefixBuffer[length], suffixBuffer[length];
BOOL addToGrouping = NO;
NSUInteger groupingSize=0;
NSUInteger secondaryGroupingSize=0;
unichar buffer[length];
unichar prefixBuffer[length], suffixBuffer[length];
BOOL addToGrouping=NO;
enum {
STATE_PREFIX,
STATE_INTEGER,
STATE_FRACTION,
STATE_SUFFIX,
} state=STATE_PREFIX;
NSUInteger minimumIntegerDigits, maximumIntegerDigits, minimumFractionDigits, maximumFractionDigits;
minimumIntegerDigits = 0;
maximumIntegerDigits = 0;
minimumFractionDigits = 0;
maximumFractionDigits = 0;
BOOL foundPrimaryGrouping = NO;
BOOL foundSecondaryGrouping = NO;
STATE_PREFIX,
STATE_INTEGER,
STATE_FRACTION,
STATE_SUFFIX,
} state = STATE_PREFIX;
NSUInteger minimumIntegerDigits, maximumIntegerDigits, minimumFractionDigits, maximumFractionDigits;
minimumIntegerDigits = 0;
maximumIntegerDigits = 0;
minimumFractionDigits = 0;
maximumFractionDigits = 0;
BOOL foundPrimaryGrouping = NO;
BOOL foundSecondaryGrouping = NO;
[format getCharacters:buffer];
NSInteger i = 0;
for( i=0; i < length; i++) {
unichar code=buffer[i];
switch(state) {
case STATE_PREFIX:
// Looking for non-numeric chars leading off the format - stop when we find a 0 or a # or a '.'
if (code == '.') {
state = STATE_FRACTION;
} else if(code=='#' || code == '0') {// starting off with a hash or a 0
state=STATE_INTEGER;
i--; // step back so we can process these chars in the right state
} else {
// Suck up chars into the prefix
prefixBuffer[prefixLength++]=code;
}
break;
case STATE_INTEGER:
if (code == '.') {
state = STATE_FRACTION;
// No need to step back - the '.' just marks the separation
} else if (code == '#') {
if (foundPrimaryGrouping) {
groupingSize++;
}
maximumIntegerDigits++;
} else if (code == '0') {
if (foundPrimaryGrouping) {
groupingSize++;
}
minimumIntegerDigits++;
maximumIntegerDigits++;
} else if (code ==',') {
if (foundPrimaryGrouping == NO) {
foundPrimaryGrouping = YES;
} else {
foundSecondaryGrouping = YES;
secondaryGroupingSize = groupingSize;
groupingSize = 0;
}
} else {
// Anything we don't recognize means we're into the suffix part
state = STATE_SUFFIX;
i--;
}
break;
case STATE_FRACTION:
if (code == '#') {
maximumFractionDigits++;
} else if (code == '0') {
minimumFractionDigits++;
maximumFractionDigits++;
} else {
state = STATE_SUFFIX;
i--; // and step back one to catch the contents
}
break;
case STATE_SUFFIX:
suffixBuffer[suffixLength++]=code;
break;
}
NSInteger i = 0;
for (i = 0; i < length; i++) {
unichar code = buffer[i];
switch (state) {
case STATE_PREFIX:
// Looking for non-numeric chars leading off the format - stop when we find a 0 or a # or a '.'
if (code == '.') {
state = STATE_FRACTION;
} else if (code == '#' || code == '0') {// starting off with a hash or a 0
state = STATE_INTEGER;
i--; // step back so we can process these chars in the right state
} else {
// Suck up chars into the prefix
prefixBuffer[prefixLength++] = code;
}
break;
case STATE_INTEGER:
if (code == '.') {
state = STATE_FRACTION;
// No need to step back - the '.' just marks the separation
} else if (code == '#') {
if (foundPrimaryGrouping) {
groupingSize++;
}
maximumIntegerDigits++;
} else if (code == '0') {
if (foundPrimaryGrouping) {
groupingSize++;
}
minimumIntegerDigits++;
maximumIntegerDigits++;
} else if (code == ',') {
if (foundPrimaryGrouping == NO) {
foundPrimaryGrouping = YES;
} else {
foundSecondaryGrouping = YES;
secondaryGroupingSize = groupingSize;
groupingSize = 0;
}
} else {
// Anything we don't recognize means we're into the suffix part
state = STATE_SUFFIX;
i--;
}
break;
case STATE_FRACTION:
if (code == '#') {
maximumFractionDigits++;
} else if (code == '0') {
minimumFractionDigits++;
maximumFractionDigits++;
} else {
state = STATE_SUFFIX;
i--; // and step back one to catch the contents
}
break;
case STATE_SUFFIX:
suffixBuffer[suffixLength++] = code;
break;
}
}
// Update all valud the parameters
if (minimumIntegerDigitsp != NULL) {
*minimumIntegerDigitsp = minimumIntegerDigits;
}
if (maximumIntegerDigitsp != NULL) {
*maximumIntegerDigitsp = maximumIntegerDigits;
}
if(minimumFractionDigitsp != NULL) {
*minimumFractionDigitsp = minimumFractionDigits;
// Update all valud the parameters
if (minimumIntegerDigitsp != NULL) {
*minimumIntegerDigitsp = minimumIntegerDigits;
}
if(maximumFractionDigitsp != NULL) {
*maximumFractionDigitsp = maximumFractionDigits;
if (maximumIntegerDigitsp != NULL) {
*maximumIntegerDigitsp = maximumIntegerDigits;
}
if(groupingSizep != NULL) {
*groupingSizep = groupingSize;
}
if(secondaryGroupingSizep != NULL) {
*secondaryGroupingSizep = secondaryGroupingSize;
}
if(prefixLength > 0) {
if (minimumFractionDigitsp != NULL) {
*minimumFractionDigitsp = minimumFractionDigits;
}
if (maximumFractionDigitsp != NULL) {
*maximumFractionDigitsp = maximumFractionDigits;
}
if (groupingSizep != NULL) {
*groupingSizep = groupingSize;
}
if (secondaryGroupingSizep != NULL) {
*secondaryGroupingSizep = secondaryGroupingSize;
}
if (prefixLength > 0) {
*prefix = [[NSString allocWithZone: NULL] initWithCharacters: prefixBuffer length: prefixLength];
}
if(suffixLength>0) {
*suffix = [[NSString allocWithZone: NULL] initWithCharacters: suffixBuffer length: suffixLength];
}
}
if (suffixLength > 0) {
*suffix = [[NSString allocWithZone: NULL] initWithCharacters: suffixBuffer length: suffixLength];
}
}
-(void)extractFromPositiveFormat {
if([_positiveFormat length]){
[_positivePrefix release];
_positivePrefix=nil;
[_positiveSuffix release];
_positiveSuffix=nil;
extractFormat(_positiveFormat, &_positivePrefix, &_positiveSuffix,
&_minimumIntegerDigits, &_maximumIntegerDigits,
&_minimumFractionDigits, &_maximumFractionDigits,
@ -214,12 +215,12 @@ static void extractFormat(NSString *format,
-(void)extractFromNegativeFormat {
if([_negativeFormat length]){
[_negativePrefix release];
_negativePrefix=nil;
[_negativeSuffix release];
_negativeSuffix=nil;
extractFormat(_negativeFormat, &_negativePrefix, &_negativeSuffix, NULL, NULL, NULL, NULL, NULL, NULL);
if ([_negativePrefix isEqualToString: @"-"]) {
// That's not a very interesting prefix...
@ -231,11 +232,11 @@ static void extractFormat(NSString *format,
-initWithCoder:(NSCoder*)coder {
[super initWithCoder:coder];
if([coder allowsKeyedCoding]){
NSDictionary *attributes=[coder decodeObjectForKey:@"NS.attributes"];
id check;
if((check=[attributes objectForKey:@"formatterBehavior"])!=nil)
_behavior=[check integerValue];
if((check=[attributes objectForKey:@"numberStyle"])!=nil)
@ -304,13 +305,13 @@ static void extractFormat(NSString *format,
_perMillSymbol=[[attributes objectForKey:@"perMillSymbol"] copy];
_roundingIncrement=[[attributes objectForKey:@"roundingIncrement"] copy];
_roundingMode=[[attributes objectForKey:@"roundingMode"] integerValue];
_positiveFormat=[[attributes objectForKey:@"positiveFormat"] copy];
_negativeFormat=[[attributes objectForKey:@"negativeFormat"] copy];
[self extractFromPositiveFormat];
[self extractFromNegativeFormat];
_textAttributesForPositiveValues=nil;
_textAttributesForNegativeValues=nil;
_textAttributesForNegativeInfinity=nil;
@ -323,7 +324,7 @@ static void extractFormat(NSString *format,
#if 0
_nilSymbol=[[coder decodeObjectForKey:@"NS.nil"] copy];
_zeroSymbol=[[coder decodeObjectForKey:@"NS.zero"] copy];
_positiveFormat=[[coder decodeObjectForKey:@"NS.positiveformat"] copy];
_negativeFormat=[[coder decodeObjectForKey:@"NS.negativeformat"] copy];
_textAttributesForPositiveValues=[[coder decodeObjectForKey:@"NS.positiveattrs"] copy]
@ -336,7 +337,7 @@ static void extractFormat(NSString *format,
_localizesFormat=[coder decodeBoolForKey:@"NS.localized"];
#endif
}
return self;
}
@ -368,7 +369,7 @@ static void extractFormat(NSString *format,
-(NSNumber *)multiplier {
if(_multiplier==nil){
if(_numberStyle==NSNumberFormatterPercentStyle)
return [NSNumber numberWithInt:100];
}
@ -431,10 +432,10 @@ static void extractFormat(NSString *format,
-(NSUInteger)maximumFractionDigits {
if(_customMaximumFractionDigits)
return _maximumFractionDigits;
if(_numberStyle==NSNumberFormatterDecimalStyle)
return 3;
return 0;
}
@ -457,7 +458,7 @@ static void extractFormat(NSString *format,
-(NSString *)notANumberSymbol {
if(_notANumberSymbol==nil)
return @"NaN";
return _notANumberSymbol;
}
@ -476,7 +477,7 @@ static void extractFormat(NSString *format,
-(NSString *)negativePrefix {
if(_negativePrefix==nil)
return @"";
return _negativePrefix;
}
@ -484,20 +485,20 @@ static void extractFormat(NSString *format,
// Suffixes return the percent symbol if specified
if(_negativeSuffix==nil){
if(_numberStyle==NSNumberFormatterPercentStyle)
return [self percentSymbol];
return @"";
}
return _negativeSuffix;
}
-(NSString *)positivePrefix {
if(_positivePrefix==nil)
return @"";
return _positivePrefix;
}
@ -505,13 +506,13 @@ static void extractFormat(NSString *format,
// Suffixes return the percent symbol if specified
if(_positiveSuffix==nil){
if(_numberStyle==NSNumberFormatterPercentStyle)
return [self percentSymbol];
return @"";
}
return _positiveSuffix;
}
@ -558,13 +559,13 @@ static void extractFormat(NSString *format,
-(NSString *)groupingSeparator {
if(_groupingSeparator==nil){
NSString *check=[_locale objectForKey:NSLocaleGroupingSeparator];
if(check!=nil)
return check;
return @",";
}
return _groupingSeparator;
}
@ -587,7 +588,7 @@ static void extractFormat(NSString *format,
-(NSString *)percentSymbol {
if(_percentSymbol==nil)
return @"%";
return _percentSymbol;
}
@ -671,7 +672,7 @@ static void extractFormat(NSString *format,
else
_negativeFormat = [NSString stringWithFormat:@"-%@", _positiveFormat];
if ([format rangeOfString:@","].location != NSNotFound ||
if ([format rangeOfString:@","].location != NSNotFound ||
[format rangeOfString:_thousandSeparator].location != NSNotFound)
[self setHasThousandSeparators:YES];
}
@ -1032,20 +1033,20 @@ static NSString *stringWithFormatGrouping(NSString *format,id locale,NSString *g
-(NSString *)_stringFromNumber:(NSNumber *)number {
NSString *string=nil;
if(number==nil)
string=[self nilSymbol];
else if(number==(NSNumber *)kCFNumberNaN)
string=[self notANumberSymbol];
else if(number==(NSNumber *)kCFNumberPositiveInfinity){
NSString *check=[self positiveInfinitySymbol];
if(check==nil){
unichar code=0x221E; // unicode infinity
check=[NSString stringWithCharacters:&code length:1];
}
string=check;
}
else if(number==(NSNumber *)kCFNumberNegativeInfinity){
@ -1053,23 +1054,23 @@ static NSString *stringWithFormatGrouping(NSString *format,id locale,NSString *g
if(check==nil){
unichar codes[2]={ '-', 0x221E }; // unicode infinity
check=[NSString stringWithCharacters:codes length:2];
}
string=check;
}
const char *objcType=[number objCType];
if(objcType==NULL || strlen(objcType)!=1)
objcType="?";
switch(*objcType){
case _C_CHR:
case _C_SHT:
case _C_INT:
#ifndef __LP64__
#ifndef __LP64__
case _C_LNG:
#endif
string=stringWithFormatGrouping(@"%i",_locale,[self groupingSeparator],[self groupingSize],[number intValue]);
@ -1079,13 +1080,13 @@ static NSString *stringWithFormatGrouping(NSString *format,id locale,NSString *g
case _C_UCHR:
case _C_USHT:
case _C_UINT:
#ifndef __LP64__
#ifndef __LP64__
case _C_ULNG:
#endif
string=stringWithFormatGrouping(@"%u",_locale,[self groupingSeparator],[self groupingSize],[number unsignedIntValue]);
break;
#ifdef __LP64__
#ifdef __LP64__
case _C_LNG:
#endif
#if defined(GCC_RUNTIME_3) || defined(APPLE_RUNTIME_4)
@ -1097,7 +1098,7 @@ static NSString *stringWithFormatGrouping(NSString *format,id locale,NSString *g
break;
break;
#ifdef __LP64__
#ifdef __LP64__
case _C_ULNG:
#endif
#if defined(GCC_RUNTIME_3) || defined(APPLE_RUNTIME_4)
@ -1111,12 +1112,12 @@ static NSString *stringWithFormatGrouping(NSString *format,id locale,NSString *g
case _C_FLT:
case _C_DBL:;
NSString *format;
format=[NSString stringWithFormat:@"%%.%df",[self minimumFractionDigits]];
string=stringWithFormatGrouping(format,_locale,[self groupingSeparator],[self groupingSize],[number doubleValue]);
break;
default:
string=[number description];
break;
@ -1128,27 +1129,27 @@ static NSString *stringWithFormatGrouping(NSString *format,id locale,NSString *g
static NSNumber *multipliedNumber(NSNumber *number,NSNumber *multiplier){
if(multiplier==nil)
return number;
return [NSNumber numberWithDouble:[number doubleValue]*[multiplier doubleValue]];
}
static BOOL numberIsNegative(NSNumber *number){
if(number==nil)
return NO;
return (copysign(1.0,[number doubleValue])<0)?YES:NO;
}
static BOOL numberIsPositive(NSNumber *number){
if(number==nil)
return YES; // ?
return (copysign(1.0,[number doubleValue])>0)?YES:NO;
}
-(NSString *)stringFromNumberNoStyle:(NSNumber *)number {
number=multipliedNumber(number,[self multiplier]);
NSString *prefix;
NSString *suffix;
//unused
@ -1164,13 +1165,13 @@ static BOOL numberIsPositive(NSNumber *number){
suffix=[self positiveSuffix];
//format=[self positiveFormat];
}
NSString *result;
result=prefix;
result=[result stringByAppendingString:[self _stringFromNumber:number]];
result=[result stringByAppendingString:suffix];
return result;
}
@ -1208,10 +1209,10 @@ static BOOL numberIsPositive(NSNumber *number){
-(NSString *)stringFromNumber:(NSNumber *)number {
NSNumberFormatterBehavior check=_behavior;
if(check==NSNumberFormatterBehaviorDefault)
check=NSNumberFormatterBehavior10_4;
if(check==NSNumberFormatterBehavior10_0)
return [self stringFromNumber10_0:number];
else
@ -1224,7 +1225,7 @@ static BOOL numberIsPositive(NSNumber *number){
}
// BROKEN
#if 0
#if 0
-(NSString *)_objectValue:(id)object withNumberFormat:(NSString *)format {
NSString *stringValue = [[NSNumber numberWithDouble:[object doubleValue]] stringValue];
//NSAllocateMemoryPages wtf??
@ -1246,7 +1247,7 @@ static BOOL numberIsPositive(NSNumber *number){
// decremented in main loop, when zero, time for a separator
if (_hasThousandSeparators)
thousandSepCounter = (prePoint % 3) ? (prePoint % 3) : 3;
thousandSepCounter = (prePoint % 3) ? (prePoint % 3) : 3;
else
thousandSepCounter = -1; // never
@ -1304,7 +1305,7 @@ static BOOL numberIsPositive(NSNumber *number){
#endif
// this section works, but it's pretty lame...
// it doesn't round, it truncates; integers in the format specifier are ignored...
// it doesn't round, it truncates; integers in the format specifier are ignored...
-(NSString *)_separatedStringIfNeededWithString:(NSString *)string {
NSUInteger thousandSepCounter, i, j = 0;
unichar buffer[256];
@ -1315,7 +1316,7 @@ static BOOL numberIsPositive(NSNumber *number){
if ([string length] < 4)
return string;
thousandSepCounter = ([string length] % 3) ? ([string length] % 3) : 3;
thousandSepCounter = ([string length] % 3) ? ([string length] % 3) : 3;
for (i = 0; i < [string length]; ++i) {
buffer[j++] = [string characterAtIndex:i];
thousandSepCounter--;
@ -1377,7 +1378,7 @@ static BOOL numberIsPositive(NSNumber *number){
// ignore?
case NSNumberFormatterSpace:
// ignore; already handled
case NSNumberFormatterThousandSeparator:
case NSNumberFormatterThousandSeparator:
break;
case NSNumberFormatterDecimalSeparator:
@ -1418,7 +1419,7 @@ static BOOL numberIsPositive(NSNumber *number){
-(NSAttributedString *)attributedStringForObjectValue10_0:object withDefaultAttributes:(NSDictionary *)defaultAttributes {
if(object==nil){
NSAttributedString *check=[self attributedStringForNil];
if(check!=nil)
return check;
}
@ -1428,27 +1429,27 @@ static BOOL numberIsPositive(NSNumber *number){
if(check!=nil)
return check;
}
NSString *string=[self stringForObjectValue:object];
NSDictionary *attributes=nil;
if(numberIsPositive(object))
attributes=[self textAttributesForPositiveValues];
else if(numberIsNegative(object))
attributes=[self textAttributesForNegativeValues];
else
attributes=[self textAttributesForZero];
if(attributes==nil)
attributes=defaultAttributes;
return [[[NSAttributedString allocWithZone:NULL] initWithString:string attributes:attributes] autorelease];
}
-(NSAttributedString *)attributedStringForObjectValue10_4:object withDefaultAttributes:(NSDictionary *)defaultAttributes {
NSString *string=[self stringForObjectValue:object];
NSDictionary *attributes=nil;
if (object == nil)
attributes=[self textAttributesForNil];
else if(object==(NSNumber *)kCFNumberNaN)
@ -1472,10 +1473,10 @@ static BOOL numberIsPositive(NSNumber *number){
-(NSAttributedString *)attributedStringForObjectValue:object withDefaultAttributes:(NSDictionary *)attributes {
NSNumberFormatterBehavior check=_behavior;
if(check==NSNumberFormatterBehaviorDefault)
check=NSNumberFormatterBehavior10_4;
if(check==NSNumberFormatterBehavior10_0)
return [self attributedStringForObjectValue10_0:object withDefaultAttributes:(NSDictionary *)attributes];
else
@ -1517,10 +1518,10 @@ static BOOL numberIsPositive(NSNumber *number){
return YES;
}
-(BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString
-(BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString
errorDescription:(NSString **)error {
//
//
return YES;
}

View File

@ -591,8 +591,8 @@ static inline BOOL isEqualString(NSString *str1,NSString *str2){
if(length1==0)
return YES;
else {
unichar *buffer1 = NSZoneMalloc(NULL, sizeof(unichar) * length1);
unichar *buffer2 = NSZoneMalloc(NULL, sizeof(unichar) * length2);
unichar *buffer1 = NSZoneMalloc(NULL, sizeof(unichar) * length1);
unichar *buffer2 = NSZoneMalloc(NULL, sizeof(unichar) * length2);
int i;
@ -988,7 +988,7 @@ U+2029 (Unicode paragraph separator), \r\n, in that order (also known as CRLF)
if(range.length==0)
return @"";
unicode=NSZoneMalloc(NULL, sizeof(unichar)*range.length);
[self getCharacters:unicode range:range];
@ -1455,58 +1455,63 @@ U+2029 (Unicode paragraph separator), \r\n, in that order (also known as CRLF)
return [self dataUsingEncoding:encoding allowLossyConversion:NO];
}
-(BOOL)getBytes:(void *)buffer maxLength:(NSUInteger)maxLength usedLength:(NSUInteger *)usedLength encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRange *)remainingRange {
NSUInteger length=[self length];
unichar *unibuffer = NSZoneMalloc(NULL, (1+range.length)*sizeof(unichar));
char *bytes=NULL;
NSUInteger byteLength=0;
- (BOOL)getBytes:(void *)buffer maxLength:(NSUInteger)maxLength usedLength:(NSUInteger *)usedLength encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRange *)remainingRange
{
NSUInteger length = [self length];
unichar *unibuffer = NSZoneMalloc(NULL, (1 + range.length) * sizeof(unichar));
char *bytes = NULL;
NSUInteger byteLength = 0;
[self getCharacters:unibuffer range:range];
bytes=NSString_unicodeToAnyCString(encoding,unibuffer, range.length,options&NSStringEncodingConversionAllowLossy ? YES : NO,&byteLength,[self zone], NO);
if (bytes==NULL) {
bytes = NSString_unicodeToAnyCString(encoding, unibuffer, range.length, options & NSStringEncodingConversionAllowLossy ? YES : NO, &byteLength, [self zone], NO);
if (bytes == NULL) {
NSZoneFree(NULL, unibuffer);
return NO;
}
if (usedLength != NULL) {
*usedLength = maxLength < byteLength ? maxLength : byteLength;
}
if (remainingRange != NULL) {
if (remainingRange != NULL) {
remainingRange->length = 0;
if (maxLength < byteLength) {
remainingRange->length = byteLength - maxLength;
}
remainingRange->location = range.location + range.length - remainingRange->length;
}
memcpy(buffer, bytes, maxLength < byteLength ? maxLength : byteLength);
NSZoneFree(NULL, unibuffer);
NSZoneFree(NULL, bytes);
return YES;
}
-(const char *)UTF8String {
NSZone *zone=[self zone];
NSUInteger length=[self length];
unichar *buffer = NSZoneMalloc(NULL, (1+length)*sizeof(unichar));
NSUInteger byteLength=0;
char *bytes=NULL;
- (const char *)UTF8String
{
NSZone *zone = [self zone];
NSUInteger length = [self length];
unichar *buffer = NSZoneMalloc(NULL, (1 + length) * sizeof(unichar));
NSUInteger byteLength = 0;
char *bytes = NULL;
[self getCharacters:buffer];
bytes=NSString_unicodeToAnyCString(NSUTF8StringEncoding,buffer,length,NO,&byteLength,zone,YES);
if(bytes==NULL) {
bytes = NSString_unicodeToAnyCString(NSUTF8StringEncoding, buffer, length, NO, &byteLength, zone, YES);
if (bytes == NULL) {
NSZoneFree(NULL, buffer);
return nil;
}
NSData* result = [NSData dataWithBytesNoCopy:bytes length:byteLength];
NSData* result = [NSData dataWithBytesNoCopy:bytes length:byteLength];
NSZoneFree(NULL, buffer);
return [result bytes];
}
-(NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding {
// FIXME: this ignores the encoding argument
@ -1600,17 +1605,17 @@ U+2029 (Unicode paragraph separator), \r\n, in that order (also known as CRLF)
result[resultLength++]=code;
}
}
NSZoneFree(NULL, unicode);
if(length==resultLength) {
NSZoneFree(NULL, result);
return self;
}
NSString *ret = [NSString stringWithCharacters:result length:resultLength];
NSZoneFree(NULL, result);
return ret;
}
@ -1618,7 +1623,7 @@ U+2029 (Unicode paragraph separator), \r\n, in that order (also known as CRLF)
NSUInteger length=[self length];
NSUInteger location=0;
unichar *buffer = NSZoneMalloc(NULL,length*sizeof(unichar));
[self getCharacters:buffer];
for(;location<length;location++)
if(![set characterIsMember:buffer[location]])
@ -1635,27 +1640,31 @@ U+2029 (Unicode paragraph separator), \r\n, in that order (also known as CRLF)
return [self substringWithRange:NSMakeRange(location,length-location)];
}
-(const char *)cStringUsingEncoding:(NSStringEncoding)encoding {
NSUInteger length=[self length];
unichar *buffer = NSZoneMalloc(NULL,length*sizeof(unichar));
NSUInteger resultLength;
[self getCharacters:buffer];
char *cstr=NSString_unicodeToAnyCString(encoding, buffer,length,NO,&resultLength,NULL,YES);
- (const char *)cStringUsingEncoding:(NSStringEncoding)encoding
{
NSUInteger length = [self length];
unichar *buffer = NSZoneMalloc(NULL, length * sizeof(unichar));
NSUInteger resultLength;
[self getCharacters:buffer];
char *cstr = NSString_unicodeToAnyCString(encoding, buffer, length, NO, &resultLength, NULL, YES);
// FIXME obviously the char* should be handled by the autorelease pool or garbage collector
// that's bad design
//NSData *data=
NSData *data=[NSData dataWithBytesNoCopy:cstr length:resultLength freeWhenDone:YES];
NSData *data = [NSData dataWithBytesNoCopy:cstr length:resultLength freeWhenDone:YES];
NSZoneFree(NULL, buffer);
return cstr;
}
-(BOOL)getCString:(char *)cString maxLength:(NSUInteger)maxLength encoding:(NSStringEncoding)encoding {
NSRange range={0,[self length]};
unichar *unicode = NSZoneMalloc(NULL,maxLength*sizeof(unichar));
- (BOOL)getCString:(char *)cString maxLength:(NSUInteger)maxLength encoding:(NSStringEncoding)encoding
{
NSRange range = {0, [self length]};
unichar *unicode = NSZoneMalloc(NULL, maxLength * sizeof(unichar));
NSUInteger location;
[self getCharacters:unicode range:range];
if(NSGetAnyCStringWithMaxLength(encoding, unicode,range.length,&range.location,cString,maxLength,YES) ==NSNotFound) {
if (NSGetAnyCStringWithMaxLength(encoding, unicode, range.length, &range.location, cString, maxLength, YES) == NSNotFound) {
NSZoneFree(NULL, unicode);
return NO;
}
@ -1663,6 +1672,7 @@ U+2029 (Unicode paragraph separator), \r\n, in that order (also known as CRLF)
return YES;
}
+(NSStringEncoding)defaultCStringEncoding {
return defaultEncoding();
}
@ -1719,18 +1729,20 @@ U+2029 (Unicode paragraph separator), \r\n, in that order (also known as CRLF)
}
-(const char *)lossyCString {
NSUInteger length=[self length];
unichar *buffer = NSZoneMalloc(NULL,length*sizeof(unichar));
- (const char *)lossyCString
{
NSUInteger length = [self length];
unichar *buffer = NSZoneMalloc(NULL, length * sizeof(unichar));
NSUInteger resultLength;
[self getCharacters:buffer];
char *cstr=NSString_unicodeToAnyCString(defaultEncoding(), buffer,length,YES,&resultLength,NULL,YES);
NSData *data=[NSData dataWithBytesNoCopy:cstr length:resultLength freeWhenDone:YES];
char *cstr = NSString_unicodeToAnyCString(defaultEncoding(), buffer, length, YES, &resultLength, NULL, YES);
NSData *data = [NSData dataWithBytesNoCopy:cstr length:resultLength freeWhenDone:YES];
NSZoneFree(NULL, buffer);
return cstr;
}
@end
#import <Foundation/NSCFTypeID.h>

View File

@ -162,22 +162,23 @@ NSInteger sortTransitions(id trans1, id trans2, void *context) {
return _data;
}
+(NSTimeZone *)systemTimeZone {
NSTimeZone *systemTimeZone = nil;
NSString *timeZoneName;
NSInteger secondsFromGMT;
NSDictionary *dictionary;
+ (NSTimeZone *)systemTimeZone
{
NSTimeZone *systemTimeZone = nil;
NSString *timeZoneName;
NSInteger secondsFromGMT;
NSDictionary *dictionary;
if ([[NSFileManager defaultManager] fileExistsAtPath:@"/etc/localtime"] == YES) {
NSError *error;
NSString *path = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:@"/etc/localtime" error:&error];
NSError *error;
NSString *path = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:@"/etc/localtime" error:&error];
if(path != nil) {
if (path != nil) {
//localtime is a symlink
timeZoneName = [path stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@/", [NSTimeZone_posix _zoneinfoPath]] withString:@""];
systemTimeZone = [self timeZoneWithName:timeZoneName];
}
else {
} else {
//localtime is a file
systemTimeZone = [[[NSTimeZone alloc] initWithName:nil data:[NSData dataWithContentsOfFile:@"/etc/localtime"]] autorelease];
}
@ -185,7 +186,7 @@ NSInteger sortTransitions(id trans1, id trans2, void *context) {
if (systemTimeZone == nil) {
//try to use TZ environment variable
const char *envTimeZoneName = getenv("TZ");
const char *envTimeZoneName = getenv("TZ");
if (envTimeZoneName != NULL) {
systemTimeZone = [self timeZoneWithName:[NSString stringWithCString:envTimeZoneName]];
@ -193,7 +194,7 @@ NSInteger sortTransitions(id trans1, id trans2, void *context) {
}
if (systemTimeZone == nil) {
NSString *abbreviation;
NSString *abbreviation;
tzset();
abbreviation = [NSString stringWithCString:tzname[0]];
@ -201,12 +202,11 @@ NSInteger sortTransitions(id trans1, id trans2, void *context) {
systemTimeZone = [self timeZoneWithAbbreviation:abbreviation];
#ifdef LINUX
if(systemTimeZone == nil) {
if (systemTimeZone == nil) {
//check if the error is because of a missing entry in NSTimeZoneAbbreviations.plist (only for logging)
if([[self abbreviationDictionary] objectForKey:abbreviation] == nil) {
if ([[self abbreviationDictionary] objectForKey:abbreviation] == nil) {
NSCLog("Abbreviation [%s] not found in NSTimeZoneAbbreviations.plist -> using absolute timezone (no daylight saving)", [abbreviation cString]);
}
else {
} else {
NSCLog("TimeZone [%s] not instantiable -> using absolute timezone (no daylight saving)", [[[self abbreviationDictionary] objectForKey:abbreviation] cString]);
}
@ -218,6 +218,7 @@ NSInteger sortTransitions(id trans1, id trans2, void *context) {
return systemTimeZone;
}
-(NSTimeZoneType *)timeZoneTypeForDate:(NSDate *)date {
if ([_timeZoneTransitions count] == 0 ||
[date compare:[[_timeZoneTransitions objectAtIndex:0] transitionDate]] == NSOrderedAscending) {