mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Simplify string comparison
This is based on Linus' idea on the mailing list. Treat NULL strings and empty strings as identical. Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
024420a60d
commit
0c836ebc47
3 changed files with 12 additions and 15 deletions
6
dive.h
6
dive.h
|
@ -7,6 +7,7 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <zip.h>
|
#include <zip.h>
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
/* Windows has no MIN/MAX macros - so let's just roll our own */
|
/* Windows has no MIN/MAX macros - so let's just roll our own */
|
||||||
#define MIN(x, y) ({ \
|
#define MIN(x, y) ({ \
|
||||||
|
@ -23,6 +24,11 @@
|
||||||
|
|
||||||
#define IS_FP_SAME(_a, _b) (fabs((_a) - (_b)) < 0.000001 * MAX(fabs(_a), fabs(_b)))
|
#define IS_FP_SAME(_a, _b) (fabs((_a) - (_b)) < 0.000001 * MAX(fabs(_a), fabs(_b)))
|
||||||
|
|
||||||
|
static inline int same_string(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
return !strcmp(a ? : "", b ? : "");
|
||||||
|
}
|
||||||
|
|
||||||
#include <libxml/tree.h>
|
#include <libxml/tree.h>
|
||||||
#include <libxslt/transform.h>
|
#include <libxslt/transform.h>
|
||||||
|
|
||||||
|
|
10
equipment.c
10
equipment.c
|
@ -71,14 +71,6 @@ bool cylinder_none(void *_data)
|
||||||
return cylinder_nodata(cyl) && cylinder_nosamples(cyl);
|
return cylinder_nodata(cyl) && cylinder_nosamples(cyl);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* descriptions are equal if they are both NULL or both non-NULL
|
|
||||||
and the same text */
|
|
||||||
static bool description_equal(const char *desc1, const char *desc2)
|
|
||||||
{
|
|
||||||
return ((!desc1 && !desc2) ||
|
|
||||||
(desc1 && desc2 && strcmp(desc1, desc2) == 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool weightsystem_none(void *_data)
|
bool weightsystem_none(void *_data)
|
||||||
{
|
{
|
||||||
weightsystem_t *ws = _data;
|
weightsystem_t *ws = _data;
|
||||||
|
@ -98,7 +90,7 @@ bool no_weightsystems(weightsystem_t *ws)
|
||||||
static bool one_weightsystem_equal(weightsystem_t *ws1, weightsystem_t *ws2)
|
static bool one_weightsystem_equal(weightsystem_t *ws1, weightsystem_t *ws2)
|
||||||
{
|
{
|
||||||
return ws1->weight.grams == ws2->weight.grams &&
|
return ws1->weight.grams == ws2->weight.grams &&
|
||||||
description_equal(ws1->description, ws2->description);
|
same_string(ws1->description, ws2->description);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool weightsystems_equal(weightsystem_t *ws1, weightsystem_t *ws2)
|
bool weightsystems_equal(weightsystem_t *ws1, weightsystem_t *ws2)
|
||||||
|
|
|
@ -856,12 +856,11 @@ void MainTab::rejectChanges()
|
||||||
|
|
||||||
// this macro is rather fragile and is intended to be used as WHAT inside
|
// this macro is rather fragile and is intended to be used as WHAT inside
|
||||||
// an invocation of EDIT_SELECTED_DIVES(WHAT)
|
// an invocation of EDIT_SELECTED_DIVES(WHAT)
|
||||||
#define EDIT_TEXT(what, text) \
|
#define EDIT_TEXT(what, text) \
|
||||||
if ((!mydive->what && !current_dive->what) || \
|
if (same_string(mydive->what, current_dive->what)) { \
|
||||||
(mydive->what && current_dive->what && strcmp(mydive->what, current_dive->what) == 0)) { \
|
QByteArray textByteArray = text.toUtf8(); \
|
||||||
QByteArray textByteArray = text.toUtf8(); \
|
free(mydive->what); \
|
||||||
free(mydive->what); \
|
mydive->what = strdup(textByteArray.data()); \
|
||||||
mydive->what = strdup(textByteArray.data()); \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define EDIT_VALUE(what, value) \
|
#define EDIT_VALUE(what, value) \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue