diff --git a/src/tag/Builder.cxx b/src/tag/Builder.cxx index f7a283d9d..61bcba26f 100644 --- a/src/tag/Builder.cxx +++ b/src/tag/Builder.cxx @@ -22,7 +22,7 @@ #include "Pool.hxx" #include "FixString.hxx" #include "Tag.hxx" -#include "util/WritableBuffer.hxx" +#include "util/AllocatedArray.hxx" #include "util/StringView.hxx" #include @@ -217,11 +217,9 @@ TagBuilder::AddItemInternal(TagType type, StringView value) noexcept auto f = FixTagString(value); if (!f.IsNull()) - value = { f.data, f.size }; + value = { f.data(), f.size() }; AddItemUnchecked(type, value); - - free(f.data); } void diff --git a/src/tag/FixString.cxx b/src/tag/FixString.cxx index f2069f033..d1877cfaa 100644 --- a/src/tag/FixString.cxx +++ b/src/tag/FixString.cxx @@ -18,7 +18,7 @@ */ #include "FixString.hxx" -#include "util/Alloc.hxx" +#include "util/AllocatedArray.hxx" #include "util/CharUtil.hxx" #include "util/WritableBuffer.hxx" #include "util/StringView.hxx" @@ -55,15 +55,15 @@ FindInvalidUTF8(const char *p, const char *const end) noexcept /** * Replace invalid sequences with the question mark. */ -static WritableBuffer +static AllocatedArray patch_utf8(StringView src, const char *_invalid) { /* duplicate the string, and replace invalid bytes in that buffer */ - char *dest = (char *)xmemdup(src.data, src.size); - char *const end = dest + src.size; + AllocatedArray dest{src}; + char *const end = dest.data() + src.size; - char *invalid = dest + (_invalid - src.data); + char *invalid = dest.data() + (_invalid - src.data); do { *invalid = '?'; @@ -71,10 +71,10 @@ patch_utf8(StringView src, const char *_invalid) invalid = const_cast(__invalid); } while (invalid != nullptr); - return { dest, src.size }; + return dest; } -static WritableBuffer +static AllocatedArray fix_utf8(StringView p) { /* check if the string is already valid UTF-8 */ @@ -100,20 +100,20 @@ find_non_printable(StringView p) * Clears all non-printable characters, convert them to space. * Returns nullptr if nothing needs to be cleared. */ -static WritableBuffer +static AllocatedArray clear_non_printable(StringView src) { const char *first = find_non_printable(src); if (first == nullptr) return nullptr; - char *dest = (char *)xmemdup(src.data, src.size); + AllocatedArray dest{src}; for (size_t i = first - src.data; i < src.size; ++i) if (IsNonPrintableASCII(dest[i])) dest[i] = ' '; - return { dest, src.size }; + return dest; } [[gnu::pure]] @@ -126,7 +126,7 @@ IsSafe(StringView s) noexcept }); } -WritableBuffer +AllocatedArray FixTagString(StringView p) { if (IsSafe(p)) @@ -134,14 +134,12 @@ FixTagString(StringView p) return nullptr; auto utf8 = fix_utf8(p); - if (!utf8.IsNull()) - p = {utf8.data, utf8.size}; + if (utf8 != nullptr) + p = {utf8.data(), utf8.size()}; - WritableBuffer cleared = clear_non_printable(p); - if (cleared.IsNull()) - cleared = utf8; - else - free(utf8.data); + auto cleared = clear_non_printable(p); + if (cleared == nullptr) + cleared = std::move(utf8); return cleared; } diff --git a/src/tag/FixString.hxx b/src/tag/FixString.hxx index 881763212..ea4d949b7 100644 --- a/src/tag/FixString.hxx +++ b/src/tag/FixString.hxx @@ -21,9 +21,9 @@ #define MPD_TAG_STRING_HXX struct StringView; -template struct WritableBuffer; +template class AllocatedArray; -WritableBuffer +AllocatedArray FixTagString(StringView p); #endif