Skip to content

Commit 7bb08b7

Browse files
committed
Unify and optimize all the "toUnix" stuff into LineReader._to_unix.
1 parent a7a2e8a commit 7bb08b7

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

python/selfie-lib/selfie_lib/LineReader.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import io
22

33

4+
def _to_unix(s: str) -> str:
5+
if s.find("\r\n") == -1:
6+
return s
7+
else:
8+
return s.replace("\r\n", "\n")
9+
10+
411
class LineReader:
512
def __init__(self, content: bytes):
613
self.__buffer = io.BytesIO(content)

python/selfie-lib/selfie_lib/Snapshot.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Iterator, Union
22

33
from .ArrayMap import ArrayMap
4+
from .LineReader import _to_unix
45
from .SnapshotValue import SnapshotValue
56

67

@@ -36,7 +37,7 @@ def plus_facet(
3637
raise ValueError("The empty string is reserved for the subject.")
3738
return Snapshot(
3839
self._subject,
39-
self._facet_data.plus(_unix_newlines(key), SnapshotValue.of(value)),
40+
self._facet_data.plus(_to_unix(key), SnapshotValue.of(value)),
4041
)
4142

4243
def plus_or_replace(
@@ -48,7 +49,7 @@ def plus_or_replace(
4849
return Snapshot(
4950
self._subject,
5051
self._facet_data.plus_or_noop_or_replace(
51-
_unix_newlines(key), SnapshotValue.of(value)
52+
_to_unix(key), SnapshotValue.of(value)
5253
),
5354
)
5455

@@ -86,7 +87,3 @@ def of_items(items: Iterator[tuple[str, SnapshotValue]]) -> "Snapshot":
8687
def items(self) -> Iterator[tuple[str, SnapshotValue]]:
8788
yield ("", self._subject)
8889
yield from self._facet_data.items()
89-
90-
91-
def _unix_newlines(string: str) -> str:
92-
return string.replace("\\r\\n", "\\n")

python/selfie-lib/selfie_lib/SnapshotValue.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from abc import ABC, abstractmethod
22
from typing import Union
33

4-
5-
def unix_newlines(string: str) -> str:
6-
return string.replace("\r\n", "\n")
4+
from .LineReader import _to_unix
75

86

97
class SnapshotValue(ABC):
@@ -24,7 +22,7 @@ def of(data: Union[bytes, str, "SnapshotValue"]) -> "SnapshotValue":
2422
if isinstance(data, bytes):
2523
return SnapshotValueBinary(data)
2624
elif isinstance(data, str):
27-
return SnapshotValueString(unix_newlines(data))
25+
return SnapshotValueString(_to_unix(data))
2826
elif isinstance(data, SnapshotValue):
2927
return data
3028
else:

python/selfie-lib/selfie_lib/SnapshotValueReader.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
from .SnapshotValue import SnapshotValue
88

99

10-
def unix_newlines(string: str) -> str:
11-
return string.replace("\r\n", "\n")
12-
13-
1410
class SnapshotValueReader:
1511
KEY_FIRST_CHAR = "╔"
1612
KEY_START = "╔═ "

0 commit comments

Comments
 (0)