Browse Source

Simplify hex_to_sha.

Jelmer Vernooij 16 years ago
parent
commit
2210d2a412
2 changed files with 11 additions and 12 deletions
  1. 1 9
      dulwich/pack.py
  2. 10 3
      dulwich/tests/test_pack.py

+ 1 - 9
dulwich/pack.py

@@ -39,15 +39,7 @@ from objects import (ShaFile,
                      _decompress,
                      )
 
-def hex_to_sha(hex):
-  """Converts a hex value to the number it represents"""
-  mapping = { '0' : 0, '1' : 1, '2' : 2, '3' : 3, '4' : 4, '5' : 5, '6' : 6,
-              '7' : 7, '8' : 8, '9' : 9, 'a' : 10, 'b' : 11, 'c' : 12,
-              'd' : 13, 'e' : 14, 'f' : 15}
-  value = 0
-  for c in hex:
-    value = (16 * value) + mapping[c]
-  return value
+hex_to_sha = lambda hex: int(hex, 16)
 
 def multi_ord(map, start, count):
   value = 0

+ 10 - 3
dulwich/tests/test_pack.py

@@ -19,9 +19,11 @@
 import os
 import unittest
 
-from dulwich.pack import (PackIndex,
-                      PackData,
-                      )
+from dulwich.pack import (
+        PackIndex,
+        PackData,
+        hex_to_sha,
+        )
 
 pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
 
@@ -75,3 +77,8 @@ class TestPackData(PackTests):
     self.assertEqual(obj._type, 'commit')
     self.assertEqual(obj.sha().hexdigest(), commit_sha)
 
+
+class TestHexToSha(unittest.TestCase):
+
+    def test_simple(self):
+        self.assertEquals(703710, hex_to_sha("abcde"))