[Lazarus] Optimize SwapEndian for widestring?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Lazarus] Optimize SwapEndian for widestring?

Free Pascal - Lazarus mailing list
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

--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] Optimize SwapEndian for widestring?

Free Pascal - Lazarus mailing list


On Tue, 1 Aug 2017, Alexey via Lazarus 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;
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] Optimize SwapEndian for widestring?

Free Pascal - Lazarus mailing list
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
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Loading...