Strings and NSRanges

Graham J Lee <[email protected]>
Newsgroups gmane.comp.lib.gnustep.user
Message-ID <[email protected]>
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/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.