Skip to content

Instantly share code, notes, and snippets.

@kulp
Created December 12, 2012 20:45
Show Gist options
  • Save kulp/4271440 to your computer and use it in GitHub Desktop.
Save kulp/4271440 to your computer and use it in GitHub Desktop.
Recursively find the bounding box of mismatched bytes between two arrays
unsigned check_range(const char *bad, const char *good, size_t len, size_t bounds[2])
{
if (len < 2) {
if (len)
return *bad != *good;
return 0;
} else if (memcmp(bad, good, len)) {
size_t subbounds[2][2] = { { 0 } };
int leftbad = check_range(&bad[0 ], &good[0 ], len / 2, subbounds[0]);
int rightbad = check_range(&bad[len / 2], &good[len / 2], len - len / 2, subbounds[1]);
// return leftmost and rightmost bad bounds
bounds[0] = subbounds[ leftbad ? 0 : 1][0] + ( leftbad ? 0 : len - len / 2);
bounds[1] = subbounds[rightbad ? 1 : 0][1] + (rightbad ? len - len / 2 : 0);
return leftbad + rightbad;
} else {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment