Skip to content

Commit

Permalink
handle UTF-8 encodings gracefully, fixes #77
Browse files Browse the repository at this point in the history
Using `ncodeunits` handles the variable byte lengths for UTF-8
strings properly.
  • Loading branch information
tlnagy committed Apr 25, 2022
1 parent 0bb669a commit e53ed60
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/tags.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ isloaded(t::Tag) = true
isloaded(t::Tag{<: RemoteData}) = false

Base.length(t::Tag{<: AbstractVector}) = length(t.data)
Base.length(t::Tag{<: AbstractString}) = (endswith(t.data, '\0') ? length(t.data) : length(t.data) + 1)
Base.length(t::Tag{<: AbstractString}) = (endswith(t.data, '\0') ? ncodeunits(t.data) : ncodeunits(t.data) + 1)
Base.length(t::Tag{<: RemoteData}) = getfield(t, :data).count
Base.length(t::Tag) = 1

Expand Down
2 changes: 2 additions & 0 deletions test/mmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
end

@testset "De novo construction" begin
rm("test.tif", force = true)
img = memmap(Gray{N0f8}, "test.tif")

# a newly initialized file should have every dimension equal to zero and
Expand All @@ -28,6 +29,7 @@ end
@test_throws AssertionError push!(img, rand(Gray{N0f8}, 99, 99)) # wrong size

@testset "BigTIFF" begin
rm("test.btif", force = true)
img = memmap(Gray{N0f8}, "test.btif"; bigtiff = true)

push!(img, rand(Gray{N0f8}, 100, 100))
Expand Down
15 changes: 15 additions & 0 deletions test/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@
@test getfield(t4, :data) == "tes\0"
@test t4.data == "tes"
end

@testset "UTF-8 strings" begin
ifd = TiffImages.IFD(UInt32)
ifd[TiffImages.IMAGEDESCRIPTION] = "αβγ" # 2 bytes each, 6 total
ifd[TiffImages.SOFTWARE] = "" # 3 byte character

tf = TiffImages.TiffFile(UInt32)
write(tf, ifd)

ifd2 = first(tf)
TiffImages.load!(tf, ifd2)

@test ifd[TiffImages.SOFTWARE] == ifd2[TiffImages.SOFTWARE]
@test ifd[TiffImages.IMAGEDESCRIPTION] == ifd2[TiffImages.IMAGEDESCRIPTION] # test remote data utf-8
end
end

@testset "ifds" begin
Expand Down

0 comments on commit e53ed60

Please sign in to comment.