Loading

Paste #p4qr0dr0g

  1. static Class    NSStringClass = 0;
  2. static Class    NSScannerClass = 0;
  3. static SEL  scanFloatSel;
  4. static SEL  scanStringSel;
  5. static SEL  scannerSel;
  6. static BOOL     (*scanFloatImp)(NSScanner*, SEL, CGFloat*);
  7. static BOOL     (*scanStringImp)(NSScanner*, SEL, NSString*, NSString**);
  8. static id  (*scannerImp)(Class, SEL, NSString*);
  9.  
  10. static inline void
  11. setupCache(void)
  12. {
  13.   if (NSStringClass == 0)
  14.     {
  15.       NSStringClass = [NSString class];
  16.       NSScannerClass = [NSScanner class];
  17.       if (sizeof(CGFloat) == sizeof(double))
  18.         {
  19.           scanFloatSel = @selector(scanDouble:);
  20.         }
  21.       else
  22.   {
  23.           scanFloatSel = @selector(scanFloat:);
  24.         }
  25.       scanStringSel = @selector(scanString:intoString:);
  26.       scannerSel = @selector(scannerWithString:);
  27.       scanFloatImp = (BOOL (*)(NSScanner*, SEL, CGFloat*))
  28.         [NSScannerClass instanceMethodForSelector: scanFloatSel];
  29.       scanStringImp = (BOOL (*)(NSScanner*, SEL, NSString*, NSString**))
  30.         [NSScannerClass instanceMethodForSelector: scanStringSel];
  31.       scannerImp = (id (*)(Class, SEL, NSString*))
  32.         [NSScannerClass methodForSelector: scannerSel];
  33.     }
  34. }
  35.  
  36. // и потом
  37.  
  38. NSSize NSSizeFromString(NSString* string)
  39. {
  40.         NSScanner       *scanner;
  41.         NSSize          size;
  42.  
  43.         setupCache();
  44.         scanner = (*scannerImp)(NSScannerClass, scannerSel, string);
  45.         if ((*scanStringImp)(scanner, scanStringSel, @"{", NULL)
  46.                 && (*scanFloatImp)(scanner, scanFloatSel, &size.width)
  47.                  && (*scanStringImp)(scanner, scanStringSel, @",", NULL)
  48.                 && (*scanFloatImp)(scanner, scanFloatSel, &size.height)
  49.                 && (*scanStringImp)(scanner, scanStringSel, @"}", NULL))
  50.     {
  51.                 return size;
  52.     }
  53.     // <...>
  54. }