[Top][All Lists]

[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 15:45:31 +0000


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)]])
          if (NSMaxRange(decimalPlaceRange) == [useFormat 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?

Graham J Lee

reply via email to

[Prev in Thread] Current Thread [Next in Thread]