lib/expat/ExpatParser: pass std::string_view to CharacterData()

This commit is contained in:
Max Kellermann 2024-01-04 20:52:33 +01:00
parent feeb21577f
commit b283fe07af
4 changed files with 19 additions and 19 deletions

View File

@ -191,12 +191,12 @@ protected:
state = NONE; state = NONE;
} }
void CharacterData(const XML_Char *s, int len) override void CharacterData(std::string_view s) override
{ {
if (tag_type != TAG_NUM_OF_ITEM_TYPES) { if (tag_type != TAG_NUM_OF_ITEM_TYPES) {
assert(object.type != UPnPDirObject::Type::UNKNOWN); assert(object.type != UPnPDirObject::Type::UNKNOWN);
value.append(s, len); value.append(s);
return; return;
} }
@ -205,11 +205,11 @@ protected:
break; break;
case RES: case RES:
object.url.assign(s, len); object.url.assign(s);
break; break;
case CLASS: case CLASS:
object.item_class = ParseItemClass(std::string_view(s, len)); object.item_class = ParseItemClass(s);
break; break;
} }
} }

View File

@ -115,7 +115,7 @@ protected:
virtual void StartElement(const XML_Char *name, virtual void StartElement(const XML_Char *name,
const XML_Char **atts) = 0; const XML_Char **atts) = 0;
virtual void EndElement(const XML_Char *name) = 0; virtual void EndElement(const XML_Char *name) = 0;
virtual void CharacterData(const XML_Char *s, int len) = 0; virtual void CharacterData(const std::string_view s) = 0;
private: private:
static void XMLCALL StartElement(void *user_data, const XML_Char *name, static void XMLCALL StartElement(void *user_data, const XML_Char *name,
@ -132,6 +132,6 @@ private:
static void XMLCALL CharacterData(void *user_data, static void XMLCALL CharacterData(void *user_data,
const XML_Char *s, int len) { const XML_Char *s, int len) {
CommonExpatParser &p = *(CommonExpatParser *)user_data; CommonExpatParser &p = *(CommonExpatParser *)user_data;
p.CharacterData(s, len); p.CharacterData({s, static_cast<std::size_t>(len)});
} }
}; };

View File

@ -69,9 +69,9 @@ protected:
} }
} }
void CharacterData(const XML_Char *s, int len) override { void CharacterData(std::string_view s) override {
if (value != nullptr) if (value != nullptr)
value->append(s, len); value->append(s);
} }
}; };

View File

@ -172,9 +172,9 @@ ParseStatus(const char *s) noexcept
[[gnu::pure]] [[gnu::pure]]
static unsigned static unsigned
ParseStatus(const char *s, size_t length) noexcept ParseStatus(std::string_view s) noexcept
{ {
return ParseStatus(std::string(s, length).c_str()); return ParseStatus(std::string{s}.c_str());
} }
[[gnu::pure]] [[gnu::pure]]
@ -186,9 +186,9 @@ ParseTimeStamp(const char *s) noexcept
[[gnu::pure]] [[gnu::pure]]
static std::chrono::system_clock::time_point static std::chrono::system_clock::time_point
ParseTimeStamp(const char *s, size_t length) noexcept ParseTimeStamp(std::string_view s) noexcept
{ {
return ParseTimeStamp(std::string(s, length).c_str()); return ParseTimeStamp(std::string{s}.c_str());
} }
[[gnu::pure]] [[gnu::pure]]
@ -200,9 +200,9 @@ ParseU64(const char *s) noexcept
[[gnu::pure]] [[gnu::pure]]
static uint64_t static uint64_t
ParseU64(const char *s, size_t length) noexcept ParseU64(std::string_view s) noexcept
{ {
return ParseU64(std::string(s, length).c_str()); return ParseU64(std::string{s}.c_str());
} }
[[gnu::pure]] [[gnu::pure]]
@ -391,7 +391,7 @@ private:
} }
} }
void CharacterData(const XML_Char *s, int len) final { void CharacterData(std::string_view s) final {
switch (state) { switch (state) {
case State::ROOT: case State::ROOT:
case State::PROPSTAT: case State::PROPSTAT:
@ -400,19 +400,19 @@ private:
break; break;
case State::HREF: case State::HREF:
response.href.append(s, len); response.href.append(s);
break; break;
case State::STATUS: case State::STATUS:
response.status = ParseStatus(s, len); response.status = ParseStatus(s);
break; break;
case State::MTIME: case State::MTIME:
response.mtime = ParseTimeStamp(s, len); response.mtime = ParseTimeStamp(s);
break; break;
case State::LENGTH: case State::LENGTH:
response.length = ParseU64(s, len); response.length = ParseU64(s);
break; break;
} }
} }