changeset 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 ab9de1e8fc36
children 9311cd5c09ed
files mercurial/fileset.py mercurial/util.py
diffstat 2 files changed, 30 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/fileset.py	Tue Apr 23 14:16:33 2013 -0700
+++ b/mercurial/fileset.py	Tue May 14 15:16:43 2013 -0700
@@ -263,23 +263,10 @@
         raise error.ParseError(_('invalid match pattern: %s') % e)
     return [f for f in mctx.existing() if r.search(mctx.ctx[f].data())]
 
-_units = dict(k=2**10, K=2**10, kB=2**10, KB=2**10,
-              M=2**20, MB=2**20, G=2**30, GB=2**30)
-
-def _sizetoint(s):
-    try:
-        s = s.strip()
-        for k, v in _units.items():
-            if s.endswith(k):
-                return int(float(s[:-len(k)]) * v)
-        return int(s)
-    except ValueError:
-        raise error.ParseError(_("couldn't parse size: %s") % s)
-
 def _sizetomax(s):
     try:
         s = s.strip()
-        for k, v in _units.items():
+        for k, v in util._sizeunits:
             if s.endswith(k):
                 # max(4k) = 5k - 1, max(4.5k) = 4.6k - 1
                 n = s[:-len(k)]
@@ -306,23 +293,23 @@
     expr = getstring(x, _("size requires an expression")).strip()
     if '-' in expr: # do we have a range?
         a, b = expr.split('-', 1)
-        a = _sizetoint(a)
-        b = _sizetoint(b)
+        a = util.sizetoint(a)
+        b = util.sizetoint(b)
         m = lambda x: x >= a and x <= b
     elif expr.startswith("<="):
-        a = _sizetoint(expr[2:])
+        a = util.sizetoint(expr[2:])
         m = lambda x: x <= a
     elif expr.startswith("<"):
-        a = _sizetoint(expr[1:])
+        a = util.sizetoint(expr[1:])
         m = lambda x: x < a
     elif expr.startswith(">="):
-        a = _sizetoint(expr[2:])
+        a = util.sizetoint(expr[2:])
         m = lambda x: x >= a
     elif expr.startswith(">"):
-        a = _sizetoint(expr[1:])
+        a = util.sizetoint(expr[1:])
         m = lambda x: x > a
     elif expr[0].isdigit or expr[0] == '.':
-        a = _sizetoint(expr)
+        a = util.sizetoint(expr)
         b = _sizetomax(expr)
         m = lambda x: x >= a and x <= b
     else:
--- 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)