2012-01-04 44 views
2

我有一個功能AppendLastSlashIfNotExist
今天,我決定再進行一次功能AppendLastBackSlashIfNotExist如何刪除這個簡單代碼中的重複?

wstring AppendLastSlashIfNotExist(__in const wstring& path) 
{ 
    if (path == L"/") 
    { 
     return path; 
    } 

    if (path.size() == 0 || path[path.size() - 1] != L'/') 
    { 
     return path + L"/"; 
    } 
    return path; 
} 

wstring AppendLastBackSlashIfNotExist(__in const wstring& path) 
{ 
    if (path == L"\\") 
    { 
     return path; 
    } 

    if (path.size() == 0 || path[path.size() - 1] != L'\\') 
    { 
     return path + L"\\"; 
    } 
    return path; 
} 

是的,它吮吸。只有斜槓 - >BackSlash是變化。我想刪除重複。

wstring AppendLastSlashIfNotExist(__in const wstring& path, bool backSlash) 
{ 
    if (path == (backSlash ? L"\\" : L"/")) 
    { 
     return path; 
    } 

    if (path.size() == 0 || path[path.size() - 1] != (backSlash ? L'\\' : L'/')) 
    { 
     return path + (backSlash ? L"\\" : L"/"); 
    } 
    return path; 
} 

我整合了它們。重複刪除。但有一個額外的參數。我仍然感到不舒服。 是否有其他方法可以消除重複?例如,使用高階函數。
請任何想法。

+0

我想你應該合併Path類而不是使用這些函數。在你的班級路徑中,你會關心你的路徑字符串的有效性。使用OO概念。 – AlexTheo 2012-01-04 09:23:55

+0

指示'bool backSlash = false'的默認值。 – atoMerz 2012-01-04 09:32:07

回答

6

wstring AppendLastSlashIfNotExist(__in const wstring& path, 
            wchar_t slash = L'\\') 
{ 
    // This is superfluous and is handled in next if condition. 
    /*if (1 == path.length() && path[0] == slash) 
    { 
     return path; 
    }*/ 

    if (path.size() == 0 || path[path.size() - 1] != slash) 
    { 
     return path + slash; 
    } 
    return path; 
} 

std::wstring s(L"test"); 
std::wcout << AppendLastSlashIfNotExist(s) << "\n"; 
std::wcout << AppendLastSlashIfNotExist(s, L'/') << "\n"; 
+0

+1找到多餘的 – Benjamin 2012-01-04 09:50:23

6

template是這類問題的答案:

template<char SLASH_TYPE> 
wstring AppendLastSlashIfNotExist(__in const wstring& path) 
{ 
    if (path[0] == SLASH_TYPE) // <--- comparing char (not const char*) 
    { 
     return path; 
    } 

    if (path.size() == 0 || path[path.size() - 1] != SLASH_TYPE) 
    { 
     return path + SLASH_TYPE; 
    } 
    return path; 
} 

你需要改變你的邏輯有點爲這個目的,你看要傳遞char而不是const char*作爲模板參數。

功能將被稱爲:

不是傳遞一個布爾值,表明斜線型,你可以只通過所要求的斜槓字符,並可能有斜槓字符默認的
y = AppendLastSlashIfNotExist<'/'>(x); 
y = AppendLastSlashIfNotExist<'\\'>(x); 
+1

+1。好多了。 – Benjamin 2012-01-04 09:24:01

+0

不應該在比較'path [0] == SLASH_TYPE'之前檢查'path.size()== 0'嗎? – 2012-01-04 09:27:26

+0

順便說一句,由於你的改變,邏輯不再一樣。 'path [0] == SLASH_TYPE' – Benjamin 2012-01-04 09:27:55

3

你應該試着想一下稍後閱讀代碼的人。 bool不可讀代碼,但AppendLastSlashIfNotExistsAppendLastBackSlashIfNotExists是。我的建議是保持這兩個功能,然後從它們中調用常用功能。

wstring AppendLastSlashIfNotExistInternal(__in const wstring& path, bool backSlash) 
{ 
    // as before.. 
    return path; 
} 

wstring AppendLastBackSlashIfNotExist(__in const wstring& path){ 
    return AppendLastSlashIfNotExistInternal(path, true); 
} 

wstring AppendLastSlashIfNotExist(__in const wstring& path) 
{ 
    return AppendLastSlashIfNotExistInternal(path, false); 
} 

這樣,你仍然保持可讀性誰以後維護代碼

+1

+1。保留可讀的函數名稱,但將其常見行爲委託給另一個函數。就我個人而言,我會通過添加字符而不是布爾值,但否則這是我會採取的方法。 – 2012-01-04 16:48:39

+0

@CarlManaster我完全同意。儘可能通用 – Default 2012-01-05 07:52:46

+0

我也會採用這種方法。 – 2012-01-10 10:30:32

0

一種解決方案可能是使用TrimEnd與參數,以從字符串的結尾去掉所有不需要的字符的單:

template<class T> 
T TrimEnd(const T& arg, const T& unwantedCharacters) 
{ 
    // Do manipulations here using stringstream in order to cut unwanted characters. 
} 

或者你可以避免使用模板和使用該函數的兩個版本的字符串和wstring參數。

之後,您只需在結果字符串中追加需要的字符尾部。