-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring test coverage back to decent levels
- Loading branch information
Showing
9 changed files
with
116 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
#include "serialization.h" | ||
#include <unicode/ucnv.h> | ||
#include <fmt/format.h> | ||
#include <utf8h/utf8.h> | ||
#include <stdexcept> | ||
|
||
std::string iggy::serialization::WireFormat::convertToUTF8(const std::string& source) { | ||
UErrorCode status = U_ZERO_ERROR; | ||
char target[256]; | ||
|
||
// Convert the string to UTF-8 | ||
ucnv_convert("UTF-8", "UTF-16", target, sizeof(target), source.c_str(), source.length(), &status); | ||
if (U_FAILURE(status)) { | ||
throw std::runtime_error("Failed to convert string to UTF-8"); | ||
std::string iggy::serialization::convertToUTF8(const std::string& source, bool strict) { | ||
if (utf8valid(source.c_str()) == 0) { | ||
return source; | ||
} else { | ||
if (strict) { | ||
throw std::invalid_argument(fmt::format("The input string is not a valid UTF-8 string: '{}'", source)); | ||
} else { | ||
char* data = new char[source.size() + 1]; | ||
std::snprintf(data, source.size() + 1, "%s", source.c_str()); | ||
utf8makevalid(data, '?'); | ||
return std::string(data); | ||
} | ||
} | ||
|
||
return std::string(target); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "../sdk/serialization.h" | ||
#include "unit_testutils.h" | ||
|
||
TEST_CASE("serialization utilities", UT_TAG) { | ||
SECTION("valid UTF-8") { | ||
auto utf8 = iggy::serialization::convertToUTF8("hello world"); | ||
REQUIRE(utf8 == "hello world"); | ||
} | ||
SECTION("invalid UTF-8, strict checking") { | ||
REQUIRE_THROWS_AS(iggy::serialization::convertToUTF8("hello \x80 world", true), std::invalid_argument); | ||
} | ||
SECTION("invalid UTF-8, non-strict checking") { | ||
auto utf8 = iggy::serialization::convertToUTF8("hello \x80 world", false); | ||
REQUIRE(utf8 == "hello ? world"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters