[PATCH 2/3] const'ify our strtod() helper functions

Linus Torvalds torvalds at linux-foundation.org
Tue Jan 7 23:36:15 UTC 2014


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Wed, 8 Jan 2014 14:51:22 +0800
Subject: [PATCH 2/3] const'ify our strtod() helper functions

The C library doesn't use const char pointers for legacy reasons (and
because you *can* modify the string the end pointer points to), but
let's do it in our internal implementation just because it's a nice
guarantee to have.

We actually used to have a non-const end pointer and replace a decimal
comma with a decimal dot, but that was because we didn't have the fancy
"allow commas" flags.  So by using our own strtod_flags() function, we
can now keep all the strings we parse read-only rather than modify them
as we parse them.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

More type cleanup. And now the XML double parsing doesn't change the 
string as it parses it.

  dive.h           |  2 +-
  parse-xml.c      | 11 +++++------
  qt-ui/models.cpp |  4 ++--
  strtod.c         |  5 +++--
  4 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/dive.h b/dive.h
index 649a7e5e69d3..7a90bf49ef84 100644
--- a/dive.h
+++ b/dive.h
@@ -806,7 +806,7 @@ extern void remove_weightsystem(struct dive *dive, int idx);
  #define STRTOD_NO_DOT		0x02
  #define STRTOD_NO_COMMA		0x04
  #define STRTOD_NO_EXPONENT	0x08
-extern double strtod_flags(char *str, char **ptr, unsigned int flags);
+extern double strtod_flags(const char *str, const char **ptr, unsigned int flags);

  #define STRTOD_ASCII (STRTOD_NO_COMMA)

diff --git a/parse-xml.c b/parse-xml.c
index 59e04efb1382..8fc504a3f49c 100644
--- a/parse-xml.c
+++ b/parse-xml.c
@@ -263,7 +263,7 @@ enum number_type {
  	FLOAT
  };

-static enum number_type parse_float(char *buffer, double *res, char **endp)
+static enum number_type parse_float(const char *buffer, double *res, const char **endp)
  {
  	double val;
  	static bool first_time = TRUE;
@@ -281,9 +281,8 @@ static enum number_type parse_float(char *buffer, double *res, char **endp)
  				fprintf(stderr, "Floating point value with decimal comma (%s)?\n", buffer);
  				first_time = FALSE;
  			}
-			/* Try again */
-			**endp = '.';
-			val = ascii_strtod(buffer, endp);
+			/* Try again in permissive mode*/
+			val = strtod_flags(buffer, endp, 0);
  		}
  	}

@@ -297,7 +296,7 @@ union int_or_float {

  static enum number_type integer_or_float(char *buffer, union int_or_float *res)
  {
-	char *end;
+	const char *end;
  	return parse_float(buffer, &res->fp, &end);
  }

@@ -459,7 +458,7 @@ static void percent(char *buffer, void *_fraction)
  {
  	fraction_t *fraction = _fraction;
  	double val;
-	char *end;
+	const char *end;

  	switch (parse_float(buffer, &val, &end)) {
  	case FLOAT:
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 7f5764779c81..b0a0dadb2b35 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -464,9 +464,9 @@ void WeightModel::passInData(const QModelIndex& index, const QVariant& value)
  	}
  }

-weight_t string_to_weight(char *str)
+weight_t string_to_weight(const char *str)
  {
-	char *end;
+	const char *end;
  	double value = strtod_flags(str, &end, 0);
  	QString rest = QString(end).trimmed();
  	QString local_kg = WeightModel::tr("kg");
diff --git a/strtod.c b/strtod.c
index 4643cfe9d66c..fe7319887546 100644
--- a/strtod.c
+++ b/strtod.c
@@ -29,9 +29,10 @@
  #include <ctype.h>
  #include "dive.h"

-double strtod_flags(char *str, char **ptr, unsigned int flags)
+double strtod_flags(const char *str, const char **ptr, unsigned int flags)
  {
-	char *p = str, c, *ep;
+	char c;
+	const char *p = str, *ep;
  	double val = 0.0;
  	double decimal = 1.0;
  	int sign = 0, esign = 0;
-- 
1.8.4.2



More information about the subsurface mailing list