Hi Guys,

This is yet another fix to bit_nsearch in bitstring.h. This patch
solves 2 problems on bit_nsearch:

1. When the cluster of 0's found is in the last bits of the string,
the algorithm was not returning the cluster as result.
2. When a cluster of size 0 was requested, it was returning a wrong answer.

Below is the patch.

Thanks,
Luis.

--- old_bitstring.h     2011-04-27 12:02:37.351084009 -0400
+++ bitstring.h 2011-04-27 11:53:38.721084004 -0400
@@ -172,34 +178,28 @@
        int _nbits = (nbits); \
        int *_value = (value); \
        int _len = (len); \
-       int _bit, _bit0; \
+       int _bit, _bit0 = -1; \
        int _tmplen = _len; \
        int _match = 0; \
        *(_value) = -1; \
-       for (_bit = 0; _bit < _nbits; _bit++) { \
+       for (_bit = 0; _bit < _nbits && _tmplen > 0; _bit++) { \
                if (_match == 0) { \
                        if (bit_test((_name), _bit) == 0) { \
                                _match = 1; \
                                _tmplen--; \
                                _bit0 = _bit; \
-                       } else { \
-                               continue; \
                        } \
                } else { \
-                       if (_tmplen > 0) { \
-                               if (bit_test((_name), _bit) == 0) { \
-                                       _tmplen--; \
-                               } else { \
-                                       _match = 0; \
-                                       _tmplen = _len; \
-                                       continue; \
-                               } \
+                       if (bit_test((_name), _bit) == 0) { \
+                               _tmplen--; \
                        } else { \
-                               *(_value) = _bit0; \
-                                break; \
+                               _match = 0; \
+                               _tmplen = _len; \
                        } \
                } \
        } \
+       if (_tmplen == 0) \
+               *(_value) = _bit0; \
 } while (0)

 #endif /* !_BITSTRING_H_ */

Reply via email to