[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Strings and NSRanges
From: |
Graham J Lee |
Subject: |
Strings and NSRanges |
Date: |
Sun, 19 Nov 2006 19:52:54 +0000 |
Looks like this hasn't come through after four hours, so trying again...
I'm trying to implement NSNumberFormatter's -stringForObjectValue: as
I said on bug-. However, I'm having trouble with parsing the format
string and I expect it has more to do with my confusion than it does
with any other problems ;-).
The Cocoa behaviour for the formatter is that if the character '.'
appears anywhere in the format string, this "turns on" displaying the
fractional part of the number (unless allowsFloats is false) with as
many decimal places as the are placeholder characters (# or a number)
to the *immediate* right of the *rightmost* '.' in the format
string. So I'm doing this:
if ([self allowsFloats] && (NSNotFound != [useFormat
rangeOfString:@"." ].location))
{
decimalPlaceRange = [useFormat rangeOfString: @"." options:
NSBackwardsSearch];
while ([placeHolders characterIsMember: [useFormat
characterAtIndex: NSMaxRange(decimalPlaceRange)]])
{
decimalPlaceRange.length++;
if (NSMaxRange(decimalPlaceRange) == [useFormat length])
break;
}
decimalPlaces=decimalPlaceRange.length;
if (0 != decimalPlaces)
displayFractionalPart = YES;
}
which doesn't work...decimalPlaces ends up being some garbage value
such as 2412439 when the length of the string is only, say, 7. For
that to happen then the length of the *range* must have always been
greater than the length of the string (so that the if(NSMaxRange
(...)...) line never breaks the loop) and the loops *starts* by
reading nonsense memory outside the string. Why would that happen?
Thanks,
Graham.
--
Graham J Lee
http://www.thaesofereode.info/
- Strings and NSRanges,
Graham J Lee <=