-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/vijos/jd4 into vijos-master
- Loading branch information
Showing
6 changed files
with
165 additions
and
27 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
cdef class StreamReader: | ||
cdef stream | ||
cdef bytes buffer | ||
cdef const char* begin | ||
cdef const char* end | ||
|
||
def __init__(self, stream): | ||
self.stream = stream | ||
self.begin = NULL | ||
self.end = NULL | ||
|
||
cdef int read(self): | ||
if self.begin == self.end: | ||
self.buffer = self.stream.read(32768) | ||
if not self.buffer: | ||
return -1 | ||
self.begin = self.buffer | ||
self.end = self.begin + len(self.buffer) | ||
cdef int result = self.begin[0] | ||
self.begin += 1 | ||
return result | ||
|
||
def compare_stream(fa, fb): | ||
cdef StreamReader ra = StreamReader(fa) | ||
cdef StreamReader rb = StreamReader(fb) | ||
cdef int both_spaced = 1 | ||
cdef int a | ||
cdef int b | ||
|
||
while True: | ||
a = ra.read() | ||
b = rb.read() | ||
while a != b: | ||
if (a == 13 or | ||
(both_spaced and (a == 32 or a == 9)) or | ||
((b == -1 or b == 10) and (a == 32 or a == 10))): | ||
a = ra.read() | ||
elif (b == 13 or | ||
(both_spaced and (b == 32 or b == 9)) or | ||
((a == -1 or a == 10) and (b == 32 or b == 10))): | ||
b = rb.read() | ||
else: | ||
return False | ||
if a == -1: | ||
return True | ||
both_spaced = (a == 32 or a == 9) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import pyximport; pyximport.install() | ||
|
||
from io import BytesIO | ||
from unittest import main, TestCase | ||
|
||
from jd4._compare import compare_stream | ||
|
||
def compare(a, b): | ||
return compare_stream(BytesIO(a), BytesIO(b)) | ||
|
||
class CompareTest(TestCase): | ||
def test_small(self): | ||
self.assertTrue(compare(b'', b'')) | ||
self.assertTrue(compare(b'a', b'a')) | ||
self.assertFalse(compare(b'a', b'b')) | ||
self.assertTrue(compare(b'bar', b'bar')) | ||
self.assertFalse(compare(b'bar', b'baz')) | ||
self.assertFalse(compare(b'bar', b'baz')) | ||
|
||
def test_a_plus_b(self): | ||
answer = b'1 2\r\n' | ||
self.assertTrue(compare(answer, b'1 2')) | ||
self.assertTrue(compare(answer, b'1 2\n')) | ||
self.assertTrue(compare(answer, b'1 2\r')) | ||
self.assertTrue(compare(answer, b'1 2\r\n')) | ||
self.assertTrue(compare(answer, b'1 2 ')) | ||
self.assertTrue(compare(answer, b'1 2 \n')) | ||
self.assertTrue(compare(answer, b'1 2 \r')) | ||
self.assertTrue(compare(answer, b'1 2 \r\n')) | ||
self.assertTrue(compare(answer, b'1 2')) | ||
self.assertTrue(compare(answer, b'1 2\n')) | ||
self.assertTrue(compare(answer, b'1 2\r')) | ||
self.assertTrue(compare(answer, b'1 2\r\n')) | ||
self.assertTrue(compare(answer, b'1 2 ')) | ||
self.assertTrue(compare(answer, b'1 2 \n')) | ||
self.assertTrue(compare(answer, b'1 2 \r')) | ||
self.assertTrue(compare(answer, b'1 2 \r\n')) | ||
self.assertTrue(compare(answer, b' 1 2')) | ||
self.assertTrue(compare(answer, b' 1 2\n')) | ||
self.assertTrue(compare(answer, b' 1 2\r')) | ||
self.assertTrue(compare(answer, b' 1 2\r\n')) | ||
self.assertTrue(compare(answer, b' 1 2 ')) | ||
self.assertTrue(compare(answer, b' 1 2 \n')) | ||
self.assertTrue(compare(answer, b' 1 2 \r')) | ||
self.assertTrue(compare(answer, b' 1 2 \r\n')) | ||
self.assertFalse(compare(answer, b'1 1')) | ||
self.assertFalse(compare(answer, b'1 1\n')) | ||
self.assertFalse(compare(answer, b'1 1\r')) | ||
self.assertFalse(compare(answer, b'1 1\r\n')) | ||
self.assertFalse(compare(answer, b'1 1 ')) | ||
self.assertFalse(compare(answer, b'1 1 \n')) | ||
self.assertFalse(compare(answer, b'1 1 \r')) | ||
self.assertFalse(compare(answer, b'1 1 \r\n')) | ||
self.assertFalse(compare(answer, b' 1 1')) | ||
self.assertFalse(compare(answer, b' 1 1\n')) | ||
self.assertFalse(compare(answer, b' 1 1\r')) | ||
self.assertFalse(compare(answer, b' 1 1\r\n')) | ||
self.assertFalse(compare(answer, b' 1 1 ')) | ||
self.assertFalse(compare(answer, b' 1 1 \n')) | ||
self.assertFalse(compare(answer, b' 1 1 \r')) | ||
self.assertFalse(compare(answer, b' 1 1 \r\n')) | ||
self.assertFalse(compare(answer, b'2 2')) | ||
self.assertFalse(compare(answer, b'2 2\n')) | ||
self.assertFalse(compare(answer, b'2 2\r')) | ||
self.assertFalse(compare(answer, b'2 2\r\n')) | ||
self.assertFalse(compare(answer, b'2 2 ')) | ||
self.assertFalse(compare(answer, b'2 2 \n')) | ||
self.assertFalse(compare(answer, b'2 2 \r')) | ||
self.assertFalse(compare(answer, b'2 2 \r\n')) | ||
self.assertFalse(compare(answer, b' 2 2')) | ||
self.assertFalse(compare(answer, b' 2 2\n')) | ||
self.assertFalse(compare(answer, b' 2 2\r')) | ||
self.assertFalse(compare(answer, b' 2 2\r\n')) | ||
self.assertFalse(compare(answer, b' 2 2 ')) | ||
self.assertFalse(compare(answer, b' 2 2 \n')) | ||
self.assertFalse(compare(answer, b' 2 2 \r')) | ||
self.assertFalse(compare(answer, b' 2 2 \r\n')) | ||
self.assertFalse(compare(answer, b'2 1')) | ||
self.assertFalse(compare(answer, b'2 1\n')) | ||
self.assertFalse(compare(answer, b'2 1\r')) | ||
self.assertFalse(compare(answer, b'2 1\r\n')) | ||
self.assertFalse(compare(answer, b'2 1 ')) | ||
self.assertFalse(compare(answer, b'2 1 \n')) | ||
self.assertFalse(compare(answer, b'2 1 \r')) | ||
self.assertFalse(compare(answer, b'2 1 \r\n')) | ||
self.assertFalse(compare(answer, b' 2 1')) | ||
self.assertFalse(compare(answer, b' 2 1\n')) | ||
self.assertFalse(compare(answer, b' 2 1\r')) | ||
self.assertFalse(compare(answer, b' 2 1\r\n')) | ||
self.assertFalse(compare(answer, b' 2 1 ')) | ||
self.assertFalse(compare(answer, b' 2 1 \n')) | ||
self.assertFalse(compare(answer, b' 2 1 \r')) | ||
self.assertFalse(compare(answer, b' 2 1 \r\n')) | ||
self.assertFalse(compare(answer, b'12')) | ||
|
||
def test_large(self): | ||
self.assertTrue(compare(b'a' * 1048576, b'a' * 1048576)) | ||
self.assertFalse(compare(b'a' * 1048576, b'a' * 1048575 + b'b')) | ||
self.assertTrue(compare(b'a' * 1048576 + b' ' + b'b' * 1048576 + b'\r\n', | ||
b'a' * 1048576 + b' ' * 1048576 + b'b' * 1048576)) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
aiohttp | ||
appdirs | ||
butter | ||
cloudpickle | ||
colorlog | ||
appdirs | ||
cython | ||
ruamel.yaml |