From b7a1954c335b2324878c5c30b47828b092ed96c2 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 10 Oct 2014 22:01:29 +0200 Subject: TagString: return WritableBuffer --- src/tag/TagBuilder.cxx | 11 ++++++----- src/tag/TagString.cxx | 33 ++++++++++++++++----------------- src/tag/TagString.hxx | 6 ++++-- 3 files changed, 26 insertions(+), 24 deletions(-) (limited to 'src/tag') diff --git a/src/tag/TagBuilder.cxx b/src/tag/TagBuilder.cxx index 85060e9ee..c9ebcd654 100644 --- a/src/tag/TagBuilder.cxx +++ b/src/tag/TagBuilder.cxx @@ -23,6 +23,7 @@ #include "TagPool.hxx" #include "TagString.hxx" #include "Tag.hxx" +#include "util/WritableBuffer.hxx" #include #include @@ -184,17 +185,17 @@ TagBuilder::AddItemInternal(TagType type, const char *value, size_t length) assert(value != nullptr); assert(length > 0); - char *p = FixTagString(value, length); - if (p != nullptr) { - value = p; - length = strlen(value); + auto f = FixTagString(value, length); + if (!f.IsNull()) { + value = f.data; + length = f.size; } tag_pool_lock.lock(); auto i = tag_pool_get_item(type, value, length); tag_pool_lock.unlock(); - free(p); + free(f.data); items.push_back(i); } diff --git a/src/tag/TagString.cxx b/src/tag/TagString.cxx index 3a0f0b3f5..f4a170fbe 100644 --- a/src/tag/TagString.cxx +++ b/src/tag/TagString.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "TagString.hxx" #include "util/Alloc.hxx" +#include "util/WritableBuffer.hxx" #ifdef HAVE_GLIB #include @@ -34,21 +35,21 @@ /** * Replace invalid sequences with the question mark. */ -static char * +static WritableBuffer patch_utf8(const char *src, size_t length, const gchar *end) { /* duplicate the string, and replace invalid bytes in that buffer */ - char *dest = xstrndup(src, length); + char *dest = (char *)xmemdup(src, length); do { dest[end - src] = '?'; } while (!g_utf8_validate(end + 1, (src + length) - (end + 1), &end)); - return dest; + return { dest, length }; } -static char * +static WritableBuffer fix_utf8(const char *str, size_t length) { const gchar *end; @@ -85,43 +86,41 @@ find_non_printable(const char *p, size_t length) * Clears all non-printable characters, convert them to space. * Returns nullptr if nothing needs to be cleared. */ -static char * +static WritableBuffer clear_non_printable(const char *p, size_t length) { const char *first = find_non_printable(p, length); - char *dest; - if (first == nullptr) return nullptr; - dest = xstrndup(p, length); + char *dest = (char *)xmemdup(p, length); for (size_t i = first - p; i < length; ++i) if (char_is_non_printable(dest[i])) dest[i] = ' '; - return dest; + return { dest, length }; } -char * +WritableBuffer FixTagString(const char *p, size_t length) { #ifdef HAVE_GLIB // TODO: implement without GLib - char *utf8 = fix_utf8(p, length); - if (utf8 != nullptr) { - p = utf8; - length = strlen(p); + auto utf8 = fix_utf8(p, length); + if (!utf8.IsNull()) { + p = utf8.data; + length = utf8.size; } #endif - char *cleared = clear_non_printable(p, length); + WritableBuffer cleared = clear_non_printable(p, length); #ifdef HAVE_GLIB - if (cleared == nullptr) + if (cleared.IsNull()) cleared = utf8; else - free(utf8); + free(utf8.data); #endif return cleared; diff --git a/src/tag/TagString.hxx b/src/tag/TagString.hxx index a1a9d9d15..eccc2aa47 100644 --- a/src/tag/TagString.hxx +++ b/src/tag/TagString.hxx @@ -25,8 +25,10 @@ #include -gcc_malloc gcc_nonnull_all -char * +template struct WritableBuffer; + +gcc_nonnull_all +WritableBuffer FixTagString(const char *p, size_t length); #endif -- cgit v1.2.3