From 60c757dcf6853f8893891ca96c5400394ec1c5f7 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 3 Jun 2018 19:02:22 +0200 Subject: [PATCH] Add c'tor overload to TagValue to prevent conversion to string --- tagvalue.h | 16 ++++++++++++++++ tests/tagvalue.cpp | 1 + 2 files changed, 17 insertions(+) diff --git a/tagvalue.h b/tagvalue.h index a3fb289..0188c95 100644 --- a/tagvalue.h +++ b/tagvalue.h @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -66,6 +67,7 @@ public: TagValue(); TagValue(const char *text, std::size_t textSize, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified); + TagValue(const char *text, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified); TagValue( const std::string &text, TagTextEncoding textEncoding = TagTextEncoding::Latin1, TagTextEncoding convertTo = TagTextEncoding::Unspecified); TagValue(int value); @@ -179,6 +181,20 @@ inline TagValue::TagValue(const char *text, std::size_t textSize, TagTextEncodin assignText(text, textSize, textEncoding, convertTo); } +/*! + * \brief Constructs a new TagValue holding a copy of the given \a text. + * \param text Specifies the text to be assigned. This string must be null-terminated. + * \param textEncoding Specifies the encoding of the given \a text. + * \param convertTo Specifies the encoding to convert \a text to; set to TagTextEncoding::Unspecified to + * use \a textEncoding without any character set conversions. + * \throws Throws a ConversionException if the conversion the specified character set fails. + * \remarks Strips the BOM of the specified \a text. + */ +inline TagValue::TagValue(const char *text, TagTextEncoding textEncoding, TagTextEncoding convertTo) +{ + assignText(text, std::strlen(text), textEncoding, convertTo); +} + /*! * \brief Constructs a new TagValue holding a copy of the given \a text. * \param text Specifies the text to be assigned. diff --git a/tests/tagvalue.cpp b/tests/tagvalue.cpp index 2b14bbf..ce235d9 100644 --- a/tests/tagvalue.cpp +++ b/tests/tagvalue.cpp @@ -146,6 +146,7 @@ void TagValueTests::testDateTime() void TagValueTests::testString() { CPPUNIT_ASSERT_EQUAL("15\xe4"s, TagValue("15ä", 4, TagTextEncoding::Utf8).toString(TagTextEncoding::Latin1)); + CPPUNIT_ASSERT_EQUAL("15\xe4"s, TagValue("15ä", TagTextEncoding::Utf8, TagTextEncoding::Latin1).toString()); CPPUNIT_ASSERT_EQUAL("15ä"s, TagValue("15ä", 4, TagTextEncoding::Utf8).toString(TagTextEncoding::Utf8)); CPPUNIT_ASSERT_EQUAL("\x31\0\x35\0"s, TagValue(15).toString(TagTextEncoding::Utf16LittleEndian)); CPPUNIT_ASSERT_EQUAL("\0\x31\0\x35"s, TagValue(15).toString(TagTextEncoding::Utf16BigEndian));