Recode - use StringView
MODIFY
src/lcf/reader_util.h
src/lcf/reader_util.h
@@ -99,7 +99,7 @@ namespace ReaderUtil {
*
* @return the recoded string.
*/
- std::string Recode(const std::string& str_to_encode, const std::string& source_encoding);
+ std::string Recode(StringView str_to_encode, StringView source_encoding);
/**
* Converts a string between encodings.
@@ -109,9 +109,9 @@ namespace ReaderUtil {
* @param dst_enc the destination encoding.
* @return the recoded string.
*/
- std::string Recode(const std::string& str_to_encode,
- const std::string& src_enc,
- const std::string& dst_enc);
+ std::string Recode(StringView str_to_encode,
+ StringView src_enc,
+ StringView dst_enc);
/**
* Converts a UTF-8 string to lowercase and then decomposes it.

MODIFY
src/reader_util.cpp
src/reader_util.cpp
@@ -282,27 +282,27 @@ std::string ReaderUtil::GetLocaleEncoding() {
return CodepageToEncoding(codepage);
}
-std::string ReaderUtil::Recode(const std::string& str_to_encode, const std::string& source_encoding) {
+std::string ReaderUtil::Recode(StringView str_to_encode, StringView source_encoding) {
return ReaderUtil::Recode(str_to_encode, source_encoding, "UTF-8");
}
-std::string ReaderUtil::Recode(const std::string& str_to_encode,
- const std::string& src_enc,
- const std::string& dst_enc) {
+std::string ReaderUtil::Recode(StringView str_to_encode,
+ StringView src_enc,
+ StringView dst_enc) {
if (src_enc.empty() || dst_enc.empty() || str_to_encode.empty()) {
- return str_to_encode;
+ return ToString(str_to_encode);
}
- auto src_cp = atoi(src_enc.c_str());
+ auto src_cp = SvAtoi(src_enc);
const auto& src_enc_str = src_cp > 0
? ReaderUtil::CodepageToEncoding(src_cp)
- : src_enc;
+ : ToString(src_enc);
- auto dst_cp = atoi(dst_enc.c_str());
+ auto dst_cp = SvAtoi(dst_enc);
const auto& dst_enc_str = dst_cp > 0
? ReaderUtil::CodepageToEncoding(dst_cp)
- : dst_enc;
+ : ToString(dst_enc);
#if LCF_SUPPORT_ICU
auto status = U_ZERO_ERROR;
@@ -325,7 +325,7 @@ std::string ReaderUtil::Recode(const std::string& str_to_encode,
status = U_ZERO_ERROR;
std::string result(str_to_encode.size() * 4, '\0');
- auto* src = &str_to_encode.front();
+ auto* src = str_to_encode.data();
auto* dst = &result.front();
ucnv_convertEx(conv_to, conv_from,
@@ -336,7 +336,7 @@ std::string ReaderUtil::Recode(const std::string& str_to_encode,
&status);
if (U_FAILURE(status)) {
- fprintf(stderr, "liblcf: ucnv_convertEx() error when encoding \"%s\": %s\n", str_to_encode.c_str(), u_errorName(status));
+ fprintf(stderr, "liblcf: ucnv_convertEx() error when encoding \"%.*s\": %s\n", (int)str_to_encode.length(), str_to_encode.data(), u_errorName(status));
return std::string();
}
@@ -347,8 +347,8 @@ std::string ReaderUtil::Recode(const std::string& str_to_encode,
#else
iconv_t cd = iconv_open(dst_enc_str.c_str(), src_enc_str.c_str());
if (cd == (iconv_t)-1)
- return str_to_encode;
- char *src = const_cast<char *>(str_to_encode.c_str());
+ return ToString(str_to_encode);
+ char *src = const_cast<char *>(str_to_encode.data());
size_t src_left = str_to_encode.size();
size_t dst_size = str_to_encode.size() * 5 + 10;
char *dst = new char[dst_size];

Recode - use StringView