code综述:找到&LT; /身体GT;在非空终止字符海峡标签反向搜索 [英] code review: finding </body> tag reverse search on a non-null terminated char str
问题描述
的src
是一个非空终止字符长度的字符串 DATA_LEN
。
我想从这个数组月底开始,并找到 HTML&LT中第一次出现; / BODY方式&gt;
标签结果 find_pos
应持&LT的位置; / BODY&GT;
与标记的src
是否低于code看起来是正确的你?
的char * strrcasestr_len(为const char *干草,为size_t haylen,为const char * NDL,为size_t ndllen)
{
字符* RET = NULL;
INT I;
为(ⅰ= haylen - ndllen; I&GT; = 0;我 - ){
如果(strncasecmp(安培;!干草[Ⅰ],NDL,ndllen)){
打破;
}
}
如果(我== -1)
返回RET;
其他
回报(字符*)及干草[I]
}
这应该这样做,非常非常快的。
字符常量* find_body_closing_tag(字符常量* const的SRC,为size_t常量DATA_LEN)
{
静态字符表[256];
静态布尔inited;
如果(!inited){
表['&LT;'] = 1;
表['/'] = 2;
表['B'] =表['B'] = 3;
表['O'] =表['O'] = 4;
表['D'] =表['D'] = 5;
表['Y'] =表['Y'] = 6;
表['&GT;'] = 7;
inited = TRUE;
} 对于(字符常量* P = SRC + DATA_LEN - 7; P&GT; = SRC,P - = 7){
如果(字符偏移量=表[* P]){
如果(0 == strnicmp(对 - (偏移1),&所述; /体&gt;中,7))返回对 - (偏移1);
}
}
返回0;
}
另外一个非常快速的方法是使用SIMD来测试对 16的连续字符'&GT;
一次(这是什么 strrchr
或了memrchr
应该是这样做)。
src
is a non-null terminated char string whose length is data_len
.
I want to start from the end of this array, and find the first occurrence of html </body>
tag.
find_pos
should hold the position of the </body>
tag with src
Does the code below look correct to 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];
}
This should do it, very 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 fast approach would be using SIMD to test 16 consecutive characters against '>'
at once (and this is what strrchr
or memrchr
ought to be doing).
这篇关于code综述:找到&LT; /身体GT;在非空终止字符海峡标签反向搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!