[fpc-devel] Patch to speed up Uppercase/Lowercase functions

Yury Sidorov jura at ce.blagovest.com
Sun Jun 12 13:38:31 CEST 2005


Oh, 

I found the bug. 
There should be: 

Inc(byte(P^), 32);

instead of previously posted:


Inc(byte(P^));

Here is correct implementation:

----

Function Lowercase41(Const S : String) : String;
var
  i: Integer;
  P: PChar;
  Changed: boolean;
begin
  Changed:=False;
  P := PChar(S);
  for i := 1 to Length(S) do begin
    if P^ in ['A'..'Z'] then begin
      if not Changed then begin
        Changed := True;
        Result := S;
        UniqueString(Result);
        P := PChar(Result) + i - 1;
      end;
      Inc(byte(P^), 32);
    end;
    Inc(P);
  end;
  if not Changed then
    Result := S;
end;

Function Lowercase42(Const S : String) : String;
var
  i: Integer;
  P: PChar;
begin
  Result := S;
  UniqueString(Result);
  P := PChar(Result);
  i := Length(Result);
  while i > 0 do begin
    if P^ in ['A'..'Z'] then
      Inc(byte(P^), 32);
    Inc(P);
    Dec(i);
  end;
end;






More information about the fpc-devel mailing list