#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/strings/utf_string_conversions.h"
#include <limits.h>
#include <stdint.h>
#include <concepts>
#include <ostream>
#include <string_view>
#include <type_traits>
#include "base/strings/string_util.h"
#include "base/strings/utf_ostream_operators.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "base/third_party/icu/icu_utf.h"
#include "build/build_config.h"
namespace base {
namespace {
constexpr base_icu::UChar32 kErrorCodePoint = …;
template <typename SrcChar, typename DestChar>
struct SizeCoefficient { … };
template <>
struct SizeCoefficient<char16_t, char> { … };
#if defined(WCHAR_T_IS_32_BIT)
template <>
struct SizeCoefficient<wchar_t, char> { … };
template <>
struct SizeCoefficient<wchar_t, char16_t> { … };
#endif
size_coefficient_v;
BitsAre;
template <typename Char>
requires(BitsAre<Char, 8>)
void UnicodeAppendUnsafe(Char* out,
size_t* size,
base_icu::UChar32 code_point) { … }
template <typename Char>
requires(BitsAre<Char, 16>)
void UnicodeAppendUnsafe(Char* out,
size_t* size,
base_icu::UChar32 code_point) { … }
template <typename Char>
requires(BitsAre<Char, 32>)
void UnicodeAppendUnsafe(Char* out,
size_t* size,
base_icu::UChar32 code_point) { … }
template <typename DestChar>
bool DoUTFConversion(const char* src,
size_t src_len,
DestChar* dest,
size_t* dest_len) { … }
template <typename DestChar>
bool DoUTFConversion(const char16_t* src,
size_t src_len,
DestChar* dest,
size_t* dest_len) { … }
#if defined(WCHAR_T_IS_32_BIT)
template <typename DestChar>
bool DoUTFConversion(const wchar_t* src,
size_t src_len,
DestChar* dest,
size_t* dest_len) { … }
#endif
template <typename InputString, typename DestString>
bool UTFConversion(const InputString& src_str, DestString* dest_str) { … }
}
bool UTF8ToUTF16(const char* src, size_t src_len, std::u16string* output) { … }
std::u16string UTF8ToUTF16(std::string_view utf8) { … }
bool UTF16ToUTF8(const char16_t* src, size_t src_len, std::string* output) { … }
std::string UTF16ToUTF8(std::u16string_view utf16) { … }
#if defined(WCHAR_T_IS_16_BIT)
bool WideToUTF16(const wchar_t* src, size_t src_len, std::u16string* output) {
output->assign(src, src + src_len);
return true;
}
std::u16string WideToUTF16(std::wstring_view wide) {
return std::u16string(wide.begin(), wide.end());
}
bool UTF16ToWide(const char16_t* src, size_t src_len, std::wstring* output) {
output->assign(src, src + src_len);
return true;
}
std::wstring UTF16ToWide(std::u16string_view utf16) {
return std::wstring(utf16.begin(), utf16.end());
}
#elif defined(WCHAR_T_IS_32_BIT)
bool WideToUTF16(const wchar_t* src, size_t src_len, std::u16string* output) { … }
std::u16string WideToUTF16(std::wstring_view wide) { … }
bool UTF16ToWide(const char16_t* src, size_t src_len, std::wstring* output) { … }
std::wstring UTF16ToWide(std::u16string_view utf16) { … }
#endif
bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { … }
std::wstring UTF8ToWide(std::string_view utf8) { … }
#if defined(WCHAR_T_IS_16_BIT)
bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output) {
return UTF16ToUTF8(as_u16cstr(src), src_len, output);
}
std::string WideToUTF8(std::wstring_view wide) {
return UTF16ToUTF8(std::u16string_view(as_u16cstr(wide), wide.size()));
}
#elif defined(WCHAR_T_IS_32_BIT)
bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output) { … }
std::string WideToUTF8(std::wstring_view wide) { … }
#endif
std::u16string ASCIIToUTF16(std::string_view ascii) { … }
std::string UTF16ToASCII(std::u16string_view utf16) { … }
#if defined(WCHAR_T_IS_16_BIT)
std::wstring ASCIIToWide(std::string_view ascii) {
DCHECK(IsStringASCII(ascii)) << ascii;
return std::wstring(ascii.begin(), ascii.end());
}
std::string WideToASCII(std::wstring_view wide) {
DCHECK(IsStringASCII(wide)) << wide;
return std::string(wide.begin(), wide.end());
}
#endif
}