chewing / libchewing

libchewing - The intelligent phonetic input method library
https://chewing.im/
GNU Lesser General Public License v2.1
357 stars 89 forks source link

Rust version of chewing_bopomofo_String_static returns empty string for HanyuPinyin layout #512

Closed wengxt closed 2 months ago

wengxt commented 2 months ago

Describe the bug It seems to be working for chewing layout, but not for hanyu

To Reproduce Steps to reproduce the behavior:

#include <chewing.h>
#include <iostream>
#include <string>
#include <cstdarg>
#include <vector>

void logger(void * /*context*/, int /*level*/, const char *fmt, ...) {
    std::va_list argp;
    va_start(argp, fmt);
    char onechar[1];
    int len = std::vsnprintf(onechar, 1, fmt, argp);
    va_end(argp);
    if (len < 1) {
        return;
    }
    std::vector<char> buf;
    buf.resize(len + 1);
    buf.back() = 0;
    va_start(argp, fmt);
    std::vsnprintf(buf.data(), len, fmt, argp);
    va_end(argp);
    std::cerr << buf.data() << std::endl;
}

int main() {
    auto *c = chewing_new();
    chewing_set_logger(c, logger, nullptr);
    chewing_set_maxChiSymbolLen(c, 18);
    chewing_set_ChiEngMode(c, CHINESE_MODE);
    int selkey[10];
    for (size_t i = 0; i < 10; i++) {
        selkey[i] = '0' + (i + 1) % 10;
    }

    chewing_set_selKey(c, selkey, 10);
    chewing_set_candPerPage(c, 10);
    chewing_set_addPhraseDirection(c, 1);
    chewing_set_phraseChoiceRearward(c, 1);
    chewing_set_autoShiftCur(c, 1);
    chewing_set_spaceAsSelection(c, 1);
    chewing_set_escCleanAllBuf(c, 1);
    chewing_set_KBType(c, chewing_KBStr2Num("KB_HANYU_PINYIN"));
    chewing_handle_Default(c, 'z');

    if (chewing_buffer_Check(c)) {
        std::cout << chewing_buffer_String_static(c) << std::endl;
    }
    if (chewing_bopomofo_Check(c)) {
        std::cout << chewing_bopomofo_String_static(c) << std::endl;
    }
    return 0;
}

Expected behavior Prints "z" in the above code.

Platform (please complete the following information):

kanru commented 2 months ago

Thanks for the report!