Skip to content

Commit

Permalink
Bug, 解决FindNLSStringEx参数_fFindNLSStringFlags传递0时会反向搜索问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed Sep 4, 2024
1 parent a9002b5 commit cb0baea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/Thunks/api-ms-win-core-localization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2568,6 +2568,13 @@

__WarningMessage__("FindNLSStringEx 暂时只支持搜索 _cchValue 的子字符串。");

const DWORD _fFindFlags = _fFindNLSStringFlags & (FIND_STARTSWITH | FIND_ENDSWITH | FIND_FROMSTART | FIND_FROMEND);
if (_fFindFlags & (_fFindFlags - 1))
{
SetLastError(ERROR_INVALID_FLAGS);
return -1;
}

if (_pVersionInformation || _pReserved|| _hSortHandle
|| _szStringSource == nullptr || _cchSource == 0 || _cchSource < -1
|| _szStringValue == nullptr || _cchValue == 0 || _cchValue < -1)
Expand Down Expand Up @@ -2597,7 +2604,7 @@
return -1;

const DWORD _fCmpFlags = _fFindNLSStringFlags & ~(FIND_STARTSWITH | FIND_ENDSWITH | FIND_FROMSTART | FIND_FROMEND);
if (_fFindNLSStringFlags & (FIND_FROMSTART | FIND_STARTSWITH))
if ((_fFindNLSStringFlags & (FIND_ENDSWITH | FIND_FROMEND)) == 0)
{
// 从头开始搜索
if (_fFindNLSStringFlags & FIND_STARTSWITH)
Expand Down
19 changes: 12 additions & 7 deletions src/YY-Thunks.UnitTest/api-ms-win-core-localization.UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,23 +808,28 @@ namespace api_ms_win_core_localization
INT _nFound;
int _nIndex;

_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"0123456789", -1, L"0123", -1, &_nFound, nullptr, nullptr, 0);
// 默认值
_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, 0, L"01234567890123", -1, L"0123", -1, &_nFound, nullptr, nullptr, 0);
Assert::AreEqual(_nIndex, 0);
Assert::AreEqual(_nFound, 4);

_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"0123450123", -1, L"0123", -1, &_nFound, nullptr, nullptr, 0);
_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"01234567890123", -1, L"0123", -1, &_nFound, nullptr, nullptr, 0);
Assert::AreEqual(_nIndex, 0);
Assert::AreEqual(_nFound, 4);

_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"0123456789", -1, L"1234", -1, &_nFound, nullptr, nullptr, 0);
_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"01234501230123", -1, L"0123", -1, &_nFound, nullptr, nullptr, 0);
Assert::AreEqual(_nIndex, 0);
Assert::AreEqual(_nFound, 4);

_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"01234567890123", -1, L"1234", -1, &_nFound, nullptr, nullptr, 0);
Assert::AreEqual(_nIndex, 1);
Assert::AreEqual(_nFound, 4);

_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"0123456789", -1, L"6789", -1, &_nFound, nullptr, nullptr, 0);
_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"01234567890123", -1, L"6789", -1, &_nFound, nullptr, nullptr, 0);
Assert::AreEqual(_nIndex, 6);
Assert::AreEqual(_nFound, 4);

_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"0123456789", -1, L"3210", -1, &_nFound, nullptr, nullptr, 0);
_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMSTART, L"01234567890123", -1, L"3210", -1, &_nFound, nullptr, nullptr, 0);
Assert::AreEqual(_nIndex, -1);
}

Expand All @@ -846,8 +851,8 @@ namespace api_ms_win_core_localization
INT _nFound;
int _nIndex;

_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMEND, L"0123456789", -1, L"0123", -1, &_nFound, nullptr, nullptr, 0);
Assert::AreEqual(_nIndex, 0);
_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMEND, L"01234567890123", -1, L"0123", -1, &_nFound, nullptr, nullptr, 0);
Assert::AreEqual(_nIndex, 10);
Assert::AreEqual(_nFound, 4);

_nIndex = ::FindNLSStringEx(LOCALE_NAME_SYSTEM_DEFAULT, FIND_FROMEND, L"0123456789", -1, L"6789", -1, &_nFound, nullptr, nullptr, 0);
Expand Down

0 comments on commit cb0baea

Please sign in to comment.