Skip to content

Commit f7a8cc8

Browse files
apply suggestions from comments
Co-authored-by: Lyndon White <[email protected]>
1 parent 896d4bc commit f7a8cc8

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/fileio.jl

+14-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function loadfile(T, file::File)
99
end
1010

1111
function loadfile(T, file::TextFile)
12-
_ignore_crlf(read(file.filename, String))
12+
_ignore_CR(read(file.filename, String))
1313
end
1414

1515
function loadfile(::Type{<:Number}, file::File{format"TXT"})
@@ -41,7 +41,7 @@ end
4141
# Some target formats are not supported by FileIO and thus require an encoding/compression process
4242
# before saving. For other formats, we should trust IO backends and make as few changes as possible.
4343
# Otherwise, reference becomes unfaithful. The encoding process helps making the actual data matches
44-
# the reference data, which is load from reference file via IO backends.
44+
# the reference data, which is loaded from reference file via IO backends.
4545
#
4646
# TODO: split `maybe_encode` to `maybe_preprocess` and `maybe_encode`
4747
"""
@@ -54,9 +54,9 @@ If there is no known method to encode `x`, then it directly return `x` without w
5454
maybe_encode(::Type{<:DataFormat}, x; kw...) = x
5555

5656
# plain TXT
57-
maybe_encode(::Type{DataFormat{:TXT}}, x; kw...) = _ignore_crlf(string(x))
57+
maybe_encode(::Type{DataFormat{:TXT}}, x; kw...) = _ignore_CR(string(x))
5858
maybe_encode(::Type{DataFormat{:TXT}}, x::AbstractArray{<:AbstractString}; kw...) = _join(x)
59-
maybe_encode(::Type{DataFormat{:TXT}}, x::AbstractString; kw...) = _ignore_crlf(x)
59+
maybe_encode(::Type{DataFormat{:TXT}}, x::AbstractString; kw...) = _ignore_CR(x)
6060
maybe_encode(::Type{DataFormat{:TXT}}, x::Number; kw...) = x # TODO: Change this to string(x) ?
6161

6262
function maybe_encode(
@@ -74,7 +74,7 @@ end
7474

7575
# SHA256
7676
maybe_encode(::Type{DataFormat{:SHA256}}, x; kw...) = _sha256(string(x))
77-
maybe_encode(::Type{DataFormat{:SHA256}}, x::AbstractString) = _sha256(_ignore_crlf(x))
77+
maybe_encode(::Type{DataFormat{:SHA256}}, x::AbstractString) = _sha256(_ignore_CR(x))
7878
maybe_encode(::Type{DataFormat{:SHA256}}, x::AbstractArray{<:AbstractString}) = _sha256(_join(x))
7979
function maybe_encode(::Type{DataFormat{:SHA256}}, img::AbstractArray{<:Colorant}; kw...)
8080
# encode image into SHA256
@@ -84,8 +84,14 @@ function maybe_encode(::Type{DataFormat{:SHA256}}, img::AbstractArray{<:Colorant
8484
return size_str * img_str
8585
end
8686

87-
8887
# Helpers
89-
_join(x::AbstractArray{<:AbstractString}) = mapreduce(_ignore_crlf, (x,y)->x*"\n"*y, x)
88+
_join(x::AbstractArray{<:AbstractString}) = _ignore_CR(join(x, "\n"))
9089
_sha256(x) = bytes2hex(sha256(x))
91-
_ignore_crlf(x::AbstractString) = replace(x, "\r"=>"")
90+
"""
91+
_ignore_CR(x::AbstractString)
92+
93+
Ignore the CRLF(`\\r\\n`) and LF(`\\n`) difference by removing `\\r` from the given string.
94+
95+
CRLF format is widely used by Windows while LF format is mainly used by Linux.
96+
"""
97+
_ignore_CR(x::AbstractString) = replace(x, "\r\n"=>"\n") # issue #39

test/fileio.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ end
3030
str1_sha256 = "64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c"
3131
str2 = "Hello\n world"
3232
str2_sha256 = "60b65ab310480818c4289227f2ec68f1714743db8571b4cb190e100c0085be3d" # bytes2hex(SHA.sha256(str2))
33-
str2_crlf = "Hello\n\r world"
33+
str2_crlf = "Hello\r\n world"
3434
str3 = "Hello\nworld"
3535
str3_sha256 = "46e0ea795802f17d0b340983ca7d7068c94d7d9172ee4daea37a1ab1168649ec" # bytes2hex(SHA.sha256(str3))
3636
str3_arr1 = ["Hello", "world"]
3737
str3_arr2 = ["Hello" "world"]
3838
str4 = "Hello\n world1\nHello\n world2"
3939
str4_sha256 = "c7dc8b82c3a6fed4afa0c8790a0586b73df0e4f35524efe6810e5d78b6b6a611" # bytes2hex(SHA.sha256(str4))
40-
str4_arr = ["Hello\n\r world1", "Hello\n world2"]
40+
str4_arr = ["Hello\r\n world1", "Hello\n world2"]
4141

4242
# string as plain text
4343
fmt = format"TXT"

0 commit comments

Comments
 (0)