From 5277f5033d47830dd5283d31901a672c91ce6a29 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Mon, 11 Dec 2023 20:18:03 -0600 Subject: [PATCH] Update: Strip NULLs from the payload number converters. Add logic to strip the NULLs from the numbers. The FSS functions use NULLs to reduce reallocations but the payload sends everything. Don't introduce unexpected NULLs when writing the numbers. I need to review the logic for any mistakes which I find is often best after sleeping on it. Even better, I should write some unit tests to expose any potential logic flaws more easily. --- level_1/fl_fss/c/fss/payload.c | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/level_1/fl_fss/c/fss/payload.c b/level_1/fl_fss/c/fss/payload.c index 601d562..2ec5c69 100644 --- a/level_1/fl_fss/c/fss/payload.c +++ b/level_1/fl_fss/c/fss/payload.c @@ -40,6 +40,40 @@ extern "C" { private_fl_fss_basic_write(F_false, *data->cache, 0, &internal->range, &internal->destinations->array[internal->destinations->used].value, state, (void * const) internal); if (F_status_is_error(state->status)) return F_true; + + // Strip NULLs that may have been introduced via private_fl_fss_basic_write(). + if (data->cache->used) { + f_number_unsigned_t i = data->cache->used; + f_number_unsigned_t j = 0; + f_number_unsigned_t total = 0; + + while (--i) { + + if (!data->cache->string[i]) { + total = 1; + + for (j = i - 1; j && !data->cache->string[j]; ++total, --j) { + // Do nothing. + } // for + + if (!j) { + if (!data->cache->string[j]) { + ++total; + } + else { + ++j; + } + } + + memmove(data->cache->string + j, data->cache->string + i + 1, total); + + data->cache->used -= total; + i = j; + + if (!i) break; + } + } // while + } } return F_false; @@ -79,6 +113,40 @@ extern "C" { private_fl_fss_basic_write(F_false, *data->cache, 0, &internal->range, &internal->destinations->array[internal->destinations->used].value, state, (void * const) internal); if (F_status_is_error(state->status)) return F_true; + + // Strip NULLs that may have been introduced via private_fl_fss_basic_write(). + if (data->cache->used) { + f_number_unsigned_t i = data->cache->used; + f_number_unsigned_t j = 0; + f_number_unsigned_t total = 0; + + while (--i) { + + if (!data->cache->string[i]) { + total = 1; + + for (j = i - 1; j && !data->cache->string[j]; ++total, --j) { + // Do nothing. + } // for + + if (!j) { + if (!data->cache->string[j]) { + ++total; + } + else { + ++j; + } + } + + memmove(data->cache->string + j, data->cache->string + i + 1, total); + + data->cache->used -= total; + i = j; + + if (!i) break; + } + } // while + } } return F_false; -- 1.8.3.1