分享Pos函数(比FastPos还要快)
function QuickPos(const Substr, S: WideString; MatchesIndex: Integer = ): Integer;
function QuickPosBack(const Substr, S: WideString; MatchesReverseIndex: Integer = ): Integer; 主要用途是搜索字符串中第n个Substr。
经过测试,这2个函数的速度比直接用Pos+Copy快好几倍(如果字符串够长,可能10几倍)
比Pos+Delete(如JVCL的函数NPos)处理要快(至少2-倍以上)。 说明1:虽然说速度上的差异是微秒级别的,但是一个快速方便的Pos函数,还是会给编程带来很多方便。
说明2:经过大量测试和FastString作者的承认,FastString的速度还没有DELPHI标准函数快。 ========================================================================
// Compares a substring with a string. *for inline use"
// C: 2004-07-05 | M: 2004-07-05
function _InlineCompareText(const Substr, S: WideString; StartIndex: Integer = ; LenOfSubstr: Integer = -; LenOfS: Integer = -): Boolean;
var
I: Integer;
begin
if LenOfSubstr = - then LenOfSubstr := Length(Substr);
if LenOfS = - then LenOfS := Length(S);
if LenOfSubstr > LenOfS then
begin
Result := False;
Exit;
end;
for I := to LenOfSubstr do
if Substr[I] <> S[I + StartIndex - ] then
begin
Result := False;
Exit;
end;
Result := True;
end; // Returns the 1. index of a substring within a string start at a certain index.
// C: 2004-07-05 | M: 2004-07-05 | P: 1.0+
function _PosForward(const Substr, S: WideString; StartIndex: Integer; LenOfSubstr: Integer = -; LenOfS: Integer = -): Integer;
var
I: Integer;
begin
Result := ;
case LenOfSubstr of
: Exit;
-: LenOfSubstr := Length(Substr);
end;
if LenOfS = - then LenOfS := Length(S); for I := StartIndex to LenOfS do
begin
if (S[I] = Substr[]) and _InlineCompareText(Substr, S, I, LenOfSubstr, LenOfS) then
begin
Result := I;
Exit;
end;
end;
end; // Returns the 1. index of a substring within a string.
// Note: Searching time will increase when MatchesIndex increased.
// C: 2004-04-09 | M: 2004-07-05 | P: 1.0+
function QuickPos(const Substr, S: WideString; MatchesIndex: Integer = ): Integer;
var
LenOfS, LenOfSubstr: Integer;
begin
Result := Pos{Pos}(Substr, S); if (MatchesIndex = ) or (Result = ) then Exit;
LenOfS := Length(S);
LenOfSubstr := Length(Substr); while (MatchesIndex > ) and (Result > ) do
begin
Result := _PosForward{Pos}(Substr, S, Result + , LenOfSubstr, LenOfS); // Tip!! Do not use func.Copy!!
if Result = then Exit;
Dec(MatchesIndex);
end;
end; // Returns the last index of a substring within a string.
// Todo: Using asm to rewrite this function. The asm-code looks very like func.Pos!
// C: 2004-04-09 | M: 2004-07-03 | P: n/a
function _PosBack(const Substr, S: WideString; StopIndex: Integer = -; LenOfSubstr: Integer = -): Integer;
var
I: Integer;
begin
Result := ;
case LenOfSubstr of
: Exit;
-: LenOfSubstr := Length(Substr);
end;
if StopIndex = - then StopIndex := Length(S); for I := StopIndex - LenOfSubstr + downto do
begin
if (S[I] = Substr[]) and _InlineCompareText(Substr, S, I, LenOfSubstr) then
begin
Result := I;
Exit;
end;
end;
end; // Returns the last index of a substring within a string.
// C: 2004-04-09 | M: 2004-07-03 | P: n/a
function QuickPosBack(const Substr, S: WideString; MatchesReverseIndex: Integer = ): Integer;
var
LenOfSubstr: Integer;
begin
Result := _PosBack{Pos}(Substr, S); if (MatchesReverseIndex = ) or (Result = ) then Exit;
LenOfSubstr := Length(Substr); while (MatchesReverseIndex > ) and (Result > ) do
begin
Result := _PosBack{Pos}(Substr, S, Result + LenOfSubstr - , LenOfSubstr);
Dec(MatchesReverseIndex);
end;
end;
分享Pos函数(比FastPos还要快)的更多相关文章
- PHP pos() 函数
实例 输出数组中的当前元素的值: <?php$people = array("Peter", "Joe", "Glenn", &quo ...
- php 自定义求数组差集,效率比自带的array_diff函数还要快(转)
<?phpfunction array_different($array_1, $array_2) { $array_2 = array_flip($array_2); //将数组键值调换 fo ...
- 【SQL】分享表值函数FMakeRows,用于生成行
------------更新:201501071730------------ 评论中又有一位[笑东风]兄给出改善建议,在此先感谢他.原理是借助行数较多的一个系统视图sys.all_columns与自 ...
- locals()函数访问当前还在作用范围内的局部变量
>>> element = 'silver' >>> number = 47 >>> 'Element {number} is {element} ...
- main函数执行前后还会发生什么
问题分析 首先main()函数只不过是提供了一个函数入口,在main()函数中的显示代码执行之前,会由编译器生成_main函数,其中会进行所有全局对象的构造以及初始化工作.简单来说对静态变量.全局变量 ...
- 微信分享JS函数(原创)[已失效]
//微信内置浏览器分享事件 //来自:http://www.cnblogs.com/cielwater //分享朋友圈事件 //UpdateWeixinJSBridge(CircleModel[Jso ...
- 16位图像Alpha混合的实现(用汇编写的,比MMX还要快)
Alpha 混合的算法很简单,基于下面的公式就可以实现: D := A * (S - D) / 255 + D D 是目标图像的像素, S 是源图像的像素 A 是 Alpha 值, 0 为全透明, 2 ...
- 打造比Dictionary还要快2倍以上的字查找类
针对一个长度为n的数组. [1,2,3,4,5,6,7,8,9] 最快的通用查找类是Dictionary其采用hashcode算法,复杂度为O(1). 而上大学时,最快的查找法为二分查找法,复杂度为O ...
- 比JSONKit还要快的第三方JSON解析器NextiveJson
这款比JSONKit还好用,效率跟iOS5原生的差不多,不过解析后对内存的释放比原生的要多.所以推荐 https://github.com/nextive/NextiveJson 顺便提一下解析XML ...
随机推荐
- jpg转yuv420抠图后转为jpg
最近遇到个需求,已有全景图和其中的人脸坐标,将人脸小图从全景图中抠出来,最开始使用libjpeg,奈何使用libjpeg将jpg转为yuv420的资料实在少,libjpeg自身的readme和exam ...
- C#使用GET、POST请求获取结果,这里以一个简单的用户登陆为例。
1. 使用GET请求获取结果 1.1 创建LoginHandler.aspx处理页面 protected void Page_Load(object sender, EventArgs e) { st ...
- win2012R2打Windows8.1-KB2919355 问题
解决方法 https://blog.csdn.net/qwq1503/article/details/65916426
- js:作用域总结1
先说几个概念: 1.js代码从上往下执行 2.变量提升: 变量提升是浏览器的一个功能,在运行js代码之前,浏览器会给js一个全局作用域叫window,window分两个模块,一个叫内存模块,一个叫运行 ...
- intellij idea 配置gitlab ssh key
1 安装git,登录官网https://www.git-scm.com/download/ ,选择相应系统版本,下载后安装好. 公司网慢的可以用第三方的软件管家下载. 2 打开git bash,不需要 ...
- JQEUERY案例
案例效果: 点击显示全部奶粉品牌前: 点击后: 源码: <!DOCTYPE html><html><head> <meta charset="utf ...
- jeesite 下载ckfinder上传的文件
在需要下载的位置,将以下代码复制到页面最下方,就可以实现文件下载了 <script> $(document).ready(function() { var fileName = $(&qu ...
- scrapy爬虫框架处理流程简介
1.SPIDERS的yeild将request发送给ENGIN2.ENGINE对request不做任何处理发送给SCHEDULER3.SCHEDULER( url调度器),生成request交给ENG ...
- git tag的用法
我们常常在代码封板时,使用git 创建一个tag ,这样一个不可修改的历史代码版本就像被我们封存起来一样,不论是运维发布拉取,或者以后的代码版本管理,都是十分方便的 git的tag功能 git 下打标 ...
- http://ctf.bugku.com/challenges#love:bugku--love
做了一道逆向题目,主要关联到base64编码的知识点.下面做一分析. 题目如下: 通过测试,可知它没有加壳.尝试使用IDA进行分析. 1.IDA分析文件 打开文件后,按[shift+F12 ...