2012-02-18 45 views
1

我有一個字符串(tagName),我想知道它是否與以下任何字符串匹配。什麼是最好/最有效的方式來做到這一點?使用數組並通過它循環?或者這種醜陋的方式是最好的方式?最優雅/有效的方法來比較一個字符串與許多目標c?

if ([tagName isEqualToString:@"a"] || 
      [tagName isEqualToString:@"dd"] || 
      [tagName isEqualToString:@"li"] || 
      [tagName isEqualToString:@"span"] || 
      [tagName isEqualToString:@"br"] || 
      [tagName isEqualToString:@"b"] || 
      [tagName isEqualToString:@"big"] || 
      [tagName isEqualToString:@"em"] || 
      [tagName isEqualToString:@"i"] || 
      [tagName isEqualToString:@"u"] || 
      [tagName isEqualToString:@"small"] || 
      [tagName isEqualToString:@"strong"] || 
      [tagName isEqualToString:@"sub"] || 
      [tagName isEqualToString:@"sup"] || 
      [tagName isEqualToString:@"ins"] || 
      [tagName isEqualToString:@"del"] || 
      [tagName isEqualToString:@"code"] || 
      [tagName isEqualToString:@"kbd"] || 
      [tagName isEqualToString:@"samp"] || 
      [tagName isEqualToString:@"tt"] || 
      [tagName isEqualToString:@"var"] || 
      [tagName isEqualToString:@"pre"] || 
      [tagName isEqualToString:@"abbr"] || 
      [tagName isEqualToString:@"center"] || 
      [tagName isEqualToString:@"acronym"] || 
      [tagName isEqualToString:@"address"] || 
      [tagName isEqualToString:@"bdo"] || 
      [tagName isEqualToString:@"blockquote"] || 
      [tagName isEqualToString:@"q"] || 
      [tagName isEqualToString:@"cite"] || 
      [tagName isEqualToString:@"img"] || 
      [tagName isEqualToString:@"p"] || 
      [tagName isEqualToString:@"s"] || 
      [tagName isEqualToString:@"font"] || 
      [tagName isEqualToString:@"strike"] || 
      [tagName isEqualToString:@"caption"] || 
      [tagName isEqualToString:@"th"] || 
      [tagName isEqualToString:@"tr"] || 
      [tagName isEqualToString:@"td"] || 
      [tagName isEqualToString:@"thead"] || 
      [tagName isEqualToString:@"tbody"] || 
      [tagName isEqualToString:@"tfoot"] || 
      [tagName isEqualToString:@"col"] || 
      [tagName isEqualToString:@"colgroup"] || 
      [tagName isEqualToString:@"dfn"] 
      ) { 

回答

6
static dispatch_once_t once; 
static NSSet *htmlTags; 
dispatch_once(&once, ^{ 
    htmlTags = [NSSet setWithObjects: 
     @"dd", @"li", @"span", 
     @"br", @"b", @"big", 
     // etc. 
     nil]; 
}); 

if ([htmlTags member:tagName]) { 
    NSLog(@"Found it!"); 
} 
+0

啊,對不起,我誤解,你是正確的。 – rooftop 2012-02-18 22:24:01

1

不將標記成小塊(例如,通過長度),或明知匹配參數的精確執行......這裏有一個非常快速的方法,它可以爲特定的情況下,可以進一步優化:

bool IsTag(NSString * tagName) { 
    const size_t NTags = 45; 
    NSString * const tags[NTags] = { 
     @"a", @"dd", @"li", @"span", @"br", @"b", @"big", @"em", @"i", @"u", 
     @"small", @"strong", @"sub", @"sup", @"ins", @"del", @"code", 
     @"kbd", @"samp", @"tt", @"var", @"pre", @"abbr", @"center", @"acronym", 
     @"address", @"bdo", @"blockquote", @"q", @"cite", @"img", @"p", @"s", 
     @"font", @"strike", @"caption", @"th", @"tr", @"td", @"thead", @"tbody", 
     @"tfoot", @"col", @"colgroup", @"dfn" 
    }; 

    /* pointer comparison will be effective if @a tagName may be derived 
     from a literal (or a copy of a literal): 
    */ 
    for (size_t idx = 0; idx < NTags; ++idx) { 
     if (tags[idx] == tagName) { 
      return true; 
     } 
    } 

    /* no match yet - perform character comparison: */ 
    for (size_t idx = 0; idx < NTags; ++idx) { 
     if ([tags[idx] isEqualToString:tagName]) { 
      return true; 
     } 
    } 

    return false; 
} 

當然,如果你經常這樣做,你可以劃分這一個函數來使用更優雅。

Rob的回答也很好,但如果你真的想要最快的話,你需要應用程序執行的上下文。我的方法可能比Rob的要快許多倍,或者Rob的速度可能比這種方法快許多倍 - 這取決於執行上下文!

0

某種散列表,如NSDictionary或NSHashTable。 [OR的NSSet]

(儘管要非常高效地搜索基數if梯子檢測單個字符是最快的一個真正固定的列表。)

相關問題