68 lines
2.2 KiB
C
68 lines
2.2 KiB
C
#include <string.h>
|
|
#include <libowfat/str.h>
|
|
#include <libowfat/uint32.h>
|
|
|
|
int bstr_diff(const char* a,const char* b) {
|
|
const char* A,* B;
|
|
int j;
|
|
/* like str_diff, just for bstrs */
|
|
if (*a && *b)
|
|
return str_diff(a,b);
|
|
if (*a) A=a+str_len(a); else { A=a+5+uint32_read(a+1); a+=5; }
|
|
if (*b) B=b+str_len(b); else { B=b+5+uint32_read(b+1); b+=5; }
|
|
for (;;) {
|
|
if (a==A) {
|
|
if (b==B)
|
|
return 0;
|
|
else
|
|
return -1;
|
|
} else
|
|
if (b==B)
|
|
return 1;
|
|
if ((j=((unsigned char)*a-(unsigned char)*b))) break;
|
|
++a; ++b;
|
|
}
|
|
return j;
|
|
}
|
|
|
|
#ifdef UNITTEST
|
|
#include <assert.h>
|
|
|
|
int main() {
|
|
assert(str_diff("bar","foo") < 0);
|
|
assert(str_diff("foo","bar") > 0);
|
|
assert(str_diff("foo1","foo") > 0);
|
|
assert(str_diff("foo","foo1") < 0);
|
|
assert(str_diff("foo","foo") == 0);
|
|
|
|
assert(bstr_diff("bar","foo") < 0);
|
|
assert(bstr_diff("foo","bar") > 0);
|
|
assert(bstr_diff("foo1","foo") > 0);
|
|
assert(bstr_diff("foo","foo1") < 0);
|
|
assert(bstr_diff("foo","foo") == 0);
|
|
|
|
assert(bstr_diff("bar","\x00\x03\x00\x00\x00""foo") < 0);
|
|
assert(bstr_diff("foo","\x00\x03\x00\x00\x00""bar") > 0);
|
|
assert(bstr_diff("foo1","\x00\x03\x00\x00\x00""foo") > 0);
|
|
assert(bstr_diff("foo","\x00\x04\x00\x00\x00""foo1") < 0);
|
|
assert(bstr_diff("foo","\x00\x03\x00\x00\x00""foo") == 0);
|
|
|
|
assert(bstr_diff("\x00\x03\x00\x00\x00""bar","\x00\x03\x00\x00\x00""foo") < 0);
|
|
assert(bstr_diff("\x00\x03\x00\x00\x00""foo","\x00\x03\x00\x00\x00""bar") > 0);
|
|
assert(bstr_diff("\x00\x04\x00\x00\x00""foo1","\x00\x03\x00\x00\x00""foo") > 0);
|
|
assert(bstr_diff("\x00\x03\x00\x00\x00""foo","\x00\x04\x00\x00\x00""foo1") < 0);
|
|
assert(bstr_diff("\x00\x03\x00\x00\x00""foo","\x00\x03\x00\x00\x00""foo") == 0);
|
|
|
|
assert(bstr_diff("\x00\x03\x00\x00\x00""bar","foo") < 0);
|
|
assert(bstr_diff("\x00\x03\x00\x00\x00""foo","bar") > 0);
|
|
assert(bstr_diff("\x00\x04\x00\x00\x00""foo1","foo") > 0);
|
|
assert(bstr_diff("\x00\x03\x00\x00\x00""foo","foo1") < 0);
|
|
assert(bstr_diff("\x00\x03\x00\x00\x00""foo","foo") == 0);
|
|
|
|
assert(bstr_diff("\x00\x05\x00\x00\x00""f\x00ord","fnord") < 0);
|
|
assert(bstr_diff("\x00\x05\x00\x00\x00""f\x00ord","\x00\x05\x00\x00\x00""f\x00ort") < 0);
|
|
|
|
assert(bstr_diff("\x00\x03\x00\x00\x00""f\xf6o","foo") > 0);
|
|
}
|
|
#endif
|