[tepl] Utils: add metadata_key_is_valid()



commit 2fecb2e2431eff844cd3b60a591d0223eaf7cc2b
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Apr 8 00:00:51 2020 +0200

    Utils: add metadata_key_is_valid()

 docs/reference/tepl-sections.txt |  1 +
 tepl/tepl-utils.c                | 56 ++++++++++++++++++++++++++++++++++++++++
 tepl/tepl-utils.h                |  2 ++
 testsuite/test-utils.c           | 24 +++++++++++++++++
 4 files changed, 83 insertions(+)
---
diff --git a/docs/reference/tepl-sections.txt b/docs/reference/tepl-sections.txt
index 736dce8..863e1dc 100644
--- a/docs/reference/tepl-sections.txt
+++ b/docs/reference/tepl-sections.txt
@@ -415,6 +415,7 @@ tepl_notebook_get_type
 tepl_utils_str_middle_truncate
 tepl_utils_str_end_truncate
 tepl_utils_str_replace
+tepl_utils_metadata_key_is_valid
 tepl_utils_get_file_extension
 tepl_utils_get_file_shortname
 tepl_utils_replace_home_dir_with_tilde
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index bc529f5..097c954 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -174,6 +174,62 @@ tepl_utils_str_replace (const gchar *string,
        return ret;
 }
 
+static gboolean
+metadata_key_char_is_valid (gchar ch)
+{
+       /* At the time of writing this, the GIO API doesn't document the
+        * requirements for valid attribute names. See the docs of
+        * g_file_query_info() for example. Clearly '*' and ',' must not be used
+        * because they serve to query several attributes. ':' is used in "::"
+        * to separate the namespace from the attribute name, I'm not sure that
+        * there can be several nested namespaces like in
+        * "metadata::gCSVedit::delimiter"; in case of doubt it's better not to
+        * support it by not allowing ':'.
+        */
+       return (g_ascii_isalnum (ch) || ch == '-' || ch == '_');
+}
+
+/**
+ * tepl_utils_metadata_key_is_valid:
+ * @metadata_key: (nullable): a string, or %NULL.
+ *
+ * Returns whether @metadata_key is a valid string that can be used as a
+ * metadata key when using the #TeplFileMetadata API.
+ *
+ * It returns %TRUE only if @metadata_key is a non-empty string containing only
+ * ASCII alphanumeric characters (see g_ascii_isalnum()), `"-"` (dash) or `"_"`
+ * (underscore).
+ *
+ * Examples of valid metadata keys:
+ * - `"gedit-spell-checking-language"`
+ * - `"gCSVedit_column_delimiter"`
+ *
+ * Returns: whether @metadata_key is valid.
+ * Since: 5.0
+ */
+gboolean
+tepl_utils_metadata_key_is_valid (const gchar *metadata_key)
+{
+       const gchar *p;
+
+       if (metadata_key == NULL || metadata_key[0] == '\0')
+       {
+               return FALSE;
+       }
+
+       for (p = metadata_key; *p != '\0'; p++)
+       {
+               gchar cur_char = *p;
+
+               if (!metadata_key_char_is_valid (cur_char))
+               {
+                       return FALSE;
+               }
+       }
+
+       return TRUE;
+}
+
 static gint
 get_extension_position (const gchar *filename)
 {
diff --git a/tepl/tepl-utils.h b/tepl/tepl-utils.h
index 4e7a559..2c89f83 100644
--- a/tepl/tepl-utils.h
+++ b/tepl/tepl-utils.h
@@ -40,6 +40,8 @@ gchar *               tepl_utils_str_replace                          (const gchar *string,
                                                                 const gchar *search,
                                                                 const gchar *replacement);
 
+gboolean       tepl_utils_metadata_key_is_valid                (const gchar *metadata_key);
+
 /* File utilities */
 
 gchar *                tepl_utils_get_file_extension                   (const gchar *filename);
diff --git a/testsuite/test-utils.c b/testsuite/test-utils.c
index 4cf63d0..2d4224a 100644
--- a/testsuite/test-utils.c
+++ b/testsuite/test-utils.c
@@ -66,6 +66,29 @@ test_str_replace (void)
        g_free (result);
 }
 
+static void
+test_metadata_key_is_valid (void)
+{
+       g_assert_true (tepl_utils_metadata_key_is_valid ("gedit-spell-checking-language"));
+       g_assert_true (tepl_utils_metadata_key_is_valid ("gCSVedit_column_delimiter"));
+       g_assert_true (tepl_utils_metadata_key_is_valid ("Fourty_Two-1337"));
+       g_assert_true (tepl_utils_metadata_key_is_valid ("1337-beginning-with-digit"));
+       g_assert_true (tepl_utils_metadata_key_is_valid ("a"));
+       g_assert_true (tepl_utils_metadata_key_is_valid ("9"));
+
+       g_assert_true (!tepl_utils_metadata_key_is_valid (NULL));
+       g_assert_true (!tepl_utils_metadata_key_is_valid (""));
+       g_assert_true (!tepl_utils_metadata_key_is_valid ("metadata::gedit-spell-checking-language"));
+       g_assert_true (!tepl_utils_metadata_key_is_valid ("foo:bar"));
+       g_assert_true (!tepl_utils_metadata_key_is_valid ("foo::bar"));
+       g_assert_true (!tepl_utils_metadata_key_is_valid ("Évolution-UTF-8"));
+       g_assert_true (!tepl_utils_metadata_key_is_valid ("a space"));
+       g_assert_true (!tepl_utils_metadata_key_is_valid ("\t"));
+
+       g_assert_true (!g_utf8_validate ("\xFF", -1, NULL));
+       g_assert_true (!tepl_utils_metadata_key_is_valid ("\xFF"));
+}
+
 static void
 test_get_file_extension (void)
 {
@@ -206,6 +229,7 @@ main (gint    argc,
        g_test_add_func ("/utils/str-middle-truncate", test_str_middle_truncate);
        g_test_add_func ("/utils/str-end-truncate", test_str_end_truncate);
        g_test_add_func ("/utils/str-replace", test_str_replace);
+       g_test_add_func ("/utils/metadata-key-is-valid", test_metadata_key_is_valid);
        g_test_add_func ("/utils/get-file-extension", test_get_file_extension);
        g_test_add_func ("/utils/get-file-shortname", test_get_file_shortname);
        g_test_add_func ("/utils/replace-home-dir-with-tilde", test_replace_home_dir_with_tilde);


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