?
Solved

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

Posted on 2003-11-06
9
Medium Priority
?
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

765 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