• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Speeding up XCode?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Speeding up XCode?


  • Subject: Re: Speeding up XCode?
  • From: email@hidden
  • Date: Fri, 21 Oct 2005 20:49:44 +1000

do you know of any other way i can get a string of say "4n35a" to return the numeric result of 435 or a string "2.3906" to return the numeric value of 23906?
there's a reason for this routine. when the system can't handle wht you need, make it. i'm inputting a string such as " 43 25% learning" and need the numeric value 4325 from that and ignore the rest.

long long result = 0, intermediate, base = 10 /* Adapt to suit */;
unsigned int initialPosition, newPosition;
NSCharacterSet *decimalDigits = [NSCharacterSet decimalDigitCharacterSet];
NSScanner *scanner = [NSScanner scannerWithString:myString];


[scanner setCharactersToBeSkipped:[decimalDigits invertedSet]];

while (![scanner isAtEnd]) {
    initialPosition = [scanner scanLocation];

    if ([scanner scanLongLong:&intermediate]) {
        newPosition = [scanner scanLocation];

        while (initialPosition < newPosition) {
            result *= base;
            ++initialPosition;
        }

        result += intermediate;
    }

    [scanner scanUpToCharactersFromSet:decimalDigits intoString:nil];
}

Dunno if it's fast, but it's accurate. And that's far more important in 99% of cases. If you really wanted to optimise it, you'd start by considering if you're happy using doubles instead of long longs, in which case you could replace that innermost loop with a single call to pow(). Then you ask yourself if you specifically *don't* want Unicode support... in which case you can look at simpler, faster implementations using vanilla chars.

Although I should add that this breaks if NSCharacterSet's decimalDigitCharacterSet contains "-" or similar... I don't think it does, but the documentation is ambiguous. Also, I'm assuming NSScanner accepts decimal digits in any encoding contained within the decimalDigitCharacterSet, which is noted explicitly to include Arabic representations, etc.

And, warning: typed in Mail.

Wade Tregaskis

    ICQ: 40056898
    AIM, Yahoo & Skype: wadetregaskis
    MSN: email@hidden
    iChat & email: email@hidden
    Jabber: email@hidden
    Google Talk: email@hidden

    http://homepage.mac.com/wadetregaskis/

-- Sed quis custodiet ipsos custodes?


_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Speeding up XCode?
      • From: email@hidden
    • Re: Speeding up XCode?
      • From: Cameron Hayne <email@hidden>
References: 
 >RE: Speeding up XCode? (From: Jeff Laing <email@hidden>)
 >Re: Speeding up XCode? (From: Robert Dell <email@hidden>)

  • Prev by Date: Re: I can't build with Xcode 2.2_preview3_8m639_018213953
  • Next by Date: Re: STL behaving improperly
  • Previous by thread: Re: Speeding up XCode?
  • Next by thread: Re: Speeding up XCode?
  • Index(es):
    • Date
    • Thread