<br><br><div class="gmail_quote">On Sat, Aug 25, 2012 at 10:23 PM, Dmitri Gribenko <span dir="ltr"><<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Fri, Aug 24, 2012 at 6:33 PM, Jordan Rose <<a href="mailto:jordan_rose@apple.com">jordan_rose@apple.com</a>> wrote:<br>
> On Aug 22, 2012, at 15:56 , Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com">gribozavr@gmail.com</a>> wrote:<br>
>> +bool isHTMLTagName(StringRef Name) {<br>
>> + return llvm::StringSwitch<bool>(Name)<br>
>> + .Cases("em", "strong", true)<br>
>> + .Cases("tt", "i", "b", "big", "small", true)<br>
>> + .Cases("strike", "s", "u", "font", true)<br>
>> + .Case("a", true)<br>
>> + .Case("hr", true)<br>
>> + .Cases("div", "span", true)<br>
>> + .Cases("h1", "h2", "h3", true)<br>
>> + .Cases("h4", "h5", "h6", true)<br>
>> + .Case("code", true)<br>
>> + .Case("blockquote", true)<br>
>> + .Cases("sub", "sup", true)<br>
>> + .Case("img", true)<br>
>> + .Case("p", true)<br>
>> + .Case("br", true)<br>
>> + .Case("pre", true)<br>
>> + .Cases("ins", "del", true)<br>
>> + .Cases("ul", "ol", "li", true)<br>
>> + .Cases("dl", "dt", "dd", true)<br>
>> + .Cases("table", "caption", true)<br>
>> + .Cases("thead", "tfoot", "tbody", true)<br>
>> + .Cases("colgroup", "col", true)<br>
>> + .Cases("tr", "th", "td", true)<br>
>> + .Default(false);<br>
>> +}<br>
><br>
> This is going to be very slow (StringSwitch just chains the string comparisons). Maybe we should use a StringMap instead?<br>
<br>
It is a good idea, but it would require some refactoring since<br>
StringMap should be populated at runtime and we want to do that only<br>
once.<br>
<span class="HOEnZb"><font color="#888888"><br>
Dmitri</font></span><br></blockquote><div><br>I am not sure a StringMap would be a good idea (memory-wise), it seems overkill and wasteful. If you want a simple whitelist, the best thing to do is a simple C-array, sorted, and a binary_search.<br>
<br>I think this should work:<br><br>static char const* const HTMLTags[] = { "a", "b", ... };<br>static size_t const HTMLTagsSize = sizeof(HtmlTag) / sizeof(char const*);<br><br>bool isHTMLTagName(StringRef Name) {<br>
#ifndef NDEBUG<br> static bool const IsSorted = std::adjacent_find(HTMLTags, HTMLTags + HTMLTagsSize, std::greater<StringRef>()) == HTMLTags + HTMLTagsSize;<br> assert(IsSorted && "The HTMLTags should be sorted in lexicographical order.");<br>
#endif<br> return std::binary_search(HTMLTags, HTMLTags + HTMLTagsSize, Name);<br>}<br><br>From past experience, it seems recommended to build an array of StringRef because it triggers the use of the constructors (unfortunately).<br>
<br>-- Matthieu.<br></div></div>