[PATCH 1/2] Models: translate the user entered weight units

Dirk Hohndel dirk at hohndel.org
Fri Jan 3 11:30:52 UTC 2014


On Fri, 2014-01-03 at 09:51 -0800, Linus Torvalds wrote:
> On Fri, Jan 3, 2014 at 9:40 AM, Dirk Hohndel <dirk at hohndel.org> wrote:
> >
> > side note: I am amused by how much energy we are spending on figuring
> > out whether we should correctly parse the local translation of an
> > antiquated unit of weight used by roughly one country in the world...
> 
> Hey, it's US and Liberia! *And* Myanmar. That's _three_ countries.
> USA! USA! USA!
> 
> More seriously, there does seem to be a lot of places that use lbs
> when it comes to diving outside of the US. It wasn't entirely clear in
> Australia, for example. I think they end up having some of the weights
> in lbs (although maybe they were "metric pounds" at 500g). And Okinawa
> was lbs, although that may be due to the US guy. And Palau (ok,
> "associated state" with the US). And I'm pretty sure Fiji weights were
> lbs too..

So I used the power of being the maintainer to rule on this one (Ha!
Neener, neener...)

/D

commit 32d26b751ae15faca812437d002c0275dcf7e7e3
Author: Dirk Hohndel <dirk at hohndel.org>
Date:   Fri Jan 3 11:22:37 2014 -0800

    Parse localized weight units
    
    We have the wonderful Qt string functions. Let's use them to make the code
    simpler and easier to read.
    
    Suggested-by: Lubomir I. Ivanov <neolit123 at gmail.com>
    Signed-off-by: Dirk Hohndel <dirk at hohndel.org>

diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index ab501a2bdfc6..aa9db450be83 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -468,12 +468,14 @@ double string_to_grams(char *str)
 {
        char *end;
        double value = strtod_flags(str, &end, 0);
+       QString rest = QString(end).trimmed();
+       QString local_kg = WeightModel::tr("kg");
+       QString local_lbs = WeightModel::tr("lbs");
 
-       while (isspace(*end))
-               end++;
-       if (!strncmp(end, "kg", 2))
+       if (rest.startsWith("kg") || rest.startsWith(local_kg))
                goto kg;
-       if (!strncmp(end, "lbs", 3))
+       // using just "lb" instead of "lbs" is intentional - some people might enter the singular
+       if (rest.startsWith("lb") || rest.startsWith(local_lbs))
                goto lbs;
        if (prefs.units.weight == prefs.units.LBS)
                goto lbs;




More information about the subsurface mailing list