Patch for strstr_binary

Mikael Pettersson mikpe@REDACTED
Tue Dec 13 03:53:44 CET 2005


> OSX and smoke tested on Linux (32-bit). I doubt it will work on  
> win32, as it uses a memcmp() function...but it should be easy to  

memcmp() is a standard ANSI/ISO-C feature. I would be very
surprised if Win32 didn't have it.

> +    if (is_binary(big) && is_binary(little)) {
> +
> +        char* big_ptr;
> +        int big_len = binary_size(big);
> +        GET_BINARY_BYTES(big, big_ptr);
> +
> +        char* little_ptr;
> +        int little_len = binary_size(little);
> +        GET_BINARY_BYTES(little, little_ptr);
> +
> +        char* cur;
> +        char* max_big = (big_ptr + big_len) - little_len ;
> +        char  first_little = *little_ptr;
> +        int   index = -1;

This isn't ANSI-C and won't compile with e.g. gcc-2.95.3.
You should make a single block of the variable declarations
at the beginning of this scope, and do the initialisations
as assignments.

> +        for (cur = big_ptr; cur <= max_big; cur++) {
> +            if (*cur == first_little && memcmp(cur, little_ptr, little_len) == 0) {
> +                index = (int)cur - (int)big_ptr;

These (int) casts are broken on 64-bit machines. Replace them
with (long) casts. Better yet, "index = cur - big_ptr;"
should have the same effect and be 100% kosher.

> +bif erlang:strstr_binary/2
> +
> +
> +

No stray empty lines please.

/Mikael



More information about the erlang-questions mailing list