Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-11-06
9
Medium Priority
?
191 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

Independent Software Vendors: 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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 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