
Boyer–Moore–Horspool algorithm
发布日期:2021-05-14 03:34:43
浏览次数:20
分类:精选文章
本文共 3695 字,大约阅读时间需要 12 分钟。
#include/* Returns a pointer to the first occurrence of "needle" within "haystack", or NULL if not found. Works like memmem(). */ /* Note: In this example needle is a C string. The ending 0x00 will be cut off, so you could call this example with boyermoore_horspool_memmem(haystack, hlen, "abc", sizeof("abc")) */ const unsigned char *boyermoore_horspool_memmem(const unsigned char* haystack, size_t hlen, const unsigned char* needle, size_t nlen){ size_t scan = 0; size_t bad_char_skip[UCHAR_MAX + 1]; /* Sanity checks on the parameters */ if (nlen <= 0 || !haystack || !needle) return NULL; /* ---- Preprocess ---- */ /* Initialize the table to default value */ /* When a character is encountered that does not occur in the needle, we can safely skip ahead for the whole length of the needle */ for (scan = 0; scan <= UCHAR_MAX; scan++) { bad_char_skip[scan] = nlen; } /* C arrays have the first byte at [0], therefore: [nlen - 1] is the last byte of the array */ size_t last = nlen - 1; /* Then populate it with the analysis of the needle */ for (scan = 0; scan < last; scan++) { bad_char_skip[needle[scan]] = last - scan; } /* ---- Do the matching ---- */ /* Search the haystack, while the needle can still be within it */ while (hlen >= nlen) { /* scan from the end of the needle */ for (scan = last; haystack[scan] == needle[scan]; scan = scan - 1) { if (scan == 0) { /* If the first byte matches, we've found it */ return haystack; } } /* otherwise, we need to skip some bytes and start again. Note that here we are getting the skip value based on the last byte of needle, no matter where we didn't match. So if needle is: "abcd" then we are skipping based on 'd' and that value will be 4, and for "abcdd" we again skip on 'd' but the value will be only 1. The alternative of pretending that the mismatched character was the last character is slower in the normal case (E.g. finding "abcd" in "...azcd..." gives 4 by using 'd' but only 4-2==2 using 'z'. */ hlen -= bad_char_skip[haystack[last]]; haystack += bad_char_skip[haystack[last]]; } return NULL; } reference:
������������������������������������memmem������������������������������������������eterangan������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月20日 02时33分18秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
[源码解析] 消息队列 Kombu 之 基本架构
2021-05-09
[源码分析] 消息队列 Kombu 之 启动过程
2021-05-09
[源码分析] 消息队列 Kombu 之 Consumer
2021-05-09
抉择之苦
2021-05-09
wx.NET CLI wrapper for wxWidgets
2021-05-09
ASP.NET MVC Action Filters
2021-05-09
Powershell中禁止执行脚本解决办法
2021-05-09
HTTP协议状态码详解(HTTP Status Code)
2021-05-09
OO_Unit2 多线程电梯总结
2021-05-09
04_Mysql配置文件(重要参数)
2021-05-09
python 序列化及其相关模块(json,pickle,shelve,xml)详解
2021-05-09
js编写动态时钟
2021-05-09
JavaSE总结
2021-05-09
手动造轮子——基于.NetCore的RPC框架DotNetCoreRpc
2021-05-09
Python IO编程
2021-05-09
CSS入门总结
2021-05-09