2012-01-04 121 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参数。

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