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还要快)的更多相关文章

  1. PHP pos() 函数

    实例 输出数组中的当前元素的值: <?php$people = array("Peter", "Joe", "Glenn", &quo ...

  2. php 自定义求数组差集,效率比自带的array_diff函数还要快(转)

    <?phpfunction array_different($array_1, $array_2) { $array_2 = array_flip($array_2); //将数组键值调换 fo ...

  3. 【SQL】分享表值函数FMakeRows,用于生成行

    ------------更新:201501071730------------ 评论中又有一位[笑东风]兄给出改善建议,在此先感谢他.原理是借助行数较多的一个系统视图sys.all_columns与自 ...

  4. locals()函数访问当前还在作用范围内的局部变量

    >>> element = 'silver' >>> number = 47 >>> 'Element {number} is {element} ...

  5. main函数执行前后还会发生什么

    问题分析 首先main()函数只不过是提供了一个函数入口,在main()函数中的显示代码执行之前,会由编译器生成_main函数,其中会进行所有全局对象的构造以及初始化工作.简单来说对静态变量.全局变量 ...

  6. 微信分享JS函数(原创)[已失效]

    //微信内置浏览器分享事件 //来自:http://www.cnblogs.com/cielwater //分享朋友圈事件 //UpdateWeixinJSBridge(CircleModel[Jso ...

  7. 16位图像Alpha混合的实现(用汇编写的,比MMX还要快)

    Alpha 混合的算法很简单,基于下面的公式就可以实现: D := A * (S - D) / 255 + D D 是目标图像的像素, S 是源图像的像素 A 是 Alpha 值, 0 为全透明, 2 ...

  8. 打造比Dictionary还要快2倍以上的字查找类

    针对一个长度为n的数组. [1,2,3,4,5,6,7,8,9] 最快的通用查找类是Dictionary其采用hashcode算法,复杂度为O(1). 而上大学时,最快的查找法为二分查找法,复杂度为O ...

  9. 比JSONKit还要快的第三方JSON解析器NextiveJson

    这款比JSONKit还好用,效率跟iOS5原生的差不多,不过解析后对内存的释放比原生的要多.所以推荐 https://github.com/nextive/NextiveJson 顺便提一下解析XML ...

随机推荐

  1. SecureCR 改变背景色和文字颜色

    1.打开SecureCR链接Linux服务器,Options->Session Options->Emulation->Terminal 选择Linux (相应的服务器系统)ANSI ...

  2. linux上用route添加/删除路由

    1. 查看 route -n 2. 添加 route add -net 9.123.0.0 netmask 255.255.0.0 gw 9.123.0.1 3. 删除 route del -net ...

  3. leetcode53

    public class Solution { public int MaxSubArray(int[] nums) { int max = int.MinValue; ; ; i < nums ...

  4. python中面向对象元类的自定义用法

    面向对象中的常用方法 1.instance 和 issubclass instance :判断两个对象是不是一类 issubclass :判断某个类是不是另一个类的子类 #两个常用方法的使用 clas ...

  5. [Ting's笔记Day2]在Github用Jekyll创建自己的blog

    昨天工程师在我们共同的群组分享他的blog,他提到是使用Jekyll(一个简单静态blog网站生成器)架在github上的. 于是好奇的我决定照着关键字来搜寻一下,如法炮制做一个出来. 也可以放一份到 ...

  6. 针对piix4_smbus ****host smbus controller not enabled的解决方法

    SMBus 目录 SMBus与I2C的差别 SMBus 是 System Management Bus 的缩写,是1995年由Intel提出的,应用于移动PC和桌面PC系统中的低速率通讯.它主要是希望 ...

  7. C++继承中关于子类构造函数的写法

    构造方法用来初始化类的对象,与父类的其它成员不同,它不能被子类继承(子类可以继承父类所有的成员变量和成员方法,但不继承父类的构造方法).因此,在创建子类对象时,为了初始化从父类继承来的数据成员,系统需 ...

  8. js函数内未声明变量

    <script> function test(){ testd = "Hello"; } test(); alert(testd); </script> 当 ...

  9. node.js中使用zlib模块进行数据压缩和解压

    我们可以使用 zlib 模块来对数据进行压缩和解压处理,减小数据体积,加快传输速度. 一.通过创建转换流,对文件进行压缩和解压 const fs = require('fs'); const zlib ...

  10. 利用Sharding-Jdbc实现分表[z]

    [z]https://www.cnblogs.com/codestory/p/5591651.html 你们团队使用SpringMVC+Spring+JPA框架,快速开发了一个NB的系统,上线后客户订 ...