[tepl] FileMetadata/Utils: move function
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] FileMetadata/Utils: move function
- Date: Sat, 18 Apr 2020 17:20:22 +0000 (UTC)
commit 9a989447fcca138d7a9f4e62364ee50ef51c12d2
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sat Apr 18 01:07:18 2020 +0200
FileMetadata/Utils: move function
docs/reference/tepl-sections.txt | 1 -
tepl/tepl-file-metadata.c | 61 ++++++++++++++++++++++++++++++++++++++--
tepl/tepl-file-metadata.h | 3 ++
tepl/tepl-utils.c | 56 ------------------------------------
tepl/tepl-utils.h | 2 --
testsuite/meson.build | 1 +
testsuite/test-file-metadata.c | 54 +++++++++++++++++++++++++++++++++++
testsuite/test-utils.c | 32 +++------------------
8 files changed, 120 insertions(+), 90 deletions(-)
---
diff --git a/docs/reference/tepl-sections.txt b/docs/reference/tepl-sections.txt
index d1b1554..bc2acf9 100644
--- a/docs/reference/tepl-sections.txt
+++ b/docs/reference/tepl-sections.txt
@@ -415,7 +415,6 @@ 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-file-metadata.c b/tepl/tepl-file-metadata.c
index ebf7ca0..b3d4646 100644
--- a/tepl/tepl-file-metadata.c
+++ b/tepl/tepl-file-metadata.c
@@ -83,7 +83,7 @@ tepl_file_metadata_get (TeplFileMetadata *metadata,
const gchar *key)
{
g_return_val_if_fail (TEPL_IS_FILE_METADATA (metadata), NULL);
- g_return_val_if_fail (tepl_utils_metadata_key_is_valid (key), NULL);
+ g_return_val_if_fail (_tepl_file_metadata_key_is_valid (key), NULL);
set_current_atime (metadata);
@@ -96,7 +96,7 @@ tepl_file_metadata_set (TeplFileMetadata *metadata,
const gchar *value)
{
g_return_if_fail (TEPL_IS_FILE_METADATA (metadata));
- g_return_if_fail (tepl_utils_metadata_key_is_valid (key));
+ g_return_if_fail (_tepl_file_metadata_key_is_valid (key));
g_return_if_fail (value == NULL || g_utf8_validate (value, -1, NULL));
set_current_atime (metadata);
@@ -152,7 +152,7 @@ _tepl_file_metadata_insert_entry (TeplFileMetadata *metadata,
const gchar *value)
{
g_return_if_fail (TEPL_IS_FILE_METADATA (metadata));
- g_return_if_fail (tepl_utils_metadata_key_is_valid (key));
+ g_return_if_fail (_tepl_file_metadata_key_is_valid (key));
g_return_if_fail (g_utf8_validate (value, -1, NULL));
g_hash_table_replace (metadata->priv->hash_table,
@@ -241,3 +241,58 @@ _tepl_file_metadata_append_xml_to_string (TeplFileMetadata *metadata,
g_free (uri);
g_free (uri_escaped);
}
+
+static gboolean
+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_file_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 Tepl metadata 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.
+ */
+gboolean
+_tepl_file_metadata_key_is_valid (const gchar *key)
+{
+ const gchar *p;
+
+ if (key == NULL || key[0] == '\0')
+ {
+ return FALSE;
+ }
+
+ for (p = key; *p != '\0'; p++)
+ {
+ gchar cur_char = *p;
+
+ if (!key_char_is_valid (cur_char))
+ {
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
diff --git a/tepl/tepl-file-metadata.h b/tepl/tepl-file-metadata.h
index 2896a91..5774ee2 100644
--- a/tepl/tepl-file-metadata.h
+++ b/tepl/tepl-file-metadata.h
@@ -86,6 +86,9 @@ void _tepl_file_metadata_append_xml_to_string (TeplFileMetadata
*metadata,
GFile *location,
GString *string);
+G_GNUC_INTERNAL
+gboolean _tepl_file_metadata_key_is_valid (const gchar *key);
+
G_END_DECLS
#endif /* TEPL_FILE_METADATA_H */
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index b705d41..6e86ce3 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -174,62 +174,6 @@ 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 Tepl metadata API. TODO: update
- *
- * 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 b43877b..b66dd56 100644
--- a/tepl/tepl-utils.h
+++ b/tepl/tepl-utils.h
@@ -40,8 +40,6 @@ 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/meson.build b/testsuite/meson.build
index b3a1f63..197615f 100644
--- a/testsuite/meson.build
+++ b/testsuite/meson.build
@@ -5,6 +5,7 @@ unit_tests = [
'test-file',
'test-file-content',
'test-file-loader',
+ 'test-file-metadata',
'test-file-saver',
'test-fold-region',
'test-info-bar',
diff --git a/testsuite/test-file-metadata.c b/testsuite/test-file-metadata.c
new file mode 100644
index 0000000..4ab0785
--- /dev/null
+++ b/testsuite/test-file-metadata.c
@@ -0,0 +1,54 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2020 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Tepl is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <tepl/tepl.h>
+
+static void
+test_key_is_valid (void)
+{
+ g_assert_true (_tepl_file_metadata_key_is_valid ("gedit-spell-checking-language"));
+ g_assert_true (_tepl_file_metadata_key_is_valid ("gCSVedit_column_delimiter"));
+ g_assert_true (_tepl_file_metadata_key_is_valid ("Fourty_Two-1337"));
+ g_assert_true (_tepl_file_metadata_key_is_valid ("1337-beginning-with-digit"));
+ g_assert_true (_tepl_file_metadata_key_is_valid ("a"));
+ g_assert_true (_tepl_file_metadata_key_is_valid ("9"));
+
+ g_assert_true (!_tepl_file_metadata_key_is_valid (NULL));
+ g_assert_true (!_tepl_file_metadata_key_is_valid (""));
+ g_assert_true (!_tepl_file_metadata_key_is_valid ("metadata::gedit-spell-checking-language"));
+ g_assert_true (!_tepl_file_metadata_key_is_valid ("foo:bar"));
+ g_assert_true (!_tepl_file_metadata_key_is_valid ("foo::bar"));
+ g_assert_true (!_tepl_file_metadata_key_is_valid ("Évolution-UTF-8"));
+ g_assert_true (!_tepl_file_metadata_key_is_valid ("a space"));
+ g_assert_true (!_tepl_file_metadata_key_is_valid ("\t"));
+
+ g_assert_true (!g_utf8_validate ("\xFF", -1, NULL));
+ g_assert_true (!_tepl_file_metadata_key_is_valid ("\xFF"));
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/file_metadata/key_is_valid", test_key_is_valid);
+
+ return g_test_run ();
+}
diff --git a/testsuite/test-utils.c b/testsuite/test-utils.c
index 2d4224a..82feb8d 100644
--- a/testsuite/test-utils.c
+++ b/testsuite/test-utils.c
@@ -1,7 +1,7 @@
/*
* This file is part of Tepl, a text editor library.
*
- * Copyright 2016 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2016-2020 - Sébastien Wilmet <swilmet gnome org>
*
* Tepl is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
@@ -66,29 +66,6 @@ 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)
{
@@ -220,16 +197,15 @@ test_get_fallback_basename_for_display (void)
g_free (basename);
}
-gint
-main (gint argc,
- gchar **argv)
+int
+main (int argc,
+ char **argv)
{
g_test_init (&argc, &argv, NULL);
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]