From 6273aa944378dc2a88fe044129a13427f74646bc Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 4 Nov 2015 00:37:39 +0000 Subject: [PATCH] Rework Buffer::distance implementation --- src/buffer.inl.hh | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/buffer.inl.hh b/src/buffer.inl.hh index cc3f83b9..c5771de4 100644 --- a/src/buffer.inl.hh +++ b/src/buffer.inl.hh @@ -43,16 +43,13 @@ inline ByteCount Buffer::distance(ByteCoord begin, ByteCoord end) const { if (begin > end) return -distance(end, begin); - ByteCount res = 0; - for (LineCount l = begin.line; l <= end.line; ++l) - { - ByteCount len = m_lines[l].length(); - res += len; - if (l == begin.line) - res -= begin.column; - if (l == end.line) - res -= len - end.column; - } + if (begin.line == end.line) + return end.column - begin.column; + + ByteCount res = m_lines[begin.line].length() - begin.column; + for (LineCount l = begin.line+1; l < end.line; ++l) + res += m_lines[l].length(); + res += end.column; return res; }