Match beginning of two strings

A

Andrew Dalke

Mañungo:
First, I'm not a python programmer... but I think is better test int.
Something like that:

int *a = (int *) ...first string...;
int *b = (int *) ...second string...;

Sure. That's an old trick. However, your code assumes your
strings are word aligned. (Perhaps for Python they are but
in general you cannot make that assumption about C char *s)

You also have an off-by-one/off-by-four error. Suppose the
two strings are "A", that is, an 'A' followed by a NUL. Then

for (i=0; i<n; i+=4)
if (*a++ != *b++)
break

will compare the four bytes and increment the counters.
You then do

char *aa = (char *) a;

and work with the characters *after* the first four characters
tested in the int compare.

Finally, you assume ints are 4 bytes long, which is
not universal.

There's something to be said for simplicity. I also
wonder if modern optimizing compliers could figure
out some of this automatically, but I don't wonder
enough to find out.

Andrew
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top