2个不错的通配符比较函数

2023-11-17

近日在和朋友讨论 MaskMatch 时偶得2个不错的算法。
函数1 只支持'*','?'模糊匹配。速度比采用递归算法的快近2倍,比TMask方法快很多。
函数2 完全支持正规表达式。速度于之前的相同。(不会正规表达式的朋友慎用)


// ===========================
// Funtion 1
// ===========================

// Check if the string can match the wildcard. It can be used for unicode strings as well!
// C: 2004-07-24 | M: 2004-07-24
function MaskMatch(const aPattern, aSource: string): Boolean;
var
  StringPtr, PatternPtr: PChar;
  StringRes, PatternRes: PChar;
begin
  Result := False;
  StringPtr := PChar(UpperCase(aSource));
  PatternPtr := PChar(UpperCase(aPattern));
  StringRes := nil;
  PatternRes := nil;
  repeat
    repeat // ohne vorangegangenes "*"
      case PatternPtr^ of
        #0 : begin
               Result := StringPtr^ = #0;
               if Result or (StringRes = nil) or (PatternRes = nil) then Exit;
               StringPtr := StringRes;
               PatternPtr := PatternRes;
               Break;
             end;
        '*': begin
               Inc(PatternPtr);
               PatternRes := PatternPtr;
               Break;
             end;
        '?': begin
               if StringPtr^ = #0 then Exit;
               Inc(StringPtr);
               Inc(PatternPtr);
             end;
        else begin
               if StringPtr^ = #0 then Exit;
               if StringPtr^ <> PatternPtr^ then
               begin
                 if (StringRes = nil) or (PatternRes = nil) then Exit;
                 StringPtr := StringRes;
                 PatternPtr := PatternRes;
                 Break;
               end else
               begin
                 Inc(StringPtr);
                 Inc(PatternPtr);
               end;
             end;
      end;
    until False;

    repeat // mit vorangegangenem "*"
      case PatternPtr^ of
        #0 : begin
               Result := True;
               Exit;
             end;
        '*': begin
               Inc(PatternPtr);
               PatternRes := PatternPtr;
             end;
        '?': begin
               if StringPtr^ = #0 then Exit;
               Inc(StringPtr);
               Inc(PatternPtr);
             end;
        else begin
               repeat
                 if StringPtr^ = #0 then Exit;
                 if StringPtr^ = PatternPtr^ then Break;
                 Inc(StringPtr);
               until False;
               Inc(StringPtr);
               StringRes := StringPtr;
               Inc(PatternPtr);
               Break;
             end;
      end;
    until False;
  until False;
end;


// ===========================
// Funtion 2
// ===========================

