[Git][NTPsec/ntpsec][unit-rewrite] Replaced duplicated code in scalestring() and rescalestring() with calls

Ian Bruene gitlab at mg.gitlab.com
Sun Apr 9 22:24:50 UTC 2017


Ian Bruene pushed to branch unit-rewrite at NTPsec / ntpsec


Commits:
a6e3ffc3 by Ian Bruene at 2017-04-09T17:19:26-05:00
Replaced duplicated code in scalestring() and rescalestring() with calls
to breaknumberstring() and gluenumberstring()

- - - - -


1 changed file:

- pylib/util.py


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -213,15 +213,7 @@ def rescalestring(value, unitsscaled):
     "Rescale a number string by a given number of units"
     if unitsscaled == 0:
         return value
-    negative = False
-    if value[0] == "-":
-        value = value[1:]
-        negative = True
-    if "." in value:
-        whole, dec = value.split(".")
-    else:
-        whole = value
-        dec = ""
+    whole, dec, negative = breaknumberstring(value)
     hilen = len(whole)
     lolen = len(dec)
     digitsmoved = abs(unitsscaled * 3)
@@ -244,26 +236,13 @@ def rescalestring(value, unitsscaled):
         else:
             newwhole = whole + dec[:digitsmoved]
             newdec = dec[digitsmoved:]
-    if len(newdec) > 0:
-        newvalue = ".".join((newwhole, newdec))
-    else:
-        newvalue = newwhole
-    if negative is True:
-        newvalue = "-" + newvalue
+    newvalue = gluenumberstring(newwhole, newdec, negative)
     return newvalue
 
 
 def scalestring(value):
     "Scales a number string to fit in the range 1.0-999.9"
-    negative = False
-    if value[0] == "-":
-        value = value[1:]
-        negative = True
-    if "." in value:
-        whole, dec = value.split(".")
-    else:
-        whole = value
-        dec = ""
+    whole, dec, negative = breaknumberstring(value)
     hilen = len(whole)
     if (hilen == 0) or (whole[0] == "0"):  # Need to shift to smaller units
         i = 0
@@ -292,12 +271,7 @@ def scalestring(value):
         newwhole = whole[:hidigits]
         newdec = whole[hidigits:] + dec
         unitsmoved = hiunits
-    if len(newdec) > 0:
-        newvalue = ".".join((newwhole, newdec))
-    else:
-        newvalue = newwhole
-    if negative is True:
-        newvalue = "-" + newvalue
+    newvalue = gluenumberstring(newwhole, newdec, negative)
     return (newvalue, unitsmoved)
 
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/a6e3ffc39833cb0177899ba7236c56beae77706e
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170409/2f498a0f/attachment.html>


More information about the vc mailing list