Skip to content

Commit 1aeebc1

Browse files
committed
Remove Iconv
We do not use this anywhere since years. Is a maintenance burden and nobody tests this. Fix #465
1 parent 28fd197 commit 1aeebc1

8 files changed

Lines changed: 57 additions & 325 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ project(liblcf VERSION 0.8 LANGUAGES CXX)
88

99
# Compilation options
1010
option(BUILD_SHARED_LIBS "Build shared library, disable for building the static library (default: ON)" ON)
11-
option(LIBLCF_WITH_ICU "ICU encoding detection (when OFF fallback to iconv, not recommended, default: ON)" ON)
11+
option(LIBLCF_WITH_ICU "ICU encoding handling (disable only for testing purposes, default: ON)" ON)
1212
option(LIBLCF_WITH_XML "XML reading support (expat, default: ON)" ON)
1313
option(LIBLCF_UPDATE_MIMEDB "Whether to run update-mime-database after install (default: ON)" ON)
1414
option(LIBLCF_ENABLE_TOOLS "Whether to build the tools (default: ON)" ON)
@@ -343,16 +343,13 @@ set_property(TARGET lcf PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)
343343
# Name of the exported library
344344
set_property(TARGET lcf PROPERTY EXPORT_NAME liblcf)
345345

346-
# icu or fallback to iconv
346+
# icu
347347
set(LCF_SUPPORT_ICU 0)
348348
if(LIBLCF_WITH_ICU)
349349
find_package(ICU COMPONENTS i18n uc data REQUIRED)
350350
target_link_libraries(lcf ICU::i18n ICU::uc ICU::data)
351351
list(APPEND LIBLCF_DEPS "icu-i18n")
352352
set(LCF_SUPPORT_ICU 1)
353-
else()
354-
find_package(Iconv REQUIRED)
355-
target_link_libraries(lcf Iconv::Iconv)
356353
endif()
357354

358355
# expat

‎builds/cmake/Modules/FindIconv.cmake‎

Lines changed: 0 additions & 133 deletions
This file was deleted.

‎builds/cmake/liblcf-config.cmake.in‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ include(CMakeFindDependencyMacro)
44

55
if(@LCF_SUPPORT_ICU@)
66
find_dependency(ICU COMPONENTS i18n uc data REQUIRED)
7-
else()
8-
find_dependency(Iconv REQUIRED)
97
endif()
108

119
if(@LCF_SUPPORT_XML@)

‎configure.ac‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ AM_CONDITIONAL(CROSS_COMPILING,[test "x$cross_compiling" = "xyes"])
2222

2323
# Checks for libraries.
2424
AC_SUBST([LCF_SUPPORT_ICU],[0])
25-
AC_ARG_ENABLE([icu],[AS_HELP_STRING([--disable-icu],[Disable ICU encoding detection (fallback to iconv) [default=no]])])
25+
AC_ARG_ENABLE([icu],[AS_HELP_STRING([--disable-icu],[Disable ICU encoding detection (only for testing purposes) [default=no]])])
2626
AS_IF([test "x$enable_icu" != "xno"],[
2727
AX_PKG_CHECK_MODULES([ICU],[],[icu-i18n],[LCF_SUPPORT_ICU=1])
2828
])

‎src/encoder.cpp‎

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "lcf/scope_guard.h"
1414
#include <cstdio>
1515
#include <cstdlib>
16-
#include <exception>
1716

1817
#if LCF_SUPPORT_ICU
1918
# include <unicode/ucsdet.h>
@@ -27,9 +26,6 @@
2726
#ifdef _WIN32
2827
# include <windows.h>
2928
#else
30-
# if !LCF_SUPPORT_ICU
31-
# include <iconv.h>
32-
# endif
3329
# include <locale>
3430
#endif
3531

@@ -77,12 +73,12 @@ void Encoder::Decode(std::string& str) {
7773
Convert(str, _conv_storage, _conv_runtime);
7874
}
7975

76+
#if LCF_SUPPORT_ICU
8077
void Encoder::Init() {
8178
if (_encoding.empty()) {
8279
return;
8380
}
8481

85-
#if LCF_SUPPORT_ICU
8682
auto code_page = atoi(_encoding.c_str());
8783
const auto& storage_encoding = code_page > 0
8884
? ReaderUtil::CodepageToEncoding(code_page)
@@ -110,27 +106,22 @@ void Encoder::Init() {
110106

111107
_conv_runtime = conv_runtime;
112108
_conv_storage = conv_storage;
113-
#else
114-
_conv_runtime = const_cast<char*>("UTF-8");
115-
_conv_storage = const_cast<char*>(_encoding.c_str());
116-
#endif
117109
}
118110

119111
void Encoder::Reset() {
120-
#if LCF_SUPPORT_ICU
121-
auto* conv = reinterpret_cast<UConverter*>(_conv_runtime);
122-
if (conv) ucnv_close(conv);
123-
conv = reinterpret_cast<UConverter*>(_conv_storage);
124-
if (conv) ucnv_close(conv);
125-
#endif
126-
}
112+
if (_conv_runtime) {
113+
ucnv_close(_conv_runtime);
114+
_conv_runtime = nullptr;
115+
}
127116

117+
if (_conv_storage) {
118+
ucnv_close(_conv_storage);
119+
_conv_storage = nullptr;
120+
}
121+
}
128122

