This repository was archived by the owner on Apr 4, 2024. It is now read-only.
forked from diffplug/selfie
-
Notifications
You must be signed in to change notification settings - Fork 1
Trickybrain Creating the LineReader file #14
Merged
Merged
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
50c908b
make some changes to edwin-pr
Trickybrain 041af02
Resolved merge conflicts
Trickybrain f4dfcce
Trickybrain Adding LineReader
Trickybrain 3d675eb
Merge branch 'main' into Trickybrain-LineReader
Trickybrain 9fa6352
Make changes to LineReader, fixing the type issue
Trickybrain 5805966
Merge branch 'Trickybrain-LineReader' of https://github.com/Trickybra…
Trickybrain 2005fdb
Make changes to assigned in python
Trickybrain 888f072
Line Reader Translation
Trickybrain 0759e42
Merge branch 'main' into Trickybrain-LineReader
Trickybrain 74a22fb
importing LineReader at init
Trickybrain 15546f1
Merge branch 'Trickybrain-LineReader' of https://github.com/Trickybra…
Trickybrain 4881e0f
Make changes to LineReader
Trickybrain 2427d97
Make changes to LineReader
Trickybrain 2ac95ec
Merge branch 'main' into Trickybrain-LineReader
Trickybrain 419a64b
Make changes to linereader test
Trickybrain fe9adc7
Merge branch 'Trickybrain-LineReader' of https://github.com/Trickybra…
Trickybrain ef49c27
Make changes to LineReader
Trickybrain 013a094
Make changes to the errors
Trickybrain 2d06b69
Merge branch 'main' into Trickybrain-LineReader
Trickybrain c78c424
Make change LineReader
Trickybrain 5ddbbd3
Merge branch 'Trickybrain-LineReader' of https://github.com/Trickybra…
Trickybrain 4596ba9
Make changes to LineReader and rename LineReader_test
Trickybrain 66aa871
Make detect_newline_type private
Trickybrain aab9d49
Make changes to detect new line and add get line number
Trickybrain ab1fe76
We want things to be as private as possible. private is two underscor…
nedtwigg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,59 @@ | ||
from typing import Optional, Union | ||
import io | ||
|
||
class LineTerminatorReader(io.BufferedReader): | ||
"""Overrides read operations to detect carriage returns.""" | ||
def __init__(self, reader: io.TextIOWrapper) -> None: | ||
super().__init__(reader.buffer) | ||
self._unix_newlines = True | ||
|
||
def read(self, size: int = -1) -> bytes: | ||
chunk = super().read(size) | ||
if b'\r' in chunk: | ||
self._unix_newlines = False | ||
return chunk | ||
|
||
def unix_newlines(self) -> bool: | ||
"""Check if the newlines are Unix style.""" | ||
return self._unix_newlines | ||
|
||
class LineTerminatorAware(io.TextIOWrapper): | ||
"""Keeps track of the first line to determine newline style.""" | ||
def __init__(self, reader: LineTerminatorReader) -> None: | ||
super().__init__(reader, encoding='utf-8') | ||
self._first_line: Optional[str] = self.readline() | ||
|
||
def readline(self, limit: int = -1) -> str: | ||
if self._first_line is not None: | ||
result, self._first_line = self._first_line, None | ||
return result | ||
return super().readline(limit) | ||
|
||
class LineReader: | ||
"""A reader that is aware of line terminators and line numbers.""" | ||
def __init__(self, reader: Union[io.StringIO, io.BufferedReader]) -> None: | ||
self._reader = LineTerminatorAware(LineTerminatorReader(reader)) | ||
|
||
@staticmethod | ||
def for_string(content: str) -> 'LineReader': | ||
"""Create a LineReader for string content.""" | ||
return LineReader(io.StringIO(content)) | ||
|
||
@staticmethod | ||
def for_binary(content: bytes) -> 'LineReader': | ||
"""Create a LineReader for binary content.""" | ||
return LineReader(io.BufferedReader(io.BytesIO(content))) | ||
|
||
def get_line_number(self) -> int: | ||
"""Get the current line number.""" | ||
# Assuming a way to track line numbers or using a wrapper that does. | ||
# This is a placeholder as Python's io does not provide a direct lineno attribute. | ||
return 0 | ||
|
||
def read_line(self) -> Optional[str]: | ||
"""Read the next line from the reader.""" | ||
return self._reader.readline() | ||
|
||
def unix_newlines(self) -> bool: | ||
"""Check if the reader uses Unix newlines.""" | ||
return self._reader.unix_newlines() |
This file contains hidden or 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 hidden or 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,42 @@ | ||
from selfie_lib import LineReader | ||
|
||
def test_should_find_unix_separator_from_binary(): | ||
reader = LineReader.for_binary(b"This is a new line\n") | ||
assert reader.unix_newlines() == True | ||
assert reader.read_line() == "This is a new line" | ||
|
||
def test_should_find_windows_separator_from_binary(): | ||
reader = LineReader.for_binary(b"This is a new line\r\n") | ||
assert reader.unix_newlines() == False | ||
assert reader.read_line() == "This is a new line" | ||
|
||
def test_should_find_unix_separator_from_string(): | ||
reader = LineReader.for_string("This is a new line\n") | ||
assert reader.unix_newlines() == True | ||
assert reader.read_line() == "This is a new line" | ||
|
||
def test_should_find_windows_separator_from_string(): | ||
reader = LineReader.for_string("This is a new line\r\n") | ||
assert reader.unix_newlines() == False | ||
assert reader.read_line() == "This is a new line" | ||
|
||
def test_should_get_unix_line_separator_when_there_is_none(): | ||
reader = LineReader.for_binary(b"This is a new line") | ||
assert reader.unix_newlines() == True | ||
assert reader.read_line() == "This is a new line" | ||
|
||
def test_should_read_next_line_without_problem(): | ||
reader = LineReader.for_binary(b"First\r\nSecond\r\n") | ||
assert reader.unix_newlines() == False | ||
assert reader.read_line() == "First" | ||
assert reader.unix_newlines() == False | ||
assert reader.read_line() == "Second" | ||
assert reader.unix_newlines() == False | ||
|
||
def test_should_use_first_line_separator_and_ignore_next(): | ||
reader = LineReader.for_binary(b"First\r\nAnother separator\n") | ||
assert reader.unix_newlines() == False | ||
assert reader.read_line() == "First" | ||
assert reader.unix_newlines() == False | ||
assert reader.read_line() == "Another separator" | ||
assert reader.unix_newlines() == False |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.