看这里的标题:https://github.com/hunspell/hunspell/blob/master/src/hunspell/hunspell.h#L80 https://github.com/hunspell/hunspell/blob/master/src/hunspell/hunspell.h#L80
/* suggest(suggestions, word) - search suggestions
* input: pointer to an array of strings pointer and the (bad) word
* array of strings pointer (here *slst) may not be initialized
* output: number of suggestions in string array, and suggestions in
* a newly allocated array of strings (*slts will be NULL when number
* of suggestion equals 0.)
*/
LIBHUNSPELL_DLL_EXPORTED int Hunspell_suggest(Hunhandle* pHunspell,
char*** slst,
const char* word);
The slst
是一个经典的“out”参数。即我们传递一个指向某个值的指针(在本例中是一个char**
即字符串数组),并且该函数将为我们设置此指针,作为返回多个结果的一种方式。 (第一个结果是建议数)
在巴拿马中,您可以通过分配一个具有参数指针类型布局的段来使用“out”参数。在这种情况下char***
是一个指向char**
,所以布局是ADDRESS
。然后,我们将创建的段传递给函数,最后在函数调用后检索/使用该段中的值,该值将填充段内容:
// char***
var suggestionsRef = allocator.allocate(ValueLayout.ADDRESS); // allocate space for an address
var suggestionCount = Hunspell_suggest(hunspellHandle, suggestionsRef, word);
// char** (the value set by the function)
MemoryAddress suggestions = suggestionsRef.get(ValueLayout.ADDRESS, 0);
之后,您可以迭代字符串数组:
for (int i = 0; i < suggestionCount; i++) {
// char* (an element in the array)
MemoryAddress suggestion = suggestions.getAtIndex(ValueLayout.ADDRESS, i);
// read the string
String javaSuggestion = suggestion.getUtf8String(suggestion, 0);
}