分享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 ...
随机推荐
- 在线安装TIDB集群
在线安装TiDB集群 服务器准备 说明:TiDB8需要能够连接外网,以便下载各类安装包 TiDB4非必须,但最好是有一台,因为后续测试Mysql数据同步或者进行性能比较时,都要用到 TiKV最好是采 ...
- Redis 主从集群搭建及哨兵模式配置
最近搭建了redis集群及哨兵模式,为方便以后查看特此记录下来: 1.Redis安装 2.主从架构 2.1 Redis主从架构图 2.2Redis主从结构搭建 Redis集群不用安装多个Redis,只 ...
- 转载:c++深拷贝和浅拷贝
文章来自:http://blog.csdn.net/u010700335/article/details/39830425 C++中类的拷贝有两种:深拷贝,浅拷贝:当出现类的等号赋值时,即会调用拷贝函 ...
- python练习题_01
1.执行python的两种方式 答:1.通过解释器执行1.py 2.通过cmd执行python,再执行1.py 2.简述位与字节的的关系 答:8位=1字节(计算机处理时以字节为单位,存储时以位为单位) ...
- js:捕获冒泡和事件委托
一.事件流(捕获,冒泡) 事件流:指从页面中接收事件的顺序,有冒泡流和捕获流. 当页面中发生某种事件(比如鼠标点击,鼠标滑过等)时,毫无疑问子元素和父元素都会接收到该事件,可具体顺序是怎样的呢?冒 ...
- NYOJ252-01串-(数位dp)
252-01串 内存限制:64MB 时间限制:1000ms 特判: No通过数:33 提交数:49 难度:2 题目描述: ACM的zyc在研究01串,他知道某一01串的长度,但他想知道不含有“11”子 ...
- java文件转发
实际开发情景中,有时会遇到文件需要从一台服务器发到另一台服务器的情况,比如外网发到内网,服务器之间文件同步的情况. 就可以用文件转发. 转发端代码: /** * * @param fileName 保 ...
- linux环境下安装oracle步骤和自启动oracle
oracle安装步骤 一.创建用户 --注释-- /etc/passwd 用户配置文件 /etc/shadow 用户密码文件 /etc/group 组 组用户文件/etc/gshadow 组密码文件 ...
- Nginx – rewrite 配置 URL重写及301跳转原理图
Nginx – rewrite 配置 URL重写 官网:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 语法:rewrite re ...
- Java18-java语法基础——集合框架
Java18-java语法基础——集合框架 一.什么是集合框架 1.集合框架:是为表示和操作集合而规定的一种统一的.标准的体系结构. 2.任何集合框架都包含三大块内容:对外的接口.接口的实现和对集合运 ...