Skip to content

Commit

Permalink
optimize lookup speed (ls0f#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
gumblex authored and ls0f committed Jun 3, 2019
1 parent 74c1659 commit 722d1d9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions phone/phone.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

if sys.version_info > (3, 0):
def get_record_content(buf, start_offset):
end_offset = start_offset + buf[start_offset:-1].find(b'\x00')
end_offset = buf.find(b'\x00', start_offset)
return buf[start_offset:end_offset].decode()
else:
def get_record_content(buf, start_offset):
end_offset = start_offset + buf[start_offset:-1].find('\x00')
end_offset = buf.find('\x00', start_offset)
return buf[start_offset:end_offset]


Expand All @@ -32,7 +32,7 @@ def __init__(self, dat_file=None):
self.version, self.first_phone_record_offset = struct.unpack(
self.head_fmt, self.buf[:self.head_fmt_length])
self.phone_record_count = (len(
self.buf) - self.first_phone_record_offset) / self.phone_fmt_length
self.buf) - self.first_phone_record_offset) // self.phone_fmt_length

def get_phone_dat_msg(self):
print("版本号:{}".format(self.version))
Expand Down Expand Up @@ -74,10 +74,12 @@ def _lookup_phone(self, phone_num):

left = 0
right = self.phone_record_count
buflen = len(self.buf)
while left <= right:
middle = int((left + right) / 2)
current_offset = int(self.first_phone_record_offset + middle * self.phone_fmt_length)
if current_offset >= len(self.buf):
middle = (left + right) // 2
current_offset = (self.first_phone_record_offset +
middle * self.phone_fmt_length)
if current_offset >= buflen:
return

buffer = self.buf[current_offset: current_offset + self.phone_fmt_length]
Expand Down

0 comments on commit 722d1d9

Please sign in to comment.