Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2017-08

Optimize SwapEndian for widestring?

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alexey

Pls tell can I optimize this func? E.g. calc each char's PWord and read/write word.

function SSwapEndian(const S: UnicodeString): UnicodeString;
var
   i: integer;
begin
   Result:= S;
   for i:= 1 to Length(Result) do
     Result[i]:= WideChar(SwapEndian(Ord(Result[i])));
end;
Regards, Alexey

Reply by email

Michael Van Canneyt
to Alexey
Alexey wrote
Pls tell can I optimize this func? E.g. calc each char's PWord and read/write word.

Probably this will be faster:

  function SSwapEndian(const S: UnicodeString): UnicodeString;
  var
    i: integer;
    p :pwidechar;
  begin
    Result:= S;
    UniqueString(Result);
    P:=PWideChar(Result[1]));
    for i:= 1 to Length(Result) do
      begin
      P^:= WideChar(SwapEndian(P^));
      Inc(P);
      end;
  end;

Reply by email

Alexey
to Michael Van Canneyt

Thanks, it didn't compile, this this did:

function SSwapEndian(const S: UnicodeString): UnicodeString;
var
   i: integer;
   p: PWord;
begin
   Result:= S;
   if S='' then exit;
   UniqueString(Result);
   P:= PWord(@Result[1]);
   for i:= 1 to Length(Result) do
   begin
     P^:= SwapEndian(P^);
     Inc(P);
   end;
end;

AlexeyT

Reply by email