Skip to content

Commit 051949f

Browse files
authored
use reference to array in IsExtension (#2808)
* simplify * use reference to array in IsExtension
1 parent 5ec2549 commit 051949f

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

gframe/game.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,6 @@ void DuelInfo::Clear() {
5050
time_left[1] = 0;
5151
}
5252

53-
bool IsExtension(const wchar_t* filename, const wchar_t* extension) {
54-
auto flen = std::wcslen(filename);
55-
auto elen = std::wcslen(extension);
56-
if (!elen || flen < elen)
57-
return false;
58-
return !mywcsncasecmp(filename + (flen - elen), extension, elen);
59-
}
60-
61-
bool IsExtension(const char* filename, const char* extension) {
62-
auto flen = std::strlen(filename);
63-
auto elen = std::strlen(extension);
64-
if (!elen || flen < elen)
65-
return false;
66-
return !mystrncasecmp(filename + (flen - elen), extension, elen);
67-
}
68-
6953
bool Game::Initialize() {
7054
LoadConfig();
7155
irr::SIrrlichtCreationParameters params{};
@@ -1141,19 +1125,21 @@ std::wstring Game::SetStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cW
11411125
}
11421126
void Game::LoadExpansions() {
11431127
FileSystem::TraversalDir(L"./expansions", [](const wchar_t* name, bool isdir) {
1128+
if (isdir)
1129+
return;
11441130
wchar_t fpath[1024];
11451131
myswprintf(fpath, L"./expansions/%ls", name);
1146-
if (!isdir && IsExtension(name, L".cdb")) {
1132+
if (IsExtension(name, L".cdb")) {
11471133
dataManager.LoadDB(fpath);
11481134
return;
11491135
}
1150-
if (!isdir && IsExtension(name, L".conf")) {
1136+
if (IsExtension(name, L".conf")) {
11511137
char upath[1024];
11521138
BufferIO::EncodeUTF8(fpath, upath);
11531139
dataManager.LoadStrings(upath);
11541140
return;
11551141
}
1156-
if (!isdir && (IsExtension(name, L".zip") || IsExtension(name, L".ypk"))) {
1142+
if (IsExtension(name, L".zip") || IsExtension(name, L".ypk")) {
11571143
#ifdef _WIN32
11581144
DataManager::FileSystem->addFileArchive(fpath, true, false, irr::io::EFAT_ZIP);
11591145
#else

gframe/game.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,23 @@ constexpr int TEXT_LINE_SIZE = 256;
2828

2929
namespace ygo {
3030

31-
bool IsExtension(const wchar_t* filename, const wchar_t* extension);
32-
bool IsExtension(const char* filename, const char* extension);
31+
template<size_t N>
32+
bool IsExtension(const wchar_t* filename, const wchar_t(&extension)[N]) {
33+
auto flen = std::wcslen(filename);
34+
constexpr size_t elen = N - 1;
35+
if (!elen || flen < elen)
36+
return false;
37+
return !mywcsncasecmp(filename + (flen - elen), extension, elen);
38+
}
39+
40+
template<size_t N>
41+
bool IsExtension(const char* filename, const char(&extension)[N]) {
42+
auto flen = std::strlen(filename);
43+
constexpr size_t elen = N - 1;
44+
if (!elen || flen < elen)
45+
return false;
46+
return !mystrncasecmp(filename + (flen - elen), extension, elen);
47+
}
3348

3449
struct Config {
3550
bool use_d3d{ false };

0 commit comments

Comments
 (0)