After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 568405 - Which is the correct replacement for g_strncasecmp, if that is deprecated?
Which is the correct replacement for g_strncasecmp, if that is deprecated?
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: docs
2.18.x
Other All
: Normal minor
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2009-01-20 10:27 UTC by Aleksander Morgado
Modified: 2013-03-18 21:40 UTC
See Also:
GNOME target: ---
GNOME version: 2.23/2.24


Attachments
elaborate on g_strncasecmp replacements (1.06 KB, patch)
2012-02-17 14:58 UTC, David King
none Details | Review

Description Aleksander Morgado 2009-01-20 10:27:24 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.
Comment 1 David King 2012-02-17 14:58:21 UTC
Created attachment 207855 [details] [review]
elaborate on g_strncasecmp replacements
Comment 2 Christian Persch 2013-03-18 17:18:03 UTC
+ * 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?
Comment 3 David King 2013-03-18 18:00:43 UTC
(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.
Comment 4 Dan Winship 2013-03-18 21:40:47 UTC
(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").