summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2025-08-12 00:35:18 +0200
committerEkaitz Zarraga <ekaitz@elenq.tech>2025-08-12 00:36:14 +0200
commit5dd98385e03b9cc7c912b1b50f7c406df117a9af (patch)
tree2fb914bac78ce8cfe77455ae47d710a4cbd0d1db
parent85088f13c5653174c27f54faac5afd89fbb10655 (diff)
piece-table: Don't allocate in piece_table_to_string
`piece_table_to_string` now receives a buffer and we also provide a function that returns the length of the piece table. TODO: the `piece_table_to_string` adds null termination, that makes the buffer needed `piece_table_length(pt) + 1`. This we should document or do more obvious.
-rw-r--r--src/piece-table.c16
-rw-r--r--src/piece-table.h3
-rw-r--r--tests/piece-table-delete.c14
-rw-r--r--tests/piece-table-insert.c23
4 files changed, 38 insertions, 18 deletions
diff --git a/src/piece-table.c b/src/piece-table.c
index a8ddd1e..b29f3be 100644
--- a/src/piece-table.c
+++ b/src/piece-table.c
@@ -415,15 +415,19 @@ piece_table_index (piece_table *pt, size_t pos)
pt->cached->start + pos - pt->cached_offset);
}
-char *
-piece_table_to_string (piece_table *pt)
+void
+piece_table_to_string (piece_table *pt, char *buf)
{
size_t i;
- char *ret = malloc (sizeof (*ret) * pt->length + 1);
for (i = 0; i < pt->length; i++)
{
- ret[i] = piece_table_index(pt, i);
+ buf[i] = piece_table_index (pt, i);
}
- ret[i] = '\0';
- return ret;
+ buf[i] = '\0';
+}
+
+size_t
+piece_table_length (piece_table *pt)
+{
+ return pt->length;
}
diff --git a/src/piece-table.h b/src/piece-table.h
index c9d6601..e90d2fa 100644
--- a/src/piece-table.h
+++ b/src/piece-table.h
@@ -29,6 +29,7 @@ char piece_table_index (piece_table *pt, size_t pos);
void piece_table_insert (piece_table *pt, size_t pos, char *in, size_t len);
void piece_table_delete (piece_table *pt, size_t pos, size_t len);
-char *piece_table_to_string (piece_table *pt);
+void piece_table_to_string (piece_table *pt, char *buf);
+size_t piece_table_length (piece_table *pt);
#endif
diff --git a/tests/piece-table-delete.c b/tests/piece-table-delete.c
index c883b34..dc54ccc 100644
--- a/tests/piece-table-delete.c
+++ b/tests/piece-table-delete.c
@@ -6,27 +6,33 @@ int
main()
{
piece_table *pt = piece_table_create("0123456789");
+ char tmp[100];
+
piece_table_delete(pt, 0, 10);
- if (strcmp(piece_table_to_string(pt), ""))
+ piece_table_to_string(pt, tmp);
+ if (strcmp(tmp, ""))
return 1;
piece_table_destroy(pt);
pt = piece_table_create("0123456789");
piece_table_delete(pt, 0, 1);
- if (strcmp(piece_table_to_string(pt), "123456789"))
+ piece_table_to_string(pt, tmp);
+ if (strcmp(tmp, "123456789"))
return 2;
piece_table_destroy(pt);
pt = piece_table_create("0123456789");
piece_table_delete(pt, 9, 1);
- if (strcmp(piece_table_to_string(pt), "012345678"))
+ piece_table_to_string(pt, tmp);
+ if (strcmp(tmp, "012345678"))
return 3;
piece_table_destroy(pt);
pt = piece_table_create("0123456789");
piece_table_delete(pt, 7, 1);
piece_table_delete(pt, 0, 1);
- if (strcmp(piece_table_to_string(pt), "12345689"))
+ piece_table_to_string(pt, tmp);
+ if (strcmp(tmp, "12345689"))
return 4;
piece_table_destroy(pt);
diff --git a/tests/piece-table-insert.c b/tests/piece-table-insert.c
index 90a97a2..bcf6dc5 100644
--- a/tests/piece-table-insert.c
+++ b/tests/piece-table-insert.c
@@ -1,19 +1,28 @@
#include <string.h>
+#include <stdlib.h>
#include "../src/piece-table.h"
int
main()
{
- piece_table *pt = piece_table_create("1234567890");
+ piece_table *pt = piece_table_create("0123456789");
+ char tmp[100];
+
piece_table_insert(pt, 10, "abcdefgh", 8);
- piece_table_insert(pt, 18, "abcdefgh", 8);
- if (strcmp(piece_table_to_string(pt), "1234567890abcdefghabcdefgh"))
+ piece_table_to_string(pt, tmp);
+ if (strcmp(tmp, "0123456789abcdefgh"))
return 1;
- /* TODO: must have two pieces if optimized properly
- 1234567890
- abcdefghabcdefgh
- TODO: check other things too */
+ piece_table_insert(pt, 18, "abcdefgh", 8);
+ piece_table_to_string(pt, tmp);
+ if (strcmp(tmp, "0123456789abcdefghabcdefgh"))
+ return 2;
+
+ piece_table_insert(pt, 4, "abcdefgh", 8);
+ piece_table_to_string(pt, tmp);
+ if (strcmp(tmp, "0123abcdefgh456789abcdefghabcdefgh"))
+ return 3;
+
piece_table_destroy(pt);
return 0;
}