GNOME Bugzilla – Bug 685878
Add g_strdiff() or similar NULL-safe string comparator
Last modified: 2013-04-16 12:21:28 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 :-)
Not worth it, imo We have too many trivial oneliner apis already
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.
Created attachment 241573 [details] [review] Add g_strdiff Copied from telepathy-glib's tp_strdiff().
I agree. This is excessively trivial.
linux-kernel had a debate about an equivalent there, I believe Linus came down against it.
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...
Even QString::isEmpty()
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.