#include "ui/base/ime/utf_offset.h"
#include <string>
#include <string_view>
#include "base/strings/utf_string_conversions.h"
namespace ui {
std::optional<size_t> Utf16OffsetFromUtf8Offset(std::string_view text,
size_t utf8_offset) {
if (utf8_offset > text.length())
return std::nullopt;
std::u16string converted;
if (!base::UTF8ToUTF16(text.data(), utf8_offset, &converted))
return std::nullopt;
return converted.length();
}
std::optional<size_t> Utf8OffsetFromUtf16Offset(std::u16string_view text,
size_t utf16_offset) {
if (utf16_offset > text.length())
return std::nullopt;
std::string converted;
if (!base::UTF16ToUTF8(text.data(), utf16_offset, &converted))
return std::nullopt;
return converted.length();
}
}