Skip to content

Commit 8072e78

Browse files
committedJan 30, 2021
Fix dirname.h windows bug
1 parent d79f95e commit 8072e78

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed
 

‎include/dirname.h

+8-15
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,24 @@ IGL_INLINE std::string igl::dirname(const std::string & path)
3737
{
3838
return std::string("");
3939
}
40-
#if defined (WIN32)
41-
char del('\\');
42-
#else
43-
char del('/');
44-
#endif
45-
// http://stackoverflow.com/questions/5077693/dirnamephp-similar-function-in-c
46-
std::string::const_reverse_iterator last_slash =
47-
std::find(
48-
path.rbegin(),
49-
path.rend(),del);
50-
if( last_slash == path.rend() )
40+
// https://stackoverflow.com/a/3071694/148668
41+
size_t found = path.find_last_of("/\\");
42+
if(found == std::string::npos)
5143
{
5244
// No slashes found
5345
return std::string(".");
54-
}else if(1 == (last_slash.base() - path.begin()))
46+
}else if(found == 0)
5547
{
5648
// Slash is first char
57-
return std::string(&del);
58-
}else if(path.end() == last_slash.base() )
49+
return std::string(path.begin(),path.begin()+1);
50+
}else if(found == path.length()-1)
5951
{
6052
// Slash is last char
6153
std::string redo = std::string(path.begin(),path.end()-1);
6254
return igl::dirname(redo);
6355
}
64-
return std::string(path.begin(),last_slash.base()-1);
56+
// Return everything up to but not including last slash
57+
return std::string(path.begin(),path.begin()+found);
6558
}
6659

6760

0 commit comments

Comments
 (0)
Please sign in to comment.