case insensitive Pos

前端 未结 9 1536
一个人的身影
一个人的身影 2021-02-02 00:56

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

I know I can use Pos(AnsiUpperCase(FindString), AnsiUpperCase(SourceString)) but

9条回答
  •  面向向阳花
    2021-02-02 01:01

    Instead 'AnsiUpperCase' you can use Table it is much faster. I have reshape my old code. It is very simple and also very fast. Check it:

    type
      TAnsiUpCaseTable = array [AnsiChar] of AnsiChar;
    
    var
      AnsiTable: TAnsiUpCaseTable;
    
    procedure InitAnsiUpCaseTable(var Table: TAnsiUpCaseTable);
    var
      n: cardinal;
    begin
      for n := 0 to SizeOf(TAnsiUpCaseTable) -1 do
      begin
        AnsiTable[AnsiChar(n)] := AnsiChar(n);
        CharUpperBuff(@AnsiTable[AnsiChar(n)], 1);
      end;
    end;
    
    function UpCasePosEx(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 AnsiTable[SubStr[n]] <> AnsiTable[s[result + n -1]] then
              goto Fail;
          exit;
    Fail:
          inc(result);
        end;
      end;
      result := 0;
    end;
    
    initialization
      InitAnsiUpCaseTable(AnsiTable);
    end.
    

提交回复
热议问题