Code review: search for the </body> tag of reverse search on a non-null completed char page

src is a null-terminated char string whose length is data_len . I want to start at the end of this array and find the first occurrence of the html </body> .
find_pos must contain the position of the </body> with src

Is the code below suitable for you?

 char *strrcasestr_len(const char *hay, size_t haylen, const char *ndl,size_t ndllen) { char *ret = NULL; int i; for (i = haylen - ndllen; i >= 0; i--) { if (!strncasecmp(&hay[i], ndl, ndllen)) { break; } } if (i == -1) return ret; else return (char *)&hay[i]; } 
-one
c
source share
1 answer

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).

+2
source share

All Articles