From 9ec10daf6991a6135cc03ec9c9a8fae5d670dcf4 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 2 Oct 2012 14:08:09 +0200 Subject: [PATCH] move clamp to utils.hh --- src/buffer.cc | 14 ++------------ src/utils.hh | 6 ++++++ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 5b868cec..1eac7bcc 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -11,16 +11,6 @@ namespace Kakoune { -template -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, String initial_content) : m_name(std::move(name)), m_type(type), @@ -83,9 +73,9 @@ BufferCoord Buffer::clamp(const BufferCoord& line_and_column, return BufferCoord(); 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)); - result.column = Kakoune::clamp(0_char, max_col, result.column); + result.column = Kakoune::clamp(result.column, 0_char, max_col); return result; } diff --git a/src/utils.hh b/src/utils.hh index 9892fc92..92c09767 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -195,6 +195,12 @@ inline String escape(const String& name) return name.replace("([ \\t;])", R"(\\\1)"); } +template +const T& clamp(const T& val, const T& min, const T& max) +{ + return (val < min ? min : (val > max ? max : val)); +} + } #endif // utils_hh_INCLUDED