129-
void Encoder::Convert(std::string& str, void* conv_dst_void, void* conv_src_void) {
130-
#if LCF_SUPPORT_ICU
123+
void Encoder::Convert(std::string& str, UConverter* conv_dst, UConverter* conv_src) {
131124
const auto& src = str;
132-
auto* conv_dst = reinterpret_cast<UConverter*>(conv_dst_void);
133-
auto* conv_src = reinterpret_cast<UConverter*>(conv_src_void);
134125

135126
auto status = U_ZERO_ERROR;
136127
_buffer.resize(src.size() * 4);
@@ -151,36 +142,8 @@ void Encoder::Convert(std::string& str, void* conv_dst_void, void* conv_src_void
151142
}
152143

153144
str.assign(_buffer.data(), dst_p);
154-
return;
155-
#else
156-
auto* conv_dst = reinterpret_cast<const char*>(conv_dst_void);
157-
auto* conv_src = reinterpret_cast<const char*>(conv_src_void);
158-
iconv_t cd = iconv_open(conv_dst, conv_src);
159-
if (cd == (iconv_t)-1)
160-
return;
161-
char *src = &str.front();
162-
size_t src_left = str.size();
163-
size_t dst_size = str.size() * 5 + 10;
164-
_buffer.resize(dst_size);
165-
char *dst = _buffer.data();
166-
size_t dst_left = dst_size;
167-
# ifdef ICONV_CONST
168-
char ICONV_CONST *p = src;
169-
# else
170-
char *p = src;
171-
# endif
172-
char *q = dst;
173-
size_t status = iconv(cd, &p, &src_left, &q, &dst_left);
174-
iconv_close(cd);
175-
if (status == (size_t) -1 || src_left > 0) {
176-
str.clear();
177-
return;
178-
}
179-
*q++ = '\0';
180-
str.assign(dst, dst_size - dst_left);
181-
return;
182-
#endif
183145
}
146+
#endif
184147

185148
} //namespace lcf
186149

‎src/lcf/encoder.h‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include <vector>
1313
#include <string>
1414

15+
#if LCF_SUPPORT_ICU
16+
class UConverter;
17+
#endif
18+
1519
namespace lcf {
1620

1721
class Encoder {
@@ -23,20 +27,39 @@ class Encoder {
2327

2428
~Encoder();
2529

30+
/**
31+
* Converts from the specified encoding to UTF-8
32+
*
33+
* @param str String to encode to UTF-8
34+
*/
2635
void Encode(std::string& str);
36+
37+
/**
38+
* Converts from UTF-8 to the specified encoding
39+
*
40+
* @param str String to decode from UTF-8
41+
*/
2742
void Decode(std::string& str);
2843

2944
bool IsOk() const;
3045

3146
const std::string& GetEncoding() const;
3247
private:
48+
#if LCF_SUPPORT_ICU
3349
void Init();
3450
void Reset();
35-
void Convert(std::string& str, void* conv_dst, void* conv_src);
36-
private:
51+
void Convert(std::string& str, UConverter* conv_dst, UConverter* conv_src);
52+
53+
UConverter* _conv_storage = nullptr;
54+
UConverter* _conv_runtime = nullptr;
55+
std::vector<char> _buffer;
56+
#else
57+
void Init() {}
58+
void Reset() {}
59+
void Convert(std::string&, void*, void*) {}
3760
void* _conv_storage = nullptr;
3861
void* _conv_runtime = nullptr;
39-
std::vector<char> _buffer;
62+
#endif
4063
std::string _encoding;
4164
};
4265

‎src/lcf/reader_util.h‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,14 @@ namespace ReaderUtil {
104104
*/
105105
std::string Recode(StringView str_to_encode, StringView source_encoding);
106106

107-
/**
108-
* Converts a string between encodings.
109-
*
110-
* @param str_to_encode the string to convert.
111-
* @param src_enc the source encoding.
112-
* @param dst_enc the destination encoding.
113-
* @return the recoded string.
114-
*/
115-
std::string Recode(StringView str_to_encode,
116-
StringView src_enc,
117-
StringView dst_enc);
118-
119107
/**
120108
* Converts a UTF-8 string to lowercase and then decomposes it.
121-
*
109+
*
122110
* @param str the string to normalize.
123111
* @return the normalized string.
124112
*/
125113
std::string Normalize(StringView str);
126114

127-
128115
/**
129116
* Helper function that returns an element from a vector using a 1-based
130117
* index as usually used by LCF data structures.

0 commit comments

Comments
 (0)