From 65be7c3a6f01feb265d9c049f770c2b5a0f4a719 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 26 Mar 2018 10:34:19 -0400 Subject: [PATCH] Add Buffer::n_lines() --- src/core/Buffer.cc | 11 +++++++++++ src/core/Buffer.h | 1 + test/src/test_Buffer.cc | 2 ++ 3 files changed, 14 insertions(+) diff --git a/src/core/Buffer.cc b/src/core/Buffer.cc index 3896bb1..9cea42d 100644 --- a/src/core/Buffer.cc +++ b/src/core/Buffer.cc @@ -502,3 +502,14 @@ void Buffer::set_filename(const std::string & filename) { m_filename = std::make_shared(filename); } + +size_t Buffer::n_lines() const +{ + auto it = end(); + it.go_back(); + if (it.valid()) + { + return it.line() + 1u; + } + return 0u; +} diff --git a/src/core/Buffer.h b/src/core/Buffer.h index 2a38533..d68c366 100644 --- a/src/core/Buffer.h +++ b/src/core/Buffer.h @@ -192,6 +192,7 @@ public: void pop_operation(); void undo(); void redo(); + size_t n_lines() const; #ifndef ENABLE_TESTING protected: diff --git a/test/src/test_Buffer.cc b/test/src/test_Buffer.cc index 50a5968..a9ed2b4 100644 --- a/test/src/test_Buffer.cc +++ b/test/src/test_Buffer.cc @@ -33,11 +33,13 @@ TEST(BufferTest, writes_an_empty_file_for_an_empty_buffer) Buffer b; ASSERT_TRUE(b.write_to_file("test/tmp/f")); TestSupport::compare_files("test/files/empty.txt", "test/tmp/f"); + ASSERT_EQ(0u, b.n_lines()); } TEST(BufferTest, allows_navigating_using_iterators) { Buffer b("test/files/line_endings/lf_format.txt"); + ASSERT_EQ(2u, b.n_lines()); auto iterator = b.add_cursor(); EXPECT_EQ(0u, iterator->line());