summaryrefslogtreecommitdiff
path: root/source/fitz
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-12-28 13:19:47 +0100
committerTor Andersson <tor.andersson@artifex.com>2017-01-09 13:21:40 +0100
commitbbcc85a9f746c161b2e23c6057e69ec7b967252b (patch)
tree8ead60bd762cc0cbf945c002c769e74699594c0b /source/fitz
parente9667e7f8ab7c154d8932916a22c33cf2bad0445 (diff)
downloadmupdf-bbcc85a9f746c161b2e23c6057e69ec7b967252b.tar.xz
Add fz_terminate_buffer function.
Non-destructively zero terminate a fz_buffer for use as a C string.
Diffstat (limited to 'source/fitz')
-rw-r--r--source/fitz/buffer.c14
-rw-r--r--source/fitz/stext-search.c5
2 files changed, 12 insertions, 7 deletions
diff --git a/source/fitz/buffer.c b/source/fitz/buffer.c
index 13aae983..a2880659 100644
--- a/source/fitz/buffer.c
+++ b/source/fitz/buffer.c
@@ -152,6 +152,15 @@ fz_trim_buffer(fz_context *ctx, fz_buffer *buf)
fz_resize_buffer(ctx, buf, buf->len);
}
+void
+fz_terminate_buffer(fz_context *ctx, fz_buffer *buf)
+{
+ /* ensure that there is a zero-byte after the end of the data */
+ if (buf->len + 1 > buf->cap)
+ fz_grow_buffer(ctx, buf);
+ buf->data[buf->len] = 0;
+}
+
size_t
fz_buffer_storage(fz_context *ctx, fz_buffer *buf, unsigned char **datap)
{
@@ -165,10 +174,7 @@ fz_string_from_buffer(fz_context *ctx, fz_buffer *buf)
{
if (!buf)
return "";
-
- if (buf->data[buf->len-1] != 0)
- fz_write_buffer_byte(ctx, buf, 0);
-
+ fz_terminate_buffer(ctx, buf);
return (const char *)buf->data;
}
diff --git a/source/fitz/stext-search.c b/source/fitz/stext-search.c
index 3904b01d..99584581 100644
--- a/source/fitz/stext-search.c
+++ b/source/fitz/stext-search.c
@@ -278,9 +278,8 @@ fz_copy_selection(fz_context *ctx, fz_stext_page *page, fz_rect rect)
}
}
- fz_write_buffer_byte(ctx, buffer, 0);
-
- fz_buffer_extract(ctx, buffer, &s);
+ fz_terminate_buffer(ctx, buffer);
+ fz_buffer_extract(ctx, buffer, &s); /* take over the data */
fz_drop_buffer(ctx, buffer);
return (char*)s;
}