分享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 ...
随机推荐
- jqGrid基本用法与示例
转自:https://chuanlu.iteye.com/blog/1953544 一.jqGrid的基本用法 1.html页面 <!DOCTYPE html PUBLIC "-//W ...
- CDialogEx::OnPaint()的问题,或者为什么在对话框程序的OnPaint中绘图无效的问题
这是一个基于对话框的程序,对话框上有按钮,还有几个CStatic用来绘图,之前都是好好的,今天改成Unicode版本后,编译正常,运行时CStatic中的图像怎么也不显示,有时候会闪现一次就消失,问题 ...
- FTRL的理解
https://blog.csdn.net/ningyanggege/article/details/81133785
- asp.net文件/大文件上传需要配置的项目整理
HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求. 最可能的原因: •Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值. 可尝 ...
- 项目(五)jumpserver企业开源跳板机搭建
跳板机是什么?跳板机是运维堡垒主机的另个称呼.作为技术或者运维人员应该不会陌生.企业为了服务器的安全,通常所有的ssh连接都是通过跳板机来完成,以便于对ssh连接进行验证和管理. 接下来,我来讲述一下 ...
- OpenStack 安装:基本环境准备
刚刚学完openstack,这几篇文章就算对过去课程的一个总结吧. 首先说说基本的结构:在一台Dell的workstation上面安装了VMware,在VMware上面安装两台CentOS,现在给每台 ...
- github 访问速度太慢
151.101.112.249 http://global-ssl.fastly.Net 192.30.253.112 http://github.com 在hosts文件添加 另 pip里的pyec ...
- 转)Ubuntu16.04下安装DDD(Data Display Debugger)
以下转自:http://www.linuxdiyf.com/linux/26393.html 前两天在Linux论坛偶然间看到了DDD这个软件,根据介绍是一个gdb界面化的调试软件,这正是我找了好 ...
- 93. Restore IP Addresses产生所有可能的ip地址
[抄题]: Given a string containing only digits, restore it by returning all possible valid IP address c ...
- [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...