分享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 ...
随机推荐
- yarn 报错 requested virtual cores < 0, or requested virtual cores > max configured, requestedVirtualCores=6, maxVirtualCores=4 原因
INFO ApplicationMaster:54 - Final app status: FAILED, exitCode: 13, (reason: Uncaught exception: org ...
- jsonArray返回
dao <select id="selectShopInfo" resultType="java.util.HashMap"> SELECT * F ...
- 在cxGrid表格中如何获得当前列的字段名
var GridDBTableView:TcxGridDBTableView; ColIndex:Integer; FieldName:string; begin GridDBTableView := ...
- 【395】yield 和 yield from
yield:生成器 yield from:将生成器 yield 的内容相当于逐一在 yield 一般 参考:Python 3: Using "yield from" in Gene ...
- JAVA设计模式一策略模式(Strategy Pattern)
什么是设计模式? 就是一些经验.让程序代码更具弹性.好维护.代码复用的经验.而且设计模式都遵从一些OO设计原则. 题外话:以下罗列出常用的OO设计原则:链接 本文章介绍策略模式(Strategy Pa ...
- BM递推
从别的大佬处看到的模板 #include<bits/stdc++.h> #define fi first #define se second #define INF 0x3f3f3f3f ...
- Django_Form验证(一)
Django为我们提供了一个模板来做Form验证,不需要我们再去写复杂的验证代码了 简单的提交信息html页面: <form action="/fff/Form" metho ...
- JAVA数据库连接池C3p0 以及阿里Druid提供的连接池
一:连接池的定义 本质上就是个容器(集合) 存放数据库连接的容器,当系统初始化后,容器被创建,容器中就会申请一些连接对象,当用户来访问数据库的时候,从容器中取连接对象,用户用完之后,归还. 二:常用的 ...
- ubuntu开启远程shell,开启上传下载
需要先安装openshell-server 具体命令如下: 1.先更新下源 sudo apt-get update 2.安装openshell-server sudo apt-get install ...
- Sublime Text3快捷键大全
选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数 ...