比 FastPos 还要快 Pos 函数

本文介绍了一种在DELPHI中快速查找特定子字符串出现次数的方法,包括自定义函数QuickPos和QuickPosBack,它们比标准函数更高效且支持Unicode。

相信字符串处理中用的最多的就是 Pos 函数了。但是如果要搜索一个字符串中第二次或者第三次出现的子字符串的,就没有现成的 DELPHI 标准函数了。所以我就自己写了一个。同时和网上比较流行的 FastStrings.SmartPos() 和 JVCL.NPos() 做了比较,速度更快,而且兼容 Unicode(WideString/WideChar)。

注:代码可能有人会觉得不太舒服,但作为最常用的字符串函数,这样的优化我觉得还是值得的。


function QuickPos(const Substr, S: WideString; MatchesIndex: Integer = 1): Integer;
function QuickPosBack(const Substr, S: WideString; MatchesReverseIndex: Integer = 1): Integer;

代码如下:

// 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 = 1; LenOfSubstr: Integer = -1; LenOfS: Integer = -1): Boolean;
var
  I: Integer;
begin
  if LenOfSubstr = -1 then LenOfSubstr := Length(Substr);
  if LenOfS = -1 then LenOfS := Length(S);
  if LenOfSubstr > LenOfS then
  begin
    Result := False;
    Exit;
  end;
  for I := 1 to LenOfSubstr do
    if Substr[I] <> S[I + StartIndex - 1] 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 = -1; LenOfS: Integer = -1): Integer;
var
  I: Integer;
begin
  Result := 0;
  case LenOfSubstr of
    0: Exit;
   -1: LenOfSubstr := Length(Substr);
  end;
  if LenOfS = -1 then LenOfS := Length(S);

  for I := StartIndex to LenOfS do
  begin
    if (S[I] = Substr[1]) 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 = 1): Integer;
var
  LenOfS, LenOfSubstr: Integer;
begin
  Result := Pos{Pos}(Substr, S);

  if (MatchesIndex = 1) or (Result = 0) then Exit;
  LenOfS := Length(S);
  LenOfSubstr := Length(Substr);

  while (MatchesIndex > 1) and (Result > 0) do
  begin
    Result := _PosForward{Pos}(Substr, S, Result + 1, LenOfSubstr, LenOfS); ?// Tip!! Do not use func.Copy!!
    if Result = 0 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 = -1; LenOfSubstr: Integer = -1): Integer;
var
  I: Integer;
begin
  Result := 0;
  case LenOfSubstr of
    0: Exit;
    -1: LenOfSubstr := Length(Substr);
  end;
  if StopIndex = -1 then StopIndex := Length(S);

  for I := StopIndex - LenOfSubstr + 1 downto 1 do
  begin
    if (S[I] = Substr[1]) 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 = 1): Integer;
var
  LenOfSubstr: Integer;
begin
  Result := _PosBack{Pos}(Substr, S);

  if (MatchesReverseIndex = 1) or (Result = 0) then Exit;
  LenOfSubstr := Length(Substr);

  while (MatchesReverseIndex > 1) and (Result > 0) do
  begin
    Result := _PosBack{Pos}(Substr, S, Result + LenOfSubstr - 2, LenOfSubstr);
    Dec(MatchesReverseIndex);
  end;
end;
 

### 关于 `pos` 函数的使用方法 在 Python 中,严格意义上并没有内置名为 `pos` 的函数。然而,在某些特定场景或者第三方库中可能会有类似的命名方式用于表示位置或其他功能。 #### 1. 自定义 `pos` 函数 如果需要创建一个自定义的 `pos` 函数来实现某种逻辑,可以利用 Python 提供的 `def` 关键字完成定义[^1]。例如: ```python def pos(x, y): """返回坐标 (x, y) 的字符串形式""" return f"Position: ({x}, {y})" ``` 上述代码片段展示了一个简单的例子,其中 `pos` 接收两个参数 `x` 和 `y` 并返回它们组成的字符串描述。 #### 2. C 语言中的光标定位函数 虽然这是关于 C 语言的内容,但在讨论 `pos` 类似的功能时可能涉及光标的控制操作。C 语言提供了诸如 `SetConsoleCursorPosition()` 这样的 API 来设置终端窗口内的光标位置[^2]。此函数接受两个参数:一个是标准输入/输出流句柄 (`HANDLE`);另一个是指定目标坐标的结构体变量 (`COORD`)。 需要注意的是这种技术主要适用于 Windows 系统环境下的命令行界面应用开发工作当中。 #### 3. 外部声明与链接器支持 对于一些高级话题比如跨文件单元共享数据对象或过程的情形下,则需要用到外部声明机制。像下面这样提前告知编译器某个符号将在其他地方被真正定义出来以便后续能够正确解析引用关系[^4]: ```c // 声明 extern 表示该函数的具体实现在别的源码文件里 extern float my_func(); ``` 尽管这不是直接针对 python 下面所谓的 “POS” 功能解释,但它展示了如何处理不同模块之间相互依赖的情况。 #### 4. URL 查询串解析假设情况 有时人们提到 `pos` 可能联想到网络请求过程中涉及到的位置信息提取等问题。假如我们要分析 HTTP GET 请求携带的数据部分(query string),那么可以根据指定分隔符拆解成多个 key-value 对的形式存储起来进一步加工处理[^5]。如下所示伪算法思路: ```python url_query = "id=123&name=john" pairs = url_query.split('&') data_dict = {} for pair in pairs: k,v = pair.split('=') data_dict[k]=v print(data_dict) #{'id': '123', 'name': 'john'} ``` 这里只是简单模拟了一下可能的应用情景,并非正式意义上的所谓 POS 方法论介绍。 --- ### 总结 综上所述,“POS”并非特指某单一固定含义的概念,而是依据上下文有所差异变化。如果是想构建属于自己的工具类辅助性质的小型程序组件的话,完全可以借助已知的知识体系自行设计满足需求的目标成果物。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值