From 2a77acf1c079b3212929651098adcc7717394cee Mon Sep 17 00:00:00 2001 From: lion Date: Tue, 21 Oct 2025 12:57:14 +0800 Subject: [PATCH] return empty string for empty match --- binding/c/xdb_api.h | 4 ++++ binding/c/xdb_searcher.c | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/binding/c/xdb_api.h b/binding/c/xdb_api.h index 6d69995..2a18c1d 100644 --- a/binding/c/xdb_api.h +++ b/binding/c/xdb_api.h @@ -221,6 +221,10 @@ XDB_PUBLIC(int) xdb_region_buffer_init(xdb_region_buffer_t *, char *, size_t); // returns: 0 for ok or failed XDB_PUBLIC(int) xdb_region_buffer_alloc(xdb_region_buffer_t *, int); +// empty alloc - empty string +// returns: 0 - always +XDB_PUBLIC(int) xdb_region_buffer_empty(xdb_region_buffer_t *); + XDB_PUBLIC(void) xdb_region_buffer_free(xdb_region_buffer_t *); // xdb searcher structure diff --git a/binding/c/xdb_searcher.c b/binding/c/xdb_searcher.c index b2b5c04..8a6338c 100644 --- a/binding/c/xdb_searcher.c +++ b/binding/c/xdb_searcher.c @@ -57,9 +57,38 @@ XDB_PUBLIC(int) xdb_region_buffer_alloc(xdb_region_buffer_t *region, int length) return 0; } +// fixed internal empty string ptr +static char * _empty_region_string = "\0"; + +XDB_PUBLIC(int) xdb_region_buffer_empty(xdb_region_buffer_t *region) { + // no allocation supports for the buffer wapper + if (region->type == xdb_region_buffer_wrapper) { + region->value[0] = '\0'; + return 0; + } + + // ensure that the value were freed + // by calling #xdb_region_buffer_free + if (region->value != NULL) { + return 3; + } + + region->value = _empty_region_string; + region->length = 0; + return 0; +} + XDB_PUBLIC(void) xdb_region_buffer_free(xdb_region_buffer_t *region) { if (region->type == xdb_region_buffer_auto) { - xdb_free(region->value); + // empty string interception + if (region->length == 0 + || region->value == _empty_region_string) { + // do nothing for empty string + } else { + xdb_free(region->value); + } + + // reset the value region->value = NULL; } } @@ -196,7 +225,9 @@ XDB_PUBLIC(int) xdb_search(xdb_searcher_t *xdb, const bytes_ip_t *ip_bytes, int // printf("data_len=%u, data_ptr=%u\n", data_len, data_ptr); if (data_len == 0) { - return 100; + // return 100; + xdb_region_buffer_empty(region); + return err; } // buffer alloc checking