yuawn

gg

Apr 25th, 2019
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import re
  3.  
  4. def rjust( s , n , c ):
  5.     return c * ( n - len( s ) ) + s
  6.  
  7. def u32( n ):
  8.     return int( ''.join( rjust( hex( ord( _ ) )[2:] , 2 , '0' ) for _ in n[::-1] ) , 16 )
  9.  
  10. def strcmp( str1 , str2 ):
  11.    
  12.     _str2 = [ u32( _ ) for _ in re.findall( '.' * 4 , str2 ) ]
  13.     l = len( str1 )
  14.     i = -4
  15.  
  16.     while not ( l <= 0 and l - 4 == -4 ):
  17.         if l - 4 < 0:
  18.             break
  19.  
  20.         i += 4
  21.  
  22.         tmp = u32( str1[ i : i + 4 ] )
  23.  
  24.         if tmp != _str2[ i / 4 ]:
  25.             return -1 if tmp < _str2[ i / 4 ] else 1;
  26.  
  27.         l -= 4
  28.  
  29.     i += 4
  30.     a = i - len( str1 )
  31.     b = a < 0
  32.  
  33.     if ( b and str1[ i ] != str2[ i ] ) or ( b and str1[ i + 1 ] != str2[ i + 1 ] and a < -1 ) or ( b and str1[ i + 2 ] != str2[ i + 2 ] and a < -2 ):
  34.  
  35.         r = str1[ i ] < str2[ i ] if b and str1[ i ] != str2[ i ] else False
  36.         r = str1[ i + 1 ] < str2[ i + 1 ] if b and str1[ i + 1 ] != str2[ i + 1 ] and a < -1 else r
  37.         r = str1[ i + 2 ] < str2[ i + 2 ] if b and str1[ i + 2 ] != str2[ i + 2 ] and a < -2 else r
  38.  
  39.         r = 1 if r else 0
  40.         r *= -1
  41.  
  42.         return r | 1
  43.  
  44.     return 0
Add Comment
Please, Sign In to add comment