function _MatchPattern(aPattern, aSource: PChar): Boolean;
begin
  Result := True;
  while (True) do
  begin
    case aPattern[0] of
      #0 : begin
             //End of pattern reached.
             Result := (aSource[0] = #0); //TRUE if end of aSource.
             Exit;
           end;

      '*': begin //Match zero or more occurances of any char.
             if (aPattern[1] = #0) then
             begin
               //Match any number of trailing chars.
               Result := True;
               Exit;
             end else
              Inc(aPattern);

             while (aSource[0] <> #0) do
             begin
               //Try to match any substring of aSource.
               if (_MatchPattern(aSource, aPattern)) then
               begin
                 Result := True;
                 Exit;
               end;

               //Continue testing next char...
               Inc(aSource);
             end;
           end;

      '?': begin //Match any one char.
             if (aSource[0] = #0) then
             begin
               Result := False;
               Exit;
             end;

             //Continue testing next char...
             Inc(aSource);
             Inc(aPattern);
           end;

      '[': begin //Match given set of chars.
             if (aPattern[1] in [#0,'[',']']) then
             begin
               //Invalid Set - So no match.
               Result := False;
               Exit;
             end;

             if (aPattern[1] = '^') then
             begin
               //Match for exclusion of given set...
               Inc(aPattern, 2);
               Result := True;
               while (aPattern[0] <> ']') do
               begin
                 if (aPattern[1] = '-') then
                 begin
                   //Match char exclusion range.
                   if (aSource[0] >= aPattern[0]) and (aSource[0] <= aPattern[2]) then
                   begin
                     //Given char failed set exclusion range.
                     Result := False;
                     Break;
                   end else
                     Inc(aPattern, 3);
                 end else
                 begin
                   //Match individual char exclusion.
                   if (aSource[0] = aPattern[0]) then
                   begin
                     //Given char failed set element exclusion.
                     Result := False;
                     Break;
                   end else
                    Inc(aPattern);
                 end;
               end;
             end else
             begin
               //Match for inclusion of given set...
               Inc(aPattern);
               Result := False;
               while (aPattern[0] <> ']') do
               begin
                 if (aPattern[1] = '-') then
                 begin
                   //Match char inclusion range.
                   if (aSource[0] >= aPattern[0]) and (aSource[0] <= aPattern[2]) then
                   begin
                     //Given char matched set range inclusion.
                     // Continue testing...
                     Result := True;
                     Break;
                   end else
                    Inc(aPattern, 3);
                 end else
                 begin
                   //Match individual char inclusion.
                   if (aSource[0] = aPattern[0]) then
                   begin
                     //Given char matched set element inclusion.
                     // Continue testing...
                     Result := True;
                     Break;
                   end else
                     Inc(aPattern);
                 end;
               end;
             end;

             if (Result) then
             begin
               //Match was found. Continue further.
               Inc(aSource);
               //Position Pattern to char after "]"
               while (aPattern[0] <> ']') and (aPattern[0] <> #0) do Inc(aPattern);
               if (aPattern[0] = #0) then
               begin
                 //Invalid Pattern - missing "]"
                 Result := False;
                 Exit;
               end else
                 Inc(aPattern);
             end else
               Exit;
           end;

      else begin //Match given single char.
             if (aSource[0] <> aPattern[0]) then
             begin
               Result := False;
               Break;
             end;

             //Continue testing next char...
             Inc(aSource);
             Inc(aPattern);
           end;
    end;
  end;
end;

function MatchPattern(const aPattern, aSource: string): Boolean;
begin
  Result := _MatchPattern(PChar(aPattern), PChar(aSource));
end;  

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

2个不错的通配符比较函数 的相关文章

随机推荐

  • k8s_day01_01

    k8s day01 01 1 it发展趋势 it 的发展趋势中 我们最早的资源粒度从最早的物理机时代 到达十年之前的 kvm 还有xen 为代表的虚拟化代表的虚拟机时代 那个时候 VMware是如日中天的时期 vmware 以产品众多且成熟
  • 【安全知识】——如何绕过cdn获取真实ip

    作者名 白昼安全 主页面链接 主页传送门 创作初心 以后赚大钱 座右铭 不要让时代的悲哀成为你的悲哀 专研方向 web安全 后渗透技术 每日鸡汤 现在的样子是你想要的吗 cdn简单来说就是 通过不同地理位置的缓存来加快访问速度 所以我们有可
  • centos7离线升级gcc到9.3.1,解决redis6安装失败

    我们在安装Redis6的时候会编译出错 大概率是因为centos7的gcc版本 centos7的默认版本是4 8 5 需要将gcc版本升级到9 如果服务器是互联网环境 可以直接通过yum命令升级 yum y install centos r
  • MFC多字节和宽字节转换方式

    第一种转换 代码中可以直接用宏定义转换 可以使用 L 或者 T 但是对于传过来的值 就不能这样了 需要写函数转换 第二种转换 必须在一个函数中执行 否则上一次调用并不会释放栈中的空间 要直到函数结束才会释放空间 测试是否释放内存空间可以使用
  • 单片机——PWM调光工作原理

    前言 如果只用单片机做一个调光系统 pwm是可以实现的 但是如果有其它的功能 比如传感器要检测 显示屏显示数据等等功能 就不推荐了 其它函数一多 定时器的时间又比较短 以至于单片机大多数时间都用在定时中断函数里去了 处理其它函数的时间太少
  • 京东云鼎服务器系统时间问题-时间戳参与与服务器时间差异大于设定值

    业务背景 目前业务对接了各大电商平台的订单 按照规定京东需要入鼎进行调用jos接口来拉取订单 相当于与订单API之间多一个应用中转 如图 该文章暂时不讨论对接的细节 后面打算出个专题文章细说各大电商平台的对接 目前好像比较少这个业务的分享
  • ios 无法发起ajax请求,PhoneGap的iOS的Ajax请求无法完成(Phonegap iOS ajax request

    请阅读更新这个答案在底部 原来的答案 我已经找到了问题所在 并设法得到基本的认证工作 问题是 Web服务器期待基本身份验证的细节与请求一起发送先发制人 要做到这一点使用 头 的jQuery AJAX的特性如下图所示 ajax type GE
  • 【debug】error: subprocess-exited-with-error

    在安装Python的某个三方包时 出现了下面的报错方式 error subprocess exited with error 解决办法 解决方案重新安装importlib metadata pip install importlib met
  • 经典面试题-大厂SQL题目

    1 如何用一个SQL找出部门下所有员工的平均工资大于某个数 例如20000元 的所有部门 正确答案 select depno avg sal as avgsal from emp group by depno having avgsal g
  • 使用python编写脚本测试目标主机的TCP端口连通性

    使用Python的Socket模块的connect 函数来尝试连接目标主机的特定端口 如果连接成功 则说明该端口是打开的 否则 该端口是关闭的 下面是一个示例脚本 可以检测目标IP的22端口是否开启 import socket def ch
  • 【c++】类模版

    1 类模板语法 类模板作用 建立一个通用类 类中的成员 数据类型可以不具体制定 用一个虚拟的类型来代表 语法 template
  • 市场监管总局关于对锂离子电池等产品实施强制性产品认证管理的公告

    按照 国务院办公厅关于深化电子电器行业管理制度改革的意见 国办发 2022 31号 有关要求 市场监管总局决定对电子电器产品使用的锂离子电池和电池组 移动电源以及电信终端产品配套用电源适配器 充电器 以下统称新纳入产品 实施强制性产品认证
  • 树莓派安装卸载软件命令apt-get

    apt get命令用法 1 安装软件 apt get install 软件名 2 卸载软件但不删除配置 apt get remove 软件名 3 卸载软件并且删除相关配置 apt get purge 软件名 4 更新数据库 apt get
  • Python简单的用户交互

    death age 80 name input your name input 接受的所有数据都是字符串 即便你输入的是数字 但依然会被当成字符串来处理 age input your age print type age int integ
  • TS复习----TS中的接口

    目录 概念 属性接口 函数类型接口 可索引的类型 类类型接口 接口继承 概念 接口的作用 在面向对象编程中 接口是一种规范的定义 他定义了行为和动作的规范 在程序设计里面 接口起到了一种限制和规范的作用接口定义了某一批类所需要遵守的规范 接
  • windos怎么查看oracle进程,怎么样查看哪个进程使用了哪个CPU

    1 在系统维护的过程中 随时可能有需要查看 CPU 使用率 并根据相应信息分析系统状况的需要 在 CentOS 中 可以通过 top 命令来查看 CPU 使用状况 运行 top 命令后 CPU 使用状态会以全屏的方式显示 并且会处在对话的模
  • java:方法引用无效-IDEA 社区版 lombok插件报错解决

    IDEA 社区版 lombok插件报错 java 方法引用无效 报错信息1 java 方法引用无效 找不到符号 符号 方法 getId 位置 类 com xxx xxxx className 打开problem面板向上找你就会发现还有一个报
  • 天九共享赋能新基建项目,易保全区块链存证助力应用场景多点开花

    在国家政策的大力扶持下 中国的区块链发展势力愈发迅猛 作为数字经济的基石 区块链技术发挥着重要作用 据数据显示 2020年全球区块链专利累计达到5 14万件 其中中国累计申请了3 01万件 占全球总数的58 同时 近期发布的 北京城市副中心
  • C语言写网络爬虫总体思路

    使用C语言编写爬虫可以实现网络数据的快速获取和处理 适用于需要高效处理海量数据的场景 与其他编程语言相比 C语言具有较高的性能和灵活性 可以进行底层操作和内存管理 适合处理较复杂的网络请求和数据处理任务 但是 使用C语言编写爬虫也存在一些挑
  • 2个不错的通配符比较函数

    近日在和朋友讨论 MaskMatch 时偶得2个不错的算法 函数1 只支持 模糊匹配 速度比采用递归算法的快近2倍 比TMask方法快很多 函数2 完全支持正规表达式 速度于之前的相同 不会正规表达式的朋友慎用 Funtion 1 Chec