From 5d0adf21954c55a9e0d77889b984b355929bc0c5 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Mon, 19 Dec 2016 22:44:30 +0100 Subject: [PATCH] Use StartsWith to reduce magic numbers in code Signed-off-by: Sven Strickroth --- src/ResText/POFile.cpp | 21 +++++++++++++-------- src/TortoiseShell/ContextMenu.cpp | 4 ++-- src/TortoiseShell/ItemIDList.cpp | 4 ++-- test/UnitTests/GitRevRefBrowseTest.cpp | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/ResText/POFile.cpp b/src/ResText/POFile.cpp index 0ed68c69d..2e70a35ad 100644 --- a/src/ResText/POFile.cpp +++ b/src/ResText/POFile.cpp @@ -41,6 +41,11 @@ CPOFile::~CPOFile(void) { } +static bool StartsWith(const wchar_t* heystacl, const wchar_t* needle) +{ + return wcsncmp(heystacl, needle, wcslen(needle)) == 0; +} + BOOL CPOFile::ParseFile(LPCTSTR szPath, BOOL bUpdateExisting, bool bAdjustEOLs) { if (!PathFileExists(szPath)) @@ -81,12 +86,12 @@ BOOL CPOFile::ParseFile(LPCTSTR szPath, BOOL bUpdateExisting, bool bAdjustEOLs) int type = 0; for (auto I = entry.cbegin(); I != entry.cend(); ++I) { - if (wcsncmp(I->c_str(), L"# ", 2)==0) + if (StartsWith(I->c_str(), L"# ")) { //user comment - if (wcsncmp(I->c_str(), L"# regexsearch=", 14) == 0) + if (StartsWith(I->c_str(), L"# regexsearch=")) regexsearch = I->substr(14); - else if (wcsncmp(I->c_str(), L"# regexreplace=", 15) == 0) + else if (StartsWith(I->c_str(), L"# regexreplace=")) regexreplace = I->substr(15); else resEntry.translatorcomments.push_back(I->c_str()); @@ -98,19 +103,19 @@ BOOL CPOFile::ParseFile(LPCTSTR szPath, BOOL bUpdateExisting, bool bAdjustEOLs) } type = 0; } - if (wcsncmp(I->c_str(), L"#.", 2)==0) + if (StartsWith(I->c_str(), L"#.")) { //automatic comments resEntry.automaticcomments.push_back(I->c_str()); type = 0; } - if (wcsncmp(I->c_str(), L"#,", 2)==0) + if (StartsWith(I->c_str(), L"#,")) { //flag resEntry.flag = I->c_str(); type = 0; } - if (wcsncmp(I->c_str(), L"msgid", 5)==0) + if (StartsWith(I->c_str(), L"msgid")) { //message id msgid = I->c_str(); @@ -122,7 +127,7 @@ BOOL CPOFile::ParseFile(LPCTSTR szPath, BOOL bUpdateExisting, bool bAdjustEOLs) nEntries++; type = 1; } - if (wcsncmp(I->c_str(), L"msgstr", 6)==0) + if (StartsWith(I->c_str(), L"msgstr")) { //message string resEntry.msgstr = I->c_str(); @@ -131,7 +136,7 @@ BOOL CPOFile::ParseFile(LPCTSTR szPath, BOOL bUpdateExisting, bool bAdjustEOLs) nTranslated++; type = 2; } - if (wcsncmp(I->c_str(), L"\"", 1)==0) + if (StartsWith(I->c_str(), L"\"")) { if (type == 1) { diff --git a/src/TortoiseShell/ContextMenu.cpp b/src/TortoiseShell/ContextMenu.cpp index 115052f26..9909ef21a 100644 --- a/src/TortoiseShell/ContextMenu.cpp +++ b/src/TortoiseShell/ContextMenu.cpp @@ -931,13 +931,13 @@ STDMETHODIMP CShellExt::QueryContextMenu_Wrap(HMENU hMenu, // which we can't handle for (const auto& file : files_) { - if (wcsncmp(file.c_str(), L"::{", 3) == 0) + if (CStringUtils::StartsWith(file.c_str(), L"::{")) return S_OK; } } else { - if (wcsncmp(folder_.c_str(), L"::{", 3) == 0) + if (CStringUtils::StartsWith(folder_.c_str(), L"::{")) return S_OK; } diff --git a/src/TortoiseShell/ItemIDList.cpp b/src/TortoiseShell/ItemIDList.cpp index ae69ac4dd..1e94ef8bf 100644 --- a/src/TortoiseShell/ItemIDList.cpp +++ b/src/TortoiseShell/ItemIDList.cpp @@ -20,7 +20,7 @@ #include "stdafx.h" #include "ShellExt.h" #include "ItemIDList.h" - +#include "StringUtils.h" ItemIDList::ItemIDList(PCUITEMID_CHILD item, PCUIDLIST_RELATIVE parent) : item_ (item) @@ -107,7 +107,7 @@ tstring ItemIDList::toString(bool resolveLibraries /*= true*/) ret = szDisplayName; CoTaskMemFree(szDisplayName); - if (!((resolveLibraries) && (wcsncmp(ret.c_str(), L"::{", 3) == 0))) + if (!((resolveLibraries) && (CStringUtils::StartsWith(ret.c_str(), L"::{")))) return ret; CComPtr plib; diff --git a/test/UnitTests/GitRevRefBrowseTest.cpp b/test/UnitTests/GitRevRefBrowseTest.cpp index fd2fa6d07..f6ce32001 100644 --- a/test/UnitTests/GitRevRefBrowseTest.cpp +++ b/test/UnitTests/GitRevRefBrowseTest.cpp @@ -68,7 +68,7 @@ static void GetGitRevRefMap() EXPECT_STREQ(L"", rev.m_Description); refMap.clear(); - EXPECT_EQ(0, GitRevRefBrowser::GetGitRevRefMap(refMap, 0, err, [](const CString& refName) { return wcsncmp(refName, L"refs/heads/", 11) == 0; })); + EXPECT_EQ(0, GitRevRefBrowser::GetGitRevRefMap(refMap, 0, err, [](const CString& refName) { return CStringUtils::StartsWith(refName, L"refs/heads/"); })); EXPECT_TRUE(err.IsEmpty()); EXPECT_EQ(5, refMap.size()); EXPECT_TRUE(refMap.find(L"refs/heads/master") != refMap.end()); -- 2.11.4.GIT