From f39d211021de7ebe4c0066b1a54272a4880e17be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Wilmet?= Date: Fri, 15 Aug 2014 20:54:01 +0200 Subject: [PATCH] GtkTextView: various code clean-ups - only one blank line is enough to separate code sections. - the 'signals' variable was in the middle of function prototypes. - compare pointers to NULL in some conditions ("if(blah) should be used only if blah is a boolean variable). It makes the code clearer. - various other things. --- gtk/gtktextbuffer.c | 51 +++++++++++++++++++------------------------- gtk/gtktextiter.c | 52 ++++++++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 56 deletions(-) diff --git a/gtk/gtktextbuffer.c b/gtk/gtktextbuffer.c index 01a068c77b..38620ef20f 100644 --- a/gtk/gtktextbuffer.c +++ b/gtk/gtktextbuffer.c @@ -40,7 +40,6 @@ #include "gtkprivate.h" #include "gtkintl.h" - /** * SECTION:gtktextbuffer * @Short_description: Stores attributed text for display in a GtkTextView @@ -53,7 +52,6 @@ * types related to the text widget and how they work together. */ - typedef struct _GtkTextLogAttrCache GtkTextLogAttrCache; struct _GtkTextBufferPrivate @@ -81,7 +79,6 @@ struct _GtkTextBufferPrivate guint has_selection : 1; }; - typedef struct _ClipboardRequest ClipboardRequest; struct _ClipboardRequest @@ -161,8 +158,6 @@ static GtkTextBuffer *create_clipboard_contents_buffer (GtkTextBuffer *buffer); static void gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer); -static guint signals[LAST_SIGNAL] = { 0 }; - static void gtk_text_buffer_set_property (GObject *object, guint prop_id, const GValue *value, @@ -174,6 +169,8 @@ static void gtk_text_buffer_get_property (GObject *object, static void gtk_text_buffer_notify (GObject *object, GParamSpec *pspec); +static guint signals[LAST_SIGNAL] = { 0 }; + G_DEFINE_TYPE_WITH_PRIVATE (GtkTextBuffer, gtk_text_buffer, G_TYPE_OBJECT) static void @@ -3892,7 +3889,6 @@ gtk_text_buffer_backspace (GtkTextBuffer *buffer, GtkTextIter end; gboolean retval = FALSE; const PangoLogAttr *attrs; - int offset; gboolean backspace_deletes_character; g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE); @@ -3906,9 +3902,9 @@ gtk_text_buffer_backspace (GtkTextBuffer *buffer, /* For no good reason, attrs is NULL for the empty last line in * a buffer. Special case that here. (#156164) */ - if (attrs) + if (attrs != NULL) { - offset = gtk_text_iter_get_line_offset (&start); + gint offset = gtk_text_iter_get_line_offset (&start); backspace_deletes_character = attrs[offset].backspace_deletes_character; } else @@ -4289,24 +4285,23 @@ struct _GtkTextLogAttrCache static void free_log_attr_cache (GtkTextLogAttrCache *cache) { - gint i = 0; - while (i < ATTR_CACHE_SIZE) - { - g_free (cache->entries[i].attrs); - ++i; - } + gint i; + + for (i = 0; i < ATTR_CACHE_SIZE; i++) + g_free (cache->entries[i].attrs); + g_slice_free (GtkTextLogAttrCache, cache); } static void clear_log_attr_cache (GtkTextLogAttrCache *cache) { - gint i = 0; - while (i < ATTR_CACHE_SIZE) + gint i; + + for (i = 0; i < ATTR_CACHE_SIZE; i++) { g_free (cache->entries[i].attrs); cache->entries[i].attrs = NULL; - ++i; } } @@ -4332,9 +4327,9 @@ compute_log_attrs (const GtkTextIter *iter, g_assert (char_len > 0); - if (char_lenp) + if (char_lenp != NULL) *char_lenp = char_len; - + attrs = g_new (PangoLogAttr, char_len + 1); /* FIXME we need to follow PangoLayout and allow different language @@ -4352,7 +4347,7 @@ compute_log_attrs (const GtkTextIter *iter, /* The return value from this is valid until you call this a second time. */ -const PangoLogAttr* +const PangoLogAttr * _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer, const GtkTextIter *anywhere_in_line, gint *char_len) @@ -4371,7 +4366,7 @@ _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer, if (gtk_text_iter_is_end (anywhere_in_line) && gtk_text_iter_get_line_offset (anywhere_in_line) == 0) { - if (char_len) + if (char_len != NULL) *char_len = 0; return NULL; } @@ -4395,19 +4390,17 @@ _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer, cache = priv->log_attr_cache; line = gtk_text_iter_get_line (anywhere_in_line); - i = 0; - while (i < ATTR_CACHE_SIZE) + for (i = 0; i < ATTR_CACHE_SIZE; i++) { - if (cache->entries[i].attrs && + if (cache->entries[i].attrs != NULL && cache->entries[i].line == line) { - if (char_len) + if (char_len != NULL) *char_len = cache->entries[i].char_len; return cache->entries[i].attrs; } - ++i; } - + /* Not in cache; open up the first cache entry */ g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs); @@ -4418,9 +4411,9 @@ _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer, cache->entries[0].attrs = compute_log_attrs (anywhere_in_line, &cache->entries[0].char_len); - if (char_len) + if (char_len != NULL) *char_len = cache->entries[0].char_len; - + return cache->entries[0].attrs; } diff --git a/gtk/gtktextiter.c b/gtk/gtktextiter.c index f215d1e2ce..7b58cb470d 100644 --- a/gtk/gtktextiter.c +++ b/gtk/gtktextiter.c @@ -2911,9 +2911,9 @@ find_word_end_func (const PangoLogAttr *attrs, static gboolean is_word_end_func (const PangoLogAttr *attrs, - gint offset, - gint min_offset, - gint len) + gint offset, + gint min_offset, + gint len) { return attrs[offset].is_word_end; } @@ -2945,18 +2945,18 @@ find_word_start_func (const PangoLogAttr *attrs, static gboolean is_word_start_func (const PangoLogAttr *attrs, - gint offset, - gint min_offset, - gint len) + gint offset, + gint min_offset, + gint len) { return attrs[offset].is_word_start; } static gboolean inside_word_func (const PangoLogAttr *attrs, - gint offset, - gint min_offset, - gint len) + gint offset, + gint min_offset, + gint len) { /* Find next word start or end */ while (offset >= min_offset && @@ -2998,9 +2998,9 @@ find_sentence_end_func (const PangoLogAttr *attrs, static gboolean is_sentence_end_func (const PangoLogAttr *attrs, - gint offset, - gint min_offset, - gint len) + gint offset, + gint min_offset, + gint len) { return attrs[offset].is_sentence_end; } @@ -3032,18 +3032,18 @@ find_sentence_start_func (const PangoLogAttr *attrs, static gboolean is_sentence_start_func (const PangoLogAttr *attrs, - gint offset, - gint min_offset, - gint len) + gint offset, + gint min_offset, + gint len) { return attrs[offset].is_sentence_start; } static gboolean inside_sentence_func (const PangoLogAttr *attrs, - gint offset, - gint min_offset, - gint len) + gint offset, + gint min_offset, + gint len) { /* Find next sentence start or end */ while (offset >= min_offset && @@ -3059,7 +3059,7 @@ test_log_attrs (const GtkTextIter *iter, { gint char_len; const PangoLogAttr *attrs; - int offset; + gint offset; gboolean result = FALSE; g_return_val_if_fail (iter != NULL, FALSE); @@ -3071,12 +3071,11 @@ test_log_attrs (const GtkTextIter *iter, /* char_len may be 0 and attrs will be NULL if so, if * iter is the end iter and the last line is empty. - * + * * offset may be equal to char_len, since attrs contains an entry - * for one past the end + * for one past the end. */ - - if (attrs && offset <= char_len) + if (attrs != NULL && offset <= char_len) result = (* func) (attrs, offset, 0, char_len); return result; @@ -3090,7 +3089,7 @@ find_line_log_attrs (const GtkTextIter *iter, { gint char_len; const PangoLogAttr *attrs; - int offset; + gint offset; gboolean result = FALSE; g_return_val_if_fail (iter != NULL, FALSE); @@ -3101,10 +3100,9 @@ find_line_log_attrs (const GtkTextIter *iter, offset = gtk_text_iter_get_line_offset (iter); /* char_len may be 0 and attrs will be NULL if so, if - * iter is the end iter and the last line is empty + * iter is the end iter and the last line is empty. */ - - if (attrs) + if (attrs != NULL) result = (* func) (attrs, offset, char_len, found_offset, already_moved_initially);