Stop using deprecated hunspell API

This commit is contained in:
wangqr 2019-09-08 00:29:41 -04:00
parent 81160b2ec0
commit e4e04c9e87
4 changed files with 55 additions and 10 deletions

View file

@ -547,6 +547,9 @@ if (HUNSPELL_FOUND)
target_link_libraries (Aegisub ${HUNSPELL_LIBRARIES})
add_definitions("-DWITH_HUNSPELL")
target_sources(Aegisub PRIVATE src/spellchecker_hunspell.cpp)
if (HUNSPELL_HAS_STRING_API)
target_compile_definitions(Aegisub PRIVATE "HUNSPELL_HAS_STRING_API")
endif(HUNSPELL_HAS_STRING_API)
endif (HUNSPELL_FOUND)
find_package(PulseAudio)

View file

@ -4,16 +4,12 @@
# HUNSPELL_FOUND - system has Hunspell
# HUNSPELL_INCLUDE_DIR - the Hunspell include directory
# HUNSPELL_LIBRARIES - Link these to use Hunspell
# HUNSPELL_HAS_STRING_API - Hunspell has vector<string> api (>=1.5.1)
#
# Redistribution and use of this file is allowed according to the terms of the
# MIT license. For details see the file COPYING-CMAKE-MODULES.
if ( HUNSPELL_INCLUDE_DIR AND HUNSPELL_LIBRARIES )
# in cache already
SET(Hunspell_FIND_QUIETLY TRUE)
endif ( HUNSPELL_INCLUDE_DIR AND HUNSPELL_LIBRARIES )
# use pkg-config to get the directories and then use these values
# in the FIND_PATH() and FIND_LIBRARY() calls
if( NOT WIN32 )
@ -45,9 +41,6 @@ FIND_LIBRARY(HUNSPELL_LIBRARIES NAMES hunspell-1.7 hunspell-1.6 hunspell-1.5 hun
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Hunspell DEFAULT_MSG HUNSPELL_LIBRARIES HUNSPELL_INCLUDE_DIR )
# show the HUNSPELL_INCLUDE_DIR and HUNSPELL_LIBRARIES variables only in the advanced view
MARK_AS_ADVANCED(HUNSPELL_INCLUDE_DIR HUNSPELL_LIBRARIES )
add_library(hunspell UNKNOWN IMPORTED)
set_target_properties(hunspell PROPERTIES IMPORTED_LOCATION ${HUNSPELL_LIBRARIES} INTERFACE_INCLUDE_DIRECTORIES ${HUNSPELL_INCLUDE_DIR})
if (NOT BUILD_SHARED_LIBS)
@ -55,3 +48,19 @@ if (NOT BUILD_SHARED_LIBS)
# For other versions, it should not hurt
set_target_properties(hunspell PROPERTIES INTERFACE_COMPILE_DEFINITIONS HUNSPELL_STATIC)
endif ()
if (HUNSPELL_FOUND)
try_compile(HUNSPELL_HAS_STRING_API "${CMAKE_BINARY_DIR}/hunspell_string_api"
"${CMAKE_CURRENT_LIST_DIR}/hunspell_string_api.cpp"
LINK_LIBRARIES ${HUNSPELL_LIBRARIES}
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${HUNSPELL_INCLUDE_DIR}" "-DLINK_LIBRARIES=${HUNSPELL_LIBRARIES}"
OUTPUT_VARIABLE debuggggg)
if (HUNSPELL_HAS_STRING_API)
message(STATUS "Hunspell has string API")
else(HUNSPELL_HAS_STRING_API)
message(STATUS "Hunspell does not have string API")
endif(HUNSPELL_HAS_STRING_API)
endif(HUNSPELL_FOUND)
# show the HUNSPELL_INCLUDE_DIR and HUNSPELL_LIBRARIES variables only in the advanced view
MARK_AS_ADVANCED(HUNSPELL_INCLUDE_DIR HUNSPELL_LIBRARIES HUNSPELL_HAS_STRING_API)

View file

@ -0,0 +1,10 @@
#include <hunspell/hunspell.hxx>
#include <string>
int main() {
Hunspell hunspell(NULL, NULL);
std::string word = "";
hunspell.suggest(word);
hunspell.spell(word);
return 0;
}

View file

@ -55,14 +55,18 @@ bool HunspellSpellChecker::CanAddWord(std::string const& word) {
}
bool HunspellSpellChecker::CanRemoveWord(std::string const& word) {
return !!customWords.count(word);
return customWords.count(word) != 0;
}
void HunspellSpellChecker::AddWord(std::string const& word) {
if (!hunspell) return;
// Add it to the in-memory dictionary
#ifdef HUNSPELL_HAS_STRING_API
hunspell->add(conv->Convert(word));
#else
hunspell->add(conv->Convert(word).c_str());
#endif
// Add the word
if (customWords.insert(word).second)
@ -73,7 +77,11 @@ void HunspellSpellChecker::RemoveWord(std::string const& word) {
if (!hunspell) return;
// Remove it from the in-memory dictionary
#ifdef HUNSPELL_HAS_STRING_API
hunspell->remove(conv->Convert(word));
#else
hunspell->remove(conv->Convert(word).c_str());
#endif
auto word_iter = customWords.find(word);
if (word_iter != customWords.end()) {
@ -120,7 +128,11 @@ void HunspellSpellChecker::WriteUserDictionary() {
bool HunspellSpellChecker::CheckWord(std::string const& word) {
if (!hunspell) return true;
try {
return hunspell->spell(conv->Convert(word).c_str()) == 1;
#ifdef HUNSPELL_HAS_STRING_API
return hunspell->spell(conv->Convert(word));
#else
return hunspell->spell(conv->Convert(word).c_str()) != 0;
#endif
}
catch (agi::charset::ConvError const&) {
return false;
@ -131,6 +143,12 @@ std::vector<std::string> HunspellSpellChecker::GetSuggestions(std::string const&
std::vector<std::string> suggestions;
if (!hunspell) return suggestions;
#ifdef HUNSPELL_HAS_STRING_API
std::vector<std::string> suggestions_dic_encoding = hunspell->suggest(conv->Convert(word));
suggestions.reserve(suggestions_dic_encoding.size());
for (std::string& result : suggestions_dic_encoding)
suggestions.push_back(rconv->Convert(result));
#else
char **results;
int n = hunspell->suggest(&results, conv->Convert(word).c_str());
@ -147,6 +165,7 @@ std::vector<std::string> HunspellSpellChecker::GetSuggestions(std::string const&
}
free(results);
#endif
return suggestions;
}
@ -212,7 +231,11 @@ void HunspellSpellChecker::OnLanguageChanged() {
for (auto const& word : customWords) {
try {
#ifdef HUNSPELL_HAS_STRING_API
hunspell->add(conv->Convert(word));
#else
hunspell->add(conv->Convert(word).c_str());
#endif
}
catch (agi::charset::ConvError const&) {
// Normally this shouldn't happen, but some versions of Aegisub