Solved

C code to Pascal.  ... just need to see if I'm correct

Posted on 2003-11-06
9
181 Views
Last Modified: 2010-04-05
Hi, I have the following code in C:

char *strmov(register char *dst, register const char *src)
{
  while ((*dst++ = *src++)) ;
  return dst-1;
}

I translated the following to pascal as:

function strmov(dst : PChar; const src : PChar) : PChar;
var
  tmp_src : PChar;
begin
  tmp_src := src;
  while tmp_src^ <> #0 do
  begin
    dst^ := tmp_src^;
    inc(dst);
    inc(tmp_src);
  end;
  Result := dst - 1;
end;

Is my code correct ? ... assuming it is, is there any room for improvement without going asm ?

Thanks
0
Comment
Question by:delphi112497
9 Comments
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9694531
It looks like the code is correct.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9694561
Though not absolutely...
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9694603
The C-code copy all the string including null-terminator and return pointer to null-terminator.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9694627
But the PAS-code not copy null-terminator and return pointer to the last copied byte.
0
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 30 total points
ID: 9694661
How about the following code:

function strmov(dst : PChar; const src : PChar) : PChar;
begin
   Result := PChar(dst + StrLen(StrCopy(dst, src)));
end;
0
 

Author Comment

by:delphi112497
ID: 9694688
GloomyFriar, your code looks logically reasonable ... its by far simpler than mine.  THanks a lot!
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9694693
The StrCopy will be faster than byte-by-byte copying for long strings because of it use dword copying.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 9695701
To correct the Pascal Code make it a "repeat until"
Drop the const of the src param because C const is different from Pascal const.
That allows to remoce tmp_src
0
 
LVL 1

Expert Comment

by:Chopinke
ID: 9705907
or similarly to StrCopy

i := StrLen(src);
Move(src^, dst^, i);
Result := PChar(dst+i);
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wininet read php file from internet issue 6 91
Delphi IDE crash without error message ... 7 89
control image tags in a string ? 12 129
Twebbrowser add css to the header 3 25
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question