Efficiently check string for one of several hundred possible suffixes

Posted by Ghostrider on Stack Overflow See other posts from Stack Overflow or by Ghostrider
Published on 2010-04-04T17:09:44Z Indexed on 2010/04/04 17:13 UTC
Read the original article Hit count: 409

Filed under:
|
|
|
|

I need to write a C/C++ function that would quickly check if string ends with one of ~1000 predefined suffixes. Specifically the string is a hostname and I need to check if it belongs to one of several hundred predefined second-level domains.

This function will be called a lot so it needs to be written as efficiently as possible. Bitwise hacks etc anything goes as long as it turns out fast.

Set of suffixes is predetermined at compile-time and doesn't change.

I am thinking of either implementing a variation of Rabin-Karp or write a tool that would generate a function with nested ifs and switches that would be custom tailored to specific set of suffixes. Since the application in question is 64-bit to speed up comparisons I could store suffixes of up to 8 bytes in length as const sorted array and do binary search within it.

Are there any other reasonable options?

© Stack Overflow or respective owner

Related posts about c++

Related posts about algorithm