[devhelp] Link: simplify dh_link_belongs_to_page()



commit e6cbdc21c11b232d2e5e57ce9af1c06b2bdf6d99
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Jan 18 18:15:47 2018 +0100

    Link: simplify dh_link_belongs_to_page()
    
    Always case sensitive. It's a page ID, so the ID needs to be correct.

 src/dh-link.c           |   18 ++++--------------
 src/dh-link.h           |    5 ++---
 src/dh-search-context.c |    2 +-
 unit-tests/test-link.c  |   22 ++++++++--------------
 4 files changed, 15 insertions(+), 32 deletions(-)
---
diff --git a/src/dh-link.c b/src/dh-link.c
index eabd8b0..e394adb 100644
--- a/src/dh-link.c
+++ b/src/dh-link.c
@@ -2,7 +2,7 @@
 /*
  * Copyright (C) 2001-2002 Mikael Hallendal <micke imendio com>
  * Copyright (C) 2008 Imendio AB
- * Copyright (C) 2017 Sébastien Wilmet <swilmet gnome org>
+ * Copyright (C) 2017, 2018 Sébastien Wilmet <swilmet gnome org>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -337,7 +337,6 @@ dh_link_match_relative_url (DhLink      *link,
  * dh_link_belongs_to_page:
  * @link: a #DhLink.
  * @page_id: a page ID, i.e. the filename without its extension.
- * @case_sensitive: whether @page_id is case sensitive.
  *
  * This function permits to know if @link belongs to a certain page.
  *
@@ -353,12 +352,10 @@ dh_link_match_relative_url (DhLink      *link,
  */
 gboolean
 dh_link_belongs_to_page (DhLink      *link,
-                         const gchar *page_id,
-                         gboolean     case_sensitive)
+                         const gchar *page_id)
 {
         const gchar *relative_url;
         gsize page_id_len;
-        gboolean has_prefix;
 
         g_return_val_if_fail (link != NULL, FALSE);
         g_return_val_if_fail (link->relative_url != NULL, FALSE);
@@ -370,16 +367,9 @@ dh_link_belongs_to_page (DhLink      *link,
 
         page_id_len = strlen (page_id);
 
-        if (case_sensitive)
-                has_prefix = strncmp (relative_url, page_id, page_id_len) == 0;
-        else
-                has_prefix = g_ascii_strncasecmp (relative_url, page_id, page_id_len) == 0;
-
         /* Check that a file extension follows. */
-        if (has_prefix)
-                return relative_url[page_id_len] == '.';
-
-        return FALSE;
+        return (g_str_has_prefix (relative_url, page_id) &&
+                relative_url[page_id_len] == '.');
 }
 
 /**
diff --git a/src/dh-link.h b/src/dh-link.h
index ebfee00..802191c 100644
--- a/src/dh-link.h
+++ b/src/dh-link.h
@@ -2,7 +2,7 @@
 /*
  * Copyright (C) 2002 Mikael Hallendal <micke imendio com>
  * Copyright (C) 2008 Imendio AB
- * Copyright (C) 2017 Sébastien Wilmet <swilmet gnome org>
+ * Copyright (C) 2017, 2018 Sébastien Wilmet <swilmet gnome org>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -96,8 +96,7 @@ gboolean     dh_link_match_relative_url (DhLink        *link,
                                          const gchar   *relative_url);
 
 gboolean     dh_link_belongs_to_page    (DhLink        *link,
-                                         const gchar   *page_id,
-                                         gboolean       case_sensitive);
+                                         const gchar   *page_id);
 
 gchar *      dh_link_get_uri            (DhLink        *link);
 
diff --git a/src/dh-search-context.c b/src/dh-search-context.c
index 192d952..674a320 100644
--- a/src/dh-search-context.c
+++ b/src/dh-search-context.c
@@ -310,7 +310,7 @@ _dh_search_context_match_link (DhSearchContext *search,
 
         /* Filter by page? */
         if (search->page_id != NULL) {
-                if (!dh_link_belongs_to_page (link, search->page_id, TRUE))
+                if (!dh_link_belongs_to_page (link, search->page_id))
                         return FALSE;
 
                 if (search->keywords == NULL)
diff --git a/unit-tests/test-link.c b/unit-tests/test-link.c
index 6c77ff6..71d0d61 100644
--- a/unit-tests/test-link.c
+++ b/unit-tests/test-link.c
@@ -22,13 +22,10 @@
 static void
 check_belongs_to_page_book_link (DhLink *book_link)
 {
-        g_assert (dh_link_belongs_to_page (book_link, "index", TRUE));
-        g_assert (!dh_link_belongs_to_page (book_link, "Index", TRUE));
-        g_assert (dh_link_belongs_to_page (book_link, "Index", FALSE));
-        g_assert (!dh_link_belongs_to_page (book_link, "", TRUE));
-        g_assert (!dh_link_belongs_to_page (book_link, "kiwi", TRUE));
-        g_assert (!dh_link_belongs_to_page (book_link, "", FALSE));
-        g_assert (!dh_link_belongs_to_page (book_link, "kiwi", FALSE));
+        g_assert (dh_link_belongs_to_page (book_link, "index"));
+        g_assert (!dh_link_belongs_to_page (book_link, "Index"));
+        g_assert (!dh_link_belongs_to_page (book_link, ""));
+        g_assert (!dh_link_belongs_to_page (book_link, "kiwi"));
 }
 
 static void
@@ -58,13 +55,10 @@ test_belongs_to_page (void)
                             "dh_link_ref",
                             "DhLink.html#dh-link-ref");
 
-        g_assert (dh_link_belongs_to_page (link, "DhLink", TRUE));
-        g_assert (!dh_link_belongs_to_page (link, "dhlink", TRUE));
-        g_assert (dh_link_belongs_to_page (link, "dhlink", FALSE));
-        g_assert (!dh_link_belongs_to_page (link, "", TRUE));
-        g_assert (!dh_link_belongs_to_page (link, "kiwi", TRUE));
-        g_assert (!dh_link_belongs_to_page (link, "", FALSE));
-        g_assert (!dh_link_belongs_to_page (link, "kiwi", FALSE));
+        g_assert (dh_link_belongs_to_page (link, "DhLink"));
+        g_assert (!dh_link_belongs_to_page (link, "dhlink"));
+        g_assert (!dh_link_belongs_to_page (link, ""));
+        g_assert (!dh_link_belongs_to_page (link, "kiwi"));
 
         dh_link_unref (book_link);
         dh_link_unref (link);


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