?
Solved

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

Posted on 2003-11-06
9
Medium Priority
?
192 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 120 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 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