move clamp to utils.hh

This commit is contained in:
Maxime Coste 2012-10-02 14:08:09 +02:00
parent 84acf2594a
commit 9ec10daf69
2 changed files with 8 additions and 12 deletions

View File

@ -11,16 +11,6 @@
namespace Kakoune namespace Kakoune
{ {
template<typename T>
T clamp(T min, T max, T val)
{
if (val < min)
return min;
if (val > max)
return max;
return val;
}
Buffer::Buffer(String name, Type type, Buffer::Buffer(String name, Type type,
String initial_content) String initial_content)
: m_name(std::move(name)), m_type(type), : m_name(std::move(name)), m_type(type),
@ -83,9 +73,9 @@ BufferCoord Buffer::clamp(const BufferCoord& line_and_column,
return BufferCoord(); return BufferCoord();
BufferCoord result(line_and_column.line, line_and_column.column); BufferCoord result(line_and_column.line, line_and_column.column);
result.line = Kakoune::clamp(0_line, line_count() - 1, result.line); result.line = Kakoune::clamp(result.line, 0_line, line_count() - 1);
CharCount max_col = std::max(0_char, line_length(result.line) - (avoid_eol ? 2 : 1)); CharCount max_col = std::max(0_char, line_length(result.line) - (avoid_eol ? 2 : 1));
result.column = Kakoune::clamp(0_char, max_col, result.column); result.column = Kakoune::clamp(result.column, 0_char, max_col);
return result; return result;
} }

View File

@ -195,6 +195,12 @@ inline String escape(const String& name)
return name.replace("([ \\t;])", R"(\\\1)"); return name.replace("([ \\t;])", R"(\\\1)");
} }
template<typename T>
const T& clamp(const T& val, const T& min, const T& max)
{
return (val < min ? min : (val > max ? max : val));
}
} }
#endif // utils_hh_INCLUDED #endif // utils_hh_INCLUDED