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 685878 - Add g_strdiff() or similar NULL-safe string comparator
Add g_strdiff() or similar NULL-safe string comparator
Status: RESOLVED WONTFIX
Product: glib
Classification: Platform
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks: 685880
 
 
Reported: 2012-10-10 12:33 UTC by Xavier Claessens
Modified: 2013-04-16 12:21 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add g_strdiff (2.05 KB, patch)
2013-04-15 15:15 UTC, Xavier Claessens
none Details | Review

Description Xavier Claessens 2012-10-10 12:33:40 UTC
It is very common to us strcmp, or g_strcmp0 to know if 2 strings are equal. But its return value is inconvenient since it returns 0 (FALSE) when they are equal.

GLib already has g_str_equal() but unfortunately it is not NULL-safe. In tp-glib we have a trivial tp_strdiff():

gboolean
tp_strdiff (const gchar *left, const gchar *right)
{
  return g_strcmp0 (left, right) != 0;
}

I would like to include this in glib, as g_strdiff, or g_str_equal0(), or any other sensible name :-)
Comment 1 Matthias Clasen 2012-10-11 20:24:30 UTC
Not worth it, imo
We have too many trivial oneliner apis already
Comment 2 Xavier Claessens 2012-10-12 09:38:20 UTC
IMO that's the kind of one liner that does not cost anything and makes code easier, safer and more readable.

For the record:
telepathy-glib$ git grep tp_strdiff | wc -l
219
empathy$ git grep tp_strdiff | wc -l
259

So that's really something we use a lot, and I'm sure most glib projects reinvented it, or had issues with missing ==0 when using strcmp-like functions.
Comment 3 Xavier Claessens 2013-04-15 15:15:07 UTC
Created attachment 241573 [details] [review]
Add g_strdiff

Copied from telepathy-glib's tp_strdiff().
Comment 4 Allison Karlitskaya (desrt) 2013-04-16 11:52:59 UTC
I agree.  This is excessively trivial.
Comment 5 Colin Walters 2013-04-16 12:10:09 UTC
linux-kernel had a debate about an equivalent there, I believe Linus came down against it.
Comment 6 Xavier Claessens 2013-04-16 12:17:05 UTC
what? in which world is it better to read if (str != NULL || str[0] == '\0') rather than if (g_str_empty (str)) ? It is not about having something trivial, but readable. All projects that I worked with have somewhere a util.h with it...
Comment 7 Xavier Claessens 2013-04-16 12:17:48 UTC
Even QString::isEmpty()
Comment 8 Xavier Claessens 2013-04-16 12:21:28 UTC
gash, sorry, mixed the bugs :(

Actually my argument for this one, is that last week, AGAIN, I had to fix a bug where a if (strcmp0()) forgot the == 0.