diff options
author | Heikki Kallasjoki <fis@zem.fi> | 2018-11-16 22:02:08 +0000 |
---|---|---|
committer | Heikki Kallasjoki <fis@zem.fi> | 2018-11-16 22:02:08 +0000 |
commit | f771790b633f02f3857689e9848b0756aeb30b98 (patch) | |
tree | e50dd29b7b217396265cd07709c1e00b3880b59d | |
parent | e30fd51e676a9d03286c6dce5950314019800de4 (diff) | |
download | nano-exporter-f771790b633f02f3857689e9848b0756aeb30b98.tar.gz nano-exporter-f771790b633f02f3857689e9848b0756aeb30b98.tar.xz nano-exporter-f771790b633f02f3857689e9848b0756aeb30b98.zip |
Bugfix: cbuf_cmp handled prefixes incorrectly.
The previous version returned 0 if the string to compare was either equal to or a prefix of the target buffer. This change fixes it to only return 0 when the string is actually equal. Otherwise, it follows the shortlex ordering, for convenience.
-rw-r--r-- | util.c | 8 | ||||
-rw-r--r-- | util.h | 1 |
2 files changed, 8 insertions, 1 deletions
diff --git a/util.c b/util.c index e88ef95..643487b 100644 --- a/util.c +++ b/util.c @@ -93,7 +93,13 @@ const char *cbuf_get(struct cbuf *buf, size_t *len) { } int cbuf_cmp(cbuf *buf, const char *other) { - return strncmp(buf->data, other, buf->len); + size_t other_len = strlen(other); + if (buf->len < other_len) + return -1; + else if (buf->len == other_len) + return memcmp(buf->data, other, buf->len); + else + return +1; } // string lists diff --git a/util.h b/util.h index 70cd474..e1acc35 100644 --- a/util.h +++ b/util.h @@ -17,6 +17,7 @@ void cbuf_puts(cbuf *buf, const char *src); void cbuf_putc(cbuf *buf, int c); void cbuf_putf(cbuf *buf, const char *fmt, ...); const char *cbuf_get(struct cbuf *buf, size_t *len); +/** Compares the contents of \p buf to the string in \p other, in shortlex order. */ int cbuf_cmp(cbuf *buf, const char *other); // string lists |