2014-05-08 11:10:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-07-10 00:32:19 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2014-05-08 11:10:54 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-02-27 15:16:49 +08:00
|
|
|
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCCommon.h"
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCConsole.h"
|
2014-05-08 11:10:54 +08:00
|
|
|
#include "ConvertUTF.h"
|
2017-10-17 11:03:32 +08:00
|
|
|
#include <limits>
|
2013-02-27 15:16:49 +08:00
|
|
|
|
2021-06-01 11:47:19 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2013-02-27 15:16:49 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace StringUtils
|
|
|
|
{
|
2021-06-01 11:47:19 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
std::string AX_DLL format(const char* format, ...)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-05-15 22:57:33 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
auto ret = vformat(format, args);
|
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-11-08 14:05:03 +08:00
|
|
|
/*--- This a C++ universal sprintf in the future.
|
|
|
|
** @pitfall: The behavior of vsnprintf between VS2013 and VS2015/2017 is different
|
2021-07-07 21:49:57 +08:00
|
|
|
** VS2013 or Unix-Like System will return -1 when buffer not enough, but VS2015/2017 will return the actural needed
|
|
|
|
*length for buffer at this station
|
2017-11-08 14:05:03 +08:00
|
|
|
** The _vsnprintf behavior is compatible API which always return -1 when buffer isn't enough at VS2013/2015/2017
|
2021-07-07 21:49:57 +08:00
|
|
|
** Yes, The vsnprintf is more efficient implemented by MSVC 19.0 or later, AND it's also standard-compliant, see
|
|
|
|
*reference: http://www.cplusplus.com/reference/cstdio/vsnprintf/
|
2017-11-08 14:05:03 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string vformat(const char* format, va_list ap)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
#define AX_VSNPRINTF_BUFFER_LENGTH 512
|
|
|
|
std::string buf(AX_VSNPRINTF_BUFFER_LENGTH, '\0');
|
2017-11-08 14:05:03 +08:00
|
|
|
|
2020-05-15 22:57:33 +08:00
|
|
|
va_list args;
|
|
|
|
va_copy(args, ap);
|
2020-05-06 16:28:34 +08:00
|
|
|
int nret = vsnprintf(&buf.front(), buf.length() + 1, format, args);
|
2017-11-08 14:05:03 +08:00
|
|
|
va_end(args);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nret >= 0)
|
|
|
|
{
|
|
|
|
if ((unsigned int)nret < buf.length())
|
|
|
|
{
|
2020-05-06 16:28:34 +08:00
|
|
|
buf.resize(nret);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if ((unsigned int)nret > buf.length())
|
|
|
|
{ // handle return required length when buffer insufficient
|
2020-05-06 16:28:34 +08:00
|
|
|
buf.resize(nret);
|
2017-11-08 14:05:03 +08:00
|
|
|
|
2020-05-15 22:57:33 +08:00
|
|
|
va_copy(args, ap);
|
2020-05-06 16:28:34 +08:00
|
|
|
nret = vsnprintf(&buf.front(), buf.length() + 1, format, args);
|
2017-11-08 14:05:03 +08:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
// else equals, do nothing.
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // handle return -1 when buffer insufficient
|
2021-07-07 21:49:57 +08:00
|
|
|
/*
|
|
|
|
vs2013/older & glibc <= 2.0.6, they would return -1 when the output was truncated.
|
|
|
|
see: http://man7.org/linux/man-pages/man3/vsnprintf.3.html
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
#if (defined(__linux__) && ((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 1)))) || \
|
|
|
|
(defined(_MSC_VER) && _MSC_VER < 1900)
|
|
|
|
enum : size_t
|
|
|
|
{
|
|
|
|
enlarge_limits = (1 << 20), // limits the buffer cost memory less than 2MB
|
2020-05-06 16:28:34 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2020-05-06 16:28:34 +08:00
|
|
|
buf.resize(buf.length() << 1);
|
2017-11-08 14:05:03 +08:00
|
|
|
|
2020-05-15 22:57:33 +08:00
|
|
|
va_copy(args, ap);
|
2020-05-06 16:28:34 +08:00
|
|
|
nret = vsnprintf(&buf.front(), buf.length() + 1, format, args);
|
2017-11-08 14:05:03 +08:00
|
|
|
va_end(args);
|
|
|
|
|
2020-05-06 16:28:34 +08:00
|
|
|
} while (nret < 0 && buf.size() <= enlarge_limits);
|
|
|
|
if (nret > 0)
|
|
|
|
buf.resize(nret);
|
|
|
|
else
|
|
|
|
buf = "strfmt: an error is encountered!";
|
|
|
|
#else
|
2021-07-07 21:49:57 +08:00
|
|
|
/* other standard implementation
|
|
|
|
see: http://www.cplusplus.com/reference/cstdio/vsnprintf/
|
|
|
|
*/
|
2020-05-06 16:28:34 +08:00
|
|
|
buf = "strfmt: an error is encountered!";
|
|
|
|
#endif
|
2017-11-08 14:05:03 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 16:28:34 +08:00
|
|
|
return buf;
|
2016-06-15 15:01:26 +08:00
|
|
|
}
|
|
|
|
|
2013-02-27 15:16:49 +08:00
|
|
|
/*
|
|
|
|
* @str: the string to search through.
|
|
|
|
* @c: the character to not look for.
|
|
|
|
*
|
|
|
|
* Return value: the index of the last character that is not c.
|
|
|
|
* */
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int getIndexOfLastNotChar16(const std::vector<char16_t>& str, char16_t c)
|
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
int len = static_cast<int>(str.size());
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2013-02-27 15:16:49 +08:00
|
|
|
int i = len - 1;
|
|
|
|
for (; i >= 0; --i)
|
2021-07-07 21:49:57 +08:00
|
|
|
if (str[i] != c)
|
|
|
|
return i;
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2013-02-27 15:16:49 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @str: the string to trim
|
|
|
|
* @index: the index to start trimming from.
|
|
|
|
*
|
|
|
|
* Trims str st str=[0, index) after the operation.
|
|
|
|
*
|
|
|
|
* Return value: the trimmed string.
|
|
|
|
* */
|
2021-12-25 10:04:45 +08:00
|
|
|
static void trimUTF16VectorFromIndex(std::vector<char16_t>& str, int index)
|
|
|
|
{
|
2014-05-08 11:10:54 +08:00
|
|
|
int size = static_cast<int>(str.size());
|
2013-02-27 15:16:49 +08:00
|
|
|
if (index >= size || index < 0)
|
|
|
|
return;
|
2014-05-08 11:10:54 +08:00
|
|
|
|
|
|
|
str.erase(str.begin() + index, str.begin() + size);
|
2013-02-27 15:16:49 +08:00
|
|
|
}
|
2021-07-07 21:49:57 +08:00
|
|
|
|
2017-02-06 16:41:52 +08:00
|
|
|
/*
|
|
|
|
* @str: the string to trim
|
|
|
|
* @index: the index to start trimming from.
|
|
|
|
*
|
|
|
|
* Trims str st str=[0, index) after the operation.
|
|
|
|
*
|
|
|
|
* Return value: the trimmed string.
|
|
|
|
* */
|
2021-12-25 10:04:45 +08:00
|
|
|
static void trimUTF32VectorFromIndex(std::vector<char32_t>& str, int index)
|
|
|
|
{
|
2017-02-06 16:41:52 +08:00
|
|
|
int size = static_cast<int>(str.size());
|
|
|
|
if (index >= size || index < 0)
|
|
|
|
return;
|
2021-07-07 21:49:57 +08:00
|
|
|
|
2017-02-06 16:41:52 +08:00
|
|
|
str.erase(str.begin() + index, str.begin() + size);
|
|
|
|
}
|
2013-02-27 15:16:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @ch is the unicode character whitespace?
|
|
|
|
*
|
|
|
|
* Reference: http://en.wikipedia.org/wiki/Whitespace_character#Unicode
|
|
|
|
*
|
|
|
|
* Return value: weather the character is a whitespace character.
|
|
|
|
* */
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isUnicodeSpace(char32_t ch)
|
|
|
|
{
|
|
|
|
return (ch >= 0x0009 && ch <= 0x000D) || ch == 0x0020 || ch == 0x0085 || ch == 0x00A0 || ch == 0x1680 ||
|
|
|
|
(ch >= 0x2000 && ch <= 0x200A) || ch == 0x2028 || ch == 0x2029 || ch == 0x202F || ch == 0x205F ||
|
|
|
|
ch == 0x3000;
|
2021-07-07 21:49:57 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isCJKUnicode(char32_t ch)
|
|
|
|
{
|
|
|
|
return (ch >= 0x4E00 && ch <= 0x9FBF) // CJK Unified Ideographs
|
|
|
|
|| (ch >= 0x2E80 && ch <= 0x2FDF) // CJK Radicals Supplement & Kangxi Radicals
|
|
|
|
||
|
|
|
|
(ch >= 0x2FF0 && ch <= 0x30FF) // Ideographic Description Characters, CJK Symbols and Punctuation & Japanese
|
|
|
|
|| (ch >= 0x3100 && ch <= 0x31BF) // Korean
|
|
|
|
|| (ch >= 0xAC00 && ch <= 0xD7AF) // Hangul Syllables
|
|
|
|
|| (ch >= 0xF900 && ch <= 0xFAFF) // CJK Compatibility Ideographs
|
|
|
|
|| (ch >= 0xFE30 && ch <= 0xFE4F) // CJK Compatibility Forms
|
|
|
|
|| (ch >= 0x31C0 && ch <= 0x4DFF) // Other extensions
|
|
|
|
|| (ch >= 0x1f004 && ch <= 0x1f682); // Emoji
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isUnicodeNonBreaking(char32_t ch)
|
|
|
|
{
|
|
|
|
return ch == 0x00A0 // Non-Breaking Space
|
|
|
|
|| ch == 0x202F // Narrow Non-Breaking Space
|
|
|
|
|| ch == 0x2007 // Figure Space
|
|
|
|
|| ch == 0x2060; // Word Joiner
|
2021-07-07 21:49:57 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void trimUTF16Vector(std::vector<char16_t>& str)
|
|
|
|
{
|
2014-05-08 11:10:54 +08:00
|
|
|
int len = static_cast<int>(str.size());
|
|
|
|
|
2021-07-07 21:49:57 +08:00
|
|
|
if (len <= 0)
|
2013-02-27 15:16:49 +08:00
|
|
|
return;
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2013-02-27 15:16:49 +08:00
|
|
|
int last_index = len - 1;
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2013-02-27 15:16:49 +08:00
|
|
|
// Only start trimming if the last character is whitespace..
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isUnicodeSpace(str[last_index]))
|
|
|
|
{
|
|
|
|
for (int i = last_index - 1; i >= 0; --i)
|
|
|
|
{
|
2014-05-08 11:10:54 +08:00
|
|
|
if (isUnicodeSpace(str[i]))
|
2013-02-27 15:16:49 +08:00
|
|
|
last_index = i;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2014-05-08 11:10:54 +08:00
|
|
|
|
|
|
|
trimUTF16VectorFromIndex(str, last_index);
|
2013-02-27 15:16:49 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-07 21:49:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void trimUTF32Vector(std::vector<char32_t>& str)
|
|
|
|
{
|
2017-02-06 16:41:52 +08:00
|
|
|
int len = static_cast<int>(str.size());
|
2021-07-07 21:49:57 +08:00
|
|
|
|
|
|
|
if (len <= 0)
|
2017-02-06 16:41:52 +08:00
|
|
|
return;
|
2021-07-07 21:49:57 +08:00
|
|
|
|
2017-02-06 16:41:52 +08:00
|
|
|
int last_index = len - 1;
|
2021-07-07 21:49:57 +08:00
|
|
|
|
2017-02-06 16:41:52 +08:00
|
|
|
// Only start trimming if the last character is whitespace..
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isUnicodeSpace(str[last_index]))
|
|
|
|
{
|
|
|
|
for (int i = last_index - 1; i >= 0; --i)
|
|
|
|
{
|
2017-02-06 16:41:52 +08:00
|
|
|
if (isUnicodeSpace(str[i]))
|
|
|
|
last_index = i;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2021-07-07 21:49:57 +08:00
|
|
|
|
2017-02-06 16:41:52 +08:00
|
|
|
trimUTF32VectorFromIndex(str, last_index);
|
|
|
|
}
|
|
|
|
}
|
2013-02-27 15:16:49 +08:00
|
|
|
|
2015-11-21 11:00:14 +08:00
|
|
|
template <typename T>
|
2021-12-25 10:04:45 +08:00
|
|
|
struct ConvertTrait
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
typedef T ArgType;
|
|
|
|
};
|
|
|
|
template <>
|
2021-12-25 10:04:45 +08:00
|
|
|
struct ConvertTrait<char>
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
typedef UTF8 ArgType;
|
|
|
|
};
|
|
|
|
template <>
|
2021-12-25 10:04:45 +08:00
|
|
|
struct ConvertTrait<char16_t>
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
typedef UTF16 ArgType;
|
|
|
|
};
|
|
|
|
template <>
|
2021-12-25 10:04:45 +08:00
|
|
|
struct ConvertTrait<char32_t>
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
typedef UTF32 ArgType;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename From, typename To, typename FromTrait = ConvertTrait<From>, typename ToTrait = ConvertTrait<To>>
|
2021-12-25 10:04:45 +08:00
|
|
|
bool utfConvert(const std::basic_string_view<From>& from,
|
|
|
|
std::basic_string<To>& to,
|
|
|
|
ConversionResult (*cvtfunc)(const typename FromTrait::ArgType**,
|
|
|
|
const typename FromTrait::ArgType*,
|
|
|
|
typename ToTrait::ArgType**,
|
|
|
|
typename ToTrait::ArgType*,
|
|
|
|
ConversionFlags))
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
static_assert(sizeof(From) == sizeof(typename FromTrait::ArgType), "Error size mismatched");
|
|
|
|
static_assert(sizeof(To) == sizeof(typename ToTrait::ArgType), "Error size mismatched");
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (from.empty())
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
to.clear();
|
2014-05-08 11:10:54 +08:00
|
|
|
return true;
|
2013-02-27 15:16:49 +08:00
|
|
|
}
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2015-11-21 11:00:14 +08:00
|
|
|
// See: http://unicode.org/faq/utf_bom.html#gen6
|
|
|
|
static const int most_bytes_per_character = 4;
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const size_t maxNumberOfChars = from.length(); // all UTFs at most one element represents one character.
|
2021-07-07 21:49:57 +08:00
|
|
|
const size_t numberOfOut = maxNumberOfChars * most_bytes_per_character / sizeof(To);
|
2015-11-21 11:00:14 +08:00
|
|
|
|
|
|
|
std::basic_string<To> working(numberOfOut, 0);
|
|
|
|
|
|
|
|
auto inbeg = reinterpret_cast<const typename FromTrait::ArgType*>(&from[0]);
|
|
|
|
auto inend = inbeg + from.length();
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2015-11-21 11:00:14 +08:00
|
|
|
auto outbeg = reinterpret_cast<typename ToTrait::ArgType*>(&working[0]);
|
|
|
|
auto outend = outbeg + working.length();
|
2021-07-07 21:49:57 +08:00
|
|
|
auto r = cvtfunc(&inbeg, inend, &outbeg, outend, strictConversion);
|
2015-11-21 11:00:14 +08:00
|
|
|
if (r != conversionOK)
|
|
|
|
return false;
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2015-11-21 11:00:14 +08:00
|
|
|
working.resize(reinterpret_cast<To*>(outbeg) - &working[0]);
|
|
|
|
to = std::move(working);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool UTF8ToUTF16(std::string_view utf8, std::u16string& outUtf16)
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
return utfConvert(utf8, outUtf16, ConvertUTF8toUTF16);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool UTF8ToUTF32(std::string_view utf8, std::u32string& outUtf32)
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
return utfConvert(utf8, outUtf32, ConvertUTF8toUTF32);
|
2014-05-08 11:10:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool UTF16ToUTF8(std::u16string_view utf16, std::string& outUtf8)
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
return utfConvert(utf16, outUtf8, ConvertUTF16toUTF8);
|
|
|
|
}
|
2021-07-07 21:49:57 +08:00
|
|
|
|
2021-11-27 11:06:51 +08:00
|
|
|
bool UTF16ToUTF32(std::u16string_view utf16, std::u32string& outUtf32)
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
return utfConvert(utf16, outUtf32, ConvertUTF16toUTF32);
|
|
|
|
}
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2021-11-27 11:06:51 +08:00
|
|
|
bool UTF32ToUTF8(std::u32string_view utf32, std::string& outUtf8)
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
return utfConvert(utf32, outUtf8, ConvertUTF32toUTF8);
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:06:51 +08:00
|
|
|
bool UTF32ToUTF16(std::u32string_view utf32, std::u16string& outUtf16)
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
return utfConvert(utf32, outUtf16, ConvertUTF32toUTF16);
|
2013-02-27 15:16:49 +08:00
|
|
|
}
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string getStringUTFCharsJNI(JNIEnv* env, jstring srcjStr, bool* ret)
|
|
|
|
{
|
2015-08-05 16:10:14 +08:00
|
|
|
std::string utf8Str;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (srcjStr != nullptr && env != nullptr)
|
|
|
|
{
|
|
|
|
const unsigned short* unicodeChar = (const unsigned short*)env->GetStringChars(srcjStr, nullptr);
|
2021-07-07 21:49:57 +08:00
|
|
|
size_t unicodeCharLength = env->GetStringLength(srcjStr);
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::u16string unicodeStr((const char16_t*)unicodeChar, unicodeCharLength);
|
2016-08-08 10:43:14 +08:00
|
|
|
bool flag = UTF16ToUTF8(unicodeStr, utf8Str);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
2016-08-08 10:43:14 +08:00
|
|
|
*ret = flag;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!flag)
|
|
|
|
{
|
2016-08-08 10:43:14 +08:00
|
|
|
utf8Str = "";
|
|
|
|
}
|
|
|
|
env->ReleaseStringChars(srcjStr, unicodeChar);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ret)
|
|
|
|
{
|
2016-08-08 10:43:14 +08:00
|
|
|
*ret = false;
|
|
|
|
}
|
2015-08-05 16:10:14 +08:00
|
|
|
utf8Str = "";
|
|
|
|
}
|
|
|
|
return utf8Str;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
jstring newStringUTFJNI(JNIEnv* env, std::string_view utf8Str, bool* ret)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2015-08-05 16:10:14 +08:00
|
|
|
std::u16string utf16Str;
|
2022-07-11 17:50:21 +08:00
|
|
|
bool flag = axis::StringUtils::UTF8ToUTF16(utf8Str, utf16Str);
|
2015-08-05 16:10:14 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
2015-08-05 16:10:14 +08:00
|
|
|
*ret = flag;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!flag)
|
|
|
|
{
|
2015-08-05 16:10:14 +08:00
|
|
|
utf16Str.clear();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
jstring stringText = env->NewString((const jchar*)utf16Str.data(), utf16Str.length());
|
2015-08-05 16:10:14 +08:00
|
|
|
return stringText;
|
2015-08-04 16:24:08 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::vector<char16_t> getChar16VectorFromUTF16String(const std::u16string& utf16)
|
|
|
|
{
|
2015-11-21 11:00:14 +08:00
|
|
|
return std::vector<char16_t>(utf16.begin(), utf16.end());
|
2014-05-08 11:10:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
int32_t getCharacterCountInUTF8String(std::string_view utf8)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
return getUTF8StringLength((const UTF8*)utf8.data());
|
2014-05-08 11:10:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool hasNonAsciiUTF8(const char* str, size_t len)
|
|
|
|
{
|
2021-07-08 00:39:22 +08:00
|
|
|
return detectNonAsciiUTF8(str, len, true, nullptr);
|
2021-07-07 21:49:57 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool detectNonAsciiUTF8(const char* str, size_t len, bool restrictUTF8, bool* pAllCharsAreAscii)
|
|
|
|
{
|
2021-07-07 21:49:57 +08:00
|
|
|
bool allCharsAreAscii = true;
|
|
|
|
bool nonAsciiUTF8Found = false;
|
2021-07-08 00:39:22 +08:00
|
|
|
bool invalidUTF8Found = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (size_t i = 0; i < len;)
|
|
|
|
{
|
2021-07-07 22:35:30 +08:00
|
|
|
auto& current = str[i];
|
|
|
|
int numByte = getNumBytesForUTF8(current);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (numByte > 1)
|
|
|
|
{
|
2021-07-08 00:39:22 +08:00
|
|
|
allCharsAreAscii = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isLegalUTF8Sequence((const UTF8*)¤t, (const UTF8*)¤t + numByte))
|
|
|
|
{
|
2021-07-07 21:49:57 +08:00
|
|
|
nonAsciiUTF8Found = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!restrictUTF8)
|
2021-07-10 00:32:19 +08:00
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // invalid utf-8 sequence found
|
2021-07-08 00:39:22 +08:00
|
|
|
invalidUTF8Found = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (restrictUTF8)
|
2021-07-10 00:32:19 +08:00
|
|
|
break;
|
2021-07-07 21:49:57 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // not a valid utf-8 chars
|
2021-07-07 22:35:30 +08:00
|
|
|
if ((current & 0x80) != 0 || current == 0)
|
|
|
|
allCharsAreAscii = false;
|
2021-06-01 11:47:19 +08:00
|
|
|
}
|
|
|
|
i += numByte;
|
|
|
|
}
|
|
|
|
|
2021-07-07 21:49:57 +08:00
|
|
|
if (pAllCharsAreAscii)
|
|
|
|
*pAllCharsAreAscii = allCharsAreAscii;
|
2021-06-01 11:47:19 +08:00
|
|
|
|
2021-07-08 00:39:22 +08:00
|
|
|
return nonAsciiUTF8Found && (!invalidUTF8Found || !restrictUTF8);
|
2021-06-01 11:47:19 +08:00
|
|
|
}
|
2016-03-07 18:59:53 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isLegalUTF8String(const char* str, size_t len)
|
|
|
|
{
|
|
|
|
return ::isLegalUTF8String((const UTF8**)&str, (const UTF8*)str + len);
|
2016-03-07 18:59:53 +08:00
|
|
|
}
|
|
|
|
|
2021-07-07 21:49:57 +08:00
|
|
|
StringUTF8::StringUTF8() {}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
StringUTF8::StringUTF8(std::string_view newStr)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2016-03-11 20:09:50 +08:00
|
|
|
replace(newStr);
|
2016-03-07 18:59:53 +08:00
|
|
|
}
|
|
|
|
|
2021-07-07 21:49:57 +08:00
|
|
|
StringUTF8::~StringUTF8() {}
|
2016-03-07 18:59:53 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::size_t StringUTF8::length() const
|
|
|
|
{
|
2016-03-07 18:59:53 +08:00
|
|
|
return _str.size();
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void StringUTF8::replace(std::string_view newStr)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2016-03-07 18:59:53 +08:00
|
|
|
_str.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!newStr.empty())
|
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
UTF8* sequenceUtf8 = (UTF8*)newStr.data();
|
2016-03-07 18:59:53 +08:00
|
|
|
|
|
|
|
int lengthString = getUTF8StringLength(sequenceUtf8);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (lengthString == 0)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Bad utf-8 set string: %s", newStr.data());
|
2016-03-07 18:59:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
while (*sequenceUtf8)
|
|
|
|
{
|
2016-03-07 18:59:53 +08:00
|
|
|
std::size_t lengthChar = getNumBytesForUTF8(*sequenceUtf8);
|
|
|
|
|
|
|
|
CharUTF8 charUTF8;
|
2021-12-25 10:04:45 +08:00
|
|
|
charUTF8._char.append((char*)sequenceUtf8, lengthChar);
|
2016-03-07 18:59:53 +08:00
|
|
|
sequenceUtf8 += lengthChar;
|
|
|
|
|
|
|
|
_str.push_back(charUTF8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string StringUTF8::getAsCharSequence() const
|
|
|
|
{
|
2017-10-17 11:03:32 +08:00
|
|
|
return getAsCharSequence(0, std::numeric_limits<std::size_t>::max());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string StringUTF8::getAsCharSequence(std::size_t pos) const
|
|
|
|
{
|
2017-10-17 11:03:32 +08:00
|
|
|
return getAsCharSequence(pos, std::numeric_limits<std::size_t>::max());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string StringUTF8::getAsCharSequence(std::size_t pos, std::size_t len) const
|
|
|
|
{
|
2016-03-07 18:59:53 +08:00
|
|
|
std::string charSequence;
|
2017-10-17 11:03:32 +08:00
|
|
|
std::size_t maxLen = _str.size() - pos;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (len > maxLen)
|
|
|
|
{
|
2017-10-17 11:03:32 +08:00
|
|
|
len = maxLen;
|
|
|
|
}
|
2016-03-07 18:59:53 +08:00
|
|
|
|
2017-10-17 11:03:32 +08:00
|
|
|
std::size_t endPos = len + pos;
|
2021-12-25 10:04:45 +08:00
|
|
|
while (pos < endPos)
|
|
|
|
{
|
2017-10-17 11:03:32 +08:00
|
|
|
charSequence.append(_str[pos++]._char);
|
2016-03-07 18:59:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return charSequence;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool StringUTF8::deleteChar(std::size_t pos)
|
|
|
|
{
|
|
|
|
if (pos < _str.size())
|
|
|
|
{
|
2016-03-07 18:59:53 +08:00
|
|
|
_str.erase(_str.begin() + pos);
|
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-07 18:59:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool StringUTF8::insert(std::size_t pos, std::string_view insertStr)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2016-03-07 18:59:53 +08:00
|
|
|
StringUTF8 utf8(insertStr);
|
|
|
|
|
|
|
|
return insert(pos, utf8);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool StringUTF8::insert(std::size_t pos, const StringUTF8& insertStr)
|
|
|
|
{
|
|
|
|
if (pos <= _str.size())
|
|
|
|
{
|
2016-03-07 18:59:53 +08:00
|
|
|
_str.insert(_str.begin() + pos, insertStr._str.begin(), insertStr._str.end());
|
|
|
|
|
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-07 18:59:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace StringUtils
|
2014-05-08 11:10:54 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|