Skip to content

Commit

Permalink
[fml] Use common FML string encoding utils
Browse files Browse the repository at this point in the history
General UTF8/UTF16 string conversion utility functions were recently
added to FML. This migrates calls in the Android JNI utils to the common
implementations.

No additional tests added since we're just dropping one implementation
of UTF8/UTF16 functions and making use of an equivalent set that already
has unit tests of its own. The rest of the Android-related code is
already covered by existing unit tests.

Issue: flutter/flutter#98061
Related PR: flutter#31334
  • Loading branch information
cbracken committed Feb 10, 2022
1 parent 0be895c commit 6696b29
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions fml/platform/android/jni_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

#include <sys/prctl.h>

#include <codecvt>
#include <string>

#include "flutter/fml/logging.h"
#include "flutter/fml/string_conversion.h"
#include "flutter/fml/thread_local.h"

namespace fml {
Expand Down Expand Up @@ -67,12 +67,6 @@ void DetachFromVM() {
}
}

static std::string UTF16StringToUTF8String(const char16_t* chars, size_t len) {
std::u16string u16_string(chars, len);
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}
.to_bytes(u16_string);
}

std::string JavaStringToString(JNIEnv* env, jstring str) {
if (env == nullptr || str == nullptr) {
return "";
Expand All @@ -81,21 +75,17 @@ std::string JavaStringToString(JNIEnv* env, jstring str) {
if (chars == nullptr) {
return "";
}
std::string u8_string = UTF16StringToUTF8String(
reinterpret_cast<const char16_t*>(chars), env->GetStringLength(str));
std::u16string u16_string(reinterpret_cast<const char16_t*>(str),
env->GetStringLength(str));
std::string u8_string = Utf16ToUtf8(u16_string);
env->ReleaseStringChars(str, chars);
ASSERT_NO_EXCEPTION();
return u8_string;
}

static std::u16string UTF8StringToUTF16String(const std::string& string) {
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}
.from_bytes(string);
}

ScopedJavaLocalRef<jstring> StringToJavaString(JNIEnv* env,
const std::string& u8_string) {
std::u16string u16_string = UTF8StringToUTF16String(u8_string);
std::u16string u16_string = Utf8ToUtf16(u8_string);
auto result = ScopedJavaLocalRef<jstring>(
env, env->NewString(reinterpret_cast<const jchar*>(u16_string.data()),
u16_string.length()));
Expand Down

0 comments on commit 6696b29

Please sign in to comment.