aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function/CharsetOperator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/function/CharsetOperator.cpp')
-rw-r--r--src/core/function/CharsetOperator.cpp39
1 files changed, 22 insertions, 17 deletions
diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp
index 28dcec11..8623428d 100644
--- a/src/core/function/CharsetOperator.cpp
+++ b/src/core/function/CharsetOperator.cpp
@@ -35,14 +35,16 @@
#include <cstddef>
-GpgFrontend::CharsetOperator::CharsetInfo GpgFrontend::CharsetOperator::Detect(
- const std::string &buffer) {
+namespace GpgFrontend {
+
+auto CharsetOperator::Detect(const std::string &buffer)
+ -> CharsetOperator::CharsetInfo {
const UCharsetMatch *ucm;
UErrorCode status = U_ZERO_ERROR;
UCharsetDetector *csd = ucsdet_open(&status);
status = U_ZERO_ERROR;
- if (U_FAILURE(status)) {
+ if (U_FAILURE(status) != 0) {
SPDLOG_ERROR("failed to open charset detector: {}", u_errorName(status));
return {"unknown", "unknown", 0};
}
@@ -51,7 +53,7 @@ GpgFrontend::CharsetOperator::CharsetInfo GpgFrontend::CharsetOperator::Detect(
status = U_ZERO_ERROR;
ucsdet_setText(csd, buffer.data(), buffer.size(), &status);
- if (U_FAILURE(status)) {
+ if (U_FAILURE(status) != 0) {
SPDLOG_ERROR("failed to set text to charset detector: {}",
u_errorName(status));
return {"unknown", "unknown", 0};
@@ -60,37 +62,37 @@ GpgFrontend::CharsetOperator::CharsetInfo GpgFrontend::CharsetOperator::Detect(
status = U_ZERO_ERROR;
ucm = ucsdet_detect(csd, &status);
- if (U_FAILURE(status)) return {"unknown", "unknown", 0};
+ if (U_FAILURE(status) != 0) return {"unknown", "unknown", 0};
status = U_ZERO_ERROR;
const char *name = ucsdet_getName(ucm, &status);
- if (U_FAILURE(status)) return {"unknown", "unknown", 0};
+ if (U_FAILURE(status) != 0) return {"unknown", "unknown", 0};
status = U_ZERO_ERROR;
int confidence = ucsdet_getConfidence(ucm, &status);
- if (U_FAILURE(status)) return {name, "unknown", 0};
+ if (U_FAILURE(status) != 0) return {name, "unknown", 0};
status = U_ZERO_ERROR;
const char *language = ucsdet_getLanguage(ucm, &status);
- if (U_FAILURE(status)) return {name, "unknown", confidence};
+ if (U_FAILURE(status) != 0) return {name, "unknown", confidence};
SPDLOG_DEBUG("Detected charset: {} {} {}", name, language, confidence);
return {name, language, confidence};
}
-bool GpgFrontend::CharsetOperator::Convert2Utf8(const std::string &buffer,
- std::string &out_buffer,
- std::string from_charset_name) {
+auto CharsetOperator::Convert2Utf8(const std::string &buffer,
+ std::string &out_buffer,
+ std::string from_charset_name) -> bool {
UErrorCode status = U_ZERO_ERROR;
const auto from_encode = std::string("utf-8");
- const auto to_encode = from_charset_name;
+ const auto &to_encode = from_charset_name;
SPDLOG_DEBUG("Converting buffer: {}", buffer.size());
// test if the charset is supported
UConverter *conv = ucnv_open(from_encode.c_str(), &status);
ucnv_close(conv);
- if (U_FAILURE(status)) {
+ if (U_FAILURE(status) != 0) {
SPDLOG_ERROR("failed to open converter: {}, from encode: {}",
u_errorName(status), from_encode);
return false;
@@ -99,14 +101,15 @@ bool GpgFrontend::CharsetOperator::Convert2Utf8(const std::string &buffer,
// test if the charset is supported
conv = ucnv_open(to_encode.c_str(), &status);
ucnv_close(conv);
- if (U_FAILURE(status)) {
+ if (U_FAILURE(status) != 0) {
SPDLOG_ERROR("failed to open converter: {}, to encode: {}",
u_errorName(status), to_encode);
return false;
}
status = U_ZERO_ERROR;
- int32_t target_limit = 0, target_capacity = 0;
+ int32_t target_limit = 0;
+ int32_t target_capacity = 0;
target_capacity =
ucnv_convert(from_encode.c_str(), to_encode.c_str(), nullptr,
@@ -120,11 +123,13 @@ bool GpgFrontend::CharsetOperator::Convert2Utf8(const std::string &buffer,
out_buffer.size(), buffer.data(), buffer.size(), &status);
}
- if (U_FAILURE(status)) {
+ if (U_FAILURE(status) != 0) {
SPDLOG_ERROR("failed to convert to utf-8: {}", u_errorName(status));
return false;
}
SPDLOG_DEBUG("converted buffer: {} bytes", out_buffer.size());
return true;
-} \ No newline at end of file
+}
+
+} // namespace GpgFrontend \ No newline at end of file