Files
OpenRCT2-Web/src/openrct2/core/String.cpp
T

859 lines
23 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2020-03-05 19:35:23 +01:00
#if defined(__MINGW32__) && !defined(WINVER) && !defined(_WIN32_WINNT)
2018-05-15 22:45:29 +02:00
// 0x0600 == vista
2018-07-21 16:17:06 +02:00
# define WINVER 0x0600
# define _WIN32_WINNT 0x0600
2018-05-15 22:45:29 +02:00
#endif // __MINGW32__
2018-08-12 12:50:40 +01:00
#include <algorithm>
2021-09-05 12:44:58 +02:00
#include <cctype>
2016-06-29 18:47:15 +01:00
#include <cwctype>
2021-09-05 12:44:58 +02:00
#include <iomanip>
#include <sstream>
2017-02-25 16:07:08 +00:00
#include <stdexcept>
2016-11-12 02:19:54 +00:00
#include <vector>
#ifndef _WIN32
2018-07-21 16:17:06 +02:00
# include <unicode/ucnv.h>
# include <unicode/unistr.h>
# include <unicode/utypes.h>
#endif
2016-06-29 18:47:15 +01:00
#ifdef _WIN32
2018-07-21 16:17:06 +02:00
# include <windows.h>
#endif
#include "../common.h"
#include "../localisation/ConversionTables.h"
#include "../localisation/FormatCodes.h"
2018-03-19 21:03:10 +01:00
#include "../localisation/Language.h"
2017-12-13 13:02:24 +01:00
#include "../util/Util.h"
#include "Memory.hpp"
#include "String.hpp"
#include "StringBuilder.h"
namespace String
{
2018-06-22 22:58:39 +02:00
std::string ToStd(const utf8* str)
{
2018-06-22 22:58:39 +02:00
if (str == nullptr)
return std::string();
else
return std::string(str);
}
2018-06-22 22:58:39 +02:00
std::string StdFormat_VA(const utf8* format, va_list args)
2018-04-24 23:08:58 +01:00
{
auto buffer = Format_VA(format, args);
auto returnValue = ToStd(buffer);
Memory::Free(buffer);
return returnValue;
}
2018-06-22 22:58:39 +02:00
std::string StdFormat(const utf8* format, ...)
2016-12-05 03:30:25 +00:00
{
va_list args;
va_start(args, format);
2018-06-22 22:58:39 +02:00
const utf8* buffer = Format_VA(format, args);
2016-12-05 03:30:25 +00:00
va_end(args);
2016-12-19 23:44:17 +01:00
std::string returnValue = ToStd(buffer);
Memory::Free(buffer);
return returnValue;
2016-12-05 03:30:25 +00:00
}
2021-01-07 01:22:22 +09:00
std::string ToUtf8(std::wstring_view src)
2017-04-01 13:38:52 +01:00
{
2018-05-14 22:45:38 +01:00
#ifdef _WIN32
2020-07-14 12:48:14 -03:00
int srcLen = static_cast<int>(src.size());
2018-05-14 22:45:38 +01:00
int sizeReq = WideCharToMultiByte(CODE_PAGE::CP_UTF8, 0, src.data(), srcLen, nullptr, 0, nullptr, nullptr);
auto result = std::string(sizeReq, 0);
WideCharToMultiByte(CODE_PAGE::CP_UTF8, 0, src.data(), srcLen, result.data(), sizeReq, nullptr, nullptr);
return result;
#else
// Which constructor to use depends on the size of wchar_t...
// UTF-32 is the default on most POSIX systems; Windows uses UTF-16.
// Unfortunately, we'll have to help the compiler here.
2018-07-21 16:17:06 +02:00
# if U_SIZEOF_WCHAR_T == 4
2020-04-22 17:09:29 +02:00
icu::UnicodeString str = icu::UnicodeString::fromUTF32(reinterpret_cast<const UChar32*>(src.data()), src.length());
2018-07-21 16:17:06 +02:00
# elif U_SIZEOF_WCHAR_T == 2
std::wstring wstr = std::wstring(src);
icu::UnicodeString str = icu::UnicodeString(static_cast<const wchar_t*>(wstr.c_str()));
2018-07-21 16:17:06 +02:00
# else
# error Unsupported U_SIZEOF_WCHAR_T size
# endif
2017-04-01 13:38:52 +01:00
std::string result;
str.toUTF8String(result);
2017-04-01 13:38:52 +01:00
return result;
2018-05-14 22:45:38 +01:00
#endif
2017-04-01 13:38:52 +01:00
}
2021-01-07 01:22:22 +09:00
std::wstring ToWideChar(std::string_view src)
2017-04-01 13:38:52 +01:00
{
2018-05-14 22:45:38 +01:00
#ifdef _WIN32
2020-07-14 12:48:14 -03:00
int srcLen = static_cast<int>(src.size());
2018-05-15 20:34:18 +01:00
int sizeReq = MultiByteToWideChar(CODE_PAGE::CP_UTF8, 0, src.data(), srcLen, nullptr, 0);
2018-05-14 22:45:38 +01:00
auto result = std::wstring(sizeReq, 0);
2018-05-15 20:34:18 +01:00
MultiByteToWideChar(CODE_PAGE::CP_UTF8, 0, src.data(), srcLen, result.data(), sizeReq);
2018-05-14 22:45:38 +01:00
return result;
#else
icu::UnicodeString str = icu::UnicodeString::fromUTF8(std::string(src));
// Which constructor to use depends on the size of wchar_t...
// UTF-32 is the default on most POSIX systems; Windows uses UTF-16.
// Unfortunately, we'll have to help the compiler here.
2018-07-21 16:17:06 +02:00
# if U_SIZEOF_WCHAR_T == 4
2020-04-22 17:09:29 +02:00
size_t length = static_cast<size_t>(str.length());
std::wstring result(length, '\0');
UErrorCode status = U_ZERO_ERROR;
2020-04-22 17:09:29 +02:00
str.toUTF32(reinterpret_cast<UChar32*>(&result[0]), str.length(), status);
2018-07-21 16:17:06 +02:00
# elif U_SIZEOF_WCHAR_T == 2
const char16_t* buffer = str.getBuffer();
std::wstring result = static_cast<wchar_t*>(buffer);
2018-07-21 16:17:06 +02:00
# else
# error Unsupported U_SIZEOF_WCHAR_T size
# endif
2017-04-01 13:38:52 +01:00
return result;
2018-05-14 22:45:38 +01:00
#endif
2017-04-01 13:38:52 +01:00
}
2020-11-29 00:40:17 +00:00
std::string_view ToStringView(const char* ch, size_t maxLen)
{
size_t len{};
for (size_t i = 0; i < maxLen; i++)
{
if (ch[i] == '\0')
{
break;
}
else
{
len++;
}
}
return std::string_view(ch, len);
}
2018-06-22 22:58:39 +02:00
bool IsNullOrEmpty(const utf8* str)
2016-01-29 23:18:28 +00:00
{
return str == nullptr || str[0] == '\0';
}
2018-06-22 22:58:39 +02:00
int32_t Compare(const std::string& a, const std::string& b, bool ignoreCase)
2017-01-05 13:13:22 +00:00
{
return Compare(a.c_str(), b.c_str(), ignoreCase);
}
2018-06-22 22:58:39 +02:00
int32_t Compare(const utf8* a, const utf8* b, bool ignoreCase)
2017-01-05 13:13:22 +00:00
{
2018-06-22 22:58:39 +02:00
if (a == b)
return 0;
if (a == nullptr)
a = "";
if (b == nullptr)
b = "";
2017-01-05 13:13:22 +00:00
if (ignoreCase)
{
return _stricmp(a, b);
}
else
{
return strcmp(a, b);
}
}
2021-01-07 01:22:22 +09:00
bool Equals(std::string_view a, std::string_view b, bool ignoreCase)
2020-10-09 00:50:08 +01:00
{
if (ignoreCase)
{
if (a.size() == b.size())
{
for (size_t i = 0; i < a.size(); i++)
{
if (tolower(a[i]) != tolower(b[i]))
{
return false;
}
}
return true;
}
else
{
return false;
}
}
else
{
return a == b;
}
}
2018-06-22 22:58:39 +02:00
bool Equals(const std::string& a, const std::string& b, bool ignoreCase)
{
2020-07-15 00:40:29 +01:00
if (a.size() != b.size())
return false;
if (ignoreCase)
{
for (size_t i = 0; i < a.size(); i++)
{
auto ai = a[i];
auto bi = b[i];
// Only do case insensitive comparison on ASCII characters
if ((ai & 0x80) != 0 || (bi & 0x80) != 0)
{
if (a[i] != b[i])
{
return false;
}
}
else if (tolower(ai) != tolower(bi))
{
return false;
}
}
}
else
{
for (size_t i = 0; i < a.size(); i++)
{
if (a[i] != b[i])
{
return false;
}
}
}
return true;
}
2018-06-22 22:58:39 +02:00
bool Equals(const utf8* a, const utf8* b, bool ignoreCase)
{
2018-06-22 22:58:39 +02:00
if (a == b)
return true;
if (a == nullptr || b == nullptr)
return false;
if (ignoreCase)
{
2016-01-10 21:39:49 +00:00
return _stricmp(a, b) == 0;
}
else
{
return strcmp(a, b) == 0;
}
}
2021-01-17 16:11:52 +00:00
bool StartsWith(std::string_view str, std::string_view match, bool ignoreCase)
{
2021-01-17 16:11:52 +00:00
if (str.size() >= match.size())
{
2021-01-17 16:11:52 +00:00
auto view = str.substr(0, match.size());
return Equals(view, match, ignoreCase);
}
2021-01-17 16:11:52 +00:00
return false;
}
2021-01-07 01:22:22 +09:00
bool EndsWith(std::string_view str, std::string_view match, bool ignoreCase)
2018-05-07 21:32:24 +01:00
{
if (str.size() >= match.size())
{
auto view = str.substr(str.size() - match.size());
2021-01-07 01:22:22 +09:00
return Equals(view, match, ignoreCase);
2018-05-07 21:32:24 +01:00
}
return false;
}
2018-06-22 22:58:39 +02:00
size_t IndexOf(const utf8* str, utf8 match, size_t startIndex)
2016-11-12 02:19:54 +00:00
{
2018-06-22 22:58:39 +02:00
const utf8* ch = str + startIndex;
2016-11-12 02:19:54 +00:00
for (; *ch != '\0'; ch++)
{
if (*ch == match)
{
2020-04-22 17:09:29 +02:00
return static_cast<size_t>(ch - str);
2016-11-12 02:19:54 +00:00
}
}
return SIZE_MAX;
}
2018-06-22 22:58:39 +02:00
ptrdiff_t LastIndexOf(const utf8* str, utf8 match)
2016-01-29 23:18:28 +00:00
{
2018-06-22 22:58:39 +02:00
const utf8* lastOccurance = nullptr;
const utf8* ch = str;
2016-01-31 12:11:09 +01:00
for (; *ch != '\0'; ch++)
2016-01-29 23:18:28 +00:00
{
if (*ch == match)
{
lastOccurance = ch;
}
}
if (lastOccurance == nullptr)
{
return -1;
2016-01-29 23:18:28 +00:00
}
else
{
return lastOccurance - str;
2016-01-29 23:18:28 +00:00
}
}
2018-06-22 22:58:39 +02:00
size_t LengthOf(const utf8* str)
{
return utf8_length(str);
}
2018-06-22 22:58:39 +02:00
size_t SizeOf(const utf8* str)
{
return strlen(str);
}
2018-06-22 22:58:39 +02:00
utf8* Set(utf8* buffer, size_t bufferSize, const utf8* src)
{
2016-01-18 20:49:52 +01:00
return safe_strcpy(buffer, src, bufferSize);
}
2018-06-22 22:58:39 +02:00
utf8* Set(utf8* buffer, size_t bufferSize, const utf8* src, size_t srcSize)
{
2018-06-22 22:58:39 +02:00
utf8* dst = buffer;
2018-06-20 17:11:35 +02:00
size_t minSize = std::min(bufferSize - 1, srcSize);
for (size_t i = 0; i < minSize; i++)
{
*dst++ = *src;
2018-06-22 22:58:39 +02:00
if (*src == '\0')
break;
src++;
}
*dst = '\0';
return buffer;
}
2018-06-22 22:58:39 +02:00
utf8* Append(utf8* buffer, size_t bufferSize, const utf8* src)
{
return safe_strcat(buffer, src, bufferSize);
}
2018-06-22 22:58:39 +02:00
utf8* Format(utf8* buffer, size_t bufferSize, const utf8* format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(buffer, bufferSize, format, args);
va_end(args);
// Terminate buffer in case formatted string overflowed
buffer[bufferSize - 1] = '\0';
return buffer;
}
2018-06-22 22:58:39 +02:00
utf8* Format(const utf8* format, ...)
2016-12-05 03:30:25 +00:00
{
va_list args;
va_start(args, format);
2018-06-22 22:58:39 +02:00
utf8* result = Format_VA(format, args);
2016-12-05 03:30:25 +00:00
va_end(args);
return result;
}
2018-06-22 22:58:39 +02:00
utf8* Format_VA(const utf8* format, va_list args)
2016-12-05 03:30:25 +00:00
{
va_list args1, args2;
va_copy(args1, args);
va_copy(args2, args);
// Try to format to a initial buffer, enlarge if not big enough
size_t bufferSize = 4096;
2018-06-22 22:58:39 +02:00
utf8* buffer = Memory::Allocate<utf8>(bufferSize);
2016-12-05 03:30:25 +00:00
// Start with initial buffer
int32_t len = vsnprintf(buffer, bufferSize, format, args);
2016-12-05 03:30:25 +00:00
if (len < 0)
{
Memory::Free(buffer);
va_end(args1);
va_end(args2);
2017-04-29 23:39:24 -05:00
// An error occurred...
2016-12-05 03:30:25 +00:00
return nullptr;
}
2020-04-22 17:09:29 +02:00
size_t requiredSize = static_cast<size_t>(len) + 1;
2016-12-05 03:30:25 +00:00
if (requiredSize > bufferSize)
{
// Try again with bigger buffer
buffer = Memory::Reallocate<utf8>(buffer, bufferSize);
2017-01-12 13:00:04 +00:00
len = vsnprintf(buffer, bufferSize, format, args);
2016-12-05 03:30:25 +00:00
if (len < 0)
{
Memory::Free(buffer);
va_end(args1);
va_end(args2);
2017-04-29 23:39:24 -05:00
// An error occurred...
2016-12-05 03:30:25 +00:00
return nullptr;
}
}
else
{
// Reduce buffer size to only what was required
bufferSize = requiredSize;
buffer = Memory::Reallocate<utf8>(buffer, bufferSize);
}
// Ensure buffer is terminated
buffer[bufferSize - 1] = '\0';
va_end(args1);
va_end(args2);
return buffer;
}
2018-06-22 22:58:39 +02:00
utf8* AppendFormat(utf8* buffer, size_t bufferSize, const utf8* format, ...)
{
2018-06-22 22:58:39 +02:00
utf8* dst = buffer;
size_t i;
for (i = 0; i < bufferSize; i++)
{
2018-06-22 22:58:39 +02:00
if (*dst == '\0')
break;
dst++;
}
if (i < bufferSize - 1)
{
2016-11-30 23:32:47 +01:00
va_list args;
va_start(args, format);
vsnprintf(dst, bufferSize - i - 1, format, args);
va_end(args);
// Terminate buffer in case formatted string overflowed
buffer[bufferSize - 1] = '\0';
}
return buffer;
}
2018-06-22 22:58:39 +02:00
utf8* Duplicate(const std::string& src)
2017-01-05 13:13:22 +00:00
{
return String::Duplicate(src.c_str());
}
2018-06-22 22:58:39 +02:00
utf8* Duplicate(const utf8* src)
{
2018-06-22 22:58:39 +02:00
utf8* result = nullptr;
2016-10-14 00:01:34 +01:00
if (src != nullptr)
{
size_t srcSize = SizeOf(src) + 1;
result = Memory::Allocate<utf8>(srcSize);
std::memcpy(result, src, srcSize);
2016-10-14 00:01:34 +01:00
}
return result;
}
2018-06-22 22:58:39 +02:00
utf8* DiscardUse(utf8** ptr, utf8* replacement)
{
Memory::Free(*ptr);
*ptr = replacement;
return replacement;
}
2018-06-22 22:58:39 +02:00
utf8* DiscardDuplicate(utf8** ptr, const utf8* replacement)
{
return DiscardUse(ptr, String::Duplicate(replacement));
}
2016-01-23 18:58:31 +00:00
2021-01-07 01:22:22 +09:00
std::vector<std::string> Split(std::string_view s, std::string_view delimiter)
2016-11-12 02:19:54 +00:00
{
2017-02-25 16:07:08 +00:00
if (delimiter.empty())
2016-11-12 02:19:54 +00:00
{
2017-02-25 16:07:08 +00:00
throw std::invalid_argument(nameof(delimiter) " can not be empty.");
}
std::vector<std::string> results;
if (!s.empty())
{
size_t index = 0;
size_t nextIndex;
do
2016-11-12 02:19:54 +00:00
{
2017-02-25 16:07:08 +00:00
nextIndex = s.find(delimiter, index);
if (nextIndex == std::string::npos)
{
2021-01-07 01:22:22 +09:00
results.emplace_back(s.substr(index));
2017-02-25 16:07:08 +00:00
}
else
{
2021-01-07 01:22:22 +09:00
results.emplace_back(s.substr(index, nextIndex - index));
2017-02-25 16:07:08 +00:00
}
index = nextIndex + delimiter.size();
2018-06-22 22:58:39 +02:00
} while (nextIndex != SIZE_MAX);
2016-11-12 02:19:54 +00:00
}
return results;
2016-11-12 02:19:54 +00:00
}
2018-06-22 22:58:39 +02:00
utf8* SkipBOM(utf8* buffer)
2016-01-23 18:58:31 +00:00
{
return const_cast<utf8*>(SkipBOM(static_cast<const utf8*>(buffer)));
2016-01-23 18:58:31 +00:00
}
2018-06-22 22:58:39 +02:00
const utf8* SkipBOM(const utf8* buffer)
2016-01-23 18:58:31 +00:00
{
2020-04-22 17:09:29 +02:00
if (static_cast<uint8_t>(buffer[0]) == 0xEF && static_cast<uint8_t>(buffer[1]) == 0xBB
&& static_cast<uint8_t>(buffer[2]) == 0xBF)
2016-01-23 18:58:31 +00:00
{
return buffer + 3;
}
return buffer;
}
size_t GetCodepointLength(codepoint_t codepoint)
{
return utf8_get_codepoint_length(codepoint);
}
2018-06-22 22:58:39 +02:00
codepoint_t GetNextCodepoint(utf8* ptr, utf8** nextPtr)
2016-01-23 18:58:31 +00:00
{
return GetNextCodepoint(static_cast<const utf8*>(ptr), const_cast<const utf8**>(nextPtr));
2016-01-23 18:58:31 +00:00
}
2018-06-22 22:58:39 +02:00
codepoint_t GetNextCodepoint(const utf8* ptr, const utf8** nextPtr)
2016-01-23 18:58:31 +00:00
{
return utf8_get_next(ptr, nextPtr);
}
2018-06-22 22:58:39 +02:00
utf8* WriteCodepoint(utf8* dst, codepoint_t codepoint)
2016-01-23 18:58:31 +00:00
{
return utf8_write_codepoint(dst, codepoint);
}
2016-06-29 18:47:15 +01:00
2020-11-28 16:52:19 +00:00
void AppendCodepoint(std::string& str, codepoint_t codepoint)
{
char buffer[8]{};
utf8_write_codepoint(buffer, codepoint);
str.append(buffer);
}
2017-12-23 12:18:15 +01:00
bool IsWhiteSpace(codepoint_t codepoint)
{
// 0x3000 is the 'ideographic space', a 'fullwidth' character used in CJK languages.
2020-04-22 17:09:29 +02:00
return iswspace(static_cast<wchar_t>(codepoint)) || codepoint == 0x3000;
2017-12-23 12:18:15 +01:00
}
2018-06-22 22:58:39 +02:00
utf8* Trim(utf8* str)
2016-06-29 18:47:15 +01:00
{
2018-06-22 22:58:39 +02:00
utf8* firstNonWhitespace = nullptr;
2016-06-29 18:47:15 +01:00
codepoint_t codepoint;
2018-06-22 22:58:39 +02:00
utf8* ch = str;
utf8* nextCh;
2016-06-29 18:47:15 +01:00
while ((codepoint = GetNextCodepoint(ch, &nextCh)) != '\0')
{
2017-12-23 12:18:15 +01:00
if (codepoint <= WCHAR_MAX && !IsWhiteSpace(codepoint))
2016-06-29 18:47:15 +01:00
{
if (firstNonWhitespace == nullptr)
{
firstNonWhitespace = ch;
}
}
ch = nextCh;
}
2018-06-22 22:58:39 +02:00
if (firstNonWhitespace != nullptr && firstNonWhitespace != str)
2016-06-29 18:47:15 +01:00
{
// Take multibyte characters into account: use the last byte of the
// current character.
size_t newStringSize = (nextCh - 1) - firstNonWhitespace;
2017-02-07 21:01:07 +01:00
#ifdef DEBUG
2016-06-29 18:47:15 +01:00
size_t currentStringSize = String::SizeOf(str);
2016-10-11 20:11:22 +01:00
Guard::Assert(newStringSize < currentStringSize, GUARD_LINE);
2016-06-29 18:47:15 +01:00
#endif
2018-01-31 13:08:42 +00:00
std::memmove(str, firstNonWhitespace, newStringSize);
2016-06-29 18:47:15 +01:00
str[newStringSize] = '\0';
}
else
{
*ch = '\0';
}
return str;
}
2016-10-11 20:11:22 +01:00
2018-06-22 22:58:39 +02:00
const utf8* TrimStart(const utf8* str)
2016-10-11 20:11:22 +01:00
{
codepoint_t codepoint;
2018-06-22 22:58:39 +02:00
const utf8* ch = str;
const utf8* nextCh;
2016-10-11 20:11:22 +01:00
while ((codepoint = GetNextCodepoint(ch, &nextCh)) != '\0')
{
2017-12-23 12:18:15 +01:00
if (codepoint <= WCHAR_MAX && !IsWhiteSpace(codepoint))
2016-10-11 20:11:22 +01:00
{
return ch;
}
ch = nextCh;
}
// String is all whitespace
return ch;
2016-10-11 20:11:22 +01:00
}
2018-06-22 22:58:39 +02:00
utf8* TrimStart(utf8* buffer, size_t bufferSize, const utf8* src)
2016-10-11 20:11:22 +01:00
{
return String::Set(buffer, bufferSize, TrimStart(src));
}
2017-02-12 21:11:28 +00:00
2018-06-22 22:58:39 +02:00
std::string TrimStart(const std::string& s)
{
2018-06-22 22:58:39 +02:00
const utf8* trimmed = TrimStart(s.c_str());
return std::string(trimmed);
}
2018-06-22 22:58:39 +02:00
std::string Trim(const std::string& s)
2017-02-12 21:11:28 +00:00
{
codepoint_t codepoint;
2018-06-22 22:58:39 +02:00
const utf8* ch = s.c_str();
const utf8* nextCh;
const utf8* startSubstr = nullptr;
const utf8* endSubstr = nullptr;
2017-02-12 21:11:28 +00:00
while ((codepoint = GetNextCodepoint(ch, &nextCh)) != '\0')
{
2017-12-23 12:18:15 +01:00
bool isWhiteSpace = codepoint <= WCHAR_MAX && IsWhiteSpace(codepoint);
2017-02-19 21:33:14 +00:00
if (!isWhiteSpace)
2017-02-12 21:11:28 +00:00
{
2017-02-24 21:48:06 +00:00
if (startSubstr == nullptr)
2017-02-12 21:11:28 +00:00
{
2017-02-24 21:48:06 +00:00
startSubstr = ch;
2017-02-12 21:11:28 +00:00
}
// Take multibyte characters into account: move pointer towards
// the last byte of the current character.
endSubstr = nextCh - 1;
2017-02-12 21:11:28 +00:00
}
ch = nextCh;
}
2017-02-24 21:48:06 +00:00
if (startSubstr == nullptr)
2017-02-12 21:11:28 +00:00
{
2017-02-24 21:48:06 +00:00
// String is all whitespace
2017-02-19 21:33:14 +00:00
return std::string();
2017-02-12 21:11:28 +00:00
}
2017-02-24 21:48:06 +00:00
size_t stringLength = endSubstr - startSubstr + 1;
return std::string(startSubstr, stringLength);
2017-02-12 21:11:28 +00:00
}
#ifndef _WIN32
static const char* GetIcuCodePage(int32_t codePage)
{
switch (codePage)
{
2018-06-22 22:58:39 +02:00
case CODE_PAGE::CP_932:
return "windows-932";
2018-06-22 22:58:39 +02:00
case CODE_PAGE::CP_936:
return "GB2312";
2018-06-22 22:58:39 +02:00
case CODE_PAGE::CP_949:
return "windows-949";
2018-06-22 22:58:39 +02:00
case CODE_PAGE::CP_950:
return "big5";
2018-06-22 22:58:39 +02:00
case CODE_PAGE::CP_1252:
return "windows-1252";
2018-06-22 22:58:39 +02:00
case CODE_PAGE::CP_UTF8:
return "utf-8";
2018-06-22 22:58:39 +02:00
default:
throw std::runtime_error("Unsupported code page: " + std::to_string(codePage));
}
}
static std::string CodePageFromUnicode(icu::UnicodeString src, int32_t dstCodePage)
{
UConverter* conv;
UErrorCode status = U_ZERO_ERROR;
const char* codepage = GetIcuCodePage(dstCodePage);
conv = ucnv_open(codepage, &status);
if (U_FAILURE(status))
{
log_error("ICU error: %s", u_errorName(status));
return nullptr;
}
// Allocate buffer to convert to.
int8_t char_size = ucnv_getMaxCharSize(conv);
std::string buffer(char_size * src.length(), '\0');
char* buffer_limit = &buffer[0] + (char_size * src.length());
// Ready the source string as well...
const char16_t* source = src.getTerminatedBuffer();
const char16_t* source_limit = source + src.length();
// Convert the lot.
char* buffer_target = &buffer[0];
2020-04-22 17:09:29 +02:00
ucnv_fromUnicode(
conv, &buffer_target, buffer_limit, static_cast<const UChar**>(&source), source_limit, nullptr, true, &status);
if (U_FAILURE(status))
{
log_error("ICU error: %s", u_errorName(status));
return nullptr;
}
ucnv_close(conv);
return buffer;
}
#endif
2021-01-07 01:22:22 +09:00
std::string Convert(std::string_view src, int32_t srcCodePage, int32_t dstCodePage)
{
#ifdef _WIN32
// Convert from source code page to UTF-16
std::wstring u16;
{
2020-07-14 12:48:14 -03:00
int srcLen = static_cast<int>(src.size());
int sizeReq = MultiByteToWideChar(srcCodePage, 0, src.data(), srcLen, nullptr, 0);
u16 = std::wstring(sizeReq, 0);
MultiByteToWideChar(srcCodePage, 0, src.data(), srcLen, u16.data(), sizeReq);
}
// Convert from UTF-16 to destination code page
std::string dst;
{
2020-07-14 12:48:14 -03:00
int srcLen = static_cast<int>(u16.size());
int sizeReq = WideCharToMultiByte(dstCodePage, 0, u16.data(), srcLen, nullptr, 0, nullptr, nullptr);
dst = std::string(sizeReq, 0);
WideCharToMultiByte(dstCodePage, 0, u16.data(), srcLen, dst.data(), sizeReq, nullptr, nullptr);
}
return dst;
#else
const char* codepage = GetIcuCodePage(srcCodePage);
icu::UnicodeString convertString(src.data(), codepage);
std::string result;
if (dstCodePage == CODE_PAGE::CP_UTF8)
{
convertString.toUTF8String(result);
}
else
{
result = CodePageFromUnicode(convertString, dstCodePage);
}
return result;
#endif
}
2021-01-07 01:22:22 +09:00
std::string ToUpper(std::string_view src)
{
#ifdef _WIN32
2019-11-08 23:16:15 +01:00
# if _WIN32_WINNT >= 0x0600
2019-07-22 23:11:12 +01:00
auto srcW = ToWideChar(src);
2018-05-14 23:50:19 +01:00
// Measure how long the destination needs to be
auto requiredSize = LCMapStringEx(
2020-07-14 12:48:14 -03:00
LOCALE_NAME_USER_DEFAULT, LCMAP_UPPERCASE | LCMAP_LINGUISTIC_CASING, srcW.c_str(), static_cast<int>(srcW.length()),
nullptr, 0, nullptr, nullptr, 0);
2018-05-14 23:50:19 +01:00
2018-05-14 22:07:05 +01:00
auto dstW = std::wstring();
2018-05-14 23:50:19 +01:00
dstW.resize(requiredSize);
2018-05-14 22:07:05 +01:00
2018-05-14 23:50:19 +01:00
// Transform the string
auto result = LCMapStringEx(
2020-07-14 12:48:14 -03:00
LOCALE_NAME_USER_DEFAULT, LCMAP_UPPERCASE | LCMAP_LINGUISTIC_CASING, srcW.c_str(), static_cast<int>(srcW.length()),
dstW.data(), static_cast<int>(dstW.length()), nullptr, nullptr, 0);
2018-05-14 23:50:19 +01:00
if (result == 0)
2018-05-14 22:07:05 +01:00
{
2018-05-14 23:50:19 +01:00
// Check the error
auto error = GetLastError();
log_warning("LCMapStringEx failed with %d", error);
return std::string(src);
}
else
{
return String::ToUtf8(dstW);
2018-05-14 22:07:05 +01:00
}
2019-11-08 23:16:15 +01:00
# else
2020-11-16 12:12:28 +01:00
std::string dst = std::string(src);
std::transform(dst.begin(), dst.end(), dst.begin(), [](unsigned char c) { return std::toupper(c); });
return dst;
2019-11-08 23:16:15 +01:00
# endif
#else
icu::UnicodeString str = icu::UnicodeString::fromUTF8(std::string(src));
str.toUpper();
std::string res;
str.toUTF8String(res);
return res;
#endif
}
std::string_view UTF8Truncate(std::string_view v, size_t size)
{
auto trunc = v.substr(0, size);
for (size_t i = 0; i < trunc.size();)
{
auto length = UTF8GetCodePointSize(trunc.substr(i, trunc.size()));
if (!length.has_value())
{
return trunc.substr(0, i);
}
i += *length;
}
return trunc;
}
2021-09-05 12:44:58 +02:00
std::string URLEncode(std::string_view value)
{
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (auto c : value)
{
// Keep alphanumeric and other accepted characters intact
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
{
escaped << c;
}
else
{
// Any other characters are percent-escaped
escaped << std::uppercase;
escaped << '%' << std::setw(2) << int32_t(static_cast<unsigned char>(c));
escaped << std::nouppercase;
}
}
return escaped.str();
}
2018-05-04 22:40:09 +02:00
} // namespace String
2020-10-14 19:40:24 +01:00
char32_t CodepointView::iterator::GetNextCodepoint(const char* ch, const char** next)
{
return utf8_get_next(ch, next);
}