GNOME Bugzilla – Bug 568405
Which is the correct replacement for g_strncasecmp, if that is deprecated?
Last modified: 2013-03-18 21:40:47 UTC
Documentation Section: GLib Reference Manual->GLib Utilities->String Utility Functions->g_strncasecmp "There are therefore two replacement functions: g_ascii_strncasecmp(), which only works on ASCII and is not locale-sensitive, and g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8." Correct version: It seems clear for me that g_utf_casefold is not a replacement for g_strncasecmp in any case. At least, not that function alone. My understanding is that after g_utf_casefold() on the two strings to be compared, you should use strcmp() at least to get the strings compared (as both strings are already case-folded). Other information: What about a new function g_utf8_strncasecmp()? That would be a real replacement.
Created attachment 207855 [details] [review] elaborate on g_strncasecmp replacements
+ * g_utf8_casefold() followed by strcmp() on the resulting strings, which is Is this really correct? AFAIK you should use g_utf8_collate with the casefolded strings, _not_ strcmp. I think you've confounded this with g_utf8_collate_key() which _does_ allow using strcmp afterwards?
(In reply to comment #2) > AFAIK you should use g_utf8_collate with the casefolded > strings, _not_ strcmp. The g_utf8_casefold() documentation is a bit vague, but says: "Converts a string into a form that is independent of case. The result will not correspond to any particular case, but can be compared for equality or ordered with the results of calling g_utf8_casefold() on other strings." If the intention of the g_utf8_casefold() followed by strcmp() is to get case-insensitive UTF-8 sorting, without depending on the current locale for the sort order, then that approach would seem correct. Using g_utf8_collate[_key]() would make the sort order depend on the current locale, which was the reasoning behind the deprecation of g_strncasecmp(), I think.
(In reply to comment #3) > If the intention of the > g_utf8_casefold() followed by strcmp() is to get case-insensitive UTF-8 > sorting, without depending on the current locale for the sort order no, g_utf8_casefold() is to get case-insensitive UTF-8 *matching*. The order that casefolded strings sort in is meaningless. Sorting actual words/names without depending on the current locale isn't possible, since different locales define the alphabet in different orders. (In reply to comment #2) > + * g_utf8_casefold() followed by strcmp() on the resulting strings, which is > > Is this really correct? AFAIK you should use g_utf8_collate with the casefolded > strings, _not_ strcmp. Yeah, g_utf8_collate() is right for sorting, but I think it should already be case-insensitive (at least to the extent that, eg "B" falls between "a" and "c", but maybe "b" always comes before "B").