[template-glib] eval: add String.title() function
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [template-glib] eval: add String.title() function
- Date: Tue, 9 Aug 2016 21:30:02 +0000 (UTC)
commit 48f6d3dd6e17d3fee97718fc5f2d237208c277e4
Author: Christian Hergert <chergert redhat com>
Date: Tue Aug 9 14:26:51 2016 -0700
eval: add String.title() function
This turns something-like-this into Something Like This.
src/tmpl-expr-eval.c | 37 +++++++++++++++++++++++++++++++++++++
1 files changed, 37 insertions(+), 0 deletions(-)
---
diff --git a/src/tmpl-expr-eval.c b/src/tmpl-expr-eval.c
index 80137ae..b65ba29 100644
--- a/src/tmpl-expr-eval.c
+++ b/src/tmpl-expr-eval.c
@@ -557,6 +557,36 @@ cleanup:
return ret;
}
+static gchar *
+make_title (const gchar *str)
+{
+ g_auto(GStrv) parts = NULL;
+ GString *ret;
+
+ g_assert (str != NULL);
+
+ ret = g_string_new (NULL);
+
+ for (; *str; str = g_utf8_next_char (str))
+ {
+ gunichar ch = g_utf8_get_char (str);
+
+ if (!g_unichar_isalnum (ch))
+ {
+ if (ret->len && ret->str[ret->len - 1] != ' ')
+ g_string_append_c (ret, ' ');
+ continue;
+ }
+
+ if (ret->len && ret->str[ret->len - 1] != ' ')
+ g_string_append_unichar (ret, ch);
+ else
+ g_string_append_unichar (ret, g_unichar_toupper (ch));
+ }
+
+ return g_string_free (ret, FALSE);
+}
+
static gboolean
tmpl_expr_gi_call_eval (TmplExprGiCall *node,
TmplScope *scope,
@@ -599,6 +629,7 @@ tmpl_expr_gi_call_eval (TmplExprGiCall *node,
* "foo".casefold()
* "foo".reverse()
* "foo".len()
+ * "foo".title()
*/
if (FALSE) {}
else if (g_str_equal (node->name, "upper"))
@@ -631,6 +662,12 @@ tmpl_expr_gi_call_eval (TmplExprGiCall *node,
g_value_set_uint (return_value, strlen (str));
ret = TRUE;
}
+ else if (g_str_equal (node->name, "title"))
+ {
+ g_value_init (return_value, G_TYPE_STRING);
+ g_value_take_string (return_value, make_title (str));
+ ret = TRUE;
+ }
goto cleanup;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]