[lasem] str: add lsm_str_consolidate function.
- From: Emmanuel Pacaud <emmanuel src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [lasem] str: add lsm_str_consolidate function.
- Date: Sun, 4 Nov 2012 23:18:28 +0000 (UTC)
commit f3a9d2b681926020d0104898c9f6062cdbd22403
Author: Emmanuel Pacaud <emmanuel gnome org>
Date: Mon Nov 5 00:17:55 2012 +0100
str: add lsm_str_consolidate function.
src/lsmstr.h | 38 ++++++++++++++++++++++++++++++++++++++
tests/Makefile.am | 5 ++++-
tests/str.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 87 insertions(+), 1 deletions(-)
---
diff --git a/src/lsmstr.h b/src/lsmstr.h
index 4e219c8..466915d 100644
--- a/src/lsmstr.h
+++ b/src/lsmstr.h
@@ -71,6 +71,44 @@ lsm_str_skip_colon_and_spaces (char **str)
(*str)++;
}
+/**
+ * lsm_str_consolidate:
+ * @str: a utf8 string
+ *
+ * Removes trailing and heading ascii spaces from str, and reduce consecutive spaces to one space.
+ */
+
+static inline void
+lsm_str_consolidate (char *str)
+{
+ char *to_ptr;
+ char *from_ptr;
+
+ if (str == NULL)
+ return;
+
+ from_ptr = str;
+ to_ptr = str;
+ while (*from_ptr != '\0') {
+ if (g_ascii_isspace (*from_ptr)) {
+ if (to_ptr != str &&
+ *(to_ptr - 1) != ' ') {
+ *to_ptr = ' ';
+ to_ptr++;
+ }
+ } else {
+ *to_ptr = *from_ptr;
+ to_ptr++;
+ }
+ from_ptr++;
+ }
+
+ if (to_ptr != str && *(to_ptr - 1) == ' ')
+ *(to_ptr - 1) = '\0';
+ else
+ *to_ptr = '\0';
+}
+
G_END_DECLS
#endif
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c4b429a..aa99932 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -12,10 +12,13 @@ noinst_PROGRAMS = lsm-test
lsm_test_SOURCES = lsmtest.c
lsm_test_LDADD = $(test_progs_ldadd)
-TEST_PROGS += dom suite
+TEST_PROGS += dom suite str
noinst_PROGRAMS += $(TEST_PROGS)
+str_SOURCES = str.c
+str_LDADD = $(test_progs_ldadd)
+
dom_SOURCES = dom.c
dom_LDADD = $(test_progs_ldadd)
diff --git a/tests/str.c b/tests/str.c
new file mode 100644
index 0000000..0b8064e
--- /dev/null
+++ b/tests/str.c
@@ -0,0 +1,45 @@
+#include <glib.h>
+#include <lsmstr.h>
+
+static void
+str_consolidate_test (void)
+{
+ char *string;
+
+ string = g_strdup (" consolidate \n \t test \t \r a b c \n ");
+ lsm_str_consolidate (string);
+ g_assert_cmpstr (string, ==, "consolidate test a b c");
+ g_free (string);
+
+ string = g_strdup (" ");
+ lsm_str_consolidate (string);
+ g_assert_cmpstr (string, ==, "");
+ g_free (string);
+
+ string = g_strdup ("");
+ lsm_str_consolidate (string);
+ g_assert_cmpstr (string, ==, "");
+ g_free (string);
+
+ string = NULL;
+ lsm_str_consolidate (string);
+ g_assert (string == NULL);
+}
+
+int
+main (int argc, char *argv[])
+{
+ int result;
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/str/str-consolidate", str_consolidate_test);
+
+ g_type_init ();
+
+ result = g_test_run();
+
+ lsm_shutdown ();
+
+ return result;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]