Skip to content

Instantly share code, notes, and snippets.

@averrin
Created November 19, 2012 09:07
Show Gist options
  • Select an option

  • Save averrin/4109724 to your computer and use it in GitHub Desktop.

Select an option

Save averrin/4109724 to your computer and use it in GitHub Desktop.
mask = "4700800000000100000"
addr = "4700500010000100100"
groups = (2,3,3,3,4,4) #длины групп
print len(bin(int(mask))), bin(int(mask))
offset = 0
for i, off in enumerate(groups):
m = mask[offset:offset + off] #очередной кусок маски
a = addr[offset:offset + off] #очередной кусок адреса
z = "0" * off #нули
if not i and a != m: #если первая группа не совпадает, сразу выходим
print "not match:", a, m
break
elif a != m: #группы не совпали
if a == z and m != z: #в адресе нули, а в маске - нет - фэил
print "not match:", a, m
break
elif m == z and a != z: #маска нулевая, а адрес нет
if not len(mask[offset + off:]) or not int(mask[offset + off:]): #конец строки или дальше в маске только нули - вин
print "match"
break
else:
print "not match:", a, m #иначе - фэил
break
else: # и маска и адрес не нули, или маска не ноль, а адрес - да - фэил
print "not match:", a, m
break
else: #группы совпали, проверяем дальше
offset += off
print "%s == %s" % (a, m)
if offset == sum(groups): # если прошли всю строку - полное совпадение - вин
print "match"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment