| 78 | static std::vector<llama_token> g_cached_tokens; |
| 79 | |
| 80 | static jstring new_jstring_utf8(JNIEnv *env, const char *text) { |
| 81 | if (!text) { |
| 82 | return env->NewStringUTF(""); |
| 83 | } |
| 84 | |
| 85 | try { |
| 86 | std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter; |
| 87 | std::u16string u16 = converter.from_bytes(text); |
| 88 | return env->NewString(reinterpret_cast<const jchar *>(u16.data()), |
| 89 | static_cast<jsize>(u16.size())); |
| 90 | } catch (const std::range_error &) { |
| 91 | std::string sanitized; |
| 92 | sanitized.reserve(strlen(text)); |
| 93 | for (const unsigned char ch: std::string(text)) { |
| 94 | sanitized.push_back(ch < 0x80 ? static_cast<char>(ch) : '?'); |
| 95 | } |
| 96 | return env->NewStringUTF(sanitized.c_str()); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | extern "C" |
| 101 | JNIEXPORT void JNICALL |
no test coverage detected