josh_feinstein wrote:
> I have an NSString object that I would like to test to make sure that it
> contains only numeric characters and no alpha characters.
I agree with Bill that it is extremely likely there won't be any
significant performance impact for using whatever is most convenient.
If you do find yourself scanning many strings of thousands of digits
or whatever, though, I'd suggest using
OmniFoundation/OFStringScanner as the fast way:
#import <OmniFoundation/OFStringScanner.h>
- (BOOL)isAllDigits:(NSString *)string
{
static CSBitmap digitBitmap;
static OFStringScanner *scanner;
if (!digitBitmap) {
digitBitmap = bitmapForCharacterSetDoRetain(
[NSCharacterSet decimalDigitCharacterSet], YES);
scanner = [[OFStringScanner alloc] init];
}
[scanner fetchMoreDataFromString:string];
return !scannerScanUpToCharacterNotInCSBitmap(scanner, digitBitmap);
}
Only one scanner object is created ever, the decimal digit character
set is referenced only once, and the actual scanning happens in an
inline C function where each unicode character is indexed directly
into a bitmap.
The one drawback of using exactly this code is that, because of the
static scanner, it isn't thread-safe. If you needed thread-safety,
you can still create the digitBitmap only once, but should alloc,
use, and release the OFStringScanner for each call.
--Greg
---
Greg Titus
Omni Development Inc.
[EMAIL PROTECTED]