tag/FixString: use std::string_view instead of StringView

This commit is contained in:
Max Kellermann 2022-05-24 12:20:33 +02:00
parent 6d113de1f8
commit 73e2ac4211
2 changed files with 14 additions and 13 deletions

View File

@ -20,7 +20,6 @@
#include "FixString.hxx"
#include "util/AllocatedArray.hxx"
#include "util/CharUtil.hxx"
#include "util/StringView.hxx"
#include "util/UTF8.hxx"
#include <algorithm>
@ -55,14 +54,14 @@ FindInvalidUTF8(const char *p, const char *const end) noexcept
* Replace invalid sequences with the question mark.
*/
static AllocatedArray<char>
patch_utf8(StringView src, const char *_invalid)
patch_utf8(std::string_view src, const char *_invalid) noexcept
{
/* duplicate the string, and replace invalid bytes in that
buffer */
AllocatedArray<char> dest{src};
char *const end = dest.data() + src.size;
char *const end = dest.data() + src.size();
char *invalid = dest.data() + (_invalid - src.data);
char *invalid = dest.data() + (_invalid - src.data());
do {
*invalid = '?';
@ -74,10 +73,10 @@ patch_utf8(StringView src, const char *_invalid)
}
static AllocatedArray<char>
fix_utf8(StringView p)
fix_utf8(std::string_view p) noexcept
{
/* check if the string is already valid UTF-8 */
const char *invalid = FindInvalidUTF8(p.begin(), p.end());
const char *invalid = FindInvalidUTF8(p.data(), p.data() + p.size());
if (invalid == nullptr)
return nullptr;
@ -85,8 +84,9 @@ fix_utf8(StringView p)
return patch_utf8(p, invalid);
}
[[gnu::pure]]
static const char *
find_non_printable(StringView p)
find_non_printable(std::string_view p) noexcept
{
for (const char &ch : p)
if (IsNonPrintableASCII(ch))
@ -100,7 +100,7 @@ find_non_printable(StringView p)
* Returns nullptr if nothing needs to be cleared.
*/
static AllocatedArray<char>
clear_non_printable(StringView src)
clear_non_printable(std::string_view src)
{
const char *first = find_non_printable(src);
if (first == nullptr)
@ -108,7 +108,7 @@ clear_non_printable(StringView src)
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]))
dest[i] = ' ';
@ -117,7 +117,7 @@ clear_non_printable(StringView src)
[[gnu::pure]]
static bool
IsSafe(StringView s) noexcept
IsSafe(std::string_view s) noexcept
{
return std::all_of(s.begin(), s.end(),
[](char ch){
@ -126,7 +126,7 @@ IsSafe(StringView s) noexcept
}
AllocatedArray<char>
FixTagString(StringView p)
FixTagString(std::string_view p) noexcept
{
if (IsSafe(p))
/* optimistic optimization for the common case */

View File

@ -20,10 +20,11 @@
#ifndef MPD_TAG_STRING_HXX
#define MPD_TAG_STRING_HXX
struct StringView;
#include <string_view>
template<typename T> class AllocatedArray;
AllocatedArray<char>
FixTagString(StringView p);
FixTagString(std::string_view p) noexcept;
#endif