Commits

Gregg Lind committed f7aada2

startswith_bytes, _util and tests.

:TODO: unicode unit tests
:TODO: source encoding, re:
http://stackoverflow.com/questions/4005928/get-current-file-encoding-for-a-python-file-or-environment
for using in the .encode(CFE) part.

  • Participants
  • Parent commits 6d7bd2d
  • Branches compatible

Comments (0)

Files changed (2)

     long = int
 else:
     long = long
+
+
+if PYTHON3:
+    _vbytes = bytes
+else:
+    _vbytes = str
+
+def startswith_bytes(target, prefix, *args):
+    """ fucntionalize string.startswith method to
+    ensure it does a bytes comparison in both py2 and py3
+    
+        startswith_bytes(target, prefix[, start[, end]]) -> bool
+    
+    Return True if S starts with the specified prefix, False otherwise.
+    With optional start, test S beginning at that position.
+    With optional end, stop comparing S at that position.
+    prefix can also be a tuple of strings to try.
+    
+    """
+    # in py3: b'something' is equivalent to:
+    # bytes("blah".encode(CURRENT_FILE_ENCODING))
+    # TODO:  figure out how to get CURRENT_FILE_ENCODING
+    # but #python wasn't helpful
+    return target.startswith(_vbytes(prefix.encode('utf8')),*args)

tests/test__util.py

 from mutagen._util import DictMixin, cdata, utf8, insert_bytes, delete_bytes
+from mutagen._util import PYTHON3
+from mutagen._util import startswith_bytes
+
 from tests import TestCase, add
 import mmap
 import random
             self.failUnless(fobj.read() == data)
 
 add(FileHandling)
+
+
+#:TODO: add some unicode tests
+class Test_startswith_bytes(TestCase):
+    def test_same_as_startswith(self):
+        lparams = (
+            (['abcd','a'],True),
+            (['abcd','b'],False),
+            (['abcd','b',1], True),
+        )
+        for params,expected in lparams:
+            ans = startswith_bytes(*params)
+            self.assertEquals(ans,expected)
+
+add(Test_startswith_bytes)
+