Skip to content

Commit

Permalink
fix decchar handling in writecell() for AbstractFloat (#1109)
Browse files Browse the repository at this point in the history
* fix decchar handling in writecell() for AbstractFloat

* test for #1109 fix decchar handling in writecell() for AbstractFloat

* fix newline format

---------

Co-authored-by: Jacob Quinn <quinn.jacobd@gmail.com>
  • Loading branch information
sara2512 and quinnj authored Oct 19, 2024
1 parent 80936af commit e84bd71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,16 @@ function writecell(buf, pos, len, io, x::AbstractFloat, opts)
bytes = codeunits(string(x))
sz = sizeof(bytes)
@check sz
for i = 1:sz
@inbounds buf[pos] = bytes[i]
pos += 1
if opts.decimal != UInt8('.')
for i = 1:sz
@inbounds buf[pos] = bytes[i] == UInt8('.') ? opts.decimal : bytes[i]
pos += 1
end
else
for i = 1:sz
@inbounds buf[pos] = bytes[i]
pos += 1
end
end
return pos
end
Expand Down
10 changes: 10 additions & 0 deletions test/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ struct StructType
aumber::Union{Real, Nothing}
end

struct AF <: AbstractFloat
f::Float64
end
Base.string(x::AF) = string(x.f)

@testset "CSV.write" begin

Expand Down Expand Up @@ -156,6 +160,12 @@ end
(delim='\t', decimal=','),
"col1\tcol2\tcol3\n1,1\t4\t7\n2,2\t5\t8\n3,3\t6\t9\n"
),
# custom abstract float decimal: #1108
(
(col1=AF.([1.1,2.2,3.3]), col2=[4,5,6], col3=[7,8,9]),
(delim='\t', decimal=','),
"col1\tcol2\tcol3\n1,1\t4\t7\n2,2\t5\t8\n3,3\t6\t9\n"
),
# issue 515
(
(col1=[""],),
Expand Down

0 comments on commit e84bd71

Please sign in to comment.