Encoder use out parameters to avoid additional copies
Speeds up cases where encoding is disabled
MODIFY
src/encoder.cpp
src/encoder.cpp
@@ -39,12 +39,16 @@ Encoder::~Encoder() {
Reset();
}
-std::string Encoder::Encode(const std::string& src) {
- return Convert(_conv_runtime, _conv_storage, src);
+void Encoder::Encode(std::string& str) {
+ if (!_encoding.empty()) {
+ Convert(str, _conv_runtime, _conv_storage);
+ }
}
-std::string Encoder::Decode(const std::string& src) {
- return Convert(_conv_storage, _conv_runtime, src);
+void Encoder::Decode(std::string& str) {
+ if (!_encoding.empty()) {
+ Convert(str, _conv_storage, _conv_runtime);
+ }
}
void Encoder::Init() {
@@ -92,11 +96,9 @@ void Encoder::Reset() {
}
-std::string Encoder::Convert(void* conv_dst_void, void* conv_src_void, const std::string& src) {
- if (_encoding.empty()) {
- return src;
- }
+void Encoder::Convert(std::string& str, void* conv_dst_void, void* conv_src_void) {
#ifdef LCF_SUPPORT_ICU
+ const auto& src = str;
auto* conv_dst = reinterpret_cast<UConverter*>(conv_dst_void);
auto* conv_src = reinterpret_cast<UConverter*>(conv_src_void);
@@ -115,10 +117,11 @@ std::string Encoder::Convert(void* conv_dst_void, void* conv_src_void, const std
if (U_FAILURE(status)) {
fprintf(stderr, "liblcf: ucnv_convertEx() error when encoding \"%s\": %s\n", src.c_str(), u_errorName(status));
- return std::string();
+ _buffer.clear();
}
- return std::string(_buffer.data(), dst_p);
+ str.assign(_buffer.data(), dst_p);
+ return;
#endif
}

MODIFY
src/encoder.h
src/encoder.h
@@ -21,14 +21,14 @@ class Encoder {
~Encoder();
- std::string Encode(const std::string& src);
- std::string Decode(const std::string& src);
+ void Encode(std::string& str);
+ void Decode(std::string& str);
const std::string& GetEncoding() const;
private:
void Init();
void Reset();
- std::string Convert(void* conv_dst, void* conv_src, const std::string& src);
+ void Convert(std::string& str, void* conv_dst, void* conv_src);
private:
void* _conv_storage = nullptr;
void* _conv_runtime = nullptr;

MODIFY
src/reader_lcf.cpp
src/reader_lcf.cpp
@@ -183,7 +183,7 @@ void LcfReader::Read<uint32_t>(std::vector<uint32_t> &buffer, size_t size) {
void LcfReader::ReadString(std::string& ref, size_t size) {
ref.resize(size);
Read((size > 0 ? &ref.front(): nullptr), 1, size);
- ref = Encode(ref);
+ Encode(ref);
}
bool LcfReader::IsOk() const {
@@ -270,11 +270,11 @@ const std::string& LcfReader::GetError() {
return error_str;
}
-std::string LcfReader::Encode(const std::string& str_to_encode) {
+void LcfReader::Encode(std::string& str) {
#ifdef LCF_SUPPORT_ICU
- return encoder.Encode(str_to_encode);
+ encoder.Encode(str);
#else
- return ReaderUtil::Recode(str_to_encode, encoder.GetEncoding(), "UTF-8");
+ str = ReaderUtil::Recode(str, encoder.GetEncoding(), "UTF-8");
#endif
}

MODIFY
src/reader_lcf.h
src/reader_lcf.h
@@ -200,10 +200,9 @@ public:
* Encodes a string to UTF-8 using the set encoding
* in the reader constructor.
*
- * @param str_to_encode string to encode.
- * @return UTF-8 version of string.
+ * @param str to convert from encoding to UTF-8
*/
- std::string Encode(const std::string& str_to_encode);
+ void Encode(std::string& str);
/**
* Calculates the size of a compressed integer.

MODIFY
src/writer_lcf.cpp
src/writer_lcf.cpp
@@ -129,11 +129,13 @@ bool LcfWriter::IsOk() const {
return (stream.good());
}
-std::string LcfWriter::Decode(const std::string& str_to_encode) {
+std::string LcfWriter::Decode(const std::string& str) {
#ifdef LCF_SUPPORT_ICU
- return encoder.Decode(str_to_encode);
+ auto copy = str;
+ encoder.Decode(copy);
+ return copy;
#else
- return ReaderUtil::Recode(str_to_encode, "UTF-8", encoder.GetEncoding());
+ return ReaderUtil::Recode(str, "UTF-8", encoder.GetEncoding());
#endif
}

Encoder use out parameters to avoid additional copies

Speeds up cases where encoding is disabled