tag/FixString: use class AllocatedArray
This commit is contained in:
parent
ed7baf3ae1
commit
8e71130e8a
@ -22,7 +22,7 @@
|
|||||||
#include "Pool.hxx"
|
#include "Pool.hxx"
|
||||||
#include "FixString.hxx"
|
#include "FixString.hxx"
|
||||||
#include "Tag.hxx"
|
#include "Tag.hxx"
|
||||||
#include "util/WritableBuffer.hxx"
|
#include "util/AllocatedArray.hxx"
|
||||||
#include "util/StringView.hxx"
|
#include "util/StringView.hxx"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -217,11 +217,9 @@ TagBuilder::AddItemInternal(TagType type, StringView value) noexcept
|
|||||||
|
|
||||||
auto f = FixTagString(value);
|
auto f = FixTagString(value);
|
||||||
if (!f.IsNull())
|
if (!f.IsNull())
|
||||||
value = { f.data, f.size };
|
value = { f.data(), f.size() };
|
||||||
|
|
||||||
AddItemUnchecked(type, value);
|
AddItemUnchecked(type, value);
|
||||||
|
|
||||||
free(f.data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "FixString.hxx"
|
#include "FixString.hxx"
|
||||||
#include "util/Alloc.hxx"
|
#include "util/AllocatedArray.hxx"
|
||||||
#include "util/CharUtil.hxx"
|
#include "util/CharUtil.hxx"
|
||||||
#include "util/WritableBuffer.hxx"
|
#include "util/WritableBuffer.hxx"
|
||||||
#include "util/StringView.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.
|
* Replace invalid sequences with the question mark.
|
||||||
*/
|
*/
|
||||||
static WritableBuffer<char>
|
static AllocatedArray<char>
|
||||||
patch_utf8(StringView src, const char *_invalid)
|
patch_utf8(StringView src, const char *_invalid)
|
||||||
{
|
{
|
||||||
/* duplicate the string, and replace invalid bytes in that
|
/* duplicate the string, and replace invalid bytes in that
|
||||||
buffer */
|
buffer */
|
||||||
char *dest = (char *)xmemdup(src.data, src.size);
|
AllocatedArray<char> dest{src};
|
||||||
char *const end = dest + src.size;
|
char *const end = dest.data() + src.size;
|
||||||
|
|
||||||
char *invalid = dest + (_invalid - src.data);
|
char *invalid = dest.data() + (_invalid - src.data);
|
||||||
do {
|
do {
|
||||||
*invalid = '?';
|
*invalid = '?';
|
||||||
|
|
||||||
@ -71,10 +71,10 @@ patch_utf8(StringView src, const char *_invalid)
|
|||||||
invalid = const_cast<char *>(__invalid);
|
invalid = const_cast<char *>(__invalid);
|
||||||
} while (invalid != nullptr);
|
} while (invalid != nullptr);
|
||||||
|
|
||||||
return { dest, src.size };
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
static WritableBuffer<char>
|
static AllocatedArray<char>
|
||||||
fix_utf8(StringView p)
|
fix_utf8(StringView p)
|
||||||
{
|
{
|
||||||
/* check if the string is already valid UTF-8 */
|
/* 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.
|
* 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 WritableBuffer<char>
|
static AllocatedArray<char>
|
||||||
clear_non_printable(StringView src)
|
clear_non_printable(StringView src)
|
||||||
{
|
{
|
||||||
const char *first = find_non_printable(src);
|
const char *first = find_non_printable(src);
|
||||||
if (first == nullptr)
|
if (first == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
char *dest = (char *)xmemdup(src.data, src.size);
|
AllocatedArray<char> dest{src};
|
||||||
|
|
||||||
for (size_t i = first - src.data; i < src.size; ++i)
|
for (size_t i = first - src.data; i < src.size; ++i)
|
||||||
if (IsNonPrintableASCII(dest[i]))
|
if (IsNonPrintableASCII(dest[i]))
|
||||||
dest[i] = ' ';
|
dest[i] = ' ';
|
||||||
|
|
||||||
return { dest, src.size };
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[gnu::pure]]
|
[[gnu::pure]]
|
||||||
@ -126,7 +126,7 @@ IsSafe(StringView s) noexcept
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
WritableBuffer<char>
|
AllocatedArray<char>
|
||||||
FixTagString(StringView p)
|
FixTagString(StringView p)
|
||||||
{
|
{
|
||||||
if (IsSafe(p))
|
if (IsSafe(p))
|
||||||
@ -134,14 +134,12 @@ FixTagString(StringView p)
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto utf8 = fix_utf8(p);
|
auto utf8 = fix_utf8(p);
|
||||||
if (!utf8.IsNull())
|
if (utf8 != nullptr)
|
||||||
p = {utf8.data, utf8.size};
|
p = {utf8.data(), utf8.size()};
|
||||||
|
|
||||||
WritableBuffer<char> cleared = clear_non_printable(p);
|
auto cleared = clear_non_printable(p);
|
||||||
if (cleared.IsNull())
|
if (cleared == nullptr)
|
||||||
cleared = utf8;
|
cleared = std::move(utf8);
|
||||||
else
|
|
||||||
free(utf8.data);
|
|
||||||
|
|
||||||
return cleared;
|
return cleared;
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,9 @@
|
|||||||
#define MPD_TAG_STRING_HXX
|
#define MPD_TAG_STRING_HXX
|
||||||
|
|
||||||
struct StringView;
|
struct StringView;
|
||||||
template<typename T> struct WritableBuffer;
|
template<typename T> class AllocatedArray;
|
||||||
|
|
||||||
WritableBuffer<char>
|
AllocatedArray<char>
|
||||||
FixTagString(StringView p);
|
FixTagString(StringView p);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user