c++实现basename

window API居然不包含Linux中很好用的basename函数,实现了一下,留个记录,省得日后重复写。

std::string m_basename(std::string fullPath)
{
	size_t index_1 = fullPath.find_last_of("/");
	size_t index_2 = fullPath.find_last_of("\\");
	if (index_1 == std::string::npos && index_2 == std::string::npos)
		return fullPath;

	size_t idx = 0;
	if (index_1 == std::string::npos)
	{
		idx = index_2;
	}
	else if (index_2 == std::string::npos)
	{
		idx = index_1;
	}
	else
	{
		idx = (index_1 > index_2) ? index_1 : index_2;
	}
	std::string basename = fullPath.substr(idx + 1, fullPath.length() - idx);
	return basename;
}

你可能感兴趣的:(Linux/C++,c++,开发语言,后端)