Skip to content

Commit de89712

Browse files
improved bom stripping
1 parent 8051a54 commit de89712

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

tests/test_vdf.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,12 @@ def test_routine_dump_writing(self):
6868

6969
class testcase_parse_routine(unittest.TestCase):
7070
def test_parse_bom_removal(self):
71-
for mark in vdf.BOMS:
72-
result = vdf.loads(mark + '"asd" "123"')
73-
self.assertEqual(result, {'asd': '123'})
71+
result = vdf.loads(vdf.BOMS + '"asd" "123"')
72+
self.assertEqual(result, {'asd': '123'})
7473

7574
if sys.version_info[0] is 2:
76-
for mark in vdf.BOMS_UNICODE:
77-
result = vdf.loads(mark + '"asd" "123"')
78-
self.assertEqual(result, {'asd': '123'})
75+
result = vdf.loads(vdf.BOMS_UNICODE + '"asd" "123"')
76+
self.assertEqual(result, {'asd': '123'})
7977

8078
def test_parse_asserts(self):
8179
for t in [5, 5.5, 1.0j, True, None, (), {}, lambda: 0]:

vdf/__init__.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,20 @@
1818
from io import IOBase as file_type
1919
string_type = str
2020
next_method_name = '__next__'
21-
BOMS = ('\ufffe', '\ufeff')
21+
BOMS = '\ufffe\ufeff'
2222

2323
def strip_bom(line):
24-
if line.startswith(BOMS):
25-
return line[1:]
26-
return line
27-
24+
return line.lstrip(BOMS)
2825
else:
2926
from StringIO import StringIO
3027
file_type = (file, StringIO)
3128
string_type = basestring
3229
next_method_name = 'next'
33-
BOMS = ('\xff\xfe', '\xfe\xff')
34-
BOMS_UNICODE = ['\\ufffe', '\\ufeff']
35-
BOMS_UNICODE = tuple(map(lambda x: x.decode('unicode-escape'), BOMS_UNICODE))
30+
BOMS = '\xef\xbb\xbf\xff\xfe\xfe\xff'
31+
BOMS_UNICODE = '\\ufffe\\ufeff'.decode('unicode-escape')
3632

3733
def strip_bom(line):
38-
if isinstance(line, str):
39-
if line.startswith(BOMS):
40-
return line[2:]
41-
else:
42-
if line.startswith(BOMS_UNICODE):
43-
return line[1:]
44-
return line
34+
return line.lstrip(BOMS if isinstance(line, str) else BOMS_UNICODE)
4535

4636
###############################################
4737
#
@@ -61,7 +51,7 @@ def parse(source):
6151
else:
6252
raise ValueError("Expected parametar to be file or str")
6353

64-
# strip anoying BOMS
54+
# strip annoying BOMS
6555
lines[0] = strip_bom(lines[0])
6656

6757
# init

0 commit comments

Comments
 (0)