nextpnr/3rdparty/python-console/Utils.h

34 lines
792 B
C
Raw Normal View History

2018-06-28 19:16:53 +08:00
#ifndef PYTHON_CONSOLE_UTILS_H
#define PYTHON_CONSOLE_UTILS_H
#include <string>
#include <vector>
/**
InputIterator has value type of std::string.
*/
template < class InputIterator >
std::string LongestCommonPrefix( InputIterator begin, InputIterator end )
{
if ( begin == end )
return "";
const std::string& str0 = *begin;
if ( ! str0.size() )
return "";
int endIndex = str0.size() - 1;
InputIterator it = begin; ++it;
for (; it != end; ++it)
{
const std::string& str = *it;
for (int j = 0; j <= endIndex; ++j)
{
2018-06-28 23:56:44 +08:00
if (j >= (int)str.size() || str[j] != str0[j])
2018-06-28 19:16:53 +08:00
endIndex = j - 1;
}
}
return (endIndex > 0)? str0.substr(0, endIndex + 1) : "";
}
#endif // PYTHON_CONSOLE_UTILS_H