[vte/wip/egmont/bidi: 80/83] index_safe



commit b0da87a73eea8e69e549f96cb74e438f46e021aa
Author: Egmont Koblinger <egmont gmail com>
Date:   Fri Dec 7 14:49:58 2018 +0100

    index_safe

 BIDI-STATUS |  1 -
 src/bidi.cc | 14 +++++++-------
 src/ring.cc |  9 +++++++++
 src/ring.hh |  1 +
 src/vte.cc  |  2 +-
 5 files changed, 18 insertions(+), 9 deletions(-)
---
diff --git a/BIDI-STATUS b/BIDI-STATUS
index 27e5d656..238708e4 100644
--- a/BIDI-STATUS
+++ b/BIDI-STATUS
@@ -15,7 +15,6 @@ Done:
 - Keyboard arrow swapping.
 
 Bugs:
-- Upon a reset, the directionality of cells isn't properly restored.
 - The way the modes apply to paragraphs, and what happens when a paragraph
   is split or two paragraphs are joined is just a first hack, needs to be
   reviewed, adjusted, fixed properly.
diff --git a/src/bidi.cc b/src/bidi.cc
index 3f2f3030..003dcf00 100644
--- a/src/bidi.cc
+++ b/src/bidi.cc
@@ -175,9 +175,9 @@ void RingView::set_rows(vte::grid::row_t start, vte::grid::row_t len)
 void RingView::update()
 {
         vte::grid::row_t i = m_start;
-        const VteRowData *row_data = m_ring->index(m_start);
+        const VteRowData *row_data = m_ring->index_safe(m_start);
 
-        if (row_data->attr.bidi_flags & VTE_BIDI_IMPLICIT) {
+        if (row_data && (row_data->attr.bidi_flags & VTE_BIDI_IMPLICIT)) {
                 i = find_paragraph(m_start);
                 if (i == -1) {
                         i = explicit_paragraph(m_start, row_data->attr.bidi_flags & VTE_BIDI_RTL);
@@ -237,7 +237,7 @@ vte::grid::row_t RingView::explicit_paragraph(vte::grid::row_t row, bool rtl)
         while (row < m_start + m_len) {
                 explicit_line(row, rtl);
 
-                row_data = m_ring->index(row++);
+                row_data = m_ring->index_safe(row++);
                 if (row_data == nullptr || !row_data->attr.soft_wrapped)
                         break;
         }
@@ -256,7 +256,7 @@ vte::grid::row_t RingView::find_paragraph(vte::grid::row_t row)
         while (row-- > row_stop) {
                 if (row < _vte_ring_delta(m_ring))
                         return row + 1;
-                row_data = m_ring->index(row);
+                row_data = m_ring->index_safe(row);
                 if (row_data == nullptr || !row_data->attr.soft_wrapped)
                         return row + 1;
         }
@@ -267,7 +267,7 @@ vte::grid::row_t RingView::find_paragraph(vte::grid::row_t row)
  * Returns the row number after the paragraph or viewport (whichever ends first). */
 vte::grid::row_t RingView::paragraph(vte::grid::row_t row)
 {
-        const VteRowData *row_data = m_ring->index(row);
+        const VteRowData *row_data = m_ring->index_safe(row);
         if (row_data == nullptr) {
                 return explicit_paragraph(row, false);
         }
@@ -304,7 +304,7 @@ vte::grid::row_t RingView::paragraph(vte::grid::row_t row)
 
         /* Extract the paragraph's contents, omitting unused and fragment cells. */
         while (row < _vte_ring_next(m_ring)) {
-                row_data = m_ring->index(row);
+                row_data = m_ring->index_safe(row);
                 if (row_data == nullptr)
                         break;
 
@@ -388,7 +388,7 @@ vte::grid::row_t RingView::paragraph(vte::grid::row_t row)
                 bidirow->m_has_foreign = true;
                 bidirow->set_width(m_width);
 
-                row_data = m_ring->index(row);
+                row_data = m_ring->index_safe(row);
                 if (row_data == nullptr)
                         break;
 
diff --git a/src/ring.cc b/src/ring.cc
index 5f2fb87c..1f3e90d7 100644
--- a/src/ring.cc
+++ b/src/ring.cc
@@ -609,6 +609,15 @@ Ring::index(row_t position)
        return &m_cached_row;
 }
 
+VteRowData const*
+Ring::index_safe(row_t position)
+{
+        if (G_UNLIKELY (position < m_start || position >= m_end))
+                return nullptr;
+
+        return index(position);
+}
+
 /*
  * Returns the hyperlink idx at the given position.
  *
diff --git a/src/ring.hh b/src/ring.hh
index 06ed22fe..831a55da 100644
--- a/src/ring.hh
+++ b/src/ring.hh
@@ -73,6 +73,7 @@ public:
         //FIXMEchpe rename this to at()
         //FIXMEchpe use references not pointers
         VteRowData const* index(row_t position); /* const? */
+        VteRowData const* index_safe(row_t position);
         VteRowData* index_writable(row_t position);
 
         void hyperlink_maybe_gc(row_t increment);
diff --git a/src/vte.cc b/src/vte.cc
index bb0199d7..e44e1263 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -380,7 +380,7 @@ Terminal::invalidate_all()
 /* Find the row in the given position in the backscroll buffer.
  * Note that calling this method may invalidate the return value of
  * a previous find_row_data() call. */
-// FIXMEchpe replace this with a method on VteRing
+// FIXMEchpe replace this with a method on VteRing (index_safe())
 VteRowData const*
 Terminal::find_row_data(vte::grid::row_t row) const
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]