From 0743e9f7ca92c298beccee4fd6312a04dfc8c246 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sun, 3 Nov 2024 00:36:23 -0400 Subject: allow empty span slices --- src/nc-util.h | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/nc-util.h b/src/nc-util.h index 36d26de..02228fb 100644 --- a/src/nc-util.h +++ b/src/nc-util.h @@ -207,14 +207,22 @@ static _nc_fn_inline span_t ncSpanSlice(span_t span, uint32_t offset, uint32_t s span_t slice; DEBUG_ASSERT2(ncSpanIsValid(span), "Expected span to be non-null"); - DEBUG_ASSERT2(offset + size <= span.size, "Expected offset + size to be less than span size") + DEBUG_ASSERT2(offset + size <= span.size, "Expected offset + size to be less than span size"); - /* Initialize slice, offset input data by the specified offset */ - ncSpanInit( - &slice, - ncSpanGetOffset(span, offset), - size - ); + /* If the size of the sliced span is 0 return an empty span */ + if (size == 0) + { + ncSpanInit(&slice, NULL, 0); + } + else + { + /* Initialize slice, offset input data by the specified offset */ + ncSpanInit( + &slice, + ncSpanGetOffset(span, offset), + size + ); + } return slice; } @@ -226,12 +234,20 @@ static _nc_fn_inline cspan_t ncSpanSliceC(cspan_t span, uint32_t offset, uint32_ DEBUG_ASSERT2(ncSpanIsValidC(span), "Expected span to be non-null"); DEBUG_ASSERT2(offset + size <= span.size, "Expected offset + size to be less than span size") - /* Initialize slice, offset input data by the specified offset */ - ncSpanInitC( - &slice, - ncSpanGetOffsetC(span, offset), - size - ); + /* If the size of the sliced span is 0 return an empty span */ + if (size == 0) + { + ncSpanInitC(&slice, NULL, 0); + } + else + { + /* Initialize slice, offset input data by the specified offset */ + ncSpanInitC( + &slice, + ncSpanGetOffsetC(span, offset), + size + ); + } return slice; } -- cgit