mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
We've implemented UTF-8/UTF-16 string encoding conversions in multiple places, from FML to //flutter/shell/platform/common, to the individual embedders. This migrates these conversions to FML and adds tests. Windows APIs use wchar_t-based strings and as a result, we continue to keep Windows-specific functions in fml/platform/win/wstring_conversion.h. We break out string_conversions into its own source set since FML brings with it some Dart dependencies (e.g. dart_timestamp_provider.cc) that are unused by some targets such as uwptool.exe in the Windows UWP embedding. Issue: https://github.com/flutter/flutter/issues/98061
36 lines
1001 B
C++
36 lines
1001 B
C++
// Copyright 2013 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "flutter/fml/string_conversion.h"
|
|
|
|
#include <codecvt>
|
|
#include <locale>
|
|
#include <string>
|
|
|
|
#include "flutter/fml/build_config.h"
|
|
|
|
#if defined(FML_OS_WIN)
|
|
// TODO(naifu): https://github.com/flutter/flutter/issues/98074
|
|
// Eliminate this workaround for a link error on Windows when the underlying
|
|
// bug is fixed.
|
|
std::locale::id std::codecvt<char16_t, char, _Mbstatet>::id;
|
|
#endif // defined(FML_OS_WIN)
|
|
|
|
namespace fml {
|
|
|
|
using Utf16StringConverter =
|
|
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>;
|
|
|
|
std::string Utf16ToUtf8(const std::u16string_view string) {
|
|
Utf16StringConverter converter;
|
|
return converter.to_bytes(string.data());
|
|
}
|
|
|
|
std::u16string Utf8ToUtf16(const std::string_view string) {
|
|
Utf16StringConverter converter;
|
|
return converter.from_bytes(string.data());
|
|
}
|
|
|
|
} // namespace fml
|