diff mercurial/util.py @ 19194:1d08df65cd3c

util: migrate fileset._sizetoint to util.sizetoint The size counting code introduced in 2c4cd1c42365 duplicated existing (but unknown-to-me) code in fileset, so prepare to eliminate the duplication.
author Bryan O'Sullivan <bryano@fb.com>
date Tue, 14 May 2013 15:16:43 -0700
parents e22107cff6bf
children 3bfd7f1e7485
line wrap: on
line diff
--- a/mercurial/util.py	Tue Apr 23 14:16:33 2013 -0700
+++ b/mercurial/util.py	Tue May 14 15:16:43 2013 -0700
@@ -1924,3 +1924,25 @@
                              (' ' * _timenesting[0], func.__name__,
                               timecount(elapsed)))
     return wrapper
+
+_sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30),
+              ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1))
+
+def sizetoint(s):
+    '''Convert a space specifier to a byte count.
+
+    >>> sizetoint('30')
+    30
+    >>> sizetoint('2.2kb')
+    2252
+    >>> sizetoint('6M')
+    6291456
+    '''
+    t = s.strip().lower()
+    try:
+        for k, u in _sizeunits:
+            if t.endswith(k):
+                return int(float(t[:-len(k)]) * u)
+        return int(t)
+    except ValueError:
+        raise error.ParseError(_("couldn't parse size: %s") % s)