2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2014-01-14 20:57:17 +01:00
|
|
|
#include <algorithm>
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2017-02-23 21:24:45 +01:00
|
|
|
#include "base/i18n/case_conversion.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "base/logging.h"
|
2014-12-13 21:18:31 +01:00
|
|
|
#include "base/strings/string_util.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/internal/cef_string_types.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void string_wide_dtor(wchar_t* str) {
|
2017-05-17 11:29:28 +02:00
|
|
|
delete[] str;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void string_utf8_dtor(char* str) {
|
2017-05-17 11:29:28 +02:00
|
|
|
delete[] str;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
void string_utf16_dtor(char16_t* str) {
|
2017-05-17 11:29:28 +02:00
|
|
|
delete[] str;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:18:31 +01:00
|
|
|
// Originally from base/strings/utf_string_conversions.cc
|
2024-04-23 22:06:00 +02:00
|
|
|
std::wstring ASCIIToWide(const std::string_view& ascii) {
|
2014-12-13 21:18:31 +01:00
|
|
|
DCHECK(base::IsStringASCII(ascii)) << ascii;
|
|
|
|
return std::wstring(ascii.begin(), ascii.end());
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
} // namespace
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_wide_set(const wchar_t* src,
|
|
|
|
size_t src_len,
|
|
|
|
cef_string_wide_t* output,
|
|
|
|
int copy) {
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_wide_clear(output);
|
|
|
|
|
|
|
|
if (copy) {
|
|
|
|
if (src && src_len > 0) {
|
2017-05-17 11:29:28 +02:00
|
|
|
output->str = new wchar_t[src_len + 1];
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!output->str) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
memcpy(output->str, src, src_len * sizeof(wchar_t));
|
|
|
|
output->str[src_len] = 0;
|
|
|
|
output->length = src_len;
|
|
|
|
output->dtor = string_wide_dtor;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output->str = const_cast<wchar_t*>(src);
|
|
|
|
output->length = src_len;
|
2020-01-15 14:36:24 +01:00
|
|
|
output->dtor = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_utf8_set(const char* src,
|
|
|
|
size_t src_len,
|
|
|
|
cef_string_utf8_t* output,
|
|
|
|
int copy) {
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_utf8_clear(output);
|
|
|
|
if (copy) {
|
|
|
|
if (src && src_len > 0) {
|
2017-05-17 11:29:28 +02:00
|
|
|
output->str = new char[src_len + 1];
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!output->str) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
memcpy(output->str, src, src_len * sizeof(char));
|
|
|
|
output->str[src_len] = 0;
|
|
|
|
output->length = src_len;
|
|
|
|
output->dtor = string_utf8_dtor;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output->str = const_cast<char*>(src);
|
|
|
|
output->length = src_len;
|
2020-01-15 14:36:24 +01:00
|
|
|
output->dtor = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
CEF_EXPORT int cef_string_utf16_set(const char16_t* src,
|
2017-05-17 11:29:28 +02:00
|
|
|
size_t src_len,
|
|
|
|
cef_string_utf16_t* output,
|
|
|
|
int copy) {
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_utf16_clear(output);
|
|
|
|
|
|
|
|
if (copy) {
|
|
|
|
if (src && src_len > 0) {
|
2023-06-01 16:06:15 +02:00
|
|
|
output->str = new char16_t[src_len + 1];
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!output->str) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
memcpy(output->str, src, src_len * sizeof(char16_t));
|
2012-04-03 03:34:16 +02:00
|
|
|
output->str[src_len] = 0;
|
|
|
|
output->length = src_len;
|
|
|
|
output->dtor = string_utf16_dtor;
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-01 16:06:15 +02:00
|
|
|
output->str = const_cast<char16_t*>(src);
|
2012-04-03 03:34:16 +02:00
|
|
|
output->length = src_len;
|
2020-01-15 14:36:24 +01:00
|
|
|
output->dtor = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT void cef_string_wide_clear(cef_string_wide_t* str) {
|
2020-01-15 14:36:24 +01:00
|
|
|
DCHECK(str != nullptr);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (str->dtor && str->str) {
|
2012-04-03 03:34:16 +02:00
|
|
|
str->dtor(str->str);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
str->str = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
str->length = 0;
|
2020-01-15 14:36:24 +01:00
|
|
|
str->dtor = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT void cef_string_utf8_clear(cef_string_utf8_t* str) {
|
2020-01-15 14:36:24 +01:00
|
|
|
DCHECK(str != nullptr);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (str->dtor && str->str) {
|
2012-04-03 03:34:16 +02:00
|
|
|
str->dtor(str->str);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
str->str = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
str->length = 0;
|
2020-01-15 14:36:24 +01:00
|
|
|
str->dtor = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT void cef_string_utf16_clear(cef_string_utf16_t* str) {
|
2020-01-15 14:36:24 +01:00
|
|
|
DCHECK(str != nullptr);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (str->dtor && str->str) {
|
2012-04-03 03:34:16 +02:00
|
|
|
str->dtor(str->str);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
str->str = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
str->length = 0;
|
2020-01-15 14:36:24 +01:00
|
|
|
str->dtor = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_string_wide_cmp(const cef_string_wide_t* str1,
|
|
|
|
const cef_string_wide_t* str2) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (str1->length == 0 && str2->length == 0) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
int r = wcsncmp(str1->str, str2->str, std::min(str1->length, str2->length));
|
|
|
|
if (r == 0) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (str1->length > str2->length) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 1;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (str1->length < str2->length) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return -1;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_string_utf8_cmp(const cef_string_utf8_t* str1,
|
|
|
|
const cef_string_utf8_t* str2) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (str1->length == 0 && str2->length == 0) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
int r = strncmp(str1->str, str2->str, std::min(str1->length, str2->length));
|
|
|
|
if (r == 0) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (str1->length > str2->length) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 1;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (str1->length < str2->length) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return -1;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_string_utf16_cmp(const cef_string_utf16_t* str1,
|
|
|
|
const cef_string_utf16_t* str2) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (str1->length == 0 && str2->length == 0) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-04-21 00:52:34 +02:00
|
|
|
int r = std::char_traits<std::u16string::value_type>::compare(
|
|
|
|
reinterpret_cast<std::u16string::value_type*>(str1->str),
|
|
|
|
reinterpret_cast<std::u16string::value_type*>(str2->str),
|
2021-03-04 23:36:57 +01:00
|
|
|
std::min(str1->length, str2->length));
|
2012-04-03 03:34:16 +02:00
|
|
|
if (r == 0) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (str1->length > str2->length) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 1;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (str1->length < str2->length) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return -1;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_wide_to_utf8(const wchar_t* src,
|
|
|
|
size_t src_len,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_utf8_t* output) {
|
|
|
|
std::string str;
|
2014-01-02 23:41:11 +01:00
|
|
|
bool ret = base::WideToUTF8(src, src_len, &str);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_string_utf8_set(str.c_str(), str.length(), output, true)) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_utf8_to_wide(const char* src,
|
|
|
|
size_t src_len,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_wide_t* output) {
|
|
|
|
std::wstring str;
|
2014-01-02 23:41:11 +01:00
|
|
|
bool ret = base::UTF8ToWide(src, src_len, &str);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_string_wide_set(str.c_str(), str.length(), output, true)) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_wide_to_utf16(const wchar_t* src,
|
|
|
|
size_t src_len,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_utf16_t* output) {
|
2021-04-21 00:52:34 +02:00
|
|
|
std::u16string str;
|
2014-01-02 23:41:11 +01:00
|
|
|
bool ret = base::WideToUTF16(src, src_len, &str);
|
2023-06-01 16:06:15 +02:00
|
|
|
if (!cef_string_utf16_set(reinterpret_cast<const char16_t*>(str.c_str()),
|
2023-01-02 23:59:03 +01:00
|
|
|
str.length(), output, true)) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
CEF_EXPORT int cef_string_utf16_to_wide(const char16_t* src,
|
2017-05-17 11:29:28 +02:00
|
|
|
size_t src_len,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_wide_t* output) {
|
|
|
|
std::wstring str;
|
2021-01-28 00:13:12 +01:00
|
|
|
bool ret = base::UTF16ToWide(
|
2021-04-21 00:52:34 +02:00
|
|
|
reinterpret_cast<const std::u16string::value_type*>(src), src_len, &str);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_string_wide_set(str.c_str(), str.length(), output, true)) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_utf8_to_utf16(const char* src,
|
|
|
|
size_t src_len,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_utf16_t* output) {
|
2021-04-21 00:52:34 +02:00
|
|
|
std::u16string str;
|
2014-01-02 23:41:11 +01:00
|
|
|
bool ret = base::UTF8ToUTF16(src, src_len, &str);
|
2023-06-01 16:06:15 +02:00
|
|
|
if (!cef_string_utf16_set(reinterpret_cast<const char16_t*>(str.c_str()),
|
2023-01-02 23:59:03 +01:00
|
|
|
str.length(), output, true)) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
CEF_EXPORT int cef_string_utf16_to_utf8(const char16_t* src,
|
2017-05-17 11:29:28 +02:00
|
|
|
size_t src_len,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_utf8_t* output) {
|
|
|
|
std::string str;
|
2021-01-28 00:13:12 +01:00
|
|
|
bool ret = base::UTF16ToUTF8(
|
2021-04-21 00:52:34 +02:00
|
|
|
reinterpret_cast<const std::u16string::value_type*>(src), src_len, &str);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_string_utf8_set(str.c_str(), str.length(), output, true)) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_ascii_to_wide(const char* src,
|
|
|
|
size_t src_len,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_wide_t* output) {
|
2014-12-13 21:18:31 +01:00
|
|
|
const std::wstring& str = ASCIIToWide(std::string(src, src_len));
|
2012-04-03 03:34:16 +02:00
|
|
|
return cef_string_wide_set(str.c_str(), str.length(), output, true);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_EXPORT int cef_string_ascii_to_utf16(const char* src,
|
|
|
|
size_t src_len,
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_string_utf16_t* output) {
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& str = base::ASCIIToUTF16(std::string(src, src_len));
|
2023-06-01 16:06:15 +02:00
|
|
|
return cef_string_utf16_set(reinterpret_cast<const char16_t*>(str.c_str()),
|
2021-01-28 00:13:12 +01:00
|
|
|
str.length(), output, true);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT cef_string_userfree_wide_t cef_string_userfree_wide_alloc() {
|
|
|
|
cef_string_wide_t* s = new cef_string_wide_t;
|
|
|
|
memset(s, 0, sizeof(cef_string_wide_t));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT cef_string_userfree_utf8_t cef_string_userfree_utf8_alloc() {
|
|
|
|
cef_string_utf8_t* s = new cef_string_utf8_t;
|
|
|
|
memset(s, 0, sizeof(cef_string_utf8_t));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT cef_string_userfree_utf16_t cef_string_userfree_utf16_alloc() {
|
|
|
|
cef_string_utf16_t* s = new cef_string_utf16_t;
|
|
|
|
memset(s, 0, sizeof(cef_string_utf16_t));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT void cef_string_userfree_wide_free(cef_string_userfree_wide_t str) {
|
|
|
|
cef_string_wide_clear(str);
|
|
|
|
delete str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT void cef_string_userfree_utf8_free(cef_string_userfree_utf8_t str) {
|
|
|
|
cef_string_utf8_clear(str);
|
|
|
|
delete str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT void cef_string_userfree_utf16_free(
|
|
|
|
cef_string_userfree_utf16_t str) {
|
|
|
|
cef_string_utf16_clear(str);
|
|
|
|
delete str;
|
|
|
|
}
|
2017-02-23 21:24:45 +01:00
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
CEF_EXPORT int cef_string_utf16_to_lower(const char16_t* src,
|
2017-05-17 11:29:28 +02:00
|
|
|
size_t src_len,
|
2017-02-23 21:24:45 +01:00
|
|
|
cef_string_utf16_t* output) {
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& str = base::i18n::ToLower(std::u16string(
|
|
|
|
reinterpret_cast<const std::u16string::value_type*>(src), src_len));
|
2023-06-01 16:06:15 +02:00
|
|
|
return cef_string_utf16_set(reinterpret_cast<const char16_t*>(str.c_str()),
|
2021-01-28 00:13:12 +01:00
|
|
|
str.length(), output, true);
|
2017-02-23 21:24:45 +01:00
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
CEF_EXPORT int cef_string_utf16_to_upper(const char16_t* src,
|
2017-05-17 11:29:28 +02:00
|
|
|
size_t src_len,
|
2017-02-23 21:24:45 +01:00
|
|
|
cef_string_utf16_t* output) {
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& str = base::i18n::ToUpper(std::u16string(
|
|
|
|
reinterpret_cast<const std::u16string::value_type*>(src), src_len));
|
2023-06-01 16:06:15 +02:00
|
|
|
return cef_string_utf16_set(reinterpret_cast<const char16_t*>(str.c_str()),
|
2021-01-28 00:13:12 +01:00
|
|
|
str.length(), output, true);
|
2017-02-23 21:24:45 +01:00
|
|
|
}
|