Skip to content

Commit 010668f

Browse files
author
nahida-mono
committed
为项目使用HutaoString
1 parent 869d88c commit 010668f

34 files changed

+2737
-437
lines changed

src/Snap.Hutao.Remastered.Native.Test/Snap.Hutao.Remastered.Native.Test.vcxproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
</ClCompile>
119119
<ClCompile Include="SimpleTestFramework.cpp" />
120120
<ClCompile Include="TestBase.cpp" />
121+
<ClCompile Include="TestHutaoString.cpp" />
121122
</ItemGroup>
122123
<ItemGroup>
123124
<None Include="packages.config" />
@@ -142,4 +143,4 @@
142143
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.props'))" />
143144
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.targets'))" />
144145
</Target>
145-
</Project>
146+
</Project>

src/Snap.Hutao.Remastered.Native.Test/TestBase.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,65 +62,65 @@ void InitializeTestCallbacks()
6262
// Here you can add non-inline helper function implementations
6363

6464
// Example: Helper function to create test directory
65-
bool CreateTestDirectory(const std::wstring& path)
65+
bool CreateTestDirectory(const HutaoString& path)
6666
{
67-
if (CreateDirectoryW(path.c_str(), nullptr) || GetLastError() == ERROR_ALREADY_EXISTS)
67+
if (CreateDirectoryW(path.Data(), nullptr) || GetLastError() == ERROR_ALREADY_EXISTS)
6868
{
69-
wprintf(L"Created test directory: %s\n", path.c_str());
69+
wprintf(L"Created test directory: %s\n", path.Data());
7070
return true;
7171
}
7272
else
7373
{
74-
wprintf(L"Failed to create test directory: %s, error code: %d\n", path.c_str(), GetLastError());
74+
wprintf(L"Failed to create test directory: %s, error code: %d\n", path.Data(), GetLastError());
7575
return false;
7676
}
7777
}
7878

7979
// Example: Helper function to delete test file
80-
bool DeleteTestFile(const std::wstring& path)
80+
bool DeleteTestFile(const HutaoString& path)
8181
{
82-
if (DeleteFileW(path.c_str()) || GetLastError() == ERROR_FILE_NOT_FOUND)
82+
if (DeleteFileW(path.Data()) || GetLastError() == ERROR_FILE_NOT_FOUND)
8383
{
84-
wprintf(L"Deleted test file: %s\n", path.c_str());
84+
wprintf(L"Deleted test file: %s\n", path.Data());
8585
return true;
8686
}
8787
else
8888
{
89-
wprintf(L"Failed to delete test file: %s, error code: %d\n", path.c_str(), GetLastError());
89+
wprintf(L"Failed to delete test file: %s, error code: %d\n", path.Data(), GetLastError());
9090
return false;
9191
}
9292
}
9393

9494
// Example: Helper function to verify file existence
95-
bool VerifyFileExists(const std::wstring& path)
95+
bool VerifyFileExists(const HutaoString& path)
9696
{
97-
DWORD attributes = GetFileAttributesW(path.c_str());
97+
DWORD attributes = GetFileAttributesW(path.Data());
9898
if (attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY))
9999
{
100-
wprintf(L"File exists: %s\n", path.c_str());
100+
wprintf(L"File exists: %s\n", path.Data());
101101
return true;
102102
}
103103
else
104104
{
105-
wprintf(L"File does not exist: %s\n", path.c_str());
105+
wprintf(L"File does not exist: %s\n", path.Data());
106106
return false;
107107
}
108108
}
109109

110110
// Example: Helper function to get file size
111-
uint64_t GetFileSizeHelper(const std::wstring& path)
111+
uint64_t GetFileSizeHelper(const HutaoString& path)
112112
{
113-
HANDLE hFile = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
113+
HANDLE hFile = CreateFileW(path.Data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
114114
if (hFile == INVALID_HANDLE_VALUE)
115115
{
116-
wprintf(L"Cannot open file to get size: %s, error code: %d\n", path.c_str(), GetLastError());
116+
wprintf(L"Cannot open file to get size: %s, error code: %d\n", path.Data(), GetLastError());
117117
return 0;
118118
}
119119

120120
LARGE_INTEGER fileSize;
121121
if (!GetFileSizeEx(hFile, &fileSize))
122122
{
123-
wprintf(L"Failed to get file size: %s, error code: %d\n", path.c_str(), GetLastError());
123+
wprintf(L"Failed to get file size: %s, error code: %d\n", path.Data(), GetLastError());
124124
CloseHandle(hFile);
125125
return 0;
126126
}

src/Snap.Hutao.Remastered.Native.Test/TestBase.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "pch.h"
44
#include "HutaoNativeImports.h"
55
#include "../Snap.Hutao.Remastered.Native/CustomImplements.h"
6+
#include "../Snap.Hutao.Remastered.Native/HutaoString.h"
67

78
using namespace winrt;
89
using namespace Windows::Foundation;
@@ -44,19 +45,25 @@ inline void TestAssert(bool condition, const wchar_t* message = nullptr)
4445
}
4546
}
4647

47-
inline std::wstring FormatString(const wchar_t* format, ...)
48+
inline HutaoString FormatString(const wchar_t* format, ...)
4849
{
4950
va_list args;
5051
va_start(args, format);
5152

5253
int len = _vscwprintf(format, args) + 1;
53-
std::wstring buffer(len, L'\0');
54-
_vsnwprintf_s(&buffer[0], len, _TRUNCATE, format, args);
54+
HutaoString buffer;
55+
buffer.EnsureCapacity(len);
5556

57+
// 临时缓冲区用于格式化
58+
wchar_t* tempBuffer = new wchar_t[len];
59+
_vsnwprintf_s(tempBuffer, len, _TRUNCATE, format, args);
60+
61+
// 将格式化后的字符串设置到HutaoString
62+
buffer = tempBuffer;
63+
64+
delete[] tempBuffer;
5665
va_end(args);
5766

58-
// Remove trailing null character
59-
buffer.resize(wcslen(buffer.c_str()));
6067
return buffer;
6168
}
6269

@@ -140,29 +147,30 @@ class TestTimer
140147
class TestDataGenerator
141148
{
142149
public:
143-
static std::wstring GenerateRandomString(size_t length)
150+
static HutaoString GenerateRandomString(size_t length)
144151
{
145152
static const wchar_t charset[] = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
146153
static const size_t charsetSize = sizeof(charset) / sizeof(charset[0]) - 1;
147154

148-
std::wstring result;
149-
result.reserve(length);
155+
HutaoString result;
156+
result.EnsureCapacity(length);
150157

151158
for (size_t i = 0; i < length; ++i)
152159
{
153-
result.push_back(charset[rand() % charsetSize]);
160+
result.Append(&charset[rand() % charsetSize], 1);
154161
}
155162

156163
return result;
157164
}
158165

159-
static std::wstring GenerateTestFilePath()
166+
static HutaoString GenerateTestFilePath()
160167
{
161168
wchar_t tempPath[MAX_PATH];
162169
GetTempPathW(MAX_PATH, tempPath);
163170

164-
std::wstring fileName = L"hutao_test_" + GenerateRandomString(8) + L".tmp";
165-
return std::wstring(tempPath) + fileName;
171+
// 修复:避免使用 const wchar_t* + HutaoString 运算符
172+
HutaoString fileName = HutaoString(L"hutao_test_") + GenerateRandomString(8) + L".tmp";
173+
return HutaoString(tempPath) + fileName;
166174
}
167175
};
168176

0 commit comments

Comments
 (0)