add g_utf8_strtitle
Submitted by Eric Albright
Link to original bug (#498065)
Description
gchar *g_utf8_strtitle (gchar *s);
This should return a copy of the string s where the first letter of the string is titlecased (as defined by unicode) and all following characters are lowercase.
This is necessary to handle U+01f2 "Dz" LATIN CAPITAL LETTER D WITH SMALL LETTER Z.
While this can be done with the current public api (but it is easy to get wrong since g_unichar_totitle is not locale sensitive).
http://bugzilla.gnome.org/show_bug.cgi?id=416390#c4 mentions g_utf8_strtitle as being problematic because "titlecase is not well defined: we just capitalize the first letter of each word, but sometimes people would like it to follow localized capitalization rules (e.g Phantom of the Opera vs Phantom Of The Opera, Murders in the Rue Morgue vs Murders In The Rue Morgue, etc)". It is true that there may be language specific overrides that need to happen, however by defining the semantics to titlecase the string instead of each word in the string, it moves that burden to the application where it belongs. Thus either of the choices above are possible and would delegate the titlecasing functionality to glib after splitting the string on spaces (and checking for short words like in, the, on, that aren't the first word of the string etc.)
The following is my implementation using glib public api but it could be simplified if within glib.
gchar* g_utf8_strtitle(const gcharstr, gssize len) { gunichar title_case_char; gchar result; gchar* upperStr, * upperTail, * lowerTail; gchar title_case_utf8[7]; gint utf8len;
upperStr = g_utf8_strup(str, len); /* for locale sensitive casing */
title_case_char = g_unichar_totitle(g_utf8_get_char(upperStr));
utf8len = g_unichar_to_utf8(title_case_char, title_case_utf8);
title_case_utf8[utf8len] = '\0';
upperTail = g_utf8_next_char(upperStr);
lowerTail = g_utf8_strdown(upperTail, -1);
result = g_strconcat(title_case_utf8,
lowerTail,
NULL);
g_free(upperStr);
g_free(lowerTail);
return result;
}
Version: 2.14.x