Skip to content

Commit

Permalink
IsVariationSelectorにstd::wstring_view型のオーバーロードを残す
Browse files Browse the repository at this point in the history
  • Loading branch information
suconbu committed May 12, 2024
1 parent fe2e51b commit e9d9d51
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion sakura_core/charset/CCodeBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ std::wstring CCodeBase::CodeToHex(const CNativeW& cSrc, const CommonSetting_Stat
EConvertResult CCodeBase::UnicodeToHex(const wchar_t* cSrc, const int iSLen, WCHAR* pDst, const CommonSetting_Statusbar* psStatusbar)
{
// IVS
if (iSLen >= 3 && IsVariationSelector(cSrc + 1, iSLen - 1)) {
if (iSLen >= 3 && IsVariationSelector(cSrc + 1)) {
if (psStatusbar->m_bDispSPCodepoint) {
auto_sprintf(pDst, L"%04X, U+%05X", cSrc[0], ConvertToUtf32(cSrc + 1));
}
Expand Down
2 changes: 1 addition & 1 deletion sakura_core/charset/CUtf8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ EConvertResult CUtf8::_UnicodeToHex(const wchar_t* cSrc, const int iSLen, WCHAR*
if (IsUTF16High(cSrc[0]) && iSLen >= 2 && IsUTF16Low(cSrc[1])) {
cBuff._GetMemory()->SetRawDataHoldBuffer(cSrc, 4);
}
else if (iSLen >= 3 && IsVariationSelector(cSrc + 1, iSLen - 1)) {
else if (iSLen >= 3 && IsVariationSelector(cSrc + 1)) {
cBuff._GetMemory()->SetRawDataHoldBuffer(cSrc, sizeof(wchar_t) * 3);
}
else {
Expand Down
9 changes: 8 additions & 1 deletion sakura_core/charset/codechecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,17 @@ char32_t ConvertToUtf32(std::wstring_view text) {

/*!
* 文字列がIVSの異体字セレクタで始まっているか判定する
* nLenを省略した時はヌル文字の手前までが有効な文字列であると解釈する
*/
inline bool IsVariationSelector(const wchar_t* pStr, size_t nLen) {
inline bool IsVariationSelector(const wchar_t* pStr, size_t nLen = SIZE_MAX) {
return (2 <= nLen) && (pStr[0] == 0xDB40) && (0xDD00 <= pStr[1]) && (pStr[1] <= 0xDDEF);
}
/*!
* 文字列がIVSの異体字セレクタで始まっているか判定する
*/
inline bool IsVariationSelector(std::wstring_view text) {
return IsVariationSelector(text.data(), text.size());
}

//! 上位バイトと下位バイトを交換 (主に UTF-16 LE/BE 向け)
inline unsigned short _SwapHLByte( const unsigned short wc ){
Expand Down
2 changes: 1 addition & 1 deletion sakura_core/mem/CNativeW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ CLogicInt CNativeW::GetSizeOfChar( const wchar_t* pData, int nDataLen, int nIdx
}

// IVSの異体字セレクタチェック
if (IsVariationSelector(pData + nIdx + 1, nDataLen - (nIdx + 1))) {
if (IsVariationSelector(pData + nIdx + 1)) {
// 正字 + 異体字セレクタで3個分
return CLogicInt(3);
}
Expand Down
2 changes: 1 addition & 1 deletion sakura_core/parse/CWordParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ ECharKind CWordParse::WhatKindOfChar(
}
// IVS(正字 + 異体字セレクタ)
else if (nCharChars == 3 &&
IsVariationSelector(pData + nIdx + 1, pDataLen - (nIdx + 1)))
IsVariationSelector(pData + nIdx + 1))
{
ret = CK_ZEN_ETC; // 全角のその他(漢字など)
}
Expand Down
2 changes: 1 addition & 1 deletion sakura_core/view/CTextMetrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const int* CTextMetrics::GenerateDxArray(
vResultArray.push_back(cache.CalcPxWidthByFont(pText[i]) + spacing);
nIndent += vResultArray.back();

if (IsVariationSelector(pText + i + 1, nLength - (i + 1))) {
if (IsVariationSelector(pText + i + 1)) {
vResultArray.push_back(0);
vResultArray.push_back(0);
i += 2;
Expand Down
41 changes: 25 additions & 16 deletions tests/unittests/test-codechecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,32 @@ TEST(ConvertToUtf32, BinaryOnSurrogate)

TEST(IsVariationSelector, VariationSelectorCheck)
{
// IVS開始
const auto& ivs1 = L"\U000E0100";
EXPECT_TRUE(IsVariationSelector(ivs1, 2));
// 異体字セレクタ開始
const auto& vs1 = L"\U000E0100";
EXPECT_TRUE(IsVariationSelector(vs1));
EXPECT_TRUE(IsVariationSelector(std::wstring(vs1)));

// IVS終了
const auto& ivs2 = L"\U000E01EF";
EXPECT_TRUE(IsVariationSelector(ivs2, 2));
// 異体字セレクタ終了
const auto& vs2 = L"\U000E01EF";
EXPECT_TRUE(IsVariationSelector(vs2));
EXPECT_TRUE(IsVariationSelector(std::wstring(vs2)));

// 長さ不足
EXPECT_FALSE(IsVariationSelector(ivs1, 0));
EXPECT_FALSE(IsVariationSelector(ivs1, 1));
// 長さ指定
EXPECT_FALSE(IsVariationSelector(vs1, 0));
EXPECT_FALSE(IsVariationSelector(vs1, 1));
EXPECT_TRUE(IsVariationSelector(vs1, 2));

// IVS開始-1
const auto& notivs1 = L"\U000E00FF";
EXPECT_FALSE(IsVariationSelector(notivs1, 2));

// IVS終了+1
const auto& notivs2 = L"\U000E01F0";
EXPECT_FALSE(IsVariationSelector(notivs2, 2));
// 非該当文字列
const auto& notvs1 = L"";
EXPECT_FALSE(IsVariationSelector(notvs1));
EXPECT_FALSE(IsVariationSelector(std::wstring(notvs1)));
const auto& notvs2 = L"\xDB40";
EXPECT_FALSE(IsVariationSelector(notvs2));
EXPECT_FALSE(IsVariationSelector(std::wstring(notvs2)));
const auto& notvs3 = L"\U000E00FF";
EXPECT_FALSE(IsVariationSelector(notvs3));
EXPECT_FALSE(IsVariationSelector(std::wstring(notvs3)));
const auto& notvs4 = L"\U000E01F0";
EXPECT_FALSE(IsVariationSelector(notvs4));
EXPECT_FALSE(IsVariationSelector(std::wstring(notvs4)));
}

0 comments on commit e9d9d51

Please sign in to comment.