Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JNI string conversions behave like ObjC string conversions #174

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions support-lib/jni/djinni_support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,12 @@ using WcharConverter = std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff
using Utf8Converter = std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::codecvt_mode::little_endian>, char16_t>;

jstring jniStringFromWString(JNIEnv * env, const std::wstring & str) {
std::string u16 = WcharConverter{}.to_bytes(str);
std::string u16{};
try {
u16 = WcharConverter{}.to_bytes(str);
} catch (...) {
// Conversion error - empty string will be returned
}
jstring res = env->NewString(reinterpret_cast<const jchar*>(u16.data()), u16.size()/sizeof(jchar));
DJINNI_ASSERT(res, env);
return res;
Expand All @@ -457,7 +462,12 @@ std::wstring jniWStringFromString(JNIEnv * env, const jstring jstr) {
}

jstring jniStringFromUTF8(JNIEnv * env, const std::string & str) {
std::u16string u16 = Utf8Converter{}.from_bytes(str);
std::u16string u16{};
try {
u16 = Utf8Converter{}.from_bytes(str);
} catch (...) {
// Conversion error - empty string will be returned
}
jstring res = env->NewString(reinterpret_cast<const jchar*>(u16.data()), u16.size());
DJINNI_ASSERT(res, env);
return res;
Expand Down
Loading