rework UsefulOutBuf_Compare and test
diff --git a/inc/qcbor/UsefulBuf.h b/inc/qcbor/UsefulBuf.h
index cb22ff3..1a4a3bf 100644
--- a/inc/qcbor/UsefulBuf.h
+++ b/inc/qcbor/UsefulBuf.h
@@ -1402,36 +1402,39 @@
*
* @param[in] pUOutBuf Pointer to the @ref UsefulOutBuf.
* @param[in] uStart1 Offset of first bytes to compare.
- * @param[in] uStart2 Offset of second bytes to compare.
+ * @param[in] uLen1 Length of first bytes to compare.
+ * @param[in] uStart2 Offset of second bytes to compare.
+ * @param[in] uLen2 Length of second bytes to compare.
*
* @return 0 for equality, positive if uStart1 is lexographically larger,
* negative if uStart2 is lexographically larger.
- *
- * TODO: update documentation
*
* This looks into bytes that have been output at the offsets @c start1
* and @c start2. It compares bytes at those two starting points until
- * they are not equal or the end of the output data is reached from
- * one of the starting points.
+ * they are not equal or @c uLen1 or @c uLen2 is reached. If the
+ * length of the string given is off the end of the output data, the
+ * string will be effectively concated to the data in the output
+ * buffer for the comparison.
*
* This returns positive when @c uStart1 lexographically sorts ahead
* of @c uStart2 and vice versa. Zero is returned if the strings
- * compare equally. This only happens when the end of the valid data
- * is reached from one of the starting points and the comparison up to
- * that point is equality.
+ * compare equally.
+ *
+ * If lengths are unequal and the first bytes are an exact subset of
+ * the second string, then a positve value will be returned and vice
+ * versa.
*
* If either start is past the end of data in the output buffer, 0
* will be returned. It is the caller's responsibility to make sure
- * the offsets are not off the end such that a comparison is actually
+ * the offsets are not off the end so that a comparison is actually
* being made. No data will ever be read off the end of the buffer so
* this safe no matter what offsets are passed.
*
* This is a relatively odd function in that it works on data in the
- * output buffer. It is employed by QCBOR to sort CBOR-encoded maps that
- * are in the output buffer.
+ * output buffer. It is employed by QCBOR to sort CBOR-encoded maps
+ * that are in the output buffer.
*/
-
-int UsefulOutBuf_Compare(UsefulOutBuf *me,
+int UsefulOutBuf_Compare(UsefulOutBuf *pUOutBuf,
size_t uStart1, size_t uLen1,
size_t uStart2, size_t uLen2);
diff --git a/src/UsefulBuf.c b/src/UsefulBuf.c
index 3c39ebd..dae4eb1 100644
--- a/src/UsefulBuf.c
+++ b/src/UsefulBuf.c
@@ -434,9 +434,9 @@
*
* Code Reviewers: THIS FUNCTION DOES POINTER MATH
*/
-int UsefulOutBuf_Compare(UsefulOutBuf *me,
- size_t uStart1, size_t uLen1,
- size_t uStart2, size_t uLen2)
+int UsefulOutBuf_Compare(UsefulOutBuf *pMe,
+ const size_t uStart1, const size_t uLen1,
+ const size_t uStart2, const size_t uLen2)
{
const uint8_t *pBase;
const uint8_t *pEnd;
@@ -446,8 +446,8 @@
const uint8_t *p2End;
int uComparison;
- pBase = me->UB.ptr;
- pEnd = (const uint8_t *)pBase + me->data_len;
+ pBase = pMe->UB.ptr;
+ pEnd = (const uint8_t *)pBase + pMe->data_len;
p1 = pBase + uStart1;
p2 = pBase + uStart2;
p1End = p1 + uLen1;
@@ -463,6 +463,16 @@
p2++;
}
+ if(uComparison == 0 && p1 != p1End && p2 != p2End) {
+ if(uLen1 > uLen2) {
+ uComparison = 1;
+ } else if(uLen2 < uLen1){
+ uComparison = -1;
+ } else {
+ return 0;
+ }
+ }
+
return uComparison;
}
@@ -481,7 +491,7 @@
while(pStart < pEnd) {
pEnd--;
- uTmp = *pStart;
+ uTmp = *pStart;
*pStart = *pEnd;
*pEnd = uTmp;
pStart++;
diff --git a/test/UsefulBuf_Tests.c b/test/UsefulBuf_Tests.c
index 34a9a00..13e87e3 100644
--- a/test/UsefulBuf_Tests.c
+++ b/test/UsefulBuf_Tests.c
@@ -940,7 +940,25 @@
return "Comparison off the end is equal";
}
- // TODO: a bit more checking off the end
+ nCompare = UsefulOutBuf_Compare(&UOB, 0, 3, 3, 2);
+ if(nCompare > 0) {
+ return "Comparison of unequal lengths incorrect";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 8, 2, 0, 3);
+ if(nCompare < 0) {
+ return "Comparison of unequal lengths incorrect 2";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 0, 2, 2, 3);
+ if(nCompare != 'c' - 'a') {
+ return "Inequal lengths, but inequal strings";
+ }
+
+ nCompare = UsefulOutBuf_Compare(&UOB, 1, 3, 4, 2);
+ if(nCompare != 'd' - 'c') {
+ return "Inequal lengths, but inequal strings";
+ }
/* Test UsefulOutBuf_Swap() */