That should do it very fast.
char const* find_body_closing_tag( char const* const src, size_t const data_len ) { static char table[256]; static bool inited; if (!inited) { table['<'] = 1; table['/'] = 2; table['b'] = table['B'] = 3; table['o'] = table['O'] = 4; table['d'] = table['D'] = 5; table['y'] = table['Y'] = 6; table['>'] = 7; inited = true; } for( char const* p = src + data_len - 7; p >= src; p -= 7 ) { if (char offset = table[*p]) { if (0 == strnicmp(p - (offset-1), "</body>", 7)) return p - (offset-1); } } return 0; }
Another very quick approach is to use SIMD to test 16 consecutive characters with '>' right away (and this is what strrchr or memrchr needs to do).
Ben voigt
source share