Solved

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

Posted on 2003-11-06
9
178 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now