不区分大小写 [英] case insensitive Pos

查看:235
本文介绍了不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在D2010(unicode)中是否有类似Pos的类似功能不区分大小写?

Is there any comparable function like Pos that is not case-sensitive in D2010 (unicode)?

我知道我可以使用Pos(AnsiUpperCase(FindString)),AnsiUpperCase (SourceString)),但是通过每次调用函数时将字符串转换为大写来增加大量的处理时间。

I know I can use Pos(AnsiUpperCase(FindString), AnsiUpperCase(SourceString)) but that adds a lot of processing time by converting the strings to uppercase every time the function is called.

例如,在1000000循环中,Pos转换为大写字母的时间为78ms,需要764ms。

For example, on a 1000000 loop, Pos takes 78ms while converting to uppercase takes 764ms.

str1 := 'dfkfkL%&/s"#<.676505';
  for i := 0 to 1000000 do
    PosEx('#<.', str1, 1); // Takes 78ms

  for i := 0 to 1000000 do
    PosEx(AnsiUpperCase('#<.'), AnsiUpperCase(str1), 1); // Takes 764ms

我知道为了提高这个具体示例的性能,我可以在循环之前将字符串转换为大写,但是为什么我希望拥有类似于Pos的函数,敏感的是从FastStrings中替换一个。我将使用的所有字符串Pos将不同,所以我需要将每一个都转换为大写。

I know that to improve the performance of this specific example I can convert the strings to uppercase first before the loop, but the reason why I'm looking to have a Pos-like function that is not case-sensitive is to replace one from FastStrings. All the strings I'll be using Pos for will be different so I will need to convert each and every one to uppercase.

有没有任何其他函数可能比Pos +更快地将字符串转换为大写?

Is there any other function that might be faster than Pos + convert the strings to uppercase?

推荐答案

Smartins我对桌面的想法是对的!

Smartins my idea about table is right!

确定我有修复代码,因为没有。现在的代码在D2007和D2010下工作正常。所以这两个编译器都可以编译它。不同之处在于,在较旧版本的编译器下,CharUpCaseTable为256位大小,D2010为128 KB。原因是字符大小。在旧版本的Delphi下,我的代码仅支持初始化当前的语言环境字符集!我的InsensPosEx比你的代码快4倍。当然可以做更快的代码,但我们将失去简单性。

OK I have repair code because you didn't. Code now works fine under D2007 and D2010. So both compilers can compile it. The difference is that under older version of compiler the CharUpCaseTable is 256 bites size and under D2010 is 128 KBytes. The reason is char size. Under older version of Delphi my code support only at initialisation current locale character set! My InsensPosEx is about 4 times faster than your code. Certainly is possible to do even faster code but we will lose simplicity.

type
  TCharUpCaseTable = array [Char] of Char;

var
  CharUpCaseTable: TCharUpCaseTable;

procedure InitCharUpCaseTable(var Table: TCharUpCaseTable);
var
  n: cardinal;
begin
  for n := 0 to Length(Table) - 1 do
    Table[Char(n)] := Char(n);
  CharUpperBuff(@Table, Length(Table));
end;

function InsensPosEx(const SubStr, S: string; Offset: Integer = 1): Integer;
var
  n              :integer;
  SubStrLength   :integer;
  SLength        :integer;
label
  Fail;
begin
  SLength := length(s);
  if (SLength > 0) and (Offset > 0) then begin
    SubStrLength := length(SubStr);
    result := Offset;
    while SubStrLength <= SLength - result + 1 do begin
      for n := 1 to SubStrLength do
        if CharUpCaseTable[SubStr[n]] <> CharUpCaseTable[s[result + n - 1]] then
          goto Fail;
      exit;
Fail:
      inc(result);
    end;
  end;
  result := 0;
end;

//...

initialization
  InitCharUpCaseTable(CharUpCaseTable);

请问。

这篇关于不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