Commit b7a1954c authored by Max Kellermann's avatar Max Kellermann

TagString: return WritableBuffer<char>

parent 6520589a
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "TagPool.hxx" #include "TagPool.hxx"
#include "TagString.hxx" #include "TagString.hxx"
#include "Tag.hxx" #include "Tag.hxx"
#include "util/WritableBuffer.hxx"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
...@@ -184,17 +185,17 @@ TagBuilder::AddItemInternal(TagType type, const char *value, size_t length) ...@@ -184,17 +185,17 @@ TagBuilder::AddItemInternal(TagType type, const char *value, size_t length)
assert(value != nullptr); assert(value != nullptr);
assert(length > 0); assert(length > 0);
char *p = FixTagString(value, length); auto f = FixTagString(value, length);
if (p != nullptr) { if (!f.IsNull()) {
value = p; value = f.data;
length = strlen(value); length = f.size;
} }
tag_pool_lock.lock(); tag_pool_lock.lock();
auto i = tag_pool_get_item(type, value, length); auto i = tag_pool_get_item(type, value, length);
tag_pool_lock.unlock(); tag_pool_lock.unlock();
free(p); free(f.data);
items.push_back(i); items.push_back(i);
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "config.h" #include "config.h"
#include "TagString.hxx" #include "TagString.hxx"
#include "util/Alloc.hxx" #include "util/Alloc.hxx"
#include "util/WritableBuffer.hxx"
#ifdef HAVE_GLIB #ifdef HAVE_GLIB
#include <glib.h> #include <glib.h>
...@@ -34,21 +35,21 @@ ...@@ -34,21 +35,21 @@
/** /**
* Replace invalid sequences with the question mark. * Replace invalid sequences with the question mark.
*/ */
static char * static WritableBuffer<char>
patch_utf8(const char *src, size_t length, const gchar *end) patch_utf8(const char *src, size_t length, const gchar *end)
{ {
/* duplicate the string, and replace invalid bytes in that /* duplicate the string, and replace invalid bytes in that
buffer */ buffer */
char *dest = xstrndup(src, length); char *dest = (char *)xmemdup(src, length);
do { do {
dest[end - src] = '?'; dest[end - src] = '?';
} while (!g_utf8_validate(end + 1, (src + length) - (end + 1), &end)); } while (!g_utf8_validate(end + 1, (src + length) - (end + 1), &end));
return dest; return { dest, length };
} }
static char * static WritableBuffer<char>
fix_utf8(const char *str, size_t length) fix_utf8(const char *str, size_t length)
{ {
const gchar *end; const gchar *end;
...@@ -85,43 +86,41 @@ find_non_printable(const char *p, size_t length) ...@@ -85,43 +86,41 @@ find_non_printable(const char *p, size_t length)
* Clears all non-printable characters, convert them to space. * Clears all non-printable characters, convert them to space.
* Returns nullptr if nothing needs to be cleared. * Returns nullptr if nothing needs to be cleared.
*/ */
static char * static WritableBuffer<char>
clear_non_printable(const char *p, size_t length) clear_non_printable(const char *p, size_t length)
{ {
const char *first = find_non_printable(p, length); const char *first = find_non_printable(p, length);
char *dest;
if (first == nullptr) if (first == nullptr)
return nullptr; return nullptr;
dest = xstrndup(p, length); char *dest = (char *)xmemdup(p, length);
for (size_t i = first - p; i < length; ++i) for (size_t i = first - p; i < length; ++i)
if (char_is_non_printable(dest[i])) if (char_is_non_printable(dest[i]))
dest[i] = ' '; dest[i] = ' ';
return dest; return { dest, length };
} }
char * WritableBuffer<char>
FixTagString(const char *p, size_t length) FixTagString(const char *p, size_t length)
{ {
#ifdef HAVE_GLIB #ifdef HAVE_GLIB
// TODO: implement without GLib // TODO: implement without GLib
char *utf8 = fix_utf8(p, length); auto utf8 = fix_utf8(p, length);
if (utf8 != nullptr) { if (!utf8.IsNull()) {
p = utf8; p = utf8.data;
length = strlen(p); length = utf8.size;
} }
#endif #endif
char *cleared = clear_non_printable(p, length); WritableBuffer<char> cleared = clear_non_printable(p, length);
#ifdef HAVE_GLIB #ifdef HAVE_GLIB
if (cleared == nullptr) if (cleared.IsNull())
cleared = utf8; cleared = utf8;
else else
free(utf8); free(utf8.data);
#endif #endif
return cleared; return cleared;
......
...@@ -25,8 +25,10 @@ ...@@ -25,8 +25,10 @@
#include <stddef.h> #include <stddef.h>
gcc_malloc gcc_nonnull_all template<typename T> struct WritableBuffer;
char *
gcc_nonnull_all
WritableBuffer<char>
FixTagString(const char *p, size_t length); FixTagString(const char *p, size_t length);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